From 73ec81131898a0a3efb02c9d9cabc2493a4ac772 Mon Sep 17 00:00:00 2001 From: Aleksandar Marinkovic Date: Mon, 2 Sep 2024 12:03:15 +0200 Subject: [PATCH] fix: unstake before staking starts --- src/libs/LibTokenizedVaultStaking.sol | 2 +- test/T06Staking.t.sol | 26 ++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/libs/LibTokenizedVaultStaking.sol b/src/libs/LibTokenizedVaultStaking.sol index c27dbb93..a2bb598b 100644 --- a/src/libs/LibTokenizedVaultStaking.sol +++ b/src/libs/LibTokenizedVaultStaking.sol @@ -190,7 +190,7 @@ library LibTokenizedVaultStaking { // staker's balance in the past are never adjusted // stakers and NLF balances are adjusted in the current interval when unstaking // in the current interval, if a reward was paid and therefor collected, staking distribution need to be adjusted according to the balances - if (lastPaidInterval == currentInterval) { + if (lastPaidInterval == currentInterval && currentInterval != 0) { bytes32 vTokenIdLastPaid = _vTokenId(_entityId, tokenId, lastPaidInterval); s.stakingDistributionAmount[vTokenIdLastPaid] -= (s.stakingDistributionAmount[vTokenIdLastPaid] * userStateAtLastPaid.balance) / totalStateAtLastPaid.balance; } diff --git a/test/T06Staking.t.sol b/test/T06Staking.t.sol index d01a49e5..618e6d26 100644 --- a/test/T06Staking.t.sol +++ b/test/T06Staking.t.sol @@ -678,6 +678,32 @@ contract T06Staking is D03ProtocolDefaults { assertEq(getRewards(lou.entityId, nlf.entityId), 0, "Lou's reward [5] should be zero".red()); } + function test_stakeUnstakeBeforeInit() public { + initStaking(4 * I); + + vm.warp(2 * I + 10 days); // important not to stake at [0] interval! + + uint256 balanceBefore = nayms.internalBalanceOf(bob.entityId, usdcId); + + assertEq(nayms.currentInterval(nlf.entityId), 0, "should be interval zero"); // make sure staking has not started yet + + startPrank(bob); + nayms.stake(nlf.entityId, bobStakeAmount); + startPrank(sue); + nayms.stake(nlf.entityId, sueStakeAmount); + startPrank(lou); + nayms.stake(nlf.entityId, louStakeAmount); + assertEq(nayms.currentInterval(nlf.entityId), 0, "should be interval zero"); // make sure staking has not started yet + + startPrank(bob); + nayms.unstake(nlf.entityId); + assertEq(nayms.currentInterval(nlf.entityId), 0, "should be interval zero"); // make sure staking has not started yet + + assertEq(nayms.internalBalanceOf(bob.entityId, usdcId), balanceBefore, "balance should be the same"); + assertStakedAmount(sue.entityId, sueStakeAmount, "Sue's staked amount should be the same".red()); + assertStakedAmount(lou.entityId, louStakeAmount, "Lou's staked amount should be the same".red()); + } + function test_skipPayingAnInterval() public { uint256 startStaking = block.timestamp + 100 days; initStaking(startStaking);