Skip to content

Commit

Permalink
tests: add unit test for Gauge.sol and Vault.sol.
Browse files Browse the repository at this point in the history
  • Loading branch information
ludete committed Jun 30, 2024
1 parent ea5cea0 commit 3fcd275
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 4 deletions.
79 changes: 75 additions & 4 deletions test/TestGauge.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ import {Voter} from "../contracts/Voter.sol";
import {IGauge} from "../contracts/interfaces/IGauge.sol";
import {DAOForwarder} from "../contracts/DAOForwarder.sol";
import {TestToken} from "../contracts/test/TestToken.sol";
//import {TestStrategyManager} from "../contracts/test/TestStrategyManager.sol";
import {ProtocolTimeLibrary} from "../contracts/libraries/ProtocolTimeLibrary.sol";


contract TestGauge is Test{
Gauge public gauge;
DAOForwarder public forwarder;
TestToken public pool;
Voter public voter;
// TestStrategyManager public manager;

fallback() external payable {}

function setUp() public {
// manager = new TestStrategyManager();
pool = new TestToken("lp_pool", "pool");
forwarder = new DAOForwarder();
// manager & _factoryRegistry not used in gauge
Expand Down Expand Up @@ -54,9 +54,80 @@ contract TestGauge is Test{
assertEq(0, gauge.earned(address (this)));
}

function test_notifyRewardAmount() external {
function test_notifyRewardAmount_getReward_earned_withdraw_left() external {
console.log("block time: ", block.timestamp);
// 0. deposit
voter.reviveGauge(address(gauge));
pool.approve(address (gauge), 2 ether);
gauge.deposit(1 ether);

// 1. rewardAmount sender not voter failed
vm.expectRevert(IGauge.NotVoter.selector);
gauge.notifyRewardAmount{value: 1 ether}();

// 2. success rewards-1
// 2.1 deposit rewards to voter
voter.notifyRewardAmount{value: 90 ether}();
// 2.2 simulate notify rewards by voter
vm.prank(address(voter));
gauge.notifyRewardAmount{value: 1 ether}();
assertEq(1, gauge.lastUpdateTime());
assertEq(ProtocolTimeLibrary.epochNext(block.timestamp), gauge.periodFinish());
uint256 firstRate = gauge.rewardRate();
uint256 firstPeriod = gauge.periodFinish();
uint256 firstRewardPerToken = gauge.rewardPerToken();
console.log("firstRewardPerToken: ", firstRewardPerToken);
skip(3000); // blockTime will set after 3000s
assertEq(3001, block.timestamp);

// 3. success reward-2 in same epoch
vm.prank(address(voter));
gauge.notifyRewardAmount{value: 1 ether}();
assertEq(3001, gauge.lastUpdateTime());
assertGt(gauge.rewardRate(), firstRate);
assertEq(firstPeriod, gauge.periodFinish());
assertEq(2 ether, address (gauge).balance);
uint256 secondRewardPerToken = gauge.rewardPerToken();

// 4. view earned in first epoch
skip(7 days - block.timestamp);
assertEq(block.timestamp, firstPeriod);
uint256 firstEarned = gauge.earned(address (this));
console.log("firstPeriod: ", firstPeriod, "; blocktime: ", block.timestamp);
console.log("first earned 1-epoch: ", firstEarned);
console.log("secondRewardPerToken: ", secondRewardPerToken);

// 5. success reward-3 in next epoch
skip(1 days);
vm.prank(address(voter));
gauge.notifyRewardAmount{value: 0.1 ether}();
uint256 thirdPeriod = gauge.periodFinish();
assertNotEq(thirdPeriod, firstPeriod);
assertGt(firstRate, gauge.rewardRate());
skip(1 days);
uint256 thirdEarned = gauge.earned(address (this));
uint256 thirdRewardPerToken = gauge.rewardPerToken();
console.log("third earned 2-epoch", thirdEarned);
console.log("thirdPeriod: ", thirdPeriod, "; blocktime: ", block.timestamp);
console.log("thirdRewardPerToken: ", thirdRewardPerToken);

// 6. getRewards
uint256 before = address (this).balance;
assertEq(0, gauge.rewards(address (this)));
gauge.getReward(address (this));
uint256 after_ = address (this).balance;
assertEq(before + thirdEarned, after_);

// 7. withdraw
before = gauge.balanceOf(address (this));
gauge.withdraw(1 ether);
after_ = gauge.balanceOf(address (this));
assertEq(before - 1 ether, after_);
// Simulate the task of claiming rewards and then withdrawing lp in one block
assertEq(0, gauge.rewards(address(this)));

// 8. left
uint256 left = gauge.left();
console.log("left+thirdEarned: ", thirdEarned+left, ", totalReward: ", 2.1 ether);
}
}
80 changes: 80 additions & 0 deletions test/TestVault.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Test, console} from "forge-std/Test.sol";
import {Gauge} from "../contracts/gauges/Gauge.sol";
import {Vault} from "../contracts/Vault.sol";
import {Voter} from "../contracts/Voter.sol";
import {IVault} from "../contracts/interfaces/IVault.sol";
import {DAOForwarder} from "../contracts/DAOForwarder.sol";
import {TestToken} from "../contracts/test/TestToken.sol";
import {ProtocolTimeLibrary} from "../contracts/libraries/ProtocolTimeLibrary.sol";
import {TestStrategyManager} from "../contracts/test/TestStrategyManager.sol";
import {FactoryRegistry} from "../contracts/factories/FactoryRegistry.sol";
import {GaugeFactory} from "../contracts/factories/GaugeFactory.sol";
import {RewardsDistributor} from "../contracts/RewardsDistributor.sol";

contract TestVault is Test{
Vault public vault;
Voter public voter;
DAOForwarder public forwarder;
GaugeFactory public gaugeFactory;
FactoryRegistry public factoryRegistry;
RewardsDistributor public rdb;
TestStrategyManager public strategyManager;

function setUp() public {
forwarder = new DAOForwarder();
gaugeFactory = new GaugeFactory();
factoryRegistry = new FactoryRegistry(address (1), address (gaugeFactory));
strategyManager = new TestStrategyManager();
rdb = new RewardsDistributor(address(strategyManager));
voter = new Voter(address (forwarder), address(strategyManager), address(factoryRegistry));
vault = new Vault();
vault.initialize(address(voter), address(rdb));
}

function test_setGovernor() public {
//1. failed of NotGovernor
vm.prank(address (2));
vm.expectRevert(IVault.NotGovernor.selector);
vault.setGovernor(address (22));

//2. failed of ZeroAddress
vm.expectRevert(IVault.ZeroAddress.selector);
vault.setGovernor(address (0));

//3. success
vault.setGovernor(address (22));
assertEq(0, vault.activePeriod());
}

function test_updatePeriod_donate_withdraw() public {
// 1. fund native token to vault
vault.donate{value: 2 ether}();
assertEq(2 ether, address(vault).balance);

// 2. withdraw token
vault.withdraw(address (0), payable(address(2)), 1 ether);
assertEq(1 ether, address (2).balance);
assertEq(1 ether, address (vault).balance);

// 3. elapsed 1 week
skip(7 days);
// 4. not enough balance to emission
vm.expectRevert(IVault.InsufficientFund.selector);
uint256 _period = vault.updatePeriod();

// 5. updatePeriod success
payable(address (vault)).transfer(vault.weekly()-1 ether);
rdb.setVault(address (vault));
voter.initialize(new address[](0), address (vault));
_period = vault.updatePeriod();
assertEq(7 days, _period);
assertEq(address (vault).balance, 0 );
assertEq(address (strategyManager).balance, vault.weekly()/10);
assertEq(address (voter).balance, 90 * vault.weekly()/100);
}

fallback() external payable {}
}
25 changes: 25 additions & 0 deletions test/TestVoter.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Test, console} from "forge-std/Test.sol";
import {stdError} from "forge-std/StdError.sol";
import {Vault} from "../contracts/Vault.sol";
import {Voter} from "../contracts/Voter.sol";
import {DAOForwarder} from "../contracts/DAOForwarder.sol";
import {TestStrategyManager} from "../contracts/test/TestStrategyManager.sol";
//import {TestStrategyManager} from "../contracts/test/TestStrategyManager.sol";

contract TestVoter is Test{
Voter public voter;
DAOForwarder public forwarder;

function setUp() public {
forwarder = new DAOForwarder();
TestStrategyManager strategyManager = new TestStrategyManager();
voter = new Voter(address(forwarder), address(strategyManager), address(1));
}

function test_MaxEpochNum() external {
assertEq(30, voter.maxVotingNum());
}
}

0 comments on commit 3fcd275

Please sign in to comment.