Withdraw Liquidity

To withdraw a position's liquidity, withdrawLiquidity is used. This function requires, as input:

function withdrawLiquidity(uint256 positionId,uint128 removedLiquidity) byPositionOwner(positionId) public 
  • 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(positionId, removedLiquidity);

_withdrawReward internally calls the _retrieveGen2LiquidityAndFees method to remove the liquidity of the user's position from the NFT of the setup, by using the decreaseLiquidity method of the nonfungiblePositionManager (Uniswap v3):

if(liquidityToRemove > 0) {
    (collectedAmount0, collectedAmount1) = nonfungiblePositionManager.decreaseLiquidity(INonfungiblePositionManager.DecreaseLiquidityParams(
        block.timestamp + 10000

If the liquidity withdrawn is equal to the position's liquidity (and thus the remaining position liquidity is 0), the position is automatically deleted from the setup and from the contract memory:

if (farmingPosition.liquidityPoolTokenAmount == 0) {
    _setupPositionsCount[farmingPosition.setupIndex] -= 1;
    delete _positions[positionId];

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), deleted from the memory of the contract and the setup NFT is burned:

if (_setupPositionsCount[farmingPosition.setupIndex] == 0 && !_setups[farmingPosition.setupIndex].active) {
    _giveBack(_rewardReceived[farmingPosition.setupIndex] - _rewardPaid[farmingPosition.setupIndex]);
    delete _setups[farmingPosition.setupIndex];

Also, not only the farmer's liquidity is withdrawn but also his earned rewards and fees from the NFT too. See the Uniswap v3 NFT Management section to learn more.

Last updated