Skip to main content

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 data
    • initialCollateralAddress: string - Initial collateral token address
    • swapPath: string[] - Array of market addresses for the swap path
    • wrappedNativeTokenAddress: string - Wrapped native token address
    • shouldUnwrapNativeToken: boolean - Whether to unwrap native token
    • isIncrease: 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 data
    • swapPath: string[] - Array of market addresses for the swap path
    • initialCollateralAddress: string - Initial collateral token address
    • wrappedNativeTokenAddress: string - Wrapped native token address
    • usdIn: bigint - USD amount input
    • shouldUnwrapNativeToken: boolean - Whether to unwrap native token
    • shouldApplyPriceImpact: boolean - Whether to apply price impact
    • isAtomicSwap: 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 information
    • tokenInAddress: string - Input token address
    • tokenOutAddress: string - Output token address
    • usdIn: bigint - USD amount input
    • shouldApplyPriceImpact: boolean - Whether to apply price impact
    • isAtomicSwap: 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 data
    • swapPath: string[] - Array of market addresses for the swap path
    • initialCollateralAddress: 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}`);