From 353a4ff770d2d47949de1596f3c5470836a05907 Mon Sep 17 00:00:00 2001 From: Ricardo Guilherme Schmidt <3esmit@gmail.com> Date: Wed, 25 Sep 2024 15:02:38 -0300 Subject: [PATCH] refactor(StakeManager): optimize stake and lock functionality by removing mintBonusMP function --- .gas-report | 34 ++++++++++----------- contracts/StakeManager.sol | 60 +++++++++++++++++--------------------- 2 files changed, 44 insertions(+), 50 deletions(-) diff --git a/.gas-report b/.gas-report index defbd73..4ec3b9e 100644 --- a/.gas-report +++ b/.gas-report @@ -1,27 +1,27 @@ | contracts/StakeManager.sol:StakeManager contract | | | | | | |--------------------------------------------------|-----------------|--------|--------|--------|---------| | Deployment Cost | Deployment Size | | | | | -| 2931383 | 14953 | | | | | +| 2943163 | 15006 | | | | | | Function Name | min | avg | median | max | # calls | | EPOCH_SIZE | 307 | 307 | 307 | 307 | 1452 | | MAX_BOOST | 285 | 285 | 285 | 285 | 637 | | MAX_LOCKUP_PERIOD | 361 | 361 | 361 | 361 | 4 | | MIN_LOCKUP_PERIOD | 287 | 287 | 287 | 287 | 12 | | YEAR | 263 | 263 | 263 | 263 | 637 | -| accounts | 1597 | 1597 | 1597 | 1597 | 144321 | +| accounts | 1597 | 1597 | 1597 | 1597 | 144345 | | calculateMPToMint | 740 | 740 | 740 | 740 | 1276 | | currentEpoch | 406 | 1691 | 2406 | 2406 | 28 | -| epochEnd | 627 | 627 | 627 | 4627 | 23603 | +| epochEnd | 627 | 627 | 627 | 4627 | 23607 | | epochReward | 1403 | 2903 | 1403 | 5903 | 3 | -| executeAccount | 28897 | 76117 | 77904 | 171987 | 141882 | -| executeEpoch | 23436 | 146208 | 147383 | 204283 | 23542 | +| executeAccount | 28897 | 76116 | 77904 | 171987 | 141906 | +| executeEpoch | 23436 | 146208 | 147383 | 204283 | 23546 | | isVault | 540 | 927 | 540 | 2540 | 671 | | lock | 23840 | 23840 | 23840 | 23840 | 1 | | migrateTo | 23869 | 23875 | 23875 | 23881 | 2 | | migration | 439 | 1439 | 1439 | 2439 | 4 | | migrationInitialize | 24602 | 24602 | 24602 | 24602 | 1 | | owner | 2432 | 2432 | 2432 | 2432 | 13 | -| pendingMPToBeMinted | 364 | 364 | 364 | 364 | 46448 | +| pendingMPToBeMinted | 364 | 364 | 364 | 364 | 46456 | | pendingReward | 364 | 1398 | 2364 | 2364 | 29 | | previousManager | 275 | 275 | 275 | 275 | 13 | | setVault | 46239 | 46239 | 46239 | 46239 | 130 | @@ -31,7 +31,7 @@ | startMigration | 108037 | 108045 | 108049 | 108049 | 3 | | totalSupply | 740 | 1921 | 2740 | 2740 | 22 | | totalSupplyBalance | 385 | 1785 | 2385 | 2385 | 20 | -| totalSupplyMP | 385 | 385 | 385 | 2385 | 46469 | +| totalSupplyMP | 385 | 385 | 385 | 2385 | 46477 | | unstake | 23819 | 23819 | 23819 | 23819 | 1 | @@ -40,7 +40,7 @@ | Deployment Cost | Deployment Size | | | | | | 0 | 0 | | | | | | Function Name | min | avg | median | max | # calls | -| getExpiredMP | 2427 | 2427 | 2427 | 2427 | 23598 | +| getExpiredMP | 2427 | 2427 | 2427 | 2427 | 23602 | | transferOwnership | 28533 | 28533 | 28533 | 28533 | 1 | @@ -51,9 +51,9 @@ | Function Name | min | avg | median | max | # calls | | acceptMigration | 35258 | 35258 | 35258 | 35258 | 2 | | leave | 35244 | 35244 | 35244 | 35244 | 1 | -| lock | 45192 | 73900 | 66395 | 158442 | 7 | +| lock | 45192 | 75369 | 63954 | 181289 | 7 | | owner | 362 | 362 | 362 | 362 | 670 | -| stake | 27265 | 283296 | 267703 | 353862 | 675 | +| stake | 27265 | 283213 | 267660 | 353635 | 675 | | stakedToken | 212 | 212 | 212 | 212 | 2 | | unstake | 42163 | 92598 | 80285 | 207456 | 11 | @@ -74,23 +74,23 @@ | 0 | 0 | | | | | | Function Name | min | avg | median | max | # calls | | approve | 46175 | 46237 | 46199 | 46367 | 670 | -| balanceOf | 561 | 2108 | 2561 | 2561 | 30693 | +| balanceOf | 561 | 2108 | 2561 | 2561 | 30697 | | script/Deploy.s.sol:Deploy contract | | | | | | |-------------------------------------|-----------------|---------|---------|---------|---------| | Deployment Cost | Deployment Size | | | | | -| 6532373 | 31450 | | | | | +| 6544164 | 31503 | | | | | | Function Name | min | avg | median | max | # calls | -| run | 5752636 | 5752636 | 5752636 | 5752636 | 56 | +| run | 5763252 | 5763252 | 5763252 | 5763252 | 56 | | script/DeployMigrationStakeManager.s.sol:DeployMigrationStakeManager contract | | | | | | |-------------------------------------------------------------------------------|-----------------|---------|---------|---------|---------| | Deployment Cost | Deployment Size | | | | | -| 3695815 | 18218 | | | | | +| 3707603 | 18271 | | | | | | Function Name | min | avg | median | max | # calls | -| run | 2738306 | 2738306 | 2738306 | 2738306 | 9 | +| run | 2748922 | 2748922 | 2748922 | 2748922 | 9 | | script/DeploymentConfig.s.sol:DeploymentConfig contract | | | | | | @@ -113,9 +113,9 @@ | test/script/DeployBroken.s.sol:DeployBroken contract | | | | | | |------------------------------------------------------|-----------------|---------|---------|---------|---------| | Deployment Cost | Deployment Size | | | | | -| 5217091 | 25248 | | | | | +| 5228880 | 25301 | | | | | | Function Name | min | avg | median | max | # calls | -| run | 4592458 | 4592458 | 4592458 | 4592458 | 1 | +| run | 4603073 | 4603073 | 4603073 | 4603073 | 1 | diff --git a/contracts/StakeManager.sol b/contracts/StakeManager.sol index d6201d8..d977f03 100644 --- a/contracts/StakeManager.sol +++ b/contracts/StakeManager.sol @@ -182,8 +182,7 @@ contract StakeManager is Ownable { * @dev Reverts when amount staked results in less than 1 MP per epoch. */ function stake(uint256 _amount, uint256 _secondsToLock) external onlyVault noPendingMigration finalizeEpoch { - Account storage account = accounts[msg.sender]; - if (account.balance > 0 || account.lockUntil != 0) { + if (accounts[msg.sender].balance > 0) { revert StakeManager__AlreadyStaked(); } if (_secondsToLock != 0 && (_secondsToLock < MIN_LOCKUP_PERIOD || _secondsToLock > MAX_LOCKUP_PERIOD)) { @@ -200,16 +199,26 @@ contract StakeManager is Ownable { uint256 epochAmountToReachMpLimit = (maxMpToMint) / mpPerEpoch; uint256 mpLimitEpoch = currentEpoch + epochAmountToReachMpLimit; uint256 lastEpochAmountToMint = ((mpPerEpoch * (epochAmountToReachMpLimit + 1)) - maxMpToMint); + uint256 bonusMP = _amount; + if (_secondsToLock > 0) { + //bonus for lock time + bonusMP += _getMPToMint(_amount, _secondsToLock); + } // account initialization - account.lockUntil = block.timestamp + _secondsToLock; - account.epoch = currentEpoch; //starts in current epoch - account.rewardAddress = StakeVault(msg.sender).owner(); - _mintBonusMP(account, _secondsToLock, _amount); //TODO: remove this function competely - account.balance = _amount; - account.mpLimitEpoch = mpLimitEpoch; + accounts[msg.sender] = Account({ + rewardAddress: StakeVault(msg.sender).owner(), + balance: _amount, + bonusMP: bonusMP, + totalMP: bonusMP, + lastMint: block.timestamp, + lockUntil: block.timestamp + _secondsToLock, + epoch: currentEpoch, + mpLimitEpoch: mpLimitEpoch + }); //update global storage + totalSupplyMP += bonusMP; totalSupplyBalance += _amount; currentEpochTotalExpiredMP += currentEpochExpiredMP; totalMPPerEpoch += mpPerEpoch; @@ -273,18 +282,27 @@ contract StakeManager is Ownable { uint256 lockUntil = account.lockUntil; uint256 deltaTime; if (lockUntil < block.timestamp) { + //if unlocked, increase from now lockUntil = block.timestamp + _secondsToIncreaseLock; deltaTime = _secondsToIncreaseLock; } else { + //if locked, increase from lock until lockUntil += _secondsToIncreaseLock; deltaTime = lockUntil - block.timestamp; } + //checks if the lock time is in range if (deltaTime < MIN_LOCKUP_PERIOD || deltaTime > MAX_LOCKUP_PERIOD) { revert StakeManager__InvalidLockTime(); } - _mintBonusMP(account, _secondsToIncreaseLock, 0); + //mints bonus multiplier points for seconds increased + uint256 bonusMP = _getMPToMint(account.balance, _secondsToIncreaseLock); + //update account storage account.lockUntil = lockUntil; + account.bonusMP += bonusMP; + account.totalMP += bonusMP; + //update global storage + totalSupplyMP += bonusMP; } /** @@ -466,30 +484,6 @@ contract StakeManager is Ownable { } } - /** - * @notice Mint bonus multiplier points for given staking amount and time - * @dev if amount is greater 0, it increases difference of amount for current remaining lock time - * @dev if increased lock time, increases difference of total new balance for increased lock time - * @param account Account to mint multiplier points - * @param increasedLockTime increased lock time - * @param amount amount to stake - */ - function _mintBonusMP(Account storage account, uint256 increasedLockTime, uint256 amount) private { - uint256 mpToMint; - if (amount > 0) { - mpToMint += amount; //initial multiplier points - } - if (increasedLockTime > 0) { - //bonus for increased lock time - mpToMint += _getMPToMint(account.balance + amount, increasedLockTime); - } - //update storage - totalSupplyMP += mpToMint; - account.bonusMP += mpToMint; - account.totalMP += mpToMint; - account.lastMint = block.timestamp; - } - /** * @notice Mint multiplier points for given account and epoch * @param account Account earning multiplier points