Skip to content

Commit

Permalink
Add some tests to resetVotes
Browse files Browse the repository at this point in the history
  • Loading branch information
xavikh committed Dec 16, 2024
1 parent 61cbba8 commit 8693c46
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 3 deletions.
52 changes: 52 additions & 0 deletions test/escrow/escrow/EscrowWithdraw.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,58 @@ contract TestWithdraw is EscrowBase, IEscrowCurveTokenStorage, IGaugeVote, ITick
assertEq(nftLock.balanceOf(address(escrow)), 0);
assertEq(escrow.totalLocked(), 0);
}

function test_CanBeginWithdrawalDuringDistributionPeriod() public {
address _who = address(1);
uint128 _dep = 100e18;

// voting window is ea. 2 weeks + 1 hour
vm.warp(2 weeks + 1 hours + 1);

token.mint(_who, _dep);
uint tokenId;
vm.startPrank(_who);
{
token.approve(address(escrow), _dep);
tokenId = escrow.createLock(_dep);

// voting active after cooldown
// +1 week: voting ends
// +2 weeks: next voting period opens
vm.warp(block.timestamp + 2 weeks);

// make a vote
voter.vote(tokenId, votes);

// warp so cooldown crosses the week boundary
// and distribution period starts
vm.warp(block.timestamp + clock.epochNextCheckpointIn() + 1);
assertFalse(voter.votingActive());

nftLock.approve(address(escrow), tokenId);
escrow.resetVotesAndBeginWithdrawal(tokenId);
}
vm.stopPrank();

// must wait till after end of cooldown
vm.warp(block.timestamp + clock.epochNextCheckpointIn() + 1);

uint fee = queue.calculateFee(tokenId);

// withdraw
vm.prank(_who);
vm.expectEmit(true, true, false, true);
emit Withdraw(_who, tokenId, _dep - fee, block.timestamp, 0);
escrow.withdraw(tokenId);

// asserts
assertEq(token.balanceOf(address(queue)), fee);
assertEq(token.balanceOf(_who), _dep - fee);
assertEq(nftLock.balanceOf(_who), 0);
assertEq(nftLock.balanceOf(address(escrow)), 0);
assertEq(escrow.totalLocked(), 0);
}

// HAL-13: locks are re-used causing reverts and duplications
function testCanCreateLockAfterBurning() public {
address USER1 = address(1);
Expand Down
58 changes: 55 additions & 3 deletions test/voting/GaugeVote.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ contract TestGaugeVote is GaugeVotingBase {
voter.createGauge(gauge, "metadata");
}

function testFuzz_cannotVoteOutsideVotingWindow(uint256 time) public {
function testFuzz_cannotVoteOutsideVotingWindow(uint256 _time) public {
// warp to a random time
vm.warp(time);
vm.warp(_time);

// should now be inactive (we don't test this part herewe have the epoch logic tests)
vm.assume(!voter.votingActive());
Expand Down Expand Up @@ -97,7 +97,7 @@ contract TestGaugeVote is GaugeVotingBase {
assertEq(voter.isVoting(tokenId), true);

// warp to the next distribution period
vm.warp(block.timestamp + 1 weeks);
_increaseTime(1 weeks);
vm.assume(!voter.votingActive());

// try to reset
Expand All @@ -107,7 +107,59 @@ contract TestGaugeVote is GaugeVotingBase {
}
vm.stopPrank();

// check the vote
assertEq(voter.isVoting(tokenId), false);
assertEq(voter.gaugesVotedFor(tokenId).length, 0);
assertEq(voter.votes(tokenId, gauge), 0);
assertEq(voter.usedVotingPower(tokenId), 0);

// global state
assertEq(voter.totalVotingPowerCast(), 0);
assertEq(voter.gaugeVotes(gauge), 0);
}

function testFuzz_canResetAnytime(uint _time) public {
// create the vote
votes.push(GaugeVote(1, gauge));

// vote
vm.startPrank(owner);
{
voter.vote(tokenId, votes);
}
vm.stopPrank();

// check the vote
uint newVotingPower = escrow.votingPower(tokenId);
assertEq(voter.isVoting(tokenId), true);
assertEq(voter.gaugesVotedFor(tokenId).length, 1);
assertEq(voter.gaugesVotedFor(tokenId)[0], gauge);
assertEq(voter.votes(tokenId, gauge), newVotingPower);
assertEq(voter.usedVotingPower(tokenId), newVotingPower);

// global state
assertEq(voter.totalVotingPowerCast(), newVotingPower);
assertEq(voter.gaugeVotes(gauge), newVotingPower);

// warp to the next distribution period
_increaseTime(_time);

// try to reset
vm.startPrank(owner);
{
voter.reset(tokenId);
}
vm.stopPrank();

// check the vote
assertEq(voter.isVoting(tokenId), false);
assertEq(voter.gaugesVotedFor(tokenId).length, 0);
assertEq(voter.votes(tokenId, gauge), 0);
assertEq(voter.usedVotingPower(tokenId), 0);

// global state
assertEq(voter.totalVotingPowerCast(), 0);
assertEq(voter.gaugeVotes(gauge), 0);
}

// can't vote if you don't own the token
Expand Down

0 comments on commit 8693c46

Please sign in to comment.