Skip to main content

swapValues

This module provides utilities for calculating swap amounts and values for token exchanges in the GMX protocol. It handles both internal GMX swaps and external swap integrations, supporting limit orders and market orders with various optimization strategies.

Methods

getSwapAmountsByFromValue

  • getSwapAmountsByFromValue(params): SwapAmounts

Parameters

  • tokenIn: TokenData - The input token data
  • tokenOut: TokenData - The output token data
  • amountIn: bigint - The input token amount
  • triggerRatio?: TokensRatio - Optional trigger ratio for limit orders
  • isLimit: boolean - Whether this is a limit order
  • swapOptimizationOrder?: SwapOptimizationOrderArray - Order preference for swap path optimization
  • allowedSwapSlippageBps?: bigint - Allowed slippage in basis points
  • uiFeeFactor: bigint - UI fee factor to apply
  • marketsInfoData: MarketsInfoData | undefined - Markets information data
  • chainId: number - The blockchain chain ID
  • externalSwapQuoteParams: ExternalSwapQuoteParams | undefined - External swap quote parameters
  • findSwapPath: FindSwapPath - Function to find optimal swap path

Calculates swap amounts based on a specified input token amount. Handles both internal GMX swaps and external swap integrations.

import { getSwapAmountsByFromValue } from "@gmx-ui/sdk/utils/swap";

const swapAmounts = getSwapAmountsByFromValue({
tokenIn: wethToken,
tokenOut: usdcToken,
amountIn: 1000000000000000000n, // 1 ETH
isLimit: false,
uiFeeFactor: 5000000000000000000000000000n,
marketsInfoData,
chainId: 42161,
externalSwapQuoteParams: undefined,
findSwapPath: myFindSwapPath
});

console.log(swapAmounts.amountOut); // Expected USDC output
console.log(swapAmounts.usdOut); // USD value of output

getSwapAmountsByToValue

  • getSwapAmountsByToValue(params): SwapAmounts

Parameters

  • tokenIn: TokenData - The input token data
  • tokenOut: TokenData - The output token data
  • amountOut: bigint - The desired output token amount
  • triggerRatio?: TokensRatio - Optional trigger ratio for limit orders
  • isLimit: boolean - Whether this is a limit order
  • swapOptimizationOrder?: SwapOptimizationOrderArray - Order preference for swap path optimization
  • allowedSwapSlippageBps?: bigint - Allowed slippage in basis points
  • uiFeeFactor: bigint - UI fee factor to apply
  • marketsInfoData: MarketsInfoData | undefined - Markets information data
  • chainId: number - The blockchain chain ID
  • externalSwapQuoteParams: ExternalSwapQuoteParams | undefined - External swap quote parameters
  • findSwapPath: FindSwapPath - Function to find optimal swap path

Calculates swap amounts based on a specified output token amount. Determines the required input amount to achieve the desired output.

import { getSwapAmountsByToValue } from "@gmx-ui/sdk/utils/swap";

const swapAmounts = getSwapAmountsByToValue({
tokenIn: wethToken,
tokenOut: usdcToken,
amountOut: 2000000000n, // 2000 USDC
isLimit: false,
uiFeeFactor: 5000000000000000000000000000n,
marketsInfoData,
chainId: 42161,
externalSwapQuoteParams: undefined,
findSwapPath: myFindSwapPath
});

console.log(swapAmounts.amountIn); // Required ETH input
console.log(swapAmounts.usdIn); // USD value of input

getSwapPathComparator

  • getSwapPathComparator(order?: SwapOptimizationOrderArray): (a: SwapRoute, b: SwapRoute) => number

Parameters

  • order?: SwapOptimizationOrderArray - Optional array defining the optimization order criteria

Returns a comparator function for sorting swap routes based on optimization criteria like liquidity and path length.

import { getSwapPathComparator } from "@gmx-ui/sdk/utils/swap";

const comparator = getSwapPathComparator(["liquidity", "path"]);

const sortedRoutes = swapRoutes.sort(comparator);

// Routes are now sorted by liquidity (descending) then by path length (ascending)
console.log(sortedRoutes[0]); // Best route according to criteria