findSwapPathsBetweenTokens
This module provides functionality to find all possible swap paths between tokens in a markets graph. It analyzes the connectivity between tokens and generates optimized routing paths while avoiding inefficient circular routes.
Methods
findSwapPathsBetweenTokens
findSwapPathsBetweenTokens(graph: MarketsGraph): SwapPaths
Parameters
graph: MarketsGraph- A graph representation of markets showing token connectivity and available swap routes
Finds all possible swap paths between tokens in the provided markets graph. The function performs a breadth-first search to discover routes while applying optimizations to avoid circular paths that would result in guaranteed losses.
import { findSwapPathsBetweenTokens } from "@gmx-ui/sdk/utils/swap";
import { buildMarketsAdjacencyGraph } from "@gmx-ui/sdk/utils/swap";
// Assuming you have markets data
const graph = buildMarketsAdjacencyGraph(markets);
const swapPaths = findSwapPathsBetweenTokens(graph);
// Access swap paths between specific tokens
const pathsFromTokenA = swapPaths["0x1234..."];
const pathsFromAToB = swapPaths["0x1234..."]["0x5678..."];
// Each path is an array of intermediate token addresses
pathsFromAToB?.forEach(path => {
console.log("Intermediate tokens:", path);
});