Skip to main content

swapRouting

This module provides utilities for finding optimal swap routes between tokens in GMX markets. It includes functions for creating swap estimators, finding best swap paths, and working with market adjacency graphs to determine the most efficient routes for token swaps.

Methods

createSwapEstimator

  • createSwapEstimator(marketsInfoData: MarketsInfoData, isAtomicSwap: boolean): SwapEstimator

Creates a swap estimator function that calculates the USD output for a given market edge and USD input amount.

import { createSwapEstimator } from "@gmx-ui/sdk/utils/swap/swapRouting";

const estimator = createSwapEstimator(marketsInfoData, false);
const result = estimator(marketEdge, 1000000000000000000000n); // 1000 USD
console.log(result.usdOut); // Output USD amount

createMarketEdgeLiquidityGetter

  • createMarketEdgeLiquidityGetter(marketsInfoData: MarketsInfoData): MarketEdgeLiquidityGetter

Creates a function that returns the available liquidity for a given market edge.

import { createMarketEdgeLiquidityGetter } from "@gmx-ui/sdk/utils/swap/swapRouting";

const getLiquidity = createMarketEdgeLiquidityGetter(marketsInfoData);
const liquidity = getLiquidity(marketEdge);
console.log(liquidity); // Available liquidity in USD

createNaiveSwapEstimator

  • createNaiveSwapEstimator(marketsInfoData: MarketsInfoData, isAtomicSwap: boolean): NaiveSwapEstimator

Creates a naive swap estimator that returns the swap yield for a given market edge and USD input.

import { createNaiveSwapEstimator } from "@gmx-ui/sdk/utils/swap/swapRouting";

const naiveEstimator = createNaiveSwapEstimator(marketsInfoData, false);
const result = naiveEstimator(marketEdge, 1000000000000000000000n);
console.log(result.swapYield); // Swap yield as a number

createNaiveNetworkEstimator

  • createNaiveNetworkEstimator(config: { gasLimits: GasLimitsConfig; tokensData: TokensData; gasPrice: bigint; chainId: number }): NaiveNetworkEstimator

Parameters

  • config: object - Configuration object containing gas limits, tokens data, gas price, and chain ID

Creates a network estimator that accounts for gas costs in swap calculations.

import { createNaiveNetworkEstimator } from "@gmx-ui/sdk/utils/swap/swapRouting";

const networkEstimator = createNaiveNetworkEstimator({
gasLimits,
tokensData,
gasPrice: 20000000000n,
chainId: 42161
});

const result = networkEstimator(1000000000000000000000n, 2); // 1000 USD, 2 swaps
console.log(result.networkYield); // Network yield after gas costs
console.log(result.usdOut); // USD output after gas costs

getBestSwapPath

  • getBestSwapPath(params: { routes: MarketEdge[][]; usdIn: bigint; estimator: SwapEstimator; networkEstimator?: NaiveNetworkEstimator }): MarketEdge[] | undefined

Parameters

  • params: object - Configuration object with routes, USD input, estimator, and optional network estimator

Finds the best swap path from a list of possible routes based on maximum USD output.

import { getBestSwapPath } from "@gmx-ui/sdk/utils/swap/swapRouting";

const bestPath = getBestSwapPath({
routes: possibleRoutes,
usdIn: 1000000000000000000000n,
estimator: swapEstimator,
networkEstimator: networkEstimator
});

if (bestPath) {
console.log("Best route found:", bestPath);
}

getNaiveBestMarketSwapPathsFromTokenSwapPaths

  • getNaiveBestMarketSwapPathsFromTokenSwapPaths(params: { graph: MarketsGraph; tokenSwapPaths: string[][]; usdIn: bigint; tokenInAddress: string; tokenOutAddress: string; estimator: NaiveSwapEstimator; topPathsCount?: number; networkEstimator?: NaiveNetworkEstimator }): string[][]

Parameters

  • params: object - Configuration object with graph, token swap paths, USD input, token addresses, estimator, and optional parameters

Returns the top market swap paths for token swaps based on naive estimation.

import { getNaiveBestMarketSwapPathsFromTokenSwapPaths } from "@gmx-ui/sdk/utils/swap/swapRouting";

const bestPaths = getNaiveBestMarketSwapPathsFromTokenSwapPaths({
graph: marketsGraph,
tokenSwapPaths: tokenPaths,
usdIn: 1000000000000000000000n,
tokenInAddress: "0x...",
tokenOutAddress: "0x...",
estimator: naiveEstimator,
topPathsCount: 5
});

console.log("Top market paths:", bestPaths);

getMarketsForTokenPair

  • getMarketsForTokenPair(graph: MarketsGraph, tokenAAddress: string, tokenBAddress: string): string[]

Returns all market addresses that support swapping between two tokens.

import { getMarketsForTokenPair } from "@gmx-ui/sdk/utils/swap/swapRouting";

const markets = getMarketsForTokenPair(
marketsGraph,
"0x...", // Token A address
"0x..." // Token B address
);

console.log("Available markets:", markets);

getBestMarketForTokenEdge

  • getBestMarketForTokenEdge(params: { marketAddresses: string[]; usdIn: bigint; tokenInAddress: string; tokenOutAddress: string; estimator: NaiveSwapEstimator; marketPath?: string[]; calculatedCache?: Record<string, number> }): { marketAddress: string; swapYield: number } | undefined

Parameters

  • params: object - Configuration object with market addresses, USD input, token addresses, estimator, and optional parameters

Finds the best market for a specific token edge based on swap yield.

import { getBestMarketForTokenEdge } from "@gmx-ui/sdk/utils/swap/swapRouting";

const bestMarket = getBestMarketForTokenEdge({
marketAddresses: ["0x...", "0x..."],
usdIn: 1000000000000000000000n,
tokenInAddress: "0x...",
tokenOutAddress: "0x...",
estimator: naiveEstimator
});

if (bestMarket) {
console.log("Best market:", bestMarket.marketAddress);
console.log("Swap yield:", bestMarket.swapYield);
}

marketRouteToMarketEdges

  • marketRouteToMarketEdges(marketPath: string[], from: string, marketsInfoData: MarketsInfoData): MarketEdge[]

Converts a market path to an array of market edges.

import { marketRouteToMarketEdges } from "@gmx-ui/sdk/utils/swap/swapRouting";

const edges = marketRouteToMarketEdges(
["0xmarket1", "0xmarket2"],
"0xfromToken",
marketsInfoData
);

console.log("Market edges:", edges);

getTokenSwapPathsForTokenPair

  • getTokenSwapPathsForTokenPair(tokenSwapPaths: SwapPaths, tokenAAddress: string, tokenBAddress: string): string[][]

Gets all possible token swap paths between two tokens from the swap paths data.

import { getTokenSwapPathsForTokenPair } from "@gmx-ui/sdk/utils/swap/swapRouting";

const paths = getTokenSwapPathsForTokenPair(
swapPathsData,
"0x...", // Token A address
"0x..." // Token B address
);

console.log("Available swap paths:", paths);

getTokenSwapPathsForTokenPairPrebuilt

  • getTokenSwapPathsForTokenPairPrebuilt(chainId: number, from: string, to: string): string[][]

Gets prebuilt token swap paths for a token pair on a specific chain.

import { getTokenSwapPathsForTokenPairPrebuilt } from "@gmx-ui/sdk/utils/swap/swapRouting";

const paths = getTokenSwapPathsForTokenPairPrebuilt(
42161, // Arbitrum chain ID
"0x...", // From token address
"0x..." // To token address
);

console.log("Prebuilt swap paths:", paths);

getMarketAdjacencyGraph

  • getMarketAdjacencyGraph(chainId: number): MarketsGraph

Returns the prebuilt market adjacency graph for a specific chain.

import { getMarketAdjacencyGraph } from "@gmx-ui/sdk/utils/swap/swapRouting";

const graph = getMarketAdjacencyGraph(42161); // Arbitrum
console.log("Markets graph:", graph);

findAllReachableTokens

  • findAllReachableTokens(chainId: number, from: string): string[]

Finds all tokens reachable from a given token on a specific chain.

import { findAllReachableTokens } from "@gmx-ui/sdk/utils/swap/swapRouting";

const reachableTokens = findAllReachableTokens(42161, "0x...");
console.log("Reachable tokens:", reachableTokens);

getMaxLiquidityMarketSwapPathFromTokenSwapPaths

  • getMaxLiquidityMarketSwapPathFromTokenSwapPaths(params: { graph: MarketsGraph; tokenSwapPaths: string[][]; tokenInAddress: string; tokenOutAddress: string; getLiquidity: MarketEdgeLiquidityGetter }): { path: string[]; liquidity: bigint } | undefined

Parameters

  • params: object - Configuration object with graph, token swap paths, token addresses, and liquidity getter

Finds the swap path with maximum liquidity from available token swap paths.

import { getMaxLiquidityMarketSwapPathFromTokenSwapPaths } from "@gmx-ui/sdk/utils/swap/swapRouting";

const maxLiquidityPath = getMaxLiquidityMarketSwapPathFromTokenSwapPaths({
graph: marketsGraph,
tokenSwapPaths: tokenPaths,
tokenInAddress: "0x...",
tokenOutAddress: "0x...",
getLiquidity: liquidityGetter
});

if (maxLiquidityPath) {
console.log("Max liquidity path:", maxLiquidityPath.path);
console.log("Available liquidity:", maxLiquidityPath.liquidity);
}

getMaxLiquidityMarketForTokenEdge

  • getMaxLiquidityMarketForTokenEdge(params: { markets: string[]; tokenInAddress: string; tokenOutAddress: string; getLiquidity: MarketEdgeLiquidityGetter }): { marketAddress: string; liquidity: bigint }

Parameters

  • params: object - Configuration object with markets, token addresses, and liquidity getter

Finds the market with maximum liquidity for a specific token edge.

import { getMaxLiquidityMarketForTokenEdge } from "@gmx-ui/sdk/utils/swap/swapRouting";

const maxLiquidityMarket = getMaxLiquidityMarketForTokenEdge({
markets: ["0xmarket1", "0xmarket2"],
tokenInAddress: "0x...",
tokenOutAddress: "0x...",
getLiquidity: liquidityGetter
});

console.log("Market with max liquidity:", maxLiquidityMarket.marketAddress);
console.log("Available liquidity:", maxLiquidityMarket.liquidity);