Add Liquidity
1. Add Liquidity
The DLMM SDK provides two main methods for adding liquidity:
calculateAddLiquidityInfo(option)- Calculates the liquidity distribution across binsaddLiquidityPayload(option, tx?)- Creates the transaction payload for adding liquidity
Method 1: calculateAddLiquidityInfo
calculateAddLiquidityInfoThis method calculates how liquidity should be distributed across different bins based on your strategy and parameters.
Parameters for CalculateAddLiquidityOption:
interface CalculateAddLiquidityOption {
pool_id: string // Pool ID
amount_a: string // Amount of token A to add
amount_b: string // Amount of token B to add
active_id: number // Current active bin ID
bin_step: number // Bin step size
lower_bin_id: number // Lower bound of bin range
upper_bin_id: number // Upper bound of bin range
active_bin_of_pool?: { // Active bin amounts (if active bin is in range)
amount_a: string
amount_b: string
}
strategy_type: StrategyType // Liquidity strategy (Spot, BidAsk, Curve)
}Parameters for CalculateAddLiquidityAutoFillOption:
Method 2: addLiquidityPayload
addLiquidityPayloadThis method creates the transaction payload for adding liquidity to an existing position or opening a new position.
Parameters for AddLiquidityOption (existing position):
Parameters for OpenAndAddLiquidityOption (new position):
Complete Example - Adding Liquidity to Existing Position:
Complete Example - Opening New Position:
2. Important Parameter Notes:
active_bin_of_pool: This parameter is crucial when the active bin falls within your position's range. UsegetActiveBinIfInRange()to get the correct values.use_bin_infos: Whenfalse, the contract calculates liquidity distribution internally; whentrue, it uses the providedbin_infos.max_price_slippage: Protects against price movements during transaction execution (e.g., 0.01 = 1% slippage tolerance).collect_fee: Only applicable for existing positions; determines whether to collect accumulated fees when adding liquidity.strategy_type: Affects how liquidity is distributed across bins:Spot: Even distribution around current price.BidAsk: Concentrated at specific price levels.Curve: Smooth distribution following a curve.
There are three strategies for adding liquidity: Spot, BidAsk, and Curve. Here's how to use each:
1. Spot Strategy
Note: The amount_a_in_active_bin and amount_b_in_active_bin parameters are used to calculate the correct liquidity distribution when the active bin is within your position's range. These values are obtained using the getActiveBinIfInRange method, which:
Checks if the active bin is within your specified range
Returns the amounts of both tokens in the active bin if it is within range
Returns undefined if the active bin is outside the range
2. BidAsk Strategy
3. Curve Strategy
3. Add Liquidity with Fixed Amount
4. Add Liquidity with Price
Last updated