Add liquidity

Once you have set a suitable tick range, you can proceed to add liquidity to this position. The quantity composition of tokens you need to add is affected by the current price of the pool and the tick interval you have chosen.

There are two situations:

  1. add liquidity with a specified liquidity

  2. add liquidity with fixed coin amount

1. add liquidity with a specified liquidity

Use sdk.Position.createAddLiquidityPayload() method.

Function Parameters

  • pool_id: The object id about which pool you want to operation

  • pos_id: The object id about position.

  • max_amount_a: The max limit about used coin a amount.

  • max_amount_b: The max limit about used coin b amount.

  • delta_liquidity: The actual change in liquidity that has been added.

  • tick_lower: Represents the index of the lower tick boundary

  • tick_upper: Represents the index of the upper tick boundary

  • collect_fee: If you already has one position, you can select collect fees while adding liquidity.

  • rewarder_coin_types: If these not empty, it will collect rewarder in this position, if you already open the position.

Important Notes

How to Calculate delta_liquidity, max_amount_a, and max_amount_b

  1. you can set fixed two coin amount you want to use.

  2. use ClmmPoolUtil.estLiquidityAndCoinAmountFromOneAmounts() to calculated the delta_liquidity.

  3. Apply price slippage adjustment to the two coin amounts(max_amount_a and max_amount_b) calculated above by method adjustForCoinSlippage

Example

2. Add liquidity with a fixed coin amount

Use sdk.Position.createAddLiquidityFixTokenPayload() method.

Function Parameters

  • pool_id: The object id about which pool you want to operation

  • coin_type_a: The coin type address about coinA

  • coin_type_b: The coin type address about coinB

  • tick_lower: Represents the index of the lower tick boundary

  • tick_upper: Represents the index of the upper tick boundary

  • is_open: true means if first add liquidity, so needs open one position

  • pos_id: The object id about position

  • fix_amount_a: true means fixed coinA amount, false means fixed coinB amount

  • slippage: Price slippage point.

  • amount_a: If fixed amount A, you must set amount_a, amount_b will be auto calculated by ClmmPoolUtil.estLiquidityAndCoinAmountFromOneAmounts()

  • amount_b: If fixed amount B, you must set amount_b, amount_a will be auto calculated by ClmmPoolUtil.estLiquidityAndCoinAmountFromOneAmounts()

  • collect_fee: If you already has one position, you can select collect fees while adding liquidity

  • rewarder_coin_types: If these not empty, it will collect rewarder in this position, if you already open the position

Important Notes

  • The tick index must be an integer multiple of tickSpacing. If the provided parameter is not a multiple of tickSpacing, the contract will throw an error.

  • -443636 < tick_lower_index < tick_upper_index < 443636, 443636 is a constant, derived from the maximum range representable by the Q32.62 fixed-point number format.

  • If you know price range, you can use TickMath.priceToTickIndex() to transform real price to tick index.

  • You can just open one position near the current price of the pool, use TickMath.getPrevInitializeTickIndex() and TickMath.getNextInitializeTickIndex() to find the next initialized tick.

  • If you want to add global liquidity, you can set:

    • tick_lower_index = -443636 + (443636 % tick_spacing)

    • tick_upper_index = 443636 - (443636 % tick_spacing)

Last updated