Skip to content

Commit

Permalink
add unstreamedETHForNoun
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrai committed Dec 2, 2024
1 parent 09d5d0d commit 172db75
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/nouns-contracts/contracts/StreamEscrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,17 @@ contract StreamEscrow is IStreamEscrow {
return !streams[nounId].canceled && streams[nounId].lastTick > currentTick;
}

function unstreamedETHForNoun(uint256 nounId) public view returns (uint256) {
Stream memory stream = streams[nounId];
uint32 currentTick_ = currentTick;
if (!isStreamActive(stream, currentTick_)) {
return 0;
}

uint256 ticksLeft = stream.lastTick - currentTick_;
return ticksLeft * stream.ethPerTick;
}

function isStreamActive(Stream memory stream, uint32 tick) internal pure returns (bool) {
return !stream.canceled && stream.lastTick > tick;
}
Expand Down
48 changes: 48 additions & 0 deletions packages/nouns-contracts/test/foundry/StreamEscrow.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,54 @@ contract RescueTokensTest is BaseStreamEscrowTest {
}
}

contract UnstreamedETHTest is BaseStreamEscrowTest {
function test_unstreamedETHForNoun() public {
vm.prank(streamCreator);
escrow.forwardAllAndCreateStream{ value: 1 ether }({ nounId: 1, streamLengthInTicks: 20 });

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

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

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

function test_unstreamETHForNoun_canceledStream() public {
vm.prank(streamCreator);
escrow.forwardAllAndCreateStream{ value: 1 ether }({ nounId: 1, streamLengthInTicks: 20 });

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

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

// cancel stream
vm.prank(streamCreator);
nounsToken.approve(address(escrow), 1);
vm.prank(streamCreator);
escrow.cancelStream(1);

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

contract StreamEscrowGasTest is BaseStreamEscrowTest {
function setUp() public virtual override {
super.setUp();
Expand Down

0 comments on commit 172db75

Please sign in to comment.