swap

Swap from one coin to another one with specific pools.

Signature

swap(options: SwapOptions): Promise<Transaction>

Params

interface SwapOptions {
  routes: Array<{
    // Pool object ID
    pool: string;
    // Swap from coinA to coinB
    a2b: boolean;
    // computed next tick index
    nextTickIndex: number;
  }>;
  // Coin A address
  coinTypeA: string;
  // Coin B address
  coinTypeB: string;
  // User's wallet address
  address: string;
  // Amount of coinA
  amountA: string | number;
  // Amount of coinB
  amountB: string | number;
  // When `true`, amountA is input by user. 
  // When `false`, amountB is input by user. 
  amountSpecifiedIsInput: boolean;
  // Acceptable wasted amount percentage. Range: [0, 100)
  slippage: string | number;
  // Expires timeout(ms), defaults 60_000
  deadline?: number;
  // Custom transaction handler
  txb?: Transaction;
}

Examples

const swapResult = await sdk.trade.computeSwapResultV2(...);
const nextTickIndex = sdk.math.bitsToNumber(swapResult.tick_current_index.bits);
const routes = [{
  pool: '0x2c6fc12bf0d093b5391e7c0fed7e044d52bc14eb29f6352a3fb358e33e80729e',
  a2b: true,
  nextTickIndex,
}];

const tx = await sdk.trade.swap({
  routes,
  coinTypeA: '0x5d1f47ea69bb0de31c313d7acf89b890dbb8991ea8e03c6c355171f84bb1ba4a::turbos::TURBOS',
  coinTypeB: '0x2::sui::SUI',
  amountA: 100,
  amounbB: 225,
  amountSpecifiedIsInput: true,
  slippage: 0.2,
});

Last updated