Skip to main content

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

The findReachableTokens function is exported from @gmx-io/sdk/utils/swap.

findReachableTokens

findReachableTokens(graph: MarketsGraph): Record<string, string[]>

Performs a breadth-first search from every token in graph and returns a map of tokenAddress → reachableTokenAddresses[]. The search stops at MAX_EDGE_PATH_LENGTH hops (currently 3). Each token's own address is always included in its reachable set.

For most SDK use cases, prefer findAllReachableTokens(chainId, from) from @gmx-io/sdk/utils/swap, which provides an O(1) lookup using precomputed data. Use findReachableTokens directly when you've built a custom MarketsGraph with buildMarketsAdjacencyGraph.

import { findReachableTokens, buildMarketsAdjacencyGraph } from "@gmx-io/sdk/utils/swap";
import { MARKETS } from "@gmx-io/sdk/configs/markets";

// Build a custom graph (for example, to exclude certain markets)
const filteredMarketsMap = Object.fromEntries(
Object.entries(MARKETS[42161]).filter(([addr]) => addr !== excludedMarketAddress)
);
const graph = buildMarketsAdjacencyGraph(filteredMarketsMap);

// Compute reachability for all tokens in the custom graph
const reachableTokens = findReachableTokens(graph);

// reachableTokens[wethAddress] — all tokens reachable from WETH in ≤3 hops
// including wethAddress itself