From 47d7555c27e251cbbc98d628991b91635f599330 Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Sat, 24 Feb 2024 16:35:34 -0300 Subject: [PATCH] chore(StakeManager): add test for process account and unstake --- test/StakeManager.t.sol | 121 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 120 insertions(+), 1 deletion(-) diff --git a/test/StakeManager.t.sol b/test/StakeManager.t.sol index ea2a36c..467a0e9 100644 --- a/test/StakeManager.t.sol +++ b/test/StakeManager.t.sol @@ -3,7 +3,7 @@ pragma solidity ^0.8.19; import { ERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; -import { Test } from "forge-std/Test.sol"; +import { Test, console } from "forge-std/Test.sol"; import { Deploy } from "../script/Deploy.s.sol"; import { DeploymentConfig } from "../script/DeploymentConfig.s.sol"; import { StakeManager } from "../contracts/StakeManager.sol"; @@ -144,6 +144,53 @@ contract UnstakeTest is StakeManagerTest { assertEq(ERC20(stakeToken).balanceOf(address(userVault)), 0); assertEq(ERC20(stakeToken).balanceOf(testUser), 1000); } + + function test_UnstakeShouldBurnMultiplierPoints() public { + uint256 stakeAmount = 1000; + uint256 percentToBurn = 90; + deal(stakeToken, testUser, stakeAmount); + StakeVault userVault = _createTestVault(testUser); + + vm.startPrank(testUser); + ERC20(stakeToken).approve(address(userVault), stakeAmount); + + userVault.stake(stakeAmount, 0); + + assertEq(stakeManager.totalSupplyMP(), stakeAmount); + for (uint256 i = 0; i < 53; i++) { + vm.warp(stakeManager.epochEnd()); + stakeManager.executeAccount(address(userVault), i + 1); + } + (, uint256 balanceBefore, uint256 initialMPBefore, uint256 currentMPBefore,,,) = + stakeManager.accounts(address(userVault)); + uint256 totalSupplyMPBefore = stakeManager.totalSupplyMP(); + uint256 unstakeAmount = stakeAmount * percentToBurn / 100; + console.log("unstake", unstakeAmount); + + assertEq(ERC20(stakeToken).balanceOf(testUser), 0); + userVault.unstake(unstakeAmount); + vm.stopPrank(); + (, uint256 balanceAfter, uint256 initialMPAfter, uint256 currentMPAfter,,,) = + stakeManager.accounts(address(userVault)); + + uint256 totalSupplyMPAfter = stakeManager.totalSupplyMP(); + console.log("totalSupplyMPBefore", totalSupplyMPBefore); + console.log("totalSupplyMPAfter", totalSupplyMPAfter); + console.log("balanceBefore", balanceBefore); + console.log("balanceAfter", balanceAfter); + console.log("initialMPBefore", initialMPBefore); + console.log("initialMPAfter", initialMPAfter); + console.log("currentMPBefore", currentMPBefore); + console.log("currentMPAfter", currentMPAfter); + + uint256 reducedInitialMp = (initialMPBefore * percentToBurn / 100); + uint256 reducedCurrentMp = (currentMPBefore * percentToBurn / 100); + assertEq(balanceAfter, balanceBefore - (balanceBefore * percentToBurn / 100)); + assertEq(initialMPAfter, initialMPBefore - reducedInitialMp); + assertEq(currentMPAfter, currentMPBefore - reducedCurrentMp); + assertEq(totalSupplyMPAfter, totalSupplyMPBefore - reducedCurrentMp); + assertEq(ERC20(stakeToken).balanceOf(testUser), unstakeAmount); + } } contract LockTest is StakeManagerTest { @@ -226,6 +273,8 @@ contract MigrateTest is StakeManagerTest { } contract ExecuteAccountTest is StakeManagerTest { + StakeVault[] private userVaults; + function setUp() public override { StakeManagerTest.setUp(); } @@ -245,6 +294,76 @@ contract ExecuteAccountTest is StakeManagerTest { vm.expectRevert(StakeManager.StakeManager__InvalidLimitEpoch.selector); stakeManager.executeAccount(address(userVault), currentEpoch + 1); } + + function _createStakingAccount( + address owner, + uint256 amount, + uint256 lockTime + ) + internal + returns (StakeVault userVault) + { + deal(stakeToken, owner, amount); + userVault = _createTestVault(owner); + vm.startPrank(owner); + ERC20(stakeToken).approve(address(userVault), amount); + userVault.stake(amount, lockTime); + vm.stopPrank(); + } + + function test_ExecuteAccountMintMP() public { + uint256 stakeAmount = 10_000_000; + deal(stakeToken, testUser, stakeAmount); + + userVaults.push(_createStakingAccount(makeAddr("testUser"), stakeAmount, 0)); + userVaults.push(_createStakingAccount(makeAddr("testUser2"), stakeAmount, 0)); + userVaults.push(_createStakingAccount(makeAddr("testUser3"), stakeAmount, 0)); + + console.log("######### NOW", block.timestamp); + console.log("# START EPOCH", stakeManager.currentEpoch()); + console.log("# PND_REWARDS", stakeManager.pendingReward()); + + for (uint256 i = 0; i < 3; i++) { + deal(stakeToken, address(stakeManager), 100 ether); + vm.warp(stakeManager.epochEnd()); + console.log("######### NOW", block.timestamp); + stakeManager.executeEpoch(); + console.log("##### NEW EPOCH", stakeManager.currentEpoch()); + console.log("# PND_REWARDS", stakeManager.pendingReward()); + + for (uint256 j = 0; j < userVaults.length; j++) { + (address rewardAddress,,, uint256 currentMPBefore, uint256 lastMintBefore,, uint256 epochBefore) = + stakeManager.accounts(address(userVaults[j])); + uint256 rewardsBefore = ERC20(stakeToken).balanceOf(rewardAddress); + console.log("-Vault number", j); + console.log("--=====BEFORE====="); + console.log("---### currentMP :", currentMPBefore); + console.log("---#### lastMint :", lastMintBefore); + console.log("---## user_epoch :", epochBefore); + console.log("---##### rewards :", rewardsBefore); + console.log("--=====AFTER======"); + stakeManager.executeAccount(address(userVaults[j]), epochBefore + 1); + (,,, uint256 currentMP, uint256 lastMint,, uint256 epoch) = + stakeManager.accounts(address(userVaults[j])); + uint256 rewards = ERC20(stakeToken).balanceOf(rewardAddress); + console.log("---### deltaTime :", lastMint - lastMintBefore); + console.log("---### currentMP :", currentMP); + console.log("---#### lastMint :", lastMint); + console.log("---## user_epoch :", epoch); + console.log("---##### rewards :", rewards); + console.log("--=======#======="); + console.log("--# TOTAL_SUPPLY", stakeManager.totalSupply()); + console.log("--# PND_REWARDS", stakeManager.pendingReward()); + assertEq(lastMint, lastMintBefore + stakeManager.EPOCH_SIZE(), "must increaase lastMint"); + assertEq(epoch, epochBefore + 1, "must increase epoch"); + assertGt(currentMP, currentMPBefore, "must increase MPs"); + assertGt(rewards, rewardsBefore, "must increase rewards"); + lastMintBefore = lastMint; + epochBefore = epoch; + currentMPBefore = currentMP; + } + } + } } contract UserFlowsTest is StakeManagerTest {