Skip to content

Commit

Permalink
feat: revert depletionTimeOf if balance is zero (#335)
Browse files Browse the repository at this point in the history
* feat: revert depletionTimeOf if balance is zero

* test: fix function name
  • Loading branch information
smol-ninja authored Nov 13, 2024
1 parent 3fc23fc commit 8348fe5
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/SablierFlow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ contract SablierFlow is
{
uint128 balance = _streams[streamId].balance;

// If the stream balance is zero, return zero.
// If the stream balance is zero, revert to avoid ambiguity from a depleting stream.
if (balance == 0) {
return 0;
revert Errors.SablierFlow_StreamBalanceZero(streamId);
}

uint8 tokenDecimals = _streams[streamId].tokenDecimals;
Expand Down
4 changes: 3 additions & 1 deletion src/interfaces/ISablierFlow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ interface ISablierFlow is

/// @notice Returns the time at which the total debt exceeds stream balance. If the total debt is less than
/// or equal to stream balance, it returns 0.
/// @dev Reverts if `streamId` references a paused or a null stream.
/// @dev Reverts on the following conditions:
/// - If `streamId` references a paused or a null stream.
/// - If stream balance is zero.
/// @param streamId The stream ID for the query.
function depletionTimeOf(uint256 streamId) external view returns (uint256 depletionTime);

Expand Down
3 changes: 3 additions & 0 deletions src/libraries/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ library Errors {
/// @notice Thrown when trying to create a stream with the sender as the zero address.
error SablierFlow_SenderZeroAddress();

/// @notice Thrown when trying to get depletion time of a stream with zero balance.
error SablierFlow_StreamBalanceZero(uint256 streamId);

/// @notice Thrown when trying to perform an action with a paused stream.
error SablierFlow_StreamPaused(uint256 streamId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity >=0.8.22;

import { UD21x18 } from "@prb/math/src/UD21x18.sol";
import { Errors } from "src/libraries/Errors.sol";

import { Integration_Test } from "../../Integration.t.sol";

Expand All @@ -16,10 +17,9 @@ contract DepletionTimeOf_Integration_Concrete_Test is Integration_Test {
expectRevert_Paused(callData);
}

function test_GivenBalanceZero() external view givenNotNull givenNotPaused {
// It should return 0.
uint256 actualDepletionTime = flow.depletionTimeOf(defaultStreamId);
assertEq(actualDepletionTime, 0, "depletion time");
function test_RevertGiven_BalanceZero() external givenNotNull givenNotPaused {
vm.expectRevert(abi.encodeWithSelector(Errors.SablierFlow_StreamBalanceZero.selector, defaultStreamId));
flow.depletionTimeOf(defaultStreamId);
}

function test_GivenUncoveredDebt() external givenNotNull givenNotPaused givenBalanceNotZero {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ DepletionTimeOf_Integration_Concrete_Test
β”‚ └── it should revert
└── given not paused
β”œβ”€β”€ given balance zero
β”‚ └── it should return 0
β”‚ └── it should revert
└── given balance not zero
β”œβ”€β”€ given uncovered debt
β”‚ └── it should return 0
Expand Down

0 comments on commit 8348fe5

Please sign in to comment.