Withdraw

Withdrawals require an existing position in the pool.

Withdraw Mode-Specific Parameters

  1. FixedOneSide

    • fixed_amount: Fixed amount to withdraw

    • fixed_coin_a: Boolean indicating whether to withdraw coin A (true) or coin B (false)

  2. OnlyCoinA/OnlyCoinB

    • burn_liquidity: Amount of liquidity to burn

    • available_liquidity: Total available liquidity in the position

Withdraw Usage Example

// Get pool and position information
const pool = await sdk.CetusClmmSDK.Pool.getPool(pool_id)
const position = await sdk.CetusClmmSDK.Position.getPositionById(pos_id)

if (!pool || !position) {
  throw new Error('Pool or Position not found')
}

// Pre-calculate withdrawal (example: OnlyCoinA mode)
const result = await sdk.Zap.preCalculateWithdrawAmount({
  mode: 'OnlyCoinA',
  pool_id,
  tick_lower: position.tick_lower_index,
  tick_upper: position.tick_upper_index,
  current_sqrt_price: pool.current_sqrt_price.toString(),
  burn_liquidity: '200000',
  available_liquidity: position.liquidity.toString(),
  coin_type_a: pool.coin_type_a,
  coin_type_b: pool.coin_type_b,
  coin_decimal_a: 6,
  coin_decimal_b: 9,
})

// Build and send transaction
const tx = await sdk.Zap.buildWithdrawPayload({
  withdraw_obj: result,
  pool_id,
  pos_id,
  close_pos: false, // Whether to close the position
  collect_fee: true, // Whether to collect accumulated fees
  collect_rewarder_types: [], // Types of rewards to collect
  coin_type_a: pool.coin_type_a,
  coin_type_b: pool.coin_type_b,
  tick_lower: position.tick_lower_index,
  tick_upper: position.tick_upper_index,
  slippage: 0.01,
})

// Simulate or send the transaction
const simulate_result = await sdk.FullClient.sendSimulationTransaction(tx, wallet)

Last updated