Preswap

Before do really swap, you need can do per swap to get the swap result. Then you can set amount limt by swap result and slippage.

This text describes two primary methods for performing a swap function, each with distinct steps and calculation methods. The first method involves initially obtaining tick data, followed by performing calculations locally. 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

  • local calculate swap result: use sdk.Swap.calculateRates()

  1. Simulate swap then get result

  • only calculate one pool swap result: use sdk.Swap.preswap().

  • calculate multi pool at the same time, use sdk.Swap.preSwapWithMultiPool(). It will select one best pool to do swap.

Local calculate swap result

This method need to get ticks data and pool object first.

Method: sdk.Swap.calculateRates

Param:

Please refer to the original function for specific parameter types.

  • params: CalculateRatesParams object.

    • decimalsA: the decimal of coinA.

    • decimalsB: the decimal of coinB.

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

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

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

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

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

Example

Preswap by simulation transaction

preSwap

Medhod: sdk.Swap.preswap

Params:

Please refer to the original function for specific parameter types.

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

  • currentSqrtPrice: pool's current_sqrt_price.

  • coinTypeA: the coin type address about coinA.

  • coinTypeB: the coin type address about coinB.

  • decimalsA: the decimal of coinA.

  • decimalsB: the decimal of coinB.

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

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

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

Example

preSwapWithMultiPool

Method: sdk.Swap.preSwapWithMultiPool()

Params:

Please refer to the original function for specific parameter types.

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

  • coinTypeA: the coin type address about coinA.

  • coinTypeB: the coin type address about coinB.

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

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

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

Example

Last updated