Router

Smart routing for optimal swap execution across multiple pools, including multi-hop swaps

Table of Contents

  • Overview

  • Load Graph

  • Find Best Route

  • Pool TVL

  • Utility Methods


Overview

The Router module finds the best swap path by:

  1. Building a graph of all available pools and coin pairs

  2. Discovering all possible 1-hop and 2-hop paths

  3. Simulating each candidate path on-chain

  4. Returning the optimal route with the best output

// Quick example: find best route for SUI -> USDC
const sdk = initFerraSDK({ network: 'mainnet', fullNodeUrl: '...', wallet: '0x...' })

await sdk.Router.loadGraphData()

const result = await sdk.Router.getBestInternalRouter(
  '0x2::sui::SUI',           // fromCoin
  '0x...::usdc::USDC',       // toCoin
  new BN('1000000000'),       // 1 SUI
  true,                       // isFixedInput
  100,                        // slippage (basis points, 100 = 1%)
  ''                          // partner object ID (empty if none)
)

if (result) {
  console.log('Best route found:')
  result.paths.forEach((path, i) => {
    console.log(`  Path ${i}: ${path.poolAddress.join(' -> ')}`)
    console.log(`  Amount out: ${path.amountOut.toString()}`)
  })
}

Load Graph

Before routing, you must load the pool graph. This fetches all pool and coin data from the protocol.

From Remote API

Custom Graph Data

Load graph with custom coin and path providers:


Find Best Route

Basic Usage

Parameters

Parameter
Type
Description

fromCoin

string

Source coin type

toCoin

string

Destination coin type

swapAmount

BN

Amount to swap

isFixedInput

boolean

true = fix input amount, false = fix output

slippagePoint

number

Slippage in basis points (100 = 1%)

partnerObjectId

string

Partner object ID for fee sharing (empty string if none)

multiPoolParams

PreSwapWithMultiPoolParams

Optional fallback params for direct pool swap

Result Structure

With Partner Fee

With Multi-Pool Fallback

If the graph doesn't contain a direct route, fall back to multi-pool preswap:

Complete Example


Pool TVL

Get all pools with their TVL (Total Value Locked) in USD:


Utility Methods

Get Pool Address and Direction

Look up the pool address and swap direction for a coin pair:

Get Token Info

Get Fee Rate


Important Notes

  • Always call loadGraphData() or loadGraph() before routing

  • The router considers both 1-hop (direct) and 2-hop (intermediate) paths

  • Up to 16 candidate paths are simulated to find the optimal route

  • Multi-hop paths are sorted by TVL to prioritize liquid routes

  • If no graph route is found, the router falls back to preSwapWithMultiPool

Last updated