Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: add unstreamedETHForNoun #870

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/nouns-contracts/contracts/StreamEscrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,22 @@ contract StreamEscrow is IStreamEscrow {
return !streams[nounId].canceled && streams[nounId].lastTick > currentTick;
}

/**
* @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) {
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
59 changes: 59 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,65 @@ 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_unstreamedETHForNoun_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);
}

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

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

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

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