Add tests to check correctness of staking contract functionality after proposal execution

This commit is contained in:
Theo 2023-06-14 06:51:42 -07:00
parent 808420b0d6
commit d90b460355
3 changed files with 83 additions and 1 deletions

@ -13,7 +13,7 @@ import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract TestProposal is MockProposal { contract TestProposal is MockProposal {
ITornadoStakingRewards staking = ITornadoStakingRewards(_stakingAddress); ITornadoStakingRewards staking = ITornadoStakingRewards(_stakingAddress);
function testGovernanceBalance() internal { function testGovernanceBalance() public {
IERC20 TORN = IERC20(_tokenAddress); IERC20 TORN = IERC20(_tokenAddress);
uint256 governanceBalanceBeforeExecution = TORN.balanceOf(_governanceAddress); uint256 governanceBalanceBeforeExecution = TORN.balanceOf(_governanceAddress);
@ -23,6 +23,11 @@ contract TestProposal is MockProposal {
uint256 governanceBalanceAfterExecution = TORN.balanceOf(_governanceAddress); uint256 governanceBalanceAfterExecution = TORN.balanceOf(_governanceAddress);
console2.log("Governance balance after proposal execution: %s TORN", governanceBalanceAfterExecution / _tornDecimals); console2.log("Governance balance after proposal execution: %s TORN", governanceBalanceAfterExecution / _tornDecimals);
uint256 governanceBalanceDifference = governanceBalanceBeforeExecution - governanceBalanceAfterExecution;
console2.log("Governance balance difference: %s TORN, %s", governanceBalanceDifference / _tornDecimals, governanceBalanceDifference);
require(governanceBalanceDifference == 51_038_101_771_805_874_869_526, "Incorrect Governance balance after execution");
} }
function testOtherUsersRewardUnchanged() public { function testOtherUsersRewardUnchanged() public {
@ -116,4 +121,72 @@ contract TestProposal is MockProposal {
"Staking replenish sum doesn't match with stakers restored rewards sum" "Staking replenish sum doesn't match with stakers restored rewards sum"
); );
} }
function testAccumulatedRewardPerTornNotChanged() public {
uint256 accumulatedRewardsPerTornBeforeExecution = staking.accumulatedRewardPerTorn();
console2.log("Accumulated reward per 1 TORN before proposal execution: %s", accumulatedRewardsPerTornBeforeExecution / _tornMaximumSupply);
createAndExecuteProposal();
uint256 accumulatedRewardsPerTornAfterExecution = staking.accumulatedRewardPerTorn();
console2.log("Accumulated reward per 1 TORN after proposal execution: %s", accumulatedRewardsPerTornAfterExecution / _tornMaximumSupply);
require(accumulatedRewardsPerTornBeforeExecution == accumulatedRewardsPerTornAfterExecution, "Accumulater reward per TORN changed");
}
function testRewardAccrualsMechanismCorrect() public executeCurrentProposalBefore {
IERC20 TORN = IERC20(_tokenAddress);
uint256 toBurn = 10_000 ether;
retrieveAndLockBalance(TEST_STAKER_PRIVATE_KEY, TEST_STAKER_ADDRESS, PROPOSAL_THRESHOLD);
uint256 stakerLockedBalance = governance.lockedBalance(TEST_STAKER_ADDRESS);
require(stakerLockedBalance == PROPOSAL_THRESHOLD, "Invalid test staker locked balance");
uint256 stakerRewardsBeforeBurning = staking.checkReward(TEST_STAKER_ADDRESS);
console2.log("Staking rewards before burning: %s TORN", stakerRewardsBeforeBurning / _tornDecimals);
burnTokens(_governanceAddress, toBurn, staking);
uint256 stakerRewardsAfterBurning = staking.checkReward(TEST_STAKER_ADDRESS);
console2.log(
"Staking rewards after burning 10 000 TORN: %s TORN\n", stakerRewardsAfterBurning / _tornDecimals
);
require(stakerRewardsAfterBurning > stakerRewardsBeforeBurning, "Rewards isn't changed after burning");
// All TORN, locked by users in Governance, is on the userVault contract balance
uint256 governanceLockedAmount = TORN.balanceOf(governance.userVault());
uint256 receivedReward = stakerRewardsAfterBurning - stakerRewardsBeforeBurning;
uint256 expectedRewards = stakerLockedBalance * toBurn / governanceLockedAmount;
console2.log("Expected staking rewards: %s TORN", expectedRewards / _tornDecimals);
console2.log("Staker received rewards: %s TORN\n", receivedReward / _tornDecimals);
require(receivedReward == expectedRewards, "Expected and received rewards don't match");
}
function testAccumulatedRewardCanBeUpdated() public executeCurrentProposalBefore {
uint256 accumulatedRewardPerTornBeforeBurning =
staking.accumulatedRewardPerTorn() / _tornMaximumSupply;
console2.log(
"Accumulated reward per TORN right after proposal execution: %s TORN",
accumulatedRewardPerTornBeforeBurning / _tornDecimals
);
burnTokens(_governanceAddress, 10_000_000 ether, staking);
uint256 accumulatedRewardPerTornAfterBurning = staking.accumulatedRewardPerTorn() / _tornMaximumSupply;
console2.log(
"Accumulated reward per TORN after burning 10 000 000 TORN: ~ %s TORN",
accumulatedRewardPerTornAfterBurning / _tornDecimals
);
require(
accumulatedRewardPerTornAfterBurning > accumulatedRewardPerTornBeforeBurning,
"Staking rewards isn't updated"
);
}
} }

@ -7,6 +7,7 @@ import { ERC20Permit } from "torn-token/contracts/ERC20Permit.sol";
import { Test } from "@forge-std/Test.sol"; import { Test } from "@forge-std/Test.sol";
import { Parameters } from "@proprietary/Parameters.sol"; import { Parameters } from "@proprietary/Parameters.sol";
import { ITornadoStakingRewards } from "@interfaces/ITornadoStakingRewards.sol";
import { Mock } from "./Mock.sol"; import { Mock } from "./Mock.sol";
import { IGovernance } from "./interfaces/IGovernance.sol"; import { IGovernance } from "./interfaces/IGovernance.sol";
@ -37,4 +38,10 @@ contract Utils is Parameters, Mock, Test {
vm.stopPrank(); vm.stopPrank();
/* ----------------------------*/ /* ----------------------------*/
} }
function burnTokens(address caller, uint256 amount, ITornadoStakingRewards staking) internal {
vm.startPrank(caller);
staking.addBurnRewards(amount);
vm.stopPrank();
}
} }

@ -41,4 +41,6 @@ interface IGovernance {
function unlock(uint256 amount) external; function unlock(uint256 amount) external;
function execute(uint256 proposalId) external payable; function execute(uint256 proposalId) external payable;
function userVault() external view returns (address);
} }