Features Available

1. Find best router swap result

Func client.findRouters

Params

  • from(String): the coin type of input coin.

  • target(String): the coin type of output coin.

  • amount(BN): the amount of input or output.

  • byAmountIn(boolean): true means fixed the amount of input, false means fixed the amount of output.

  • providers:(String Array): Now, support cetus, deepbook, kriya v2/v3, flowx v2/v3, aftermath, turbos, afsui, volo, haedal.

  • depth(Option, number): now depths support betweent 1 and 3, default setting is 3, it means you can get 3 hops path.

  • splitAlgorithm(Option, String): devide_equally or geometric, recommand just use default devide_equally.

  • splitFactor: (Option, number): used to contral geometric split width.

  • splitCount(Option, number): this field represents the maximum number of splits for a transaction. If you do not want to split, set it to 1. The default value is 20.

Example

const amount = new BN(1000000)

const from = M_SUI // 0x2::sui::SUI
const target = M_CETUS // 0x06864a6f921804860930db6ddbe2e16acdf8504495ea7481637a1c8b9a8fe54b::cetus::CETUS

const routerRes = await client.findRouters({
  from,
  target,
  amount,
  byAmountIn: true, // true means fix input amount, false means fix output amount
  depth: 3, // max allow 3, means 3 hops
  providers: [
    "CETUS",
    "DEEPBOOK",
    "KRIYA",
    "KRIYAV3",
    "FLOWX",
    "FLOWXV3",
    "AFTERMATH",
    "TURBOS",
    "HADEAL",
    "VOLO",
    "AFSUI"
  ], //  now max support above 11 platforms.
  // splitAlgorithm: null, // select split algotirhm, recommended default set null
  // splitFactor: null,
  // splitCount: 100, // set max expect split count
})

if (routerRes != null) {
  console.log(JSON.stringify(res, null, 2))
}

2. Build fast swap transaction.

This method will send swaped coins to user directly.

Params

  • routers(Router Object): The result from findRouter(), representing the path chosen for the swap.

  • byAmountIn(boolean): The input params as the same as request in findRouter().

  • txb(Transaction): The programmable transaction builder.

  • slippage:(number): A value between 0 and 1, representing the maximum allowed price slippage as a percentage. A value of 0.01 allows up to 1% price slippage. This parameter is crucial for protecting your assets in a rapidly changing market.

  • isMergeTragetCoin(boolean): If set to true, the output coins are merged into the existing coin object to prevent an indefinite increase in the number of coin types held.

  • refreshAllCoins(Option(boolean)): If true, retrieves all coin types when setting up the swap; if false, uses the existing coins.

  • partner(Option(string)): The partner address. Details about partner can be found here.

Example

const routerTx = new Transaction()

if (routerRes != null) {
  await client.fastRouterSwap({
    routers: routerRes.routes,
    byAmountIn,
    txb: routerTx,
    slippage: 0.01,
    isMergeTragetCoin: true,
    refreshAllCoins: true,
  })

  let result = await client.devInspectTransactionBlock(routerTx, keypair)

  if (result.effects.status.status === "success") {
    console.log("Sim exec transaction success")
    const result = await client.signAndExecuteTransaction(routerTx, keypair)
  }
  console.log("result", result)
}

Build swap transaction and return target coin object.

This method will build transaction and return coin objects, used to build ptb.

Params

  • routers(Router Object): The result from findRouter(), representing the path chosen for the swap.

  • byAmountIn(boolean): The input params as the same as request in findRouter().

  • inputCoin(TransactionObjectArgument): This represents the input coin object. If you only support swaps with a fixed input, simply extract a fixed amount from the coin object. If you support fixed output, you need to extract an amount limited by the specified constraints from the coin object.

  • txb(Transaction): The programmable transaction builder.

  • slippage:(number): A value between 0 and 1, representing the maximum allowed price slippage as a percentage. A value of 0.01 allows up to 1% price slippage. This parameter is crucial for protecting your assets in a rapidly changing market.

  • partner(Option(string)): The partner address. Details about partner can be found here.

Return

  • targetCoin: TransactionObjectArgument

Example

const routerTx = new Transaction()
const byAmountIn = true;

if (routerRes != null) {
  const targetCoin = await client.routerSwap({
    routers: routerRes.routes,
    byAmountIn,
    txb: routerTx,
    inputCoin,
    slippage: 0.01,
  })
  
  // you can use this target coin object argument to build your ptb.
  const client.transferOrDestoryCoin(
      txb,
      targetCoinRes.targetCoin,
      targetCoinType
  )

  let result = await client.devInspectTransactionBlock(routerTx, keypair)

  if (result.effects.status.status === "success") {
    console.log("Sim exec transaction success")
    const result = await client.signAndExecuteTransaction(routerTx, keypair)
  }
  console.log("result", result)
}

Last updated