refactor(StakeManager): replace epoch.startTime with a global startTime

As now all epochEnd() are multiples of EPOCH_SIZE, we can drop the startTime from the struct Epoch, and avoid storing an information that can be cheaply calculated on the fly.

- Refactored the StakeManager contract to optimize the finalization of epochs and execution of accounts and epochs.
- Replaced the Epoch.startTime with a new global variable startTime to keep track of the start time of the contract.
- Modified the finalizeEpoch function to use a temporary variable for the current epoch and calculate the epoch reward only for the current epoch.
- Updated the migrationInitialize function to set the start time using the new startTime variable.
- Modified the epochEnd function to calculate the end time of the current epoch using the startTime variable.
- Modified the newEpoch function to calculate the number of the next epoch based on the startTime variable.

refactor(StakeManager): set startTime as immutable, load startTime from previousManager at constructor, verify startTime is correct on initializeMigration
This commit is contained in:
Ricardo Guilherme Schmidt 2024-09-25 12:55:53 -03:00
parent 34eea3c892
commit 9a4e3f4607
No known key found for this signature in database
GPG Key ID: 54B4454CC123AD17
3 changed files with 128 additions and 122 deletions

View File

@ -1,41 +1,42 @@
| contracts/StakeManager.sol:StakeManager contract | | | | | | | contracts/StakeManager.sol:StakeManager contract | | | | | |
|--------------------------------------------------|-----------------|--------|--------|---------|---------| |--------------------------------------------------|-----------------|--------|--------|--------|---------|
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 2447267 | 12701 | | | | | | 2465702 | 13041 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| EPOCH_SIZE | 263 | 263 | 263 | 263 | 1498 | | EPOCH_SIZE | 307 | 307 | 307 | 307 | 1498 |
| MAX_BOOST | 307 | 307 | 307 | 307 | 637 | | MAX_BOOST | 285 | 285 | 285 | 285 | 637 |
| MAX_LOCKUP_PERIOD | 361 | 361 | 361 | 361 | 4 | | MAX_LOCKUP_PERIOD | 361 | 361 | 361 | 361 | 4 |
| MIN_LOCKUP_PERIOD | 264 | 264 | 264 | 264 | 12 | | MIN_LOCKUP_PERIOD | 264 | 264 | 264 | 264 | 12 |
| YEAR | 263 | 263 | 263 | 263 | 637 | | YEAR | 263 | 263 | 263 | 263 | 637 |
| accounts | 1619 | 1619 | 1619 | 1619 | 144243 | | accounts | 1597 | 1597 | 1597 | 1597 | 144273 |
| calculateMPToMint | 740 | 740 | 740 | 740 | 1276 | | calculateMPToMint | 740 | 740 | 740 | 740 | 1276 |
| currentEpoch | 406 | 1072 | 406 | 2406 | 54 | | currentEpoch | 384 | 1050 | 384 | 2384 | 54 |
| epochEnd | 649 | 649 | 649 | 4649 | 23670 | | epochEnd | 627 | 627 | 627 | 2627 | 23675 |
| epochReward | 1425 | 2925 | 1425 | 5925 | 3 | | epochReward | 1425 | 2925 | 1425 | 5925 | 3 |
| executeAccount(address) | 171751 | 171751 | 171751 | 171751 | 2 | | executeAccount(address) | 151152 | 151152 | 151152 | 151152 | 2 |
| executeAccount(address,uint256) | 30909 | 76511 | 78308 | 240839 | 141830 | | executeAccount(address,uint256) | 26562 | 72616 | 74122 | 217879 | 141860 |
| executeEpoch() | 23458 | 146010 | 147059 | 3562421 | 23559 | | executeEpoch() | 23458 | 120800 | 121956 | 938985 | 23564 |
| executeEpoch(uint256) | 28208 | 28242 | 28208 | 28330 | 7 | | executeEpoch(uint256) | 23861 | 24497 | 23861 | 26090 | 7 |
| isVault | 562 | 970 | 562 | 2562 | 680 | | isVault | 540 | 948 | 540 | 2540 | 680 |
| lock | 23862 | 23862 | 23862 | 23862 | 1 | | lock | 23862 | 23862 | 23862 | 23862 | 1 |
| migrateTo | 23891 | 23897 | 23897 | 23903 | 2 | | migrateTo | 23891 | 23897 | 23897 | 23903 | 2 |
| migration | 395 | 1395 | 1395 | 2395 | 4 | | migration | 395 | 1395 | 1395 | 2395 | 4 |
| migrationInitialize | 24602 | 24602 | 24602 | 24602 | 1 | | migrationInitialize | 24602 | 24602 | 24602 | 24602 | 1 |
| newEpoch | 788 | 788 | 788 | 788 | 5 | | newEpoch | 441 | 441 | 441 | 441 | 5 |
| owner | 2432 | 2432 | 2432 | 2432 | 13 | | owner | 2432 | 2432 | 2432 | 2432 | 13 |
| pendingMPToBeMinted | 386 | 386 | 386 | 386 | 46422 | | pendingMPToBeMinted | 364 | 364 | 364 | 364 | 46432 |
| pendingReward | 386 | 1420 | 2386 | 2386 | 29 | | pendingReward | 386 | 1420 | 2386 | 2386 | 29 |
| previousManager | 275 | 275 | 275 | 275 | 13 | | previousManager | 275 | 275 | 275 | 275 | 13 |
| setVault | 46216 | 46216 | 46216 | 46216 | 139 | | setVault | 46239 | 46239 | 46239 | 46239 | 139 |
| stake | 23983 | 23983 | 23983 | 23983 | 1 | | stake | 23983 | 23983 | 23983 | 23983 | 1 |
| stakeRewardEstimate | 436 | 2345 | 2436 | 2436 | 22 | | stakeRewardEstimate | 436 | 2345 | 2436 | 2436 | 22 |
| stakedToken | 295 | 295 | 295 | 295 | 696 | | stakedToken | 295 | 295 | 295 | 295 | 696 |
| startMigration | 108281 | 108289 | 108293 | 108293 | 3 | | startMigration | 103580 | 103588 | 103592 | 103592 | 3 |
| totalSupply | 762 | 1943 | 2762 | 2762 | 22 | | startTime | 306 | 306 | 306 | 306 | 21 |
| totalSupplyBalance | 407 | 1807 | 2407 | 2407 | 20 | | totalSupply | 762 | 1943 | 2762 | 2762 | 22 |
| totalSupplyMP | 362 | 362 | 362 | 2362 | 46443 | | totalSupplyBalance | 407 | 1807 | 2407 | 2407 | 20 |
| unstake | 23841 | 23841 | 23841 | 23841 | 1 | | totalSupplyMP | 362 | 362 | 362 | 2362 | 46453 |
| unstake | 23819 | 23819 | 23819 | 23819 | 1 |
| contracts/StakeManager.sol:StakeRewardEstimate contract | | | | | | | contracts/StakeManager.sol:StakeRewardEstimate contract | | | | | |
@ -43,7 +44,7 @@
| 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 | 23720 | | getExpiredMP | 2427 | 2427 | 2427 | 2427 | 23725 |
| transferOwnership | 28533 | 28533 | 28533 | 28533 | 1 | | transferOwnership | 28533 | 28533 | 28533 | 28533 | 1 |
@ -54,11 +55,11 @@
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| acceptMigration | 35280 | 35280 | 35280 | 35280 | 2 | | acceptMigration | 35280 | 35280 | 35280 | 35280 | 2 |
| leave | 35266 | 35266 | 35266 | 35266 | 1 | | leave | 35266 | 35266 | 35266 | 35266 | 1 |
| lock | 45569 | 108497 | 66661 | 254253 | 7 | | lock | 43329 | 96146 | 64421 | 204745 | 7 |
| owner | 362 | 362 | 362 | 362 | 679 | | owner | 362 | 362 | 362 | 362 | 679 |
| stake | 27265 | 284241 | 267959 | 354106 | 684 | | stake | 27265 | 282079 | 265719 | 351866 | 684 |
| stakedToken | 212 | 212 | 212 | 212 | 2 | | stakedToken | 212 | 212 | 212 | 212 | 2 |
| unstake | 42429 | 104547 | 80498 | 272053 | 11 | | unstake | 40167 | 95818 | 78688 | 233549 | 11 |
| contracts/VaultFactory.sol:VaultFactory contract | | | | | | | contracts/VaultFactory.sol:VaultFactory contract | | | | | |
@ -76,24 +77,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 |
| approve | 46175 | 46240 | 46199 | 46367 | 679 | | approve | 46175 | 46239 | 46199 | 46367 | 679 |
| balanceOf | 561 | 2102 | 2561 | 2561 | 30842 | | balanceOf | 561 | 2107 | 2561 | 2561 | 30744 |
| script/Deploy.s.sol:Deploy contract | | | | | | | script/Deploy.s.sol:Deploy contract | | | | | |
|-------------------------------------|-----------------|---------|---------|---------|---------| |-------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 6048121 | 29198 | | | | | | 6118940 | 29538 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| run | 5301161 | 5301161 | 5301161 | 5301161 | 66 | | run | 5316305 | 5316305 | 5316305 | 5316305 | 66 |
| script/DeployMigrationStakeManager.s.sol:DeployMigrationStakeManager contract | | | | | | | script/DeployMigrationStakeManager.s.sol:DeployMigrationStakeManager contract | | | | | |
|-------------------------------------------------------------------------------|-----------------|---------|---------|---------|---------| |-------------------------------------------------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 3211677 | 15966 | | | | | | 3282471 | 16306 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| run | 2286831 | 2286831 | 2286831 | 2286831 | 19 | | run | 2302633 | 2302633 | 2302633 | 2302633 | 19 |
| script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | | | script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | |
@ -116,9 +117,9 @@
| test/script/DeployBroken.s.sol:DeployBroken contract | | | | | | | test/script/DeployBroken.s.sol:DeployBroken contract | | | | | |
|------------------------------------------------------|-----------------|---------|---------|---------|---------| |------------------------------------------------------|-----------------|---------|---------|---------|---------|
| Deployment Cost | Deployment Size | | | | | | Deployment Cost | Deployment Size | | | | |
| 4732891 | 22996 | | | | | | 4803693 | 23336 | | | | |
| Function Name | min | avg | median | max | # calls | | Function Name | min | avg | median | max | # calls |
| run | 4140983 | 4140983 | 4140983 | 4140983 | 1 | | run | 4156127 | 4156127 | 4156127 | 4156127 | 1 |

View File

@ -1,67 +1,67 @@
CreateVaultTest:testDeployment() (gas: 9774) CreateVaultTest:testDeployment() (gas: 9774)
CreateVaultTest:test_createVault() (gas: 714022) CreateVaultTest:test_createVault() (gas: 714022)
ExecuteAccountTest:testDeployment() (gas: 28807) ExecuteAccountTest:testDeployment() (gas: 28785)
ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1624700) ExecuteAccountTest:test_ExecuteAccountLimit() (gas: 1580255)
ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 5413915) ExecuteAccountTest:test_ExecuteAccountMintMP() (gas: 5297777)
ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1869543) ExecuteAccountTest:test_RevertWhen_InvalidLimitEpoch() (gas: 1788412)
ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 339213337) ExecuteAccountTest:test_ShouldNotMintMoreThanCap() (gas: 321397553)
ExecuteEpochTest:testDeployment() (gas: 28808) ExecuteEpochTest:testDeployment() (gas: 28786)
ExecuteEpochTest:testNewDeployment() (gas: 30880) ExecuteEpochTest:testNewDeployment() (gas: 30858)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterEpochEnd() (gas: 1375214) ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterEpochEnd() (gas: 1366211)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1402953) ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1385045)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 1654146) ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 1629507)
ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1412668) ExecuteEpochTest:test_ExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1395200)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochAfterEnd() (gas: 2068294) ExecuteEpochTest:test_ExecuteEpochExecuteEpochAfterEnd() (gas: 1936210)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochs() (gas: 2794284) ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochs() (gas: 2523553)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1502361) ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsJumoMany() (gas: 1479491)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 2804020) ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTime() (gas: 2533729)
ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1512053) ExecuteEpochTest:test_ExecuteEpochExecuteEpochExecuteAccountAfterManyEpochsWithBrokenTimeJumpMany() (gas: 1489623)
ExecuteEpochTest:test_ExecuteEpochNewEpoch() (gas: 3781443) ExecuteEpochTest:test_ExecuteEpochNewEpoch() (gas: 1122339)
ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 119582) ExecuteEpochTest:test_ExecuteEpochShouldIncreaseEpoch() (gas: 92413)
ExecuteEpochTest:test_ExecuteEpochShouldIncreasePendingReward() (gas: 278146) ExecuteEpochTest:test_ExecuteEpochShouldIncreasePendingReward() (gas: 256446)
ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() (gas: 43290) ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochBeforeEnd() (gas: 38984)
ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 154405) ExecuteEpochTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 149638)
LeaveTest:testDeployment() (gas: 28785) LeaveTest:testDeployment() (gas: 28763)
LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 1330232) LeaveTest:test_RevertWhen_NoPendingMigration() (gas: 1327993)
LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 31964) LeaveTest:test_RevertWhen_SenderIsNotVault() (gas: 31964)
LockTest:testDeployment() (gas: 28785) LockTest:testDeployment() (gas: 28763)
LockTest:test_NewLockupPeriod() (gas: 1333546) LockTest:test_NewLockupPeriod() (gas: 1329045)
LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1305853) LockTest:test_RevertWhen_InvalidNewLockupPeriod() (gas: 1301374)
LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1589343) LockTest:test_RevertWhen_InvalidUpdateLockupPeriod() (gas: 1545884)
LockTest:test_RevertWhen_SenderIsNotVault() (gas: 31856) LockTest:test_RevertWhen_SenderIsNotVault() (gas: 31856)
LockTest:test_ShouldIncreaseBonusMP() (gas: 1316098) LockTest:test_ShouldIncreaseBonusMP() (gas: 1311575)
LockTest:test_UpdateLockupPeriod() (gas: 1704020) LockTest:test_UpdateLockupPeriod() (gas: 1634540)
MigrateTest:testDeployment() (gas: 28785) MigrateTest:testDeployment() (gas: 28763)
MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1294285) MigrateTest:test_RevertWhen_NoPendingMigration() (gas: 1292046)
MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 31976) MigrateTest:test_RevertWhen_SenderIsNotVault() (gas: 31976)
MigrationInitializeTest:testDeployment() (gas: 28785) MigrationInitializeTest:testDeployment() (gas: 28763)
MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5149125) MigrationInitializeTest:test_RevertWhen_MigrationPending() (gas: 5181388)
MigrationStakeManagerTest:testDeployment() (gas: 28785) MigrationStakeManagerTest:testDeployment() (gas: 28763)
MigrationStakeManagerTest:testNewDeployment() (gas: 30924) MigrationStakeManagerTest:testNewDeployment() (gas: 30902)
MigrationStakeManagerTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 154370) MigrationStakeManagerTest:test_ExecuteEpochShouldNotIncreaseEpochInMigration() (gas: 149603)
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: 28557) StakeManagerTest:testDeployment() (gas: 28535)
StakeTest:testDeployment() (gas: 28763) StakeTest:testDeployment() (gas: 28741)
StakeTest:test_RevertWhen_InvalidLockupPeriod() (gas: 1085114) StakeTest:test_RevertWhen_InvalidLockupPeriod() (gas: 1080635)
StakeTest:test_RevertWhen_Restake() (gas: 1321326) StakeTest:test_RevertWhen_Restake() (gas: 1316847)
StakeTest:test_RevertWhen_RestakeWithLock() (gas: 1325340) StakeTest:test_RevertWhen_RestakeWithLock() (gas: 1320861)
StakeTest:test_RevertWhen_SenderIsNotVault() (gas: 32018) StakeTest:test_RevertWhen_SenderIsNotVault() (gas: 32018)
StakeTest:test_RevertWhen_StakeIsTooLow() (gas: 820001) StakeTest:test_RevertWhen_StakeIsTooLow() (gas: 817762)
StakeTest:test_RevertWhen_StakeTokenTransferFails() (gas: 211363) StakeTest:test_RevertWhen_StakeTokenTransferFails() (gas: 211363)
StakeTest:test_StakeWithLockBonusMP() (gas: 2364371) StakeTest:test_StakeWithLockBonusMP() (gas: 2357564)
StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 1325644) StakeTest:test_StakeWithoutLockUpTimeMintsMultiplierPoints() (gas: 1322003)
StakedTokenTest:testStakeToken() (gas: 7616) StakedTokenTest:testStakeToken() (gas: 7616)
UnstakeTest:testDeployment() (gas: 28807) UnstakeTest:testDeployment() (gas: 28785)
UnstakeTest:test_RevertWhen_AmountMoreThanBalance() (gas: 1301908) UnstakeTest:test_RevertWhen_AmountMoreThanBalance() (gas: 1297407)
UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1348907) UnstakeTest:test_RevertWhen_FundsLocked() (gas: 1342144)
UnstakeTest:test_RevertWhen_SenderIsNotVault() (gas: 31879) UnstakeTest:test_RevertWhen_SenderIsNotVault() (gas: 31857)
UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 7404912) UnstakeTest:test_UnstakeShouldBurnMultiplierPoints() (gas: 6526945)
UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1323170) UnstakeTest:test_UnstakeShouldReturnFund_NoLockUp() (gas: 1319573)
UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1471322) UnstakeTest:test_UnstakeShouldReturnFund_WithLockUp() (gas: 1440205)
UserFlowsTest:testDeployment() (gas: 28785) UserFlowsTest:testDeployment() (gas: 28763)
UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8,uint128) (runs: 106, μ: 141478753, ~: 140916642) UserFlowsTest:test_PendingMPToBeMintedCannotBeGreaterThanTotalSupplyMP(uint8,uint128) (runs: 106, μ: 131402731, ~: 130856521)
UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1514680) UserFlowsTest:test_StakeWithLockUpTimeLocksStake() (gas: 1481301)
UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 2502892) UserFlowsTest:test_StakedSupplyShouldIncreaseAndDecreaseAgain() (gas: 2494771)
VaultFactoryTest:testDeployment() (gas: 9774) VaultFactoryTest:testDeployment() (gas: 9774)

View File

@ -74,6 +74,7 @@ contract StakeManager is Ownable {
uint256 public currentEpoch; uint256 public currentEpoch;
uint256 public pendingReward; uint256 public pendingReward;
uint256 public immutable startTime;
uint256 public pendingMPToBeMinted; uint256 public pendingMPToBeMinted;
uint256 public totalSupplyMP; uint256 public totalSupplyMP;
@ -140,30 +141,33 @@ contract StakeManager is Ownable {
* @param _limitEpoch Until what epoch it should be executed * @param _limitEpoch Until what epoch it should be executed
*/ */
function finalizeEpoch(uint256 _limitEpoch) private { function finalizeEpoch(uint256 _limitEpoch) private {
while (currentEpoch < _limitEpoch) { uint256 tempCurrentEpoch = currentEpoch;
Epoch storage thisEpoch = epochs[currentEpoch]; while (tempCurrentEpoch < _limitEpoch) {
uint256 expiredMP = stakeRewardEstimate.getExpiredMP(currentEpoch); Epoch storage thisEpoch = epochs[tempCurrentEpoch];
uint256 expiredMP = stakeRewardEstimate.getExpiredMP(tempCurrentEpoch);
if (expiredMP > 0) { if (expiredMP > 0) {
totalMPPerEpoch -= expiredMP; totalMPPerEpoch -= expiredMP;
stakeRewardEstimate.deleteExpiredMP(currentEpoch); stakeRewardEstimate.deleteExpiredMP(tempCurrentEpoch);
} }
thisEpoch.estimatedMP = totalMPPerEpoch - currentEpochTotalExpiredMP; thisEpoch.estimatedMP = totalMPPerEpoch - currentEpochTotalExpiredMP;
delete currentEpochTotalExpiredMP; delete currentEpochTotalExpiredMP;
pendingMPToBeMinted += thisEpoch.estimatedMP; pendingMPToBeMinted += thisEpoch.estimatedMP;
//finalize current epoch //finalize current epoch
thisEpoch.epochReward = epochReward(); if (tempCurrentEpoch == currentEpoch) {
thisEpoch.epochReward = epochReward();
pendingReward += thisEpoch.epochReward;
}
thisEpoch.totalSupply = totalSupply(); thisEpoch.totalSupply = totalSupply();
pendingReward += thisEpoch.epochReward;
//create new epoch //create new epoch
currentEpoch++; tempCurrentEpoch++;
epochs[currentEpoch].startTime = thisEpoch.startTime + EPOCH_SIZE;
} }
currentEpoch = tempCurrentEpoch;
} }
constructor(address _stakedToken, address _previousManager) { constructor(address _stakedToken, address _previousManager) {
epochs[0].startTime = block.timestamp; startTime = (_previousManager == address(0)) ? block.timestamp : StakeManager(_previousManager).startTime();
previousManager = StakeManager(_previousManager); previousManager = StakeManager(_previousManager);
stakedToken = ERC20(_stakedToken); stakedToken = ERC20(_stakedToken);
if (address(previousManager) != address(0)) { if (address(previousManager) != address(0)) {
@ -351,7 +355,7 @@ contract StakeManager is Ownable {
currentEpoch, currentEpoch,
totalSupplyMP, totalSupplyMP,
totalSupplyBalance, totalSupplyBalance,
epochs[currentEpoch].startTime, startTime,
totalMPPerEpoch, totalMPPerEpoch,
pendingMPToBeMinted, pendingMPToBeMinted,
currentEpochTotalExpiredMP currentEpochTotalExpiredMP
@ -364,13 +368,13 @@ contract StakeManager is Ownable {
* @param _currentEpoch epoch of old manager * @param _currentEpoch epoch of old manager
* @param _totalSupplyMP MP supply on old manager * @param _totalSupplyMP MP supply on old manager
* @param _totalSupplyBalance stake supply on old manager * @param _totalSupplyBalance stake supply on old manager
* @param _epochStartTime epoch start time of old manager * @param _startTime start time of old manager
*/ */
function migrationInitialize( function migrationInitialize(
uint256 _currentEpoch, uint256 _currentEpoch,
uint256 _totalSupplyMP, uint256 _totalSupplyMP,
uint256 _totalSupplyBalance, uint256 _totalSupplyBalance,
uint256 _epochStartTime, uint256 _startTime,
uint256 _totalMPPerEpoch, uint256 _totalMPPerEpoch,
uint256 _pendingMPToBeMinted, uint256 _pendingMPToBeMinted,
uint256 _currentEpochExpiredMP uint256 _currentEpochExpiredMP
@ -382,10 +386,12 @@ contract StakeManager is Ownable {
if (currentEpoch > 0) { if (currentEpoch > 0) {
revert StakeManager__AlreadyProcessedEpochs(); revert StakeManager__AlreadyProcessedEpochs();
} }
if (_startTime != startTime) {
revert StakeManager__InvalidMigration();
}
currentEpoch = _currentEpoch; currentEpoch = _currentEpoch;
totalSupplyMP = _totalSupplyMP; totalSupplyMP = _totalSupplyMP;
totalSupplyBalance = _totalSupplyBalance; totalSupplyBalance = _totalSupplyBalance;
epochs[currentEpoch].startTime = _epochStartTime;
totalMPPerEpoch = _totalMPPerEpoch; totalMPPerEpoch = _totalMPPerEpoch;
pendingMPToBeMinted = _pendingMPToBeMinted; pendingMPToBeMinted = _pendingMPToBeMinted;
currentEpochTotalExpiredMP = _currentEpochExpiredMP; currentEpochTotalExpiredMP = _currentEpochExpiredMP;
@ -458,7 +464,7 @@ contract StakeManager is Ownable {
while (userEpoch < _limitEpoch) { while (userEpoch < _limitEpoch) {
Epoch storage iEpoch = epochs[userEpoch]; Epoch storage iEpoch = epochs[userEpoch];
//mint multiplier points to that epoch //mint multiplier points to that epoch
_mintMP(account, iEpoch.startTime + EPOCH_SIZE, iEpoch); _mintMP(account, startTime + (EPOCH_SIZE * (userEpoch + 1)), iEpoch);
uint256 userSupply = account.balance + account.totalMP; uint256 userSupply = account.balance + account.totalMP;
uint256 userEpochReward = Math.mulDiv(userSupply, iEpoch.epochReward, iEpoch.totalSupply); uint256 userEpochReward = Math.mulDiv(userSupply, iEpoch.epochReward, iEpoch.totalSupply);
userReward += userEpochReward; userReward += userEpochReward;
@ -608,7 +614,7 @@ contract StakeManager is Ownable {
* @return _epochEnd end time of current epoch * @return _epochEnd end time of current epoch
*/ */
function epochEnd() public view returns (uint256 _epochEnd) { function epochEnd() public view returns (uint256 _epochEnd) {
return epochs[currentEpoch].startTime + EPOCH_SIZE; return startTime + (EPOCH_SIZE * (currentEpoch + 1));
} }
/** /**
@ -616,7 +622,6 @@ contract StakeManager is Ownable {
* @return _newEpoch the number of the epoch after all epochs that can be processed * @return _newEpoch the number of the epoch after all epochs that can be processed
*/ */
function newEpoch() public view returns (uint256 _newEpoch) { function newEpoch() public view returns (uint256 _newEpoch) {
_newEpoch = currentEpoch; _newEpoch = (block.timestamp - startTime) / EPOCH_SIZE;
_newEpoch = _newEpoch + ((block.timestamp - epochs[_newEpoch].startTime) / EPOCH_SIZE);
} }
} }