Skip to content

Commit

Permalink
96% tc
Browse files Browse the repository at this point in the history
  • Loading branch information
jordaniza committed Sep 10, 2024
1 parent 5968811 commit 62fbeae
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 22 deletions.
23 changes: 6 additions & 17 deletions src/escrow/increasing/QuadraticIncreasingEscrow.sol
Original file line number Diff line number Diff line change
Expand Up @@ -343,25 +343,14 @@ contract QuadraticIncreasingEscrow is
// check to see if we have an existing epoch for this token
uint256 userEpoch = userPointEpoch[_tokenId];

// TODO: this could be made a lot clearer
if (
// if we do have a point AND
// if we've already recorded a point for this timestamp
userEpoch != 0 && _userPointHistory[_tokenId][userEpoch].ts == uNew.ts
) {
// overwrite the last point
_userPointHistory[_tokenId][userEpoch] = uNew;

// the userpoint warmup records when the warmup would end
// irrespective of the start time of the lock
_userPointWarmup[_tokenId][userEpoch] = block.timestamp + warmupPeriod;
} else {
// otherwise, create a new epoch by incrementing the userEpoch
// and record the new point
// If this is a new timestamp, increment the epoch
if (userEpoch == 0 || _userPointHistory[_tokenId][userEpoch].ts != uNew.ts) {
userPointEpoch[_tokenId] = ++userEpoch;
_userPointHistory[_tokenId][userEpoch] = uNew;
_userPointWarmup[_tokenId][userEpoch] = block.timestamp + warmupPeriod;
}

// Record the new point and warmup period
_userPointHistory[_tokenId][userEpoch] = uNew;
_userPointWarmup[_tokenId][userEpoch] = block.timestamp + warmupPeriod;
}
}

Expand Down
9 changes: 5 additions & 4 deletions src/escrow/increasing/VotingEscrowIncreasing.sol
Original file line number Diff line number Diff line change
Expand Up @@ -366,16 +366,17 @@ contract VotingEscrow is
LockedBalance memory oldLocked = _locked[_tokenId];
uint256 value = oldLocked.amount;

// clear out the token data
_locked[_tokenId] = LockedBalance(0, 0);
totalLocked -= value;

// check for fees to be transferred
// do this before clearing the lock or it will be incorrect
uint256 fee = IExitQueue(queue).exit(_tokenId);
if (fee > 0) {
IERC20(token).safeTransfer(address(queue), fee);
}

// clear out the token data
_locked[_tokenId] = LockedBalance(0, 0);
totalLocked -= value;

// Burn the NFT and transfer the tokens to the user
_burn(_tokenId);
IERC20(token).safeTransfer(sender, value - fee);
Expand Down
9 changes: 9 additions & 0 deletions test/escrow/escrow/EscrowAdmin.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,13 @@ contract TestEscrowAdmin is EscrowBase {
}
vm.stopPrank();
}

// test unusued function revert
function testUnusedFunctionRevert() public {
vm.expectRevert();
escrow.totalVotingPowerAt(0);

vm.expectRevert();
escrow.totalVotingPower();
}
}
129 changes: 128 additions & 1 deletion test/escrow/escrow/EscrowWithdraw.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,136 @@ import {IEscrowCurveUserStorage} from "@escrow-interfaces/IEscrowCurveIncreasing
import {VotingEscrow} from "@escrow/VotingEscrowIncreasing.sol";

import {SimpleGaugeVoter, SimpleGaugeVoterSetup} from "src/voting/SimpleGaugeVoterSetup.sol";
import {IGaugeVote} from "src/voting/ISimpleGaugeVoter.sol";

contract TestWithdraw is EscrowBase, IEscrowCurveUserStorage, IGaugeVote {
address gauge = address(1);

GaugeVote[] votes;

contract TestCreateLock is EscrowBase, IEscrowCurveUserStorage {
function setUp() public override {
super.setUp();

vm.warp(1);

// make a voting gauge
voter.createGauge(gauge, "metadata");
votes.push(GaugeVote({gauge: gauge, weight: 1}));
}

// setup a fee withdrawal
function testFuzz_feeWithdrawal(uint64 _fee, uint128 _dep, address _who) public {
vm.assume(_who != address(0));
vm.assume(_dep > 0);

if (_fee > 1e18) _fee = 1e18;

queue.setFeePercent(_fee);

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

// voting active after cooldown
vm.warp(block.timestamp + 2 weeks + 1 hours);

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

// can't enter a withdrawal while voting
vm.expectRevert(CannotExit.selector);
escrow.beginWithdrawal(tokenId);

// enter a withdrawal
vm.startPrank(_who);
{
escrow.approve(address(escrow), tokenId);
escrow.resetVotesAndBeginWithdrawal(tokenId);
}
vm.stopPrank();

// can't withdraw if not ticket holder
vm.expectRevert(NotTicketHolder.selector);
escrow.withdraw(tokenId);

// can't force approve
vm.expectRevert("ERC721: approve caller is not token owner or approved for all");
vm.prank(_who);
escrow.approve(_who, tokenId);

// must wait till end of queue
vm.warp(block.timestamp + queue.cooldown() - 1);
vm.expectRevert(CannotExit.selector);
vm.prank(_who);
escrow.withdraw(tokenId);

uint fee = queue.calculateFee(tokenId);

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

// fee sent to queue
assertEq(token.balanceOf(address(queue)), fee);

// remainder sent to user
assertEq(token.balanceOf(_who), _dep - fee);

// nft is burned
assertEq(escrow.balanceOf(_who), 0);
assertEq(escrow.balanceOf(address(escrow)), 0);
assertEq(escrow.totalLocked(), 0);
}

function testFuzz_enterWithdrawal(uint128 _dep, address _who) public {
vm.assume(_who != address(0));
vm.assume(_dep > 0);

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

// voting active after cooldown
vm.warp(block.timestamp + 2 weeks + 1 hours);

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

// can't enter a withdrawal while voting
vm.expectRevert(CannotExit.selector);
escrow.beginWithdrawal(tokenId);

// enter a withdrawal
vm.startPrank(_who);
{
escrow.approve(address(escrow), tokenId);
escrow.resetVotesAndBeginWithdrawal(tokenId);
}
vm.stopPrank();

// should now have the nft in the escrow
assertEq(escrow.balanceOf(_who), 0);
assertEq(escrow.balanceOf(address(escrow)), 1);

// should be zero vp with the nft
assertEq(escrow.votingPower(tokenId), 0);

// should have a ticket expiring in a few days
assertEq(queue.canExit(tokenId), false);
assertEq(queue.queue(tokenId).exitDate, block.timestamp + 3 days);
}
}

0 comments on commit 62fbeae

Please sign in to comment.