proposal-25-restore-rewards/test/Proposal.t.sol

108 lines
4.7 KiB
Solidity
Raw Normal View History

// SPDX-License-Identifier: MIT
pragma solidity ^0.6.12;
pragma experimental ABIEncoderV2;
import { MockProposal } from "./MockProposal.sol";
2023-06-13 01:17:34 +03:00
import { Staker, ITornadoStakingRewards } from "@interfaces/ITornadoStakingRewards.sol";
import { Test } from "@forge-std/Test.sol";
import "@forge-std/console2.sol";
2023-06-13 01:17:34 +03:00
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
contract TestProposal is MockProposal {
2023-06-13 01:17:34 +03:00
ITornadoStakingRewards staking = ITornadoStakingRewards(_stakingAddress);
2023-06-13 01:17:34 +03:00
function testOtherUsersRewardUnchanged() public {
uint256 rewardsBeforeProposal = staking.checkReward(TEST_REAL_ADDRESS_WITH_BALANCE);
console2.log("Developer rewards before proposal execution: %s TORN", rewardsBeforeProposal);
createAndExecuteProposal();
uint256 rewardsAfterProposal = staking.checkReward(TEST_REAL_ADDRESS_WITH_BALANCE);
console2.log("Developer rewards after proposal execution: %s TORN", rewardsBeforeProposal);
require(rewardsAfterProposal == rewardsBeforeProposal, "Other stakers rewards changed");
}
function testCannotCallSetRewardsNotFromGovernance() public executeCurrentProposalBefore {
// Trying to set rewards to myself, expect error revert call
vm.startPrank(TEST_ADDRESS_ONE);
vm.expectRevert(bytes("only governance"));
staking.setReward(TEST_ADDRESS_ONE, 100_000 ether);
vm.stopPrank();
require(staking.checkReward(TEST_ADDRESS_ONE) == 0, "Rewards accrued without permissions (not from Governance call)");
vm.startPrank(_governanceAddress);
staking.setReward(TEST_ADDRESS_ONE, 100_000 ether);
vm.stopPrank();
require(staking.checkReward(TEST_ADDRESS_ONE) == 100_000 ether, "Rewards not accrued by Governance");
}
function getStakersRewardsSum(Staker[385] memory stakers) internal returns (uint256) {
uint256 rewardsSum = 0;
for (uint16 i = 0; i < stakers.length; i++) {
rewardsSum += staking.checkReward(stakers[i].addr);
}
return rewardsSum;
}
function testVerifyStakersAccrual() public {
Staker[385] memory stakers = getOldStakers();
uint256[385] memory rewardsBefore;
uint256[385] memory rewardsAfter;
for (uint16 i = 0; i < stakers.length; i++) {
rewardsBefore[i] = staking.checkReward(stakers[i].addr);
}
createAndExecuteProposal();
for (uint16 i = 0; i < stakers.length; i++) {
rewardsAfter[i] = staking.checkReward(stakers[i].addr);
}
for (uint16 i = 0; i < stakers.length; i++) {
console2.log("\nStaker address: %s", stakers[i].addr);
console2.log("Rewards before: %s", rewardsBefore[i]);
console2.log("Rewards after: %s", rewardsAfter[i]);
console2.log("Real difference: %s", (rewardsAfter[i] - rewardsBefore[i]));
console2.log("Expected difference: %s", stakers[i].oldRewards);
require(rewardsAfter[i] - rewardsBefore[i] == stakers[i].oldRewards);
}
}
function testStakingContractReplenishedCorrect() public {
Staker[385] memory stakers = getOldStakers();
uint256 stakersRewardsSumBeforeExecution = getStakersRewardsSum(stakers);
console2.log("Old stakers rewards sum before proposal execution: %s TORN", stakersRewardsSumBeforeExecution / _tornDecimals);
uint256 stakingContractBalanceBeforeExecution = IERC20(_tokenAddress).balanceOf(_stakingAddress);
console2.log("Staking contract balance before proposal execution: %s TORN", stakingContractBalanceBeforeExecution / _tornDecimals);
createAndExecuteProposal();
uint256 stakersRewardsSumAfterExecution = getStakersRewardsSum(stakers);
console2.log("\nOld stakers rewards sum after proposal exectuion: %s TORN", stakersRewardsSumAfterExecution / _tornDecimals);
uint256 stakingContractBalanceAfterExecution = IERC20(_tokenAddress).balanceOf(_stakingAddress);
console2.log("Staking contract balance after proposal execution: %s TORN", stakingContractBalanceAfterExecution / _tornDecimals);
uint256 stakersRewardSumDifference = stakersRewardsSumAfterExecution - stakersRewardsSumBeforeExecution;
uint256 stakingContractBalanceDifference = stakingContractBalanceAfterExecution - stakingContractBalanceBeforeExecution;
console2.log("\nStaking contract replenish amount: %s TORN", stakingContractBalanceDifference / _tornDecimals);
console2.log("Stakers restored rewards sum: %s TORN", stakersRewardSumDifference / _tornDecimals);
require(
stakersRewardSumDifference == stakingContractBalanceDifference,
"Staking replenish sum doesn't match with stakers restored rewards sum"
);
}
}