Best Route

Returns Best Route via Fibrous Finance

/**
 * Gets the best route from the API
 * @param amount: Amount to swap, formatted
 * @param tokenInAddress: Token to swap from
 * @param tokenOutAddress: Token to swap to
 * @param chainName: Chain name where the transaction will take place
 * @param options: Optional parameters such as slippage and excluded protocols
 * @returns Promise<RouteResponse>
 * @throws Error if the API returns an error
 */
async getBestRoute(
    amount: BigNumber,
    tokenInAddress: string,
    tokenOutAddress: string,
    chainName: string,
    options?: Partial<RouteOverrides>
): Promise<RouteResponse>;
 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. supportPairs : This function returns a list of supported AMMs, along with the AMM IDs.

RouteResponse

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 { BigNumber } from "@ethersproject/bignumber";
import { parseUnits } from "ethers";

const router = new FibrousRouter();

const tokenInAddress = tokens["usdt"].address;
const tokenOutAddress = tokens["usdc"].address;
const tokenInDecimals = tokens["usdt"].decimals;
const inputAmount = BigNumber.from(parseUnits("5", tokenInDecimals)); // for 5 USDT

const route = await router.getBestRoute(
    inputAmount, // amount
    tokenInAddress, // token input
    tokenOutAddress, // token output
    "scroll" // chain name (Starknet or Scroll)
); 

Last updated