Pre Swap

Before performing an actual swap, you can do a pre-swap to get the swap result. Then you can set amount limit by swap result and slippage.

This text describes two primary methods for performing a swap function, each with distinct steps and calculation methods:

  1. The first method involves initially obtaining tick data, followed by performing calculations locally

  2. The second method involves conducting a simulated swap and then obtaining the resultant data through an event on Sui

There are three pre-calculation strategies for executing a swap:

  1. Local Calculate

  2. Simulate swap then get result

    • Single pool swap result: use sdk.Swap.preSwap()

    • Multi pool calculation: use sdk.Swap.preSwapWithMultiPool()

Local Calculate Swap Result

This method requires getting ticks data and pool object first using sdk.Swap.calculateRates method.

Function Parameters

  • params: CalculateRatesParams object

  • decimals_a: the decimal of coinA

  • decimals_b: the decimal of coinB

  • 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)

  • swap_ticks: the array of TickData, get them by sdk.Pool.fetchTicks()

  • current_pool: the pool object, get it by sdk.Pool.getPool()

Example

PreSwap by Simulation Transaction

preSwap

Use sdk.Swap.preSwap method.

Function Parameters

  • pool: pool object, you can get it by sdk.Pool.getPool() method

  • current_sqrt_price: pool's current_sqrt_price

  • coin_type_a: the coin type address about coinA

  • coin_type_b: the coin type address about coinB

  • decimals_a: the decimal of coinA

  • decimals_b: the decimal of coinB

  • 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)

Example

preSwapWithMultiPool

Use sdk.Swap.preSwapWithMultiPool() method.

Function Parameters

  • pool_ids: An array of pool objects ID. All pools must have the same type

  • coin_type_a: the coin type address about coinA

  • coin_type_b: the coin type address about coinB

  • 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)

Example

Last updated