Open position and add liquidity
Before you want to deposit liquidity, you need to choose an appropriate price range (corresponding to the tick range) to open a position. In most cases, opening a position and adding liquidity are supposed to be done simultaneously.
Function input params
pool_id: The object id about which pool you want to operation.
tick_lower: Represents the index of the lower tick boundary.
tick_upper: Represents the index of the upper tick boundary.
fix_amount_a: true means fixed coinA amount, false means fixed coinB amount
offer_amount_a:If fixed amount A, you must set amountA, amountB will be auto calculated by ClmmPoolUtil.estLiquidityAndcoinAmountFromOneAmounts().
offer_amount_b: f fixed amount B, you must set amountB, amountA will be auto calculated by ClmmPoolUtil.estLiquidityAndcoinAmountFromOneAmounts().
Example
const pool = await sdk.Pool.getPool(poolAddress)
const fix_amount_a = true
const coin_amount = '1000000'
//Price slippage point.
const slippage = 0.1
const curSqrtPrice = new BN(pool.current_sqrt_price)
const tick_lower = TickMath.getPrevInitializableTickIndex(pool.current_tick_index, Number(pool.tick_spacing))
const tick_upper = TickMath.getNextInitializableTickIndex(pool.current_tick_index, Number(pool.tick_spacing))
const liquidityInput = ClmmPoolUtil.estLiquidityAndcoinAmountFromOneAmounts(
tick_lower,
tick_upper,
new BN(coin_amount),
fix_amount_a,
true,
slippage,
curSqrtPrice
)
const amount_a = fix_amount_a ? coin_amount : liquidityInput.tokenMaxA.toString()
const amount_b = fix_amount_a ? liquidityInput.tokenMaxB.toString() : coin_amount
// Define parameters for opening position and adding liquidity
const params: OpenPositionWithLiquidityParams = {
pool_id: pool.pool_address,
tick_lower,
tick_upper,
fix_amount_a,
offer_amount_a: amount_a,
offer_amount_b: amount_b
}
const payload = sdk.Position.addLiquidityPaylod(params)
const respone = await executeTransaction([payload], sdk, key, true)
Last updated