ghoul-sol / treasure-staking

10 stars 1 forks source link

Fix: [L-3] Particular order of operations leads to deposit positions which cannot be properly cleaned up #87

Closed ghoul-sol closed 2 years ago

ghoul-sol commented 2 years ago

withdrawPosition uses modifier updateRewards so execution of the function is done on the updated rewards state. user.depositAmount == 0 && user.lockLpAmount == 0 conditions make sure that the user withdrew the whole deposit from the position and lp tokens have been zero'd out as well. By calling _recalculateGlobalLp(msg.sender, -amountInt, -lockLpAmountInt) we make sure that global lp tokens are adjusted properly.

It does not matter if pending rewards are present or not because lp tokens for this given deposit have been subtracted already and this deposit will not earn any additional rewards. That means it can be removed even if pending rewards are present.

Pending rewards check was a leftover from the previous implementation where rewards were calculated per deposit and removing a deposit was equivalent to losing unclaimed rewards which is not the case anymore.