Swap

Key Parameters Explained

  1. In preSwapQuote:

  • a2b: swap direction, true means swap from coinA to coinB, false means swap from coinB to CoinA

  • by_amount_in: true means fixed the amount of input, false means fixed the amount of output

  • amount: the amount of input (by_amount_in = true) or output (by_amount_in = false)

  1. In swapPayload:

  • slippage : Acceptable slippage percentage, 0.01 = 1%.

Example

// Get pool information
const pool = await sdk.Pool.getPool(pool_id)
const { coin_type_a, coin_type_b } = pool

// Get swap quote
const quote_obj = await sdk.Swap.preSwapQuote({
  pool_id,
  a2b: true, // true for A to B, false for B to A
  by_amount_in: true, // true for exact input, false for exact output
  in_amount: '2000000',
  coin_type_a,
  coin_type_b
})

// Build and send swap transaction
const tx = sdk.Swap.swapPayload({
  coin_type_a,
  coin_type_b,
  quote_obj,
  by_amount_in: true,
  slippage: 0.01
})

Last updated