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:
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:
Local Calculate
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 objectdecimals_a: the decimal of coinAdecimals_b: the decimal of coinBa2b: swap direction, true means swap from coinA to coinB, false means swap from coinB to CoinAby_amount_in: true means fixed the amount of input, false means fixed the amount of outputamount: the amount of input (by_amount_in = true) or output (by_amount_in = false)swap_ticks: the array of TickData, get them bysdk.Pool.fetchTicks()current_pool: the pool object, get it bysdk.Pool.getPool()
Example
PreSwap by Simulation Transaction
preSwap
Use sdk.Swap.preSwap method.
Function Parameters
pool: pool object, you can get it bysdk.Pool.getPool()methodcurrent_sqrt_price: pool's current_sqrt_pricecoin_type_a: the coin type address about coinAcoin_type_b: the coin type address about coinBdecimals_a: the decimal of coinAdecimals_b: the decimal of coinBa2b: swap direction, true means swap from coinA to coinB, false means swap from coinB to CoinAby_amount_in: true means fixed the amount of input, false means fixed the amount of outputamount: 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 typecoin_type_a: the coin type address about coinAcoin_type_b: the coin type address about coinBa2b: swap direction, true means swap from coinA to coinB, false means swap from coinB to CoinAby_amount_in: true means fixed the amount of input, false means fixed the amount of outputamount: the amount of input (by_amount_in = true) or output (by_amount_in = false)
Example
Last updated