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, which removes 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(
        tokenId,
        LiquidityToRemove,
        0,
        0,
        block.timestamp + 10000
    ));
}

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 NFT is burned.

if (farmingPosition.liquidityPoolTokenAmount == 0) {
    _setupPositionsCount[farmingPosition.setupIndex] -= 1;
    nonfungiblePositionManager.burn(farmingPosition.tokenId);
    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) and deleted from the memory of the contract.

function _tryClearSetup(uint256 setupIndex) private {
    if (_setupPositionsCount[setupIndex] == 0 && !_setups[setupIndex].active) {
        delete _setups[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