preSwap

To initiate a swap, start with a preliminary swap assessment to understand the potential outcome. Afterward, you can set the amount limit based on the swap result and the slippage tolerance.

Preswap by simulation transaction

Signature

computeSwapResultV2(options: ComputeSwapResultOptionsV2): Promise<Transaction>

Params

interface ComputeSwapResultOptionsV2 {
  pools: Array<{
    // Pool object ID
    pool: string;
    // swap from coinA to coinB
    a2b: boolean;
    amountSpecified: string | number 
  }>;
  // Operator's wallet address
  address: string;
  amountSpecifiedIsInput: boolean;
  tickStep?: number;
}
  • pools : Multiple pools can be passed for prediction, which is particularly useful when there are multiple fee pools with the same token.

  • address : Transaction sender

  • a2b : Swap Direction: A value of true indicates a swap from CoinA to CoinB

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

  • tickStep: Represented this pre swap can move up to how many tickSpacing

Examples

const turbosSdk = new TurbosSdk(Network.testnet, {
  url: 'https://fullnode.testnet.sui.io',
});
const signer = turbosSdk.account.getKeypairFromMnemonics("your mnemonic");
const poolId = '0xb32fb8ec0b447a833066496bd7251d3a2ee57a0a97c54bf4cc945a986235582a';
const preSwapResult = await turbosSdk.trade.computeSwapResultV2({
  pools: [
    {
      pool: poolId,
      a2b: true,
      amountSpecified: 1000000,
    },
  ],
  amountSpecifiedIsInput: true,
  tickStep: 100,
  address: signer.toSuiAddress(),
});
console.log(preSwapResult);

Response

[
  {
    a_to_b: true,
    amount_a: '1000000',
    amount_b: '297',
    fee_amount: '10000',
    is_exact_in: true,
    liquidity: '15182876622',
    pool: '0xb32fb8ec0b447a833066496bd7251d3a2ee57a0a97c54bf4cc945a986235582a',
    protocol_fee: '0',
    recipient: '0x9563578937da5bd33657306871057dbe50c18900d300ecd97c93c3cbda2262ed',
    sqrt_price: '319730214661613148',
    tick_current_index: { bits: 4294886188 },
    tick_pre_index: { bits: 4294886188 }
  }
]

Last updated