swapStats
This module provides utilities for calculating swap statistics, capacity, and liquidity for token swaps within GMX markets. It includes functions for analyzing swap paths, calculating fees and price impacts, and determining available liquidity.
Methods
getSwapCapacityUsd
getSwapCapacityUsd(marketInfo: MarketInfo, isLong: boolean): bigint
Calculates the available swap capacity in USD for a specific market pool.
import { getSwapCapacityUsd } from "@gmx-ui/sdk/utils/swap";
const capacityUsd = getSwapCapacityUsd(marketInfo, true);
console.log(`Long pool capacity: ${capacityUsd}`);
getSwapPathOutputAddresses
getSwapPathOutputAddresses(p: object): { outTokenAddress: string | undefined; outMarketAddress: string | undefined }
Parameters
p: object- Configuration object containing:marketsInfoData: MarketsInfoData- Markets information datainitialCollateralAddress: string- Initial collateral token addressswapPath: string[]- Array of market addresses for the swap pathwrappedNativeTokenAddress: string- Wrapped native token addressshouldUnwrapNativeToken: boolean- Whether to unwrap native tokenisIncrease: boolean- Whether this is an increase operation
Determines the output token and market addresses for a given swap path.
import { getSwapPathOutputAddresses } from "@gmx-ui/sdk/utils/swap";
const result = getSwapPathOutputAddresses({
marketsInfoData,
initialCollateralAddress: "0x123...",
swapPath: ["0xmarket1", "0xmarket2"],
wrappedNativeTokenAddress: "0xweth...",
shouldUnwrapNativeToken: true,
isIncrease: false
});
console.log(`Output token: ${result.outTokenAddress}`);
getSwapPathStats
getSwapPathStats(p: object): SwapPathStats | undefined
Parameters
p: object- Configuration object containing:marketsInfoData: MarketsInfoData- Markets information dataswapPath: string[]- Array of market addresses for the swap pathinitialCollateralAddress: string- Initial collateral token addresswrappedNativeTokenAddress: string- Wrapped native token addressusdIn: bigint- USD amount inputshouldUnwrapNativeToken: boolean- Whether to unwrap native tokenshouldApplyPriceImpact: boolean- Whether to apply price impactisAtomicSwap: boolean- Whether this is an atomic swap
Calculates comprehensive statistics for a multi-step swap path including fees, price impact, and output amounts.
import { getSwapPathStats } from "@gmx-ui/sdk/utils/swap";
const pathStats = getSwapPathStats({
marketsInfoData,
swapPath: ["0xmarket1", "0xmarket2"],
initialCollateralAddress: "0x123...",
wrappedNativeTokenAddress: "0xweth...",
usdIn: 1000000000000000000000n, // 1000 USD
shouldUnwrapNativeToken: false,
shouldApplyPriceImpact: true,
isAtomicSwap: false
});
if (pathStats) {
console.log(`Total swap fee: ${pathStats.totalSwapFeeUsd}`);
console.log(`Amount out: ${pathStats.amountOut}`);
}
getSwapStats
getSwapStats(p: object): SwapStats
Parameters
p: object- Configuration object containing:marketInfo: MarketInfo- Market informationtokenInAddress: string- Input token addresstokenOutAddress: string- Output token addressusdIn: bigint- USD amount inputshouldApplyPriceImpact: boolean- Whether to apply price impactisAtomicSwap: boolean- Whether this is an atomic swap
Calculates detailed swap statistics for a single swap step including fees, price impact, and liquidity checks.
import { getSwapStats } from "@gmx-ui/sdk/utils/swap";
const swapStats = getSwapStats({
marketInfo,
tokenInAddress: "0xtoken1...",
tokenOutAddress: "0xtoken2...",
usdIn: 1000000000000000000000n, // 1000 USD
shouldApplyPriceImpact: true,
isAtomicSwap: false
});
console.log(`Swap fee: ${swapStats.swapFeeUsd}`);
console.log(`Price impact: ${swapStats.priceImpactDeltaUsd}`);
console.log(`Amount out: ${swapStats.amountOut}`);
getMaxSwapPathLiquidity
getMaxSwapPathLiquidity(p: object): bigint
Parameters
p: object- Configuration object containing:marketsInfoData: MarketsInfoData- Markets information dataswapPath: string[]- Array of market addresses for the swap pathinitialCollateralAddress: string- Initial collateral token address
Calculates the maximum available liquidity across all markets in a swap path, returning the minimum liquidity bottleneck.
import { getMaxSwapPathLiquidity } from "@gmx-ui/sdk/utils/swap";
const maxLiquidity = getMaxSwapPathLiquidity({
marketsInfoData,
swapPath: ["0xmarket1", "0xmarket2"],
initialCollateralAddress: "0x123..."
});
console.log(`Max available liquidity: ${maxLiquidity}`);