// Developers

TypeScript SDK

@routerx/sdk is a typed client for quoting, simulating, and settling routes. It wraps the REST API and the Router contract behind one ergonomic surface built on viem.

Installation#

bash
npm install @routerx/sdk viem

The SDK targets Node 18+ and modern bundlers. viem is a peer dependency you provide, so the SDK shares your existing client and account setup.

Creating a client#

Instantiate RouterX with a viem chain. Pass a custom transport if you run your own RPC — quoting will use it for on-chain reads.

client.ts
import { RouterX } from '@routerx/sdk'
import { http } from 'viem'
import { mainnet } from 'viem/chains'

const rx = new RouterX({
  chain: mainnet,
  transport: http(process.env.RPC_URL), // optional
})

quote()#

Requests the best gas-adjusted route for a swap. Returns a typed, executable quote.

quote.ts
const quote = await rx.quote({
  tokenIn: WETH,
  tokenOut: USDC,
  amountIn: 1_000000000000000000n,
  slippageBps: 50,
})
OptionTypeDescription
tokenInAddressInput token. Required.
tokenOutAddressOutput token. Required.
amountInbigintInput amount in base units. Required.
slippageBpsnumberMax slippage in bps. Default 50.
include / excludestring[]Pin or ban specific adapters.

simulate()#

Dry-runs a quote against the current block using eth_call. Returns the expected output and a boolean executable flag — essential for liquidation and arbitrage timing.

simulate.ts
const sim = await rx.simulate(quote)

if (!sim.executable) {
  // route decayed — re-quote or abort before spending gas
  return
}
console.log(sim.amountOut)

settle()#

Submits the route on-chain with a wallet client. Handles the minimum-out guard, recipient, and deadline, and returns the transaction hash.

settle.ts
const { hash, amountOut } = await rx.settle({
  quote,
  wallet,
  recipient: account.address,
  deadline: Math.floor(Date.now() / 1000) + 45,
})

Deadlines are enforced on-chain

The Router reverts once the deadline passes. Keep it tight for MEV-sensitive trades.

Batching#

Quote several trades in parallel with a single call. Useful for scanning many liquidation opportunities at once without serial round-trips.

batch.ts
const quotes = await rx.quoteBatch([
  { tokenIn: WETH, tokenOut: USDC, amountIn: 1n * 10n ** 18n },
  { tokenIn: WBTC, tokenOut: DAI,  amountIn: 5n * 10n ** 7n  },
])

const best = quotes
  .filter((q) => q.ok)
  .sort((a, b) => Number(b.value.netOut - a.value.netOut))[0]

Types#

The SDK is fully typed. The core shapes you will work with:

types.ts
interface Quote {
  chainId: number
  amountIn: bigint
  amountOut: bigint
  amountOutMin: bigint
  gasEstimate: bigint
  priceImpactBps: number
  route: RouteHop[]
  calldata: `0x${string}`
  to: `0x${string}`
}

interface RouteHop {
  adapter: string
  tokenIn: `0x${string}`
  tokenOut: `0x${string}`
  share: number // 0..1 portion of input routed through this hop
}