Skip to content

Commit

Permalink
fix: unstake before staking starts
Browse files Browse the repository at this point in the history
  • Loading branch information
amarinkovic committed Sep 2, 2024
1 parent e663ddc commit 73ec811
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/libs/LibTokenizedVaultStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
26 changes: 26 additions & 0 deletions test/T06Staking.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 73ec811

Please sign in to comment.