Skip to content

Route Class

The Route class represents a found route for token swapping. It contains methods for executing swaps and managing approvals.

TypeScript Types

// Import for viem
import { Route } from 'symphony-sdk/viem';
import type { RoutePayload, SwapResult } from 'symphony-sdk/types/viem';
import type { TransactionReceipt } from 'viem';
 
// Import for ethersV5
import { Route } from 'symphony-sdk/ethersV5';
import type { RoutePayload, SwapResult } from 'symphony-sdk/types/ethersV5';
import type { TransactionReceipt } from 'ethers';
 
// Import for ethers v6
import { Route } from 'symphony-sdk/ethers';
import type { RoutePayload, SwapResult } from 'symphony-sdk/types/ethers';
import type { TransactionReceipt } from 'ethers';

Properties

  • route: Raw route data from the API
  • amountIn: Input amount in raw units
  • amountInFormatted: Input amount formatted
  • amountOut: Output amount in raw units
  • amountOutFormatted: Output amount formatted
  • pathPercentages: Split between paths
  • pathCount: Number of paths
  • includesNative: Whether route includes native token
  • tokenIn: Input token address
  • tokenOut: Output token address
  • swapFee: Fee that Symphony gets on execution of Route.
  • tokenInUsd: USD value of a tokenIn.
  • tokenOutUsd: USD value of a tokenOut.
  • priceImpactPercentage: Price impact percentage that Route has. Could be positive due to efficient routing provided by Symphony.

Methods

getConfig

getConfig(): SymphonyConfig

Returns the current configuration of the Symphony instance.

setConfig

setConfig(options: Partial<SymphonyConfig>): void

Updates the configuration of the Symphony instance.

swap

async swap(params?: {
  signer?: Signer; // ethers/ethersV5
  walletClient?: WalletClient; // viem
  options?: {
    skipApproval?: boolean;
    skipCheckApproval?: boolean;
    wait?: number;
    isRaw?: boolean;
    outTokenDecimals?: number;
  };
  slippage?: {
    slippageAmount?: string | number;
    isBps?: boolean;
  }
}): Promise<SwapResult>

Executes the token swap using the found route. Requires a connected wallet.

Returns

  • swapReceipt: Transaction receipt of the swap
  • approveReceipt: Transaction receipt of the approval (if applicable)

Example

const route = await symphony.getRoute(tokenIn, tokenOut, "1.0");
const { swapReceipt, approveReceipt } = await route.swap();
console.log(`Swap executed: ${swapReceipt.transactionHash}`);
if (approveReceipt) {
  console.log(`Approval executed: ${approveReceipt.transactionHash}`);
}

giveApproval

async giveApproval(params?: {
  signer?: Signer; // ethers/ethersV5
  walletClient?: WalletClient; // viem
  amount?: string | bigint;
  options?: {
    isRaw?: boolean;
    wait?: number;
  }
}): Promise<TransactionReceipt>

Approves the Symphony contract to spend tokens on behalf of the user. Supports custom approval amounts.

Returns

  • Transaction receipt of the approval transaction (already waited for confirmations)

checkApproval

async checkApproval(params?: {
  signer?: Signer; // ethers/ethersV5
  walletClient?: WalletClient; // viem
  amount?: string | bigint;
  includesNative?: boolean;
  options?: {
    isRaw?: boolean;
  }
}): Promise<boolean>

Checks if the Symphony contract has approval to spend tokens.

Returns

  • true if approval is sufficient, false otherwise

refresh

async refresh(): Promise<Route>

Refreshes the route with current market prices.

Returns

  • A new Route instance with updated prices

Example

let route = await symphony.getRoute(tokenIn, tokenOut, "1.0");
// Wait some time...
route = await route.refresh(); // Get updated prices

getRouteDetails

async getRouteDetails(): Promise<RouteDetails>

Returns detailed information about the route. This method is async as of v3.0.0.

getTokenIn

getTokenIn(): string

Returns the input token address.

getTokenOut

getTokenOut(): string

Returns the output token address.

getSwapTypes

getSwapTypes(): number[][]

Returns the types of swaps used in the route.

getTokenList

getTokenList(): Token[]

Returns a list of all tokens involved in the route.

getTotalAmountIn

async getTotalAmountIn(): Promise<{
  amountIn: bigint | BigNumber; // bigint for viem/ethers v6, BigNumber for ethersV5
  amountInFormatted: string;
  tokenIn: string;
  tokenOut: string;
}>

Returns the total input amount required for the swap. This method is async as of v3.0.0.

getTotalAmountOut

async getTotalAmountOut(): Promise<{
  amountOut: bigint | BigNumber; // bigint for viem/ethers v6, BigNumber for ethersV5
  amountOutFormatted: string;
  tokenIn: string;
  tokenOut: string;
}>

Returns the expected output amount from the swap. This method is async as of v3.0.0.