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.
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 bysdk.Pool.fetchTicks()
pool
: thepool
object, get it bysdk.Pool.getPool()
Example
const a2b = false
const pool = await sdk.Pool.getPool('0x0128aade80123e3f6c5c0eac1a2dee2512bbdc92c9c1b386b0fd66e6cddfaa72')
const byAmountIn = false
const amount = new BN(80000000000000)
const swapTicks = await sdk.Pool.fetchTicks({
pool_id: pool.poolAddress,
coinTypeA: pool.coinTypeA,
coinTypeB: pool.coinTypeB
})
// const swapTicks = await sdk.Pool.fetchTicksByRpc(pool.ticks_handle)
console.log("swapTicks length: ", swapTicks.length);
const res = sdk.Swap.calculateRates({
decimalsA: 6,
decimalsB: 6,
a2b,
byAmountIn,
amount,
swapTicks,
currentPool: pool
})
console.log('calculateRates###res###', {
estimatedAmountIn: res.estimatedAmountIn.toString(),
estimatedAmountOut: res.estimatedAmountOut.toString(),
estimatedEndSqrtPrice: res.estimatedEndSqrtPrice.toString(),
estimatedFeeAmount: res.estimatedFeeAmount.toString(),
isExceed: res.isExceed,
extraComputeLimit: res.extraComputeLimit,
amount: res.amount.toString(),
aToB: res.aToB,
byAmountIn: res.byAmountIn,
})
console.log('swap: ', transferTxn)
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 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
const sendKeypair = buildTestAccount()
// Whether the swap direction is token a to token b
const a2b = true
// fix input token amount
const coinAmount = new BN(120000)
// input token amount is token a
const byAmountIn = true
// slippage value
const slippage = Percentage.fromDecimal(d(5))
// Fetch pool data
const pool = await sdk.Pool.getPool(poolAddress)
// Estimated amountIn amountOut fee
const res: any = await sdk.Swap.preswap({
pool: pool,
current_sqrt_price: pool.current_sqrt_price,
coinTypeA: pool.coinTypeA,
coinTypeB: pool.coinTypeB,
decimalsA: 6, // coin a 's decimals
decimalsB: 8, // coin b 's decimals
a2b,
by_amount_in,
amount,
})
const partner = "0x00"
const toAmount = byAmountIn ? res.estimatedAmountOut : res.estimatedAmountIn
const amountLimit = adjustForSlippage(toAmount, slippage, !byAmountIn)
// build swap Payload
const swapPayload = sdk.Swap.createSwapTransactionPayload(
{
pool_id: pool.poolAddress,
coinTypeA: pool.coinTypeA,
coinTypeB: pool.coinTypeB
a2b: a2b,
by_amount_in: by_amount_in,
amount: res.amount.toString(),
amount_limit: amountLimit.toString(),
swap_partner: partner
},
)
onst transferTxn = await sendTransaction(signer,swapPayload)
console.log('swap: ', transferTxn)
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
const a2b = true
const poolAddresses = [
'0x0128aade80123e3f6c5c0eac1a2dee2512bbdc92c9c1b386b0fd66e6cddfaa72',
'0xd2e4df21b920fba945aa8ad148069a196f0680879c40118beabc3046e2cd8d24',
'0x11f63063e721a99da6b33df2de0749f564c31d709b3c8689cf20dffcb983232f',
]
const pool0 = await buildTestPool(sdk, poolAddresses[0])
const pool1 = await buildTestPool(sdk, poolAddresses[1])
const pool2 = await buildTestPool(sdk, poolAddresses[2])
const byAmountIn = true
const amount = '10000000'
const resWithMultiPool: any = await sdk.Swap.preSwapWithMultiPool({
poolAddresses: poolAddresses,
coinTypeA: pool0.coinTypeA,
coinTypeB: pool0.coinTypeB,
a2b,
byAmountIn,
amount,
})
Last updated