swapPath
This module provides utilities for finding optimal swap paths between tokens in the GMX protocol. It handles path discovery, liquidity analysis, and gas cost estimation to determine the most efficient routes for token swaps.
Methods
getWrappedAddress
getWrappedAddress(chainId: number, address: string | undefined): string | undefined
Converts a token address to its wrapped equivalent for the specified chain.
import { getWrappedAddress } from "@gmx-ui/sdk/utils/swap";
const wrappedAddress = getWrappedAddress(42161, "0x1234...5678");
console.log(wrappedAddress); // Returns wrapped token address
createFindSwapPath
createFindSwapPath(params: CreateFindSwapPathParams): FindSwapPath
Parameters
params.chainId: number- The blockchain chain IDparams.fromTokenAddress: string | undefined- Address of the token to swap fromparams.toTokenAddress: string | undefined- Address of the token to swap toparams.marketsInfoData: MarketsInfoData | undefined- Market information data for liquidity calculationsparams.gasEstimationParams?: object- Optional gas estimation parameters containing gasPrice, gasLimits, and tokensDataparams.isExpressFeeSwap: boolean | undefined- Whether this is an express fee swapparams.disabledMarkets?: string[]- Optional array of disabled market addressesparams.manualPath?: string[]- Optional manual swap path to use instead of automatic path finding
Creates a function that finds optimal swap paths between tokens based on liquidity, gas costs, and other constraints.
import { createFindSwapPath } from "@gmx-ui/sdk/utils/swap";
const findSwapPath = createFindSwapPath({
chainId: 42161,
fromTokenAddress: "0x1234...5678",
toTokenAddress: "0x8765...4321",
marketsInfoData: marketsInfo,
gasEstimationParams: {
gasPrice: 1000000000n,
gasLimits: gasLimits,
tokensData: tokensData
},
isExpressFeeSwap: false,
disabledMarkets: ["0xmarket1", "0xmarket2"]
});
// Use the function to find a swap path
const swapPath = findSwapPath(1000000000000000000n, {
order: ["liquidity", "length"]
});
if (swapPath) {
console.log("Swap path found:", swapPath.swapPath);
console.log("Expected output:", swapPath.targetTokenAmount);
}