Skip to content

Commit

Permalink
perf: slightly optimize streamDebtOf function
Browse files Browse the repository at this point in the history
refactor: remove pauseMultiple function
  • Loading branch information
andreivladbrg committed May 23, 2024
1 parent 621ff8c commit aef3c12
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 28 deletions.
24 changes: 6 additions & 18 deletions src/SablierV2OpenEnded.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,13 @@ contract SablierV2OpenEnded is
/// @inheritdoc ISablierV2OpenEnded
function streamDebtOf(uint256 streamId) external view override notNull(streamId) returns (uint128 debt) {
uint128 balance = _streams[streamId].balance;
uint128 remainingAmount = _streams[streamId].remainingAmount;

// If the stream is pause it will return zero.
uint128 streamedAmount = _streamedAmountOf(streamId, uint40(block.timestamp));

uint128 sum = streamedAmount + remainingAmount;
uint128 recipientAmount = streamedAmount + _streams[streamId].remainingAmount;

if (balance < sum) {
debt = sum - balance;
if (balance < recipientAmount) {
debt = recipientAmount - balance;
} else {
return 0;
}
Expand Down Expand Up @@ -214,16 +212,6 @@ contract SablierV2OpenEnded is
_pause(streamId);
}

/// @inheritdoc ISablierV2OpenEnded
function pauseMultiple(uint256[] calldata streamIds) external override {
// Iterate over the provided array of stream IDs and pause each stream.
uint256 count = streamIds.length;
for (uint256 i = 0; i < count; ++i) {
// Effects and Interactions: pause the stream.
pause(streamIds[i]);
}
}

/// @inheritdoc ISablierV2OpenEnded
function restartStream(
uint256 streamId,
Expand Down Expand Up @@ -519,12 +507,12 @@ contract SablierV2OpenEnded is

/// @dev Helper function to calculate the transfer amount and to perform the ERC-20 transfer.
function _extractFromStream(uint256 streamId, address to, uint128 amount) internal {
// Effect: update the stream balance.
_streams[streamId].balance -= amount;

// Calculate the transfer amount.
uint128 transferAmount = _calculateTransferAmount(streamId, amount);

// Effect: update the stream balance.
_streams[streamId].balance -= amount;

// Interaction: perform the ERC-20 transfer.
_streams[streamId].asset.safeTransfer(to, transferAmount);
}
Expand Down
10 changes: 0 additions & 10 deletions src/interfaces/ISablierV2OpenEnded.sol
Original file line number Diff line number Diff line change
Expand Up @@ -241,16 +241,6 @@ interface ISablierV2OpenEnded is
/// @param streamId The ID of the stream to pause.
function pause(uint256 streamId) external;

/// @notice Pauses multiple streams by setting the rate per second to zero.
///
/// @dev Emits multiple {Transfer} and {PauseOpenEndedStream} events.
///
/// Requirements:
/// - All requirements from {pause} must be met for each stream.
///
/// @param streamIds The IDs of the streams to pause.
function pauseMultiple(uint256[] calldata streamIds) external;

/// @notice Refunds the provided amount of assets from the stream to the sender's address.
///
/// @dev Emits a {Transfer} and {RefundFromOpenEndedStream} event.
Expand Down

0 comments on commit aef3c12

Please sign in to comment.