Withdraw Liquidity
To withdraw a position's liquidity, withdrawLiquidity
is used. It can be called at any time. The function retrieves the liquidity, the reward amount up to that moment and the the trading fees earned. This function requires, as input:
positionId
-> id corresponding to the position.removedLiquidity
-> amount of liquidity to be removed. It is possible to remove, at any time, any amount of liquidity from one's position (i.e not necessarily all of the liquidity).
withdrawLiquidity
internally calls the _withdrawReward
method, passing the positionId
and the required removedLiquidity
amount.
_withdrawReward
internally calls the _retrieveGen2LiquidityAndFees
method, which removes the liquidity of the user’s position from his NFT by using the decreaseLiquidity
method of the nonfungiblePositionManager
(Uniswap v3):
Afterwards, the Uniswap v3 collect
function is called to send to the farmer his withdrawn liquidity and all of the trading fees he has earned up until that point.
If the withdrawn liquidity is equal to the position’s liquidity (and therefore the remaining liquidity in the position is 0), the position is automatically deleted from the setup and contract memory, and the position NFT is burned.
In this case, the _setupPositionsCount
is decreased by 1.
When the last position of a setup is closed, the setup is deactivated (see the Activate / Deactivate Setups section to learn more) and deleted from the memory of the contract.
Also, not only the farmer's liquidity is withdrawn but also his earned rewards and fees from his NFT too. See the Uniswap v3 NFT Management section to learn more.
Last updated