The getBestRoute function finds the optimal trading route for a token swap through Fibrous’s liquidity pools.

Function Signature

/**
  * Fetches the best route from the API.
  * @param amount Amount to swap.
  * @param tokenInAddress Input token address.
  * @param tokenOutAddress Output token address.
  * @param chainName Chain name (e.g. "starknet", "scroll").
  * @param options Optional route parameters and overrides.
  * @param chainId Chain ID (may be used instead of `chainName` for backward compatibility).
  * @returns Route response.
  * @throws Error if the chain is not supported.
  */
getBestRoute(
    amount: AmountType,
    tokenInAddress: string,
    tokenOutAddress: string,
    chainName: string,
    options?: Partial<RouteOverrides>,
    chainId?: number,
): Promise<RouteResponse>;

Parameters

ParameterTypeDescription
amountBigNumberAmount to swap, formatted according to token decimals
tokenInAddressstringToken address to swap from
tokenOutAddressstringToken address to swap to
chainNamestringChain name where the transaction will take place (will be deprecated in the future, use chainId instead)
optionsRouteOverridesOptional parameters for route customization
chainIdnumberChain ID (may be used instead of chainName for backward compatibility)

RouteOverrides Type

type RouteOverrides = {
  reverse: boolean;
  direct: boolean;
  excludeProtocols: string[];
};
  • excludeProtocols: This is where you list the IDs of the AMMs you don’t want to include. For example, if there are certain AMMs you prefer not to use due to high fees or other reasons, you simply put their unique IDs in this list.
  • Use supportPairs to get a list of supported AMMs and their IDs.

RouteResponse Type

type RouteResponse = {
  success: true;
  inputToken: Token;
  inputAmount: string;
  outputToken: Token;
  outputAmount: string;
  estimatedGasUsed: string;
  route: Route[];
  time: number;
  swapType?: string; // Specific to EVM
};

Example Usage

import { Router as FibrousRouter } from "fibrous-router-sdk";
import { parseUnits } from "ethers";

const fibrous = new FibrousRouter();
const chainId = fibrous.supportedChains.find(chain => chain.chain_name == "hyperevm")?.chain_id;
if (!chainId) {
    throw new Error("Chain not supported");
}

// Build route options
const tokens = await fibrous.supportedTokens(chainId);
// Get input token from tokens map
const inputToken = tokens.get("hype");
if (!inputToken) {
    throw new Error("Input token not found");
}
// Get output token by address (for unverified tokens)
const outputToken = await fibrous.getToken(
    "0xb8ce59fc3717ada4c02eadf9682a9e934f625ebb",
    chainId,
);
if (!outputToken) {
    throw new Error("Output token not found");
}
const tokenInAddress = inputToken.address;
const tokenOutAddress = outputToken.address;
const tokenInDecimals = Number(inputToken.decimals);
const inputAmount = BigInt(parseUnits("1", tokenInDecimals)); // 1 Hype

const route = await fibrous.getBestRoute(
  inputAmount,
  tokenInAddress,
  tokenOutAddress,
  "hyperevm", // chainName will be deprecated in the future, use chainId instead
  {
      reverse: false,
  },
  chainId,
);
// returns route type (src/types/route.ts)