Skip to content

Commit

Permalink
refactor(StakeManager): optimize stake and lock functionality by remo…
Browse files Browse the repository at this point in the history
…ving mintBonusMP function
  • Loading branch information
3esmit committed Sep 22, 2024
1 parent 2a90b3e commit 22db53b
Showing 1 changed file with 27 additions and 9 deletions.
36 changes: 27 additions & 9 deletions contracts/StakeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

/**
Expand Down

0 comments on commit 22db53b

Please sign in to comment.