Function: withdraw(uint256 _amount)
The function allows users to withdraw tokens from the staking pool while ensuring that the user's staking and reward statuses are correctly updated.
Inputs
_amount
Control: Arbitrary.
Constraints: Less than or equal to the
stakeToken
amount staked by the user.Impact: The amount of tokens the user wants to withdraw from the
StakePool
.
Branches and code coverage
Intended branches
Check whether the user's stake amount is sufficient to withdraw the requested
_amount
.Call
getAllRewards
to calculate and distribute all pending rewards for the user.Update the user's stake amount
user.amount
and update therewardDebt
of eachrewardPool
.Transfer
_amount
ofstakeToken
from the contract back to the user.
Negative behavior
Revert if the
_amount
that the user withdraws is greater than theuser.amount
.Revert if the current time (
block.timestamp
) is less than the user'slastStakeTime + unstakingFrozenTime
.