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:
Local Calculate
local calculate swap result: use
sdk.Swap.calculateRates()
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.
use sdk.Swap.calculateRates
method.
Function input 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 ofTickData
,
get them bysdk.Pool.fetchTicks()
.pool
: thepool
object, get it bysdk.Pool.getPool()
.
Example
Preswap by simulation transaction
preSwap
use sdk.Swap.preswap
method.
Function input param
Please refer to the original function for specific parameter types.
pool
: pool object, you can get it bysdk.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
use sdk.Swap.preSwapWithMultiPool()
method.
Function input param
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