Make sure you have your wallet set up. For this tutorial, I’m using a wallet
loaded from the command line.
Strategy 1: Removing Liquidity from a Specific Range
This strategy is useful when you want to remove liquidity only from a certain part of the price range.1. Define a Range and Filter Positions
First, we define the range from which we want to remove liquidity. In this example, we are targeting 10 bins above and below the active bin. Then, we filter thepositions we fetched earlier.
2. Prepare the Position List for Removal
Next, we prepare the list of positions for theremoveMultipleLiquidity function. We need to specify the exact start and end bins for removal for each position, making sure they don’t go outside our defined range.
Strategy 2: Removing All Your Liquidity
If you want to withdraw all your liquidity from the pool, the process is even simpler. Since we have already fetched all positions, we can directly proceed to prepare them for removal.1. Prepare the Full Position List for Removal
We map over all positions to prepare them for removal. Thestart and end bins will be the position’s own lowerBinId and upperBinId.
Removing the Liquidity
This step is the same for both strategies. Now we are ready to call the main function to remove the liquidity using themaxPositionList we prepared.
removeMultipleLiquidity function creates the necessary transactions to withdraw your tokens. In the type parameter, we have specified RemoveLiquidityType.Both, which means we are withdrawing both tokens from the pool.
You can change this based on your needs:
RemoveLiquidityType.Both: To withdraw both the base and quote tokens.RemoveLiquidityType.BaseToken: To withdraw only the base token.RemoveLiquidityType.QuoteToken: To withdraw only the quote token.
Sending the Transactions
Finally, we collect all the transactions returned by theremoveMultipleLiquidity function, sign them, and send them to the Solana network. This part is also common for both strategies.
-
Collect Transactions: We put all the transactions into a single array.
-
Sign and Send: We loop through each transaction, sign it with our wallet, and send it.
