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 datatokenOut: TokenData- The output token dataamountIn: bigint- The input token amounttriggerRatio?: TokensRatio- Optional trigger ratio for limit ordersisLimit: boolean- Whether this is a limit orderswapOptimizationOrder?: SwapOptimizationOrderArray- Order preference for swap path optimizationallowedSwapSlippageBps?: bigint- Allowed slippage in basis pointsuiFeeFactor: bigint- UI fee factor to applymarketsInfoData: MarketsInfoData | undefined- Markets information datachainId: number- The blockchain chain IDexternalSwapQuoteParams: ExternalSwapQuoteParams | undefined- External swap quote parametersfindSwapPath: 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 datatokenOut: TokenData- The output token dataamountOut: bigint- The desired output token amounttriggerRatio?: TokensRatio- Optional trigger ratio for limit ordersisLimit: boolean- Whether this is a limit orderswapOptimizationOrder?: SwapOptimizationOrderArray- Order preference for swap path optimizationallowedSwapSlippageBps?: bigint- Allowed slippage in basis pointsuiFeeFactor: bigint- UI fee factor to applymarketsInfoData: MarketsInfoData | undefined- Markets information datachainId: number- The blockchain chain IDexternalSwapQuoteParams: ExternalSwapQuoteParams | undefined- External swap quote parametersfindSwapPath: 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