chore(RewardsStreamerMP): allow users to stake again without increasing the lock period

This commit is contained in:
Andrea Franz
2025-03-19 13:21:13 +01:00
parent 90d523c55d
commit 4ea57c3c4a
2 changed files with 18 additions and 10 deletions

View File

@@ -188,13 +188,6 @@ contract RewardsStreamerMP is
VaultData storage vault = vaultData[msg.sender];
bool isCurrentlyLocked = vault.lockUntil > block.timestamp;
// Can't have `lockPeriod = 0` if vault is currently locked
if (lockPeriod == 0 && isCurrentlyLocked) {
revert StakingManager__InvalidLockPeriod();
}
if (lockPeriod > 0) {
if (vault.totalLockTime + lockPeriod > MAX_LOCKUP_PERIOD) {
revert StakingManager__InvalidLockPeriod();
@@ -431,6 +424,7 @@ contract RewardsStreamerMP is
newVault.lastMPUpdateTime = oldVault.lastMPUpdateTime;
newVault.lockUntil = oldVault.lockUntil;
newVault.rewardsAccrued = oldVault.rewardsAccrued;
newVault.totalLockTime = oldVault.totalLockTime;
delete vaultData[msg.sender];
@@ -503,6 +497,16 @@ contract RewardsStreamerMP is
totalMPAccrued -= _deltaMpTotal;
totalMaxMP -= _deltaMpMax;
totalStaked -= amount;
// if the user can unstake it means the lock period has ended
// and we can reset lockUntil
vault.lockUntil = 0;
// if the user withdraws all their staked balance
// we reset the totalLockTime
if (vault.stakedBalance == 0) {
vault.totalLockTime = 0;
}
}
function _totalShares() internal view returns (uint256) {

View File

@@ -963,13 +963,17 @@ contract StakeTest is RewardsStreamerMPTest {
function test_StakeMultipleTimesWithLockZeroAfterMaxLock() public {
uint256 stakeAmount = 10e18;
uint256 initialTime = vm.getBlockTimestamp();
// stake and lock 4 years
_stake(alice, stakeAmount, 4 * YEAR);
vm.warp(vm.getBlockTimestamp() + 4 * YEAR);
// staking with lock 0 should now work because the previous
// lock up has expired
// staking with lock 0 should work even before lock up has expired
vm.warp(initialTime + 2 * YEAR);
_stake(alice, stakeAmount, 0);
// staking with lock 0 should work again when lock up has expired
vm.warp(initialTime + 4 * YEAR);
_stake(alice, stakeAmount, 0);
// staking with lock > 0 should revert as max lock time was reached