refactor(StakeManager): Use maxMP instead of totalMP

This commit is contained in:
Ricardo Guilherme Schmidt 2024-11-12 10:10:21 -03:00
parent 3f4f6a7aaf
commit 9b7836c8cf
4 changed files with 131 additions and 156 deletions

View File

@ -1,7 +1,7 @@
| contracts/StakeManager.sol:StakeManager contract | | | | | | | contracts/StakeManager.sol:StakeManager contract | | | | | |
|--------------------------------------------------|-----------------|--------|--------|--------|---------| |--------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 2600451 | 13644 | | | | | | 2605863 | 13669 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| ACCURE_RATE | 328 | 328 | 328 | 328 | 1546 | | ACCURE_RATE | 328 | 328 | 328 | 328 | 1546 |
| MAX_LOCKUP_PERIOD | 405 | 405 | 405 | 405 | 4 | | MAX_LOCKUP_PERIOD | 405 | 405 | 405 | 405 | 4 |
@ -10,14 +10,14 @@
| REWARD_TOKEN | 317 | 317 | 317 | 317 | 744 | | REWARD_TOKEN | 317 | 317 | 317 | 317 | 744 |
| YEAR | 308 | 308 | 308 | 308 | 685 | | YEAR | 308 | 308 | 308 | 308 | 685 |
| acceptUpdate | 23633 | 23633 | 23633 | 23633 | 1 | | acceptUpdate | 23633 | 23633 | 23633 | 23633 | 1 |
| accounts | 1572 | 1572 | 1572 | 1572 | 154869 | | accounts | 1572 | 1572 | 1572 | 1572 | 155271 |
| calculateMP | 798 | 798 | 798 | 798 | 1372 | | calculateMP | 798 | 798 | 798 | 798 | 1371 |
| currentEpoch | 384 | 1050 | 384 | 2384 | 54 | | currentEpoch | 384 | 1050 | 384 | 2384 | 54 |
| epochEnd | 627 | 627 | 627 | 2627 | 25441 | | epochEnd | 627 | 627 | 627 | 2627 | 25508 |
| epochReward | 1381 | 2881 | 1381 | 5881 | 3 | | epochReward | 1381 | 2881 | 1381 | 5881 | 3 |
| executeAccount(address) | 33469 | 110665 | 149264 | 149264 | 3 | | executeAccount(address) | 33469 | 110371 | 148822 | 148822 | 3 |
| executeAccount(address,uint256) | 26562 | 72195 | 74101 | 199919 | 152455 | | executeAccount(address,uint256) | 26562 | 71632 | 73548 | 195495 | 152857 |
| executeEpoch() | 23435 | 120639 | 121820 | 900335 | 25330 | | executeEpoch() | 23435 | 120644 | 121820 | 900335 | 25397 |
| executeEpoch(uint256) | 23861 | 24497 | 23861 | 26090 | 7 | | executeEpoch(uint256) | 23861 | 24497 | 23861 | 26090 | 7 |
| expiredStakeStorage | 416 | 2325 | 2416 | 2416 | 22 | | expiredStakeStorage | 416 | 2325 | 2416 | 2416 | 22 |
| isTrustedCodehash | 541 | 944 | 541 | 2541 | 728 | | isTrustedCodehash | 541 | 944 | 541 | 2541 | 728 |
@ -28,13 +28,13 @@
| newEpoch | 463 | 463 | 463 | 463 | 5 | | newEpoch | 463 | 463 | 463 | 463 | 5 |
| owner | 2432 | 2432 | 2432 | 2432 | 13 | | owner | 2432 | 2432 | 2432 | 2432 | 13 |
| pendingReward | 408 | 1442 | 2408 | 2408 | 29 | | pendingReward | 408 | 1442 | 2408 | 2408 | 29 |
| potentialMP | 408 | 408 | 408 | 408 | 49964 | | potentialMP | 408 | 408 | 408 | 408 | 50098 |
| previousManager | 297 | 297 | 297 | 297 | 13 | | previousManager | 297 | 297 | 297 | 297 | 13 |
| setTrustedCodehash | 47982 | 47982 | 47982 | 47982 | 147 | | setTrustedCodehash | 47982 | 47982 | 47982 | 47982 | 147 |
| stake | 24047 | 24047 | 24047 | 24047 | 1 | | stake | 24047 | 24047 | 24047 | 24047 | 1 |
| startMigration | 103624 | 103632 | 103636 | 103636 | 3 | | startMigration | 103624 | 103632 | 103636 | 103636 | 3 |
| startTime | 306 | 306 | 306 | 306 | 21 | | startTime | 306 | 306 | 306 | 306 | 21 |
| totalMP | 385 | 385 | 385 | 2385 | 49985 | | totalMP | 385 | 385 | 385 | 2385 | 50119 |
| totalStaked | 386 | 1786 | 2386 | 2386 | 20 | | totalStaked | 386 | 1786 | 2386 | 2386 | 20 |
| totalSupply | 784 | 1965 | 2784 | 2784 | 22 | | totalSupply | 784 | 1965 | 2784 | 2784 | 22 |
| unstake | 23841 | 23841 | 23841 | 23841 | 1 | | unstake | 23841 | 23841 | 23841 | 23841 | 1 |
@ -48,10 +48,10 @@
| STAKING_TOKEN | 193 | 193 | 193 | 193 | 2 | | STAKING_TOKEN | 193 | 193 | 193 | 193 | 2 |
| acceptMigration | 35141 | 35141 | 35141 | 35141 | 2 | | acceptMigration | 35141 | 35141 | 35141 | 35141 | 2 |
| leave | 35196 | 35196 | 35196 | 35196 | 1 | | leave | 35196 | 35196 | 35196 | 35196 | 1 |
| lock | 43196 | 71076 | 62042 | 162843 | 7 | | lock | 43196 | 71013 | 62042 | 162400 | 7 |
| owner | 318 | 318 | 318 | 318 | 727 | | owner | 318 | 318 | 318 | 318 | 727 |
| stake | 27291 | 282802 | 266296 | 351880 | 732 | | stake | 27291 | 283772 | 266970 | 352542 | 732 |
| unstake | 40179 | 88236 | 78765 | 184769 | 11 | | unstake | 40179 | 88155 | 78765 | 184327 | 11 |
| contracts/VaultFactory.sol:VaultFactory contract | | | | | | | contracts/VaultFactory.sol:VaultFactory contract | | | | | |
@ -69,24 +69,24 @@
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 0 | 0 | | | | | | 0 | 0 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| getExpiredMP | 2427 | 2427 | 2427 | 2427 | 25486 | | getExpiredMP | 2427 | 2427 | 2427 | 2427 | 25553 |
| transferOwnership | 28533 | 28533 | 28533 | 28533 | 1 | | transferOwnership | 28533 | 28533 | 28533 | 28533 | 1 |
| script/Deploy.s.sol:Deploy contract | | | | | | | script/Deploy.s.sol:Deploy contract | | | | | |
|-------------------------------------|-----------------|---------|---------|---------|---------| |-------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 6220738 | 29931 | | | | | | 6226159 | 29956 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| run | 5420174 | 5420174 | 5420174 | 5420174 | 66 | | run | 5425181 | 5425181 | 5425181 | 5425181 | 66 |
| script/DeployMigrationStakeManager.s.sol:DeployMigrationStakeManager contract | | | | | | | script/DeployMigrationStakeManager.s.sol:DeployMigrationStakeManager contract | | | | | |
|-------------------------------------------------------------------------------|-----------------|---------|---------|---------|---------| |-------------------------------------------------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 3412741 | 16827 | | | | | | 3418160 | 16852 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| run | 2427736 | 2427736 | 2427736 | 2427736 | 19 | | run | 2432743 | 2432743 | 2432743 | 2432743 | 19 |
| script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | | | script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | |
@ -111,16 +111,16 @@
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 0 | 0 | | | | | | 0 | 0 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| approve | 46175 | 46235 | 46199 | 46367 | 727 | | approve | 46175 | 46233 | 46199 | 46367 | 727 |
| balanceOf | 561 | 2127 | 2561 | 2561 | 32612 | | balanceOf | 561 | 2128 | 2561 | 2561 | 32679 |
| test/script/DeployBroken.s.sol:DeployBroken contract | | | | | | | test/script/DeployBroken.s.sol:DeployBroken contract | | | | | |
|------------------------------------------------------|-----------------|---------|---------|---------|---------| |------------------------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 4918894 | 23867 | | | | | | 4924318 | 23892 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| run | 4266410 | 4266410 | 4266410 | 4266410 | 1 | | run | 4271418 | 4271418 | 4271418 | 4271418 | 1 |

View File

@ -1,67 +1,67 @@
CreateVaultTest:testDeployment() (gas: 9774) CreateVaultTest:testDeployment() (gas: 9774)
CreateVaultTest:test_createVault() (gas: 700333) CreateVaultTest:test_createVault() (gas: 700333)
ExecuteAccountTest:testDeployment() (gas: 28875) ExecuteAccountTest:testDeployment() (gas: 28853)
ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1559514) ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1559565)
ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 5232040) ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 5229838)
ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1769214) ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1769243)
ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 320225995) ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 319099087)
ExecuteEpochTest:testDeployment() (gas: 28876) ExecuteEpochTest:testDeployment() (gas: 28898)
ExecuteEpochTest:testNewDeployment() (gas: 30948) ExecuteEpochTest:testNewDeployment() (gas: 31015)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterEpochEnd() (gas: 1350443) ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterEpochEnd() (gas: 1351146)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1367228) ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1367601)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 1613317) ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 1613976)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1377593) ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1377966)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochAfterEnd() (gas: 1910717) ExecuteEpochTest:test_ExecuteEpochExecuteEpochAfterEnd() (gas: 1909295)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochs() (gas: 2492323) ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochs() (gas: 2489731)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1460710) ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1460839)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 2502709) ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 2500073)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1471052) ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1471203)
ExecuteEpochTest:test_ExecuteEpochNewEpoch() (gas: 1083615) ExecuteEpochTest:test_ExecuteEpochNewEpoch() (gas: 1083615)
ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 92277) ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 92233)
ExecuteEpochTest:test_ExecuteEpochShouldIncreasePendingReward() (gas: 252659) ExecuteEpochTest:test_ExecuteEpochShouldIncreasePendingReward() (gas: 252720)
ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() (gas: 38961) ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() (gas: 38962)
ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 149703) ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 149703)
LeaveTest:testDeployment() (gas: 28853) LeaveTest:testDeployment() (gas: 28831)
LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 1311974) LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 1312647)
LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 31740) LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 31763)
LockTest:testDeployment() (gas: 28853) LockTest:testDeployment() (gas: 28831)
LockTest:test_NewLockupPeriod() (gas: 1310978) LockTest:test_NewLockupPeriod() (gas: 1311618)
LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1285586) LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1286248)
LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1406860) LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1407167)
LockTest:test_RevertWhen_SenderIsNotVault() (gas: 31834) LockTest:test_RevertWhen_SenderIsNotVault() (gas: 31857)
LockTest:test_ShouldIncreaseBonusMP() (gas: 1293620) LockTest:test_ShouldIncreaseMaxMP() (gas: 1294282)
LockTest:test_UpdateLockupPeriod() (gas: 1470415) LockTest:test_UpdateLockupPeriod() (gas: 1470744)
MigrateTest:testDeployment() (gas: 28853) MigrateTest:testDeployment() (gas: 28831)
MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1276097) MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1276770)
MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 31739) MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 31762)
MigrationInitializeTest:testDeployment() (gas: 28853) MigrationInitializeTest:testDeployment() (gas: 28831)
MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5451158) MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5462037)
MigrationStakeManagerTest:testDeployment() (gas: 28853) MigrationStakeManagerTest:testDeployment() (gas: 28831)
MigrationStakeManagerTest:testNewDeployment() (gas: 30992) MigrationStakeManagerTest:testNewDeployment() (gas: 30948)
MigrationStakeManagerTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 149668) MigrationStakeManagerTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 149713)
SetStakeManagerTest:testDeployment() (gas: 9774) SetStakeManagerTest:testDeployment() (gas: 9774)
SetStakeManagerTest:test_RevertWhen_InvalidStakeManagerAddress() (gas: 63105) SetStakeManagerTest:test_RevertWhen_InvalidStakeManagerAddress() (gas: 63105)
SetStakeManagerTest:test_SetStakeManager() (gas: 41301) SetStakeManagerTest:test_SetStakeManager() (gas: 41301)
StakeManagerTest:testDeployment() (gas: 28625) StakeManagerTest:testDeployment() (gas: 28603)
StakeTest:testDeployment() (gas: 28831) StakeTest:testDeployment() (gas: 28831)
StakeTest:test_RevertWhen_InvalidLockupPeriod() (gas: 1057656) StakeTest:test_RevertWhen_InvalidLockupPeriod() (gas: 1057645)
StakeTest:test_RevertWhen_Restake() (gas: 1301092) StakeTest:test_RevertWhen_Restake() (gas: 1301832)
StakeTest:test_RevertWhen_RestakeWithLock() (gas: 1304525) StakeTest:test_RevertWhen_RestakeWithLock() (gas: 1305220)
StakeTest:test_RevertWhen_SenderIsNotVault() (gas: 32082) StakeTest:test_RevertWhen_SenderIsNotVault() (gas: 32104)
StakeTest:test_RevertWhen_StakeIsTooLow() (gas: 803915) StakeTest:test_RevertWhen_StakeIsTooLow() (gas: 803959)
StakeTest:test_RevertWhen_StakeTokenTransferFails() (gas: 207758) StakeTest:test_RevertWhen_StakeTokenTransferFails() (gas: 207758)
StakeTest:test_StakeWithLockBonusMP() (gas: 2323783) StakeTest:test_StakeWithLockMaxMP() (gas: 2325472)
StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 1297379) StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 1298474)
StakedTokenTest:testStakeToken() (gas: 7597) StakedTokenTest:testStakeToken() (gas: 7597)
UnstakeTest:testDeployment() (gas: 28875) UnstakeTest:testDeployment() (gas: 28853)
UnstakeTest:test_RevertWhen_AmountMoreThanBalance() (gas: 1281609) UnstakeTest:test_RevertWhen_AmountMoreThanBalance() (gas: 1282314)
UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1325777) UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1326462)
UnstakeTest:test_RevertWhen_SenderIsNotVault() (gas: 31879) UnstakeTest:test_RevertWhen_SenderIsNotVault() (gas: 31857)
UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 6432067) UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 6413830)
UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1303800) UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1304463)
UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1387208) UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1387562)
UserFlowsTest:testDeployment() (gas: 28853) UserFlowsTest:testDeployment() (gas: 28831)
UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8,uint128) (runs: 114, μ: 130857603, ~: 130246926) UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8,uint128) (runs: 114, μ: 130456714, ~: 129540928)
UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1425816) UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1426181)
UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 2461484) UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 2462875)
VaultFactoryTest:testDeployment() (gas: 9774) VaultFactoryTest:testDeployment() (gas: 9774)

View File

@ -25,7 +25,7 @@ contract StakeManager is StakeMath, TrustedCodehashAccess, IStakeManager {
struct Account { struct Account {
address rewardAddress; address rewardAddress;
uint256 balance; uint256 balance;
uint256 bonusMP; uint256 maxMP;
uint256 totalMP; uint256 totalMP;
uint256 lastMint; uint256 lastMint;
uint256 lockUntil; uint256 lockUntil;
@ -167,12 +167,13 @@ contract StakeManager is StakeMath, TrustedCodehashAccess, IStakeManager {
uint256 mpLimitEpoch = currentEpoch + epochAmountToReachMpLimit; uint256 mpLimitEpoch = currentEpoch + epochAmountToReachMpLimit;
uint256 lastEpochAmountToMint = ((mpPerEpoch * (epochAmountToReachMpLimit + 1)) - maxMpToMint); uint256 lastEpochAmountToMint = ((mpPerEpoch * (epochAmountToReachMpLimit + 1)) - maxMpToMint);
uint256 bonusMP = _calculateInitialMP(_amount) + _calculateBonusMP(_amount, _seconds); uint256 bonusMP = _calculateInitialMP(_amount) + _calculateBonusMP(_amount, _seconds);
uint256 maxMP = _calculateMaxMP(_amount, _seconds);
// account initialization // account initialization
accounts[msg.sender] = Account({ accounts[msg.sender] = Account({
rewardAddress: StakeVault(msg.sender).owner(), rewardAddress: StakeVault(msg.sender).owner(),
balance: _amount, balance: _amount,
bonusMP: bonusMP, maxMP: maxMP,
totalMP: bonusMP, totalMP: bonusMP,
lastMint: block.timestamp, lastMint: block.timestamp,
lockUntil: block.timestamp + _seconds, lockUntil: block.timestamp + _seconds,
@ -208,8 +209,8 @@ contract StakeManager is StakeMath, TrustedCodehashAccess, IStakeManager {
} }
_processAccount(account, currentEpoch); _processAccount(account, currentEpoch);
uint256 reducedMP = Math.mulDiv(_amount, account.totalMP, account.balance); uint256 reducedTotalMP = Math.mulDiv(_amount, account.totalMP, account.balance);
uint256 reducedInitialMP = Math.mulDiv(_amount, account.bonusMP, account.balance); uint256 reducedMaxMP = Math.mulDiv(_amount, account.maxMP, account.balance);
uint256 mpPerEpoch = _calculateAccuredMP(account.balance, ACCURE_RATE); uint256 mpPerEpoch = _calculateAccuredMP(account.balance, ACCURE_RATE);
expiredStakeStorage.decrementExpiredMP(account.mpLimitEpoch, mpPerEpoch); expiredStakeStorage.decrementExpiredMP(account.mpLimitEpoch, mpPerEpoch);
@ -219,10 +220,10 @@ contract StakeManager is StakeMath, TrustedCodehashAccess, IStakeManager {
//update storage //update storage
account.balance -= _amount; account.balance -= _amount;
account.bonusMP -= reducedInitialMP; account.maxMP -= reducedMaxMP;
account.totalMP -= reducedMP; account.totalMP -= reducedTotalMP;
totalStaked -= _amount; totalStaked -= _amount;
totalMP -= reducedMP; totalMP -= reducedTotalMP;
} }
/** /**
@ -261,7 +262,7 @@ contract StakeManager is StakeMath, TrustedCodehashAccess, IStakeManager {
//update account storage //update account storage
account.lockUntil = lockUntil; account.lockUntil = lockUntil;
account.bonusMP += bonusMP; account.maxMP += bonusMP;
account.totalMP += bonusMP; account.totalMP += bonusMP;
//update global storage //update global storage
totalMP += bonusMP; totalMP += bonusMP;
@ -477,50 +478,18 @@ contract StakeManager is StakeMath, TrustedCodehashAccess, IStakeManager {
* @param epoch Epoch to increment total supply * @param epoch Epoch to increment total supply
*/ */
function _mintMP(Account storage account, uint256 processTime, Epoch storage epoch) private { function _mintMP(Account storage account, uint256 processTime, Epoch storage epoch) private {
uint256 mpToMint = _getMaxMPToMint( uint256 accruedMP = _calculateAccuredMP(account.balance, processTime - account.lastMint);
_calculateAccuredMP(account.balance, processTime - account.lastMint), if (accruedMP + account.totalMP > account.maxMP) {
account.balance, accruedMP = account.maxMP - account.totalMP; //how much left to reach cap
account.bonusMP, }
account.totalMP
);
//update storage //update storage
account.lastMint = processTime; account.lastMint = processTime;
account.totalMP += mpToMint; account.totalMP += accruedMP;
totalMP += mpToMint; totalMP += accruedMP;
//mp estimation //mp estimation
epoch.potentialMP -= mpToMint; epoch.potentialMP -= accruedMP;
potentialMP -= mpToMint; potentialMP -= accruedMP;
}
/**
* @notice Calculates maximum multiplier point increase for given balance
* @param _mpToMint tested value
* @param _balance balance of account
* @param _totalMP total multiplier point of the account
* @param _bonusMP bonus multiplier point of the account
* @return _maxMpToMint maximum multiplier points to mint
*/
function _getMaxMPToMint(
uint256 _mpToMint,
uint256 _balance,
uint256 _bonusMP,
uint256 _totalMP
)
private
pure
returns (uint256 _maxMpToMint)
{
// Maximum multiplier point for given balance
_maxMpToMint = _calculateMaxAccuredMP(_balance) + _bonusMP;
if (_mpToMint + _totalMP > _maxMpToMint) {
//reached cap when increasing MP
return _maxMpToMint - _totalMP; //how much left to reach cap
} else {
//not reached capw hen increasing MP
return _mpToMint; //just return tested value
}
} }
/** /**

View File

@ -9,11 +9,11 @@ import { DeployMigrationStakeManager } from "../script/DeployMigrationStakeManag
import { DeploymentConfig } from "../script/DeploymentConfig.s.sol"; import { DeploymentConfig } from "../script/DeploymentConfig.s.sol";
import { StakeManager, IStakeManager, ExpiredStakeStorage } from "../contracts/StakeManager.sol"; import { StakeManager, IStakeManager, ExpiredStakeStorage } from "../contracts/StakeManager.sol";
import { ITrustedCodehashAccess } from "../contracts/interfaces/ITrustedCodehashAccess.sol"; import { ITrustedCodehashAccess } from "../contracts/interfaces/ITrustedCodehashAccess.sol";
import { MultiplierPointMath } from "../contracts/MultiplierPointMath.sol"; import { StakeMath } from "../contracts/StakeMath.sol";
import { StakeVault } from "../contracts/StakeVault.sol"; import { StakeVault } from "../contracts/StakeVault.sol";
import { VaultFactory } from "../contracts/VaultFactory.sol"; import { VaultFactory } from "../contracts/VaultFactory.sol";
contract StakeManagerTest is Test { contract StakeManagerTest is Test, StakeMath {
DeploymentConfig internal deploymentConfig; DeploymentConfig internal deploymentConfig;
StakeManager internal stakeManager; StakeManager internal stakeManager;
VaultFactory internal vaultFactory; VaultFactory internal vaultFactory;
@ -89,37 +89,43 @@ contract StakeTest is StakeManagerTest {
stakeManager.stake(100, 1); stakeManager.stake(100, 1);
} }
function test_StakeWithLockBonusMP() public { function test_StakeWithLockMaxMP() public {
uint256 stakeAmount = 10_000; uint256 stakeAmount = 10_000;
uint256 lockTime = stakeManager.MIN_LOCKUP_PERIOD(); uint256 lockTime = stakeManager.MIN_LOCKUP_PERIOD();
StakeVault userVault = _createStakingAccount(testUser, stakeAmount, 0, stakeAmount); StakeVault userVault = _createStakingAccount(testUser, stakeAmount, 0, stakeAmount);
(, uint256 balance, uint256 bonusMP, uint256 totalMP,,,,) = stakeManager.accounts(address(userVault)); (, uint256 balance, uint256 maxMP, uint256 totalMP,,,,) = stakeManager.accounts(address(userVault));
assertEq(balance, stakeAmount, "balance of user vault should be equal to stake amount after stake"); assertEq(balance, stakeAmount, "balance of user vault should be equal to stake amount after stake");
assertEq(bonusMP, stakeAmount, "bonusMP of user vault should be equal to stake amount after stake if no lock"); assertEq(
maxMP,
_calculateMaxMP(stakeAmount, 0),
"maxMP of user vault should be equal to stake amount after stake if no lock"
);
assertEq( assertEq(
totalMP, stakeAmount, "totalMP of user vault should be equal to stakeAmount after stake if no epochs passed" totalMP, stakeAmount, "totalMP of user vault should be equal to stakeAmount after stake if no epochs passed"
); );
vm.prank(testUser); vm.prank(testUser);
userVault.lock(lockTime); userVault.lock(lockTime);
uint256 estimatedBonusMp = stakeAmount + stakeManager.calculateMP(stakeAmount, lockTime); uint256 estimatedMaxMP = maxMP + _calculateAccuredMP(stakeAmount, lockTime);
uint256 estimatedTotalMP = _calculateInitialMP(stakeAmount) + _calculateBonusMP(stakeAmount, lockTime);
(, balance, bonusMP, totalMP,,,,) = stakeManager.accounts(address(userVault)); (, balance, maxMP, totalMP,,,,) = stakeManager.accounts(address(userVault));
assertEq(balance, stakeAmount, "balance of user vault should be equal to stake amount after lock"); assertEq(balance, stakeAmount, "balance of user vault should be equal to stake amount after lock");
assertEq(bonusMP, estimatedBonusMp, "bonusMP of user vault should be equal to estimated bonusMP after lock"); assertEq(maxMP, estimatedMaxMP, "maxMP of user vault should be equal to estimated maxMP after lock");
assertEq(totalMP, bonusMP, "totalMP of user vault should be equal to bonusMP after lock if no epochs passed"); assertEq(
totalMP, estimatedTotalMP, "totalMP of user vault should be equal to maxMP after lock if no epochs passed"
);
StakeVault userVault2 = _createStakingAccount(testUser, stakeAmount, lockTime, stakeAmount); StakeVault userVault2 = _createStakingAccount(testUser, stakeAmount, lockTime, stakeAmount);
(, balance, bonusMP, totalMP,,,,) = stakeManager.accounts(address(userVault2)); (, balance, maxMP, totalMP,,,,) = stakeManager.accounts(address(userVault2));
assertEq(balance, stakeAmount, "balance of user vault should be equal to stake amount after stake locked"); assertEq(balance, stakeAmount, "balance of user vault should be equal to stake amount after stake locked");
assertEq(maxMP, estimatedMaxMP, "maxMP of user vault should be equal to estimated maxMP after stake locked");
assertEq( assertEq(
bonusMP, estimatedBonusMp, "bonusMP of user vault should be equal to estimated bonusMP after stake locked" totalMP,
); estimatedTotalMP,
assertEq( "totalMP of user vault should be equal to maxMP after stake locked if no epochs passed"
totalMP, bonusMP, "totalMP of user vault should be equal to bonusMP after stake locked if no epochs passed"
); );
} }
@ -173,10 +179,10 @@ contract StakeTest is StakeManagerTest {
} }
function test_StakeWithoutLockUpTimeMintsMultiplierPoints() public { function test_StakeWithoutLockUpTimeMintsMultiplierPoints() public {
uint256 stakeAmount = 54; uint256 stakeAmount = MIN_BALANCE;
StakeVault userVault = _createStakingAccount(testUser, stakeAmount, 0, stakeAmount); StakeVault userVault = _createStakingAccount(testUser, stakeAmount, 0, stakeAmount);
(,, uint256 totalMP,,,,,) = stakeManager.accounts(address(userVault)); (,,, uint256 totalMP,,,,) = stakeManager.accounts(address(userVault));
assertEq(stakeManager.totalMP(), stakeAmount, "total multiplier point supply"); assertEq(stakeManager.totalMP(), stakeAmount, "total multiplier point supply");
assertEq(totalMP, stakeAmount, "user multiplier points"); assertEq(totalMP, stakeAmount, "user multiplier points");
@ -254,7 +260,7 @@ contract UnstakeTest is StakeManagerTest {
vm.warp(stakeManager.epochEnd()); vm.warp(stakeManager.epochEnd());
stakeManager.executeAccount(address(userVault), i + 1); stakeManager.executeAccount(address(userVault), i + 1);
} }
(, uint256 balanceBefore, uint256 bonusMPBefore, uint256 totalMPBefore,,,,) = (, uint256 balanceBefore, uint256 maxMPBefore, uint256 totalMPBefore,,,,) =
stakeManager.accounts(address(userVault)); stakeManager.accounts(address(userVault));
uint256 totalSupplyMPBefore = stakeManager.totalMP(); uint256 totalSupplyMPBefore = stakeManager.totalMP();
uint256 unstakeAmount = stakeAmount * percentToBurn / 100; uint256 unstakeAmount = stakeAmount * percentToBurn / 100;
@ -262,7 +268,7 @@ contract UnstakeTest is StakeManagerTest {
assertEq(ERC20(stakeToken).balanceOf(testUser), 0); assertEq(ERC20(stakeToken).balanceOf(testUser), 0);
userVault.unstake(unstakeAmount); userVault.unstake(unstakeAmount);
(, uint256 balanceAfter, uint256 bonusMPAfter, uint256 totalMPAfter,,,,) = (, uint256 balanceAfter, uint256 maxMPAfter, uint256 totalMPAfter,,,,) =
stakeManager.accounts(address(userVault)); stakeManager.accounts(address(userVault));
uint256 totalSupplyMPAfter = stakeManager.totalMP(); uint256 totalSupplyMPAfter = stakeManager.totalMP();
@ -270,13 +276,13 @@ contract UnstakeTest is StakeManagerTest {
console.log("totalSupplyMPAfter", totalSupplyMPAfter); console.log("totalSupplyMPAfter", totalSupplyMPAfter);
console.log("balanceBefore", balanceBefore); console.log("balanceBefore", balanceBefore);
console.log("balanceAfter", balanceAfter); console.log("balanceAfter", balanceAfter);
console.log("bonusMPBefore", bonusMPBefore); console.log("maxMPBefore", maxMPBefore);
console.log("bonusMPAfter", bonusMPAfter); console.log("maxMPAfter", maxMPAfter);
console.log("totalMPBefore", totalMPBefore); console.log("totalMPBefore", totalMPBefore);
console.log("totalMPAfter", totalMPAfter); console.log("totalMPAfter", totalMPAfter);
assertEq(balanceAfter, balanceBefore - (balanceBefore * percentToBurn / 100)); assertEq(balanceAfter, balanceBefore - (balanceBefore * percentToBurn / 100));
assertEq(bonusMPAfter, bonusMPBefore - (bonusMPBefore * percentToBurn / 100)); assertEq(maxMPAfter, maxMPBefore - (maxMPBefore * percentToBurn / 100));
assertEq(totalMPAfter, totalMPBefore - (totalMPBefore * percentToBurn / 100)); assertEq(totalMPAfter, totalMPBefore - (totalMPBefore * percentToBurn / 100));
assertEq(totalSupplyMPAfter, totalSupplyMPBefore - (totalMPBefore * percentToBurn / 100)); assertEq(totalSupplyMPAfter, totalSupplyMPBefore - (totalMPBefore * percentToBurn / 100));
assertEq(ERC20(stakeToken).balanceOf(testUser), unstakeAmount); assertEq(ERC20(stakeToken).balanceOf(testUser), unstakeAmount);
@ -304,10 +310,10 @@ contract LockTest is StakeManagerTest {
vm.startPrank(testUser); vm.startPrank(testUser);
userVault.lock(lockTime); userVault.lock(lockTime);
(, uint256 balance, uint256 bonusMP, uint256 totalMP,,,,) = stakeManager.accounts(address(userVault)); (, uint256 balance, uint256 maxMP, uint256 totalMP,,,,) = stakeManager.accounts(address(userVault));
console.log("balance", balance); console.log("balance", balance);
console.log("bonusMP", bonusMP); console.log("maxMP", maxMP);
console.log("totalMP", totalMP); console.log("totalMP", totalMP);
} }
@ -326,12 +332,12 @@ contract LockTest is StakeManagerTest {
vm.warp(block.timestamp + stakeManager.MIN_LOCKUP_PERIOD() - 1); vm.warp(block.timestamp + stakeManager.MIN_LOCKUP_PERIOD() - 1);
stakeManager.executeAccount(address(userVault)); stakeManager.executeAccount(address(userVault));
(, uint256 balance, uint256 bonusMP, uint256 totalMP,, uint256 lockUntil,,) = (, uint256 balance, uint256 maxMP, uint256 totalMP,, uint256 lockUntil,,) =
stakeManager.accounts(address(userVault)); stakeManager.accounts(address(userVault));
vm.startPrank(testUser); vm.startPrank(testUser);
userVault.lock(minLockup - 1); userVault.lock(minLockup - 1);
(, balance, bonusMP, totalMP,, lockUntil,,) = stakeManager.accounts(address(userVault)); (, balance, maxMP, totalMP,, lockUntil,,) = stakeManager.accounts(address(userVault));
assertEq(lockUntil, block.timestamp + minLockup); assertEq(lockUntil, block.timestamp + minLockup);
@ -353,21 +359,21 @@ contract LockTest is StakeManagerTest {
userVault.lock(minLockup - 1); userVault.lock(minLockup - 1);
} }
function test_ShouldIncreaseBonusMP() public { function test_ShouldIncreaseMaxMP() public {
uint256 stakeAmount = 100; uint256 stakeAmount = 100;
uint256 lockTime = stakeManager.MAX_LOCKUP_PERIOD(); uint256 lockTime = stakeManager.MAX_LOCKUP_PERIOD();
StakeVault userVault = _createStakingAccount(testUser, stakeAmount); StakeVault userVault = _createStakingAccount(testUser, stakeAmount);
(, uint256 balance, uint256 bonusMP, uint256 totalMP,,,,) = stakeManager.accounts(address(userVault)); (, uint256 balance, uint256 maxMP, uint256 totalMP,,,,) = stakeManager.accounts(address(userVault));
uint256 totalSupplyMPBefore = stakeManager.totalMP(); uint256 totalSupplyMPBefore = stakeManager.totalMP();
vm.startPrank(testUser); vm.startPrank(testUser);
userVault.lock(lockTime); userVault.lock(lockTime);
//solhint-disable-next-line max-line-length //solhint-disable-next-line max-line-length
(, uint256 newBalance, uint256 newBonusMP, uint256 newCurrentMP,,,,) = stakeManager.accounts(address(userVault)); (, uint256 newBalance, uint256 newMaxMP, uint256 newCurrentMP,,,,) = stakeManager.accounts(address(userVault));
uint256 totalSupplyMPAfter = stakeManager.totalMP(); uint256 totalSupplyMPAfter = stakeManager.totalMP();
assertGt(totalSupplyMPAfter, totalSupplyMPBefore, "totalMP"); assertGt(totalSupplyMPAfter, totalSupplyMPBefore, "totalMP");
assertGt(newBonusMP, bonusMP, "bonusMP"); assertGt(newMaxMP, maxMP, "maxMP");
assertGt(newCurrentMP, totalMP, "totalMP"); assertGt(newCurrentMP, totalMP, "totalMP");
assertEq(newBalance, balance, "balance"); assertEq(newBalance, balance, "balance");
} }
@ -717,7 +723,7 @@ contract UserFlowsTest is StakeManagerTest {
public public
{ {
uint8 accountNum = 5; uint8 accountNum = 5;
uint256 minimumPossibleStake = 53; //less than this the stake per epoch of the account would be 0 uint256 minimumPossibleStake = MIN_BALANCE; //less than this the stake per epoch of the account would be 0
uint256 baseStakeAmount = uint256 baseStakeAmount =
(minimumPossibleStake * (uint256(randomStakeMultiplier) + 1)) + uint256(randomStakeAddition); (minimumPossibleStake * (uint256(randomStakeMultiplier) + 1)) + uint256(randomStakeAddition);
uint256 epochsAmountToReachCap = 0; uint256 epochsAmountToReachCap = 0;