Skip to content

Commit

Permalink
Merge pull request #875 from nounsDAO/verbs-stream-escrow-add-ticks-left
Browse files Browse the repository at this point in the history
add view function for ticksLeft in stream
  • Loading branch information
davidbrai authored Dec 12, 2024
2 parents 575b15b + 00edb18 commit c46406b
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
32 changes: 27 additions & 5 deletions packages/nouns-contracts/contracts/StreamEscrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,41 @@ contract StreamEscrow is IStreamEscrow {
}

/**
* @notice Returns the amount of ETH that was not yet streamed for a specific Noun token.
* @notice Returns the amount of ETH that was not yet streamed and the number of ticks left for a specific Noun token.
* Returns zero for inactive streams.
* @param nounId The ID of the Noun token to check the stream for.
*/
function unstreamedETHForNoun(uint256 nounId) public view returns (uint256) {
function unstreamedETHAndTicksLeftForNoun(
uint256 nounId
) public view returns (uint256 unstreamedETH, uint256 ticksLeft) {
Stream memory stream = streams[nounId];
uint32 currentTick_ = currentTick;
if (!isStreamActive(stream, currentTick_)) {
return 0;
return (0, 0);
} else {
ticksLeft = stream.lastTick - currentTick_;
unstreamedETH = ticksLeft * stream.ethPerTick;
}
}

/**
* @notice Returns the amount of ETH that was not yet streamed for a specific Noun token.
* Returns zero for inactive streams.
* @param nounId The ID of the Noun token to check the stream for.
*/
function unstreamedETHForNoun(uint256 nounId) public view returns (uint256) {
(uint256 unstreamedETH, ) = unstreamedETHAndTicksLeftForNoun(nounId);
return unstreamedETH;
}

uint256 ticksLeft = stream.lastTick - currentTick_;
return ticksLeft * stream.ethPerTick;
/**
* @notice Returns the number of ticks left in a stream for a specific Noun token.
* Returns zero for inactive streams.
* @param nounId The ID of the Noun token to check the stream for.
*/
function ticksLeftForNoun(uint256 nounId) public view returns (uint256) {
(, uint256 ticksLeft) = unstreamedETHAndTicksLeftForNoun(nounId);
return ticksLeft;
}

function isStreamActive(Stream memory stream, uint32 tick) internal pure returns (bool) {
Expand Down
12 changes: 12 additions & 0 deletions packages/nouns-contracts/test/foundry/StreamEscrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -735,20 +735,27 @@ contract UnstreamedETHTest is BaseStreamEscrowTest {

// 1 ether / 20 = 0.05 eth per tick
assertEq(escrow.unstreamedETHForNoun(1), 1 ether);
assertEq(escrow.ticksLeftForNoun(1), 20);

// forward 5 ticks
for (uint i; i < 5; i++) {
forwardOneDay();
}
// check unstreamed eth
assertEq(escrow.unstreamedETHForNoun(1), 0.75 ether);
assertEq(escrow.ticksLeftForNoun(1), 15);

(uint256 unstreamedETH, uint256 ticksLeft) = escrow.unstreamedETHAndTicksLeftForNoun(1);
assertEq(unstreamedETH, 0.75 ether);
assertEq(ticksLeft, 15);

// forward 15 more ticks
for (uint i; i < 15; i++) {
forwardOneDay();
}
// check unstreamed eth
assertEq(escrow.unstreamedETHForNoun(1), 0 ether);
assertEq(escrow.ticksLeftForNoun(1), 0);
}

function test_unstreamedETHForNoun_canceledStream() public {
Expand All @@ -757,13 +764,15 @@ contract UnstreamedETHTest is BaseStreamEscrowTest {

// 1 ether / 20 = 0.05 eth per tick
assertEq(escrow.unstreamedETHForNoun(1), 1 ether);
assertEq(escrow.ticksLeftForNoun(1), 20);

// forward 5 ticks
for (uint i; i < 5; i++) {
forwardOneDay();
}
// check unstreamed eth
assertEq(escrow.unstreamedETHForNoun(1), 0.75 ether);
assertEq(escrow.ticksLeftForNoun(1), 15);

// cancel stream
vm.prank(streamCreator);
Expand All @@ -773,17 +782,20 @@ contract UnstreamedETHTest is BaseStreamEscrowTest {

// check unstreamed eth is zero
assertEq(escrow.unstreamedETHForNoun(1), 0 ether);
assertEq(escrow.ticksLeftForNoun(1), 0);
}

function test_unstreamedETHForNoun_returnsZeroForNonExistentStream() public {
assertEq(escrow.unstreamedETHForNoun(1), 0 ether);
assertEq(escrow.ticksLeftForNoun(1), 0);

// forward 5 ticks
for (uint i; i < 5; i++) {
forwardOneDay();
}

assertEq(escrow.unstreamedETHForNoun(3), 0 ether);
assertEq(escrow.ticksLeftForNoun(3), 0);
}
}

Expand Down

0 comments on commit c46406b

Please sign in to comment.