findReachableTokens
This module provides functionality to find all tokens that can be reached from each token in a markets graph through swap operations, respecting maximum path length constraints.
Methods
findReachableTokens
findReachableTokens(graph: MarketsGraph): Record<string, string[]>
Parameters
graph: MarketsGraph- The markets adjacency graph representing possible token swaps
Finds all tokens that can be reached from each token in the graph through a series of swaps. The function performs a breadth-first search from each token to discover all reachable destinations within the maximum allowed path length.
import { findReachableTokens } from "@gmx-ui/sdk/utils/swap";
import { buildMarketsAdjacencyGraph } from "@gmx-ui/sdk/utils/swap";
// Assuming you have markets data and tokens info
const graph = buildMarketsAdjacencyGraph(marketsInfoData, tokensData);
// Find all reachable tokens from each starting token
const reachableTokens = findReachableTokens(graph);
// Example result structure:
// {
// "0x123...": ["0x123...", "0x456...", "0x789..."], // USDC can reach USDC, WETH, ARB
// "0x456...": ["0x456...", "0x123...", "0x789..."], // WETH can reach WETH, USDC, ARB
// "0x789...": ["0x789...", "0x123...", "0x456..."] // ARB can reach ARB, USDC, WETH
// }
// Check what tokens are reachable from a specific token
const usdcAddress = "0x123...";
const tokensReachableFromUsdc = reachableTokens[usdcAddress];
console.log(`From USDC, you can reach: ${tokensReachableFromUsdc.length} tokens`);