trade
This module provides utility functions for trade operations including slippage calculations, swap counting, and trade flag creation for the GMX protocol.
Methods
applySlippageToPrice
applySlippageToPrice(allowedSlippage: number, price: bigint, isIncrease: boolean, isLong: boolean): bigint
Parameters
allowedSlippage: number- The allowed slippage in basis pointsprice: bigint- The original price to apply slippage toisIncrease: boolean- Whether this is an increase position operationisLong: boolean- Whether this is a long position
Applies slippage to a price based on position direction and operation type. The function determines whether to increase or decrease the price based on whether max price should be used.
import { applySlippageToPrice } from "@gmx-ui/sdk/utils/trade";
const originalPrice = 50000000000000000000000n; // $50,000 in wei
const slippage = 50; // 0.5% in basis points
const isIncrease = true;
const isLong = true;
const adjustedPrice = applySlippageToPrice(slippage, originalPrice, isIncrease, isLong);
applySlippageToMinOut
applySlippageToMinOut(allowedSlippage: number, minOutputAmount: bigint): bigint
Parameters
allowedSlippage: number- The allowed slippage in basis pointsminOutputAmount: bigint- The minimum output amount before slippage
Applies slippage to reduce the minimum output amount, accounting for potential price impact during execution.
import { applySlippageToMinOut } from "@gmx-ui/sdk/utils/trade";
const minOutput = 1000000000000000000n; // 1 token in wei
const slippage = 100; // 1% in basis points
const adjustedMinOut = applySlippageToMinOut(slippage, minOutput);
getSwapCount
getSwapCount(params: { isSwap: boolean; isIncrease: boolean; swapAmounts?: SwapAmounts; increaseAmounts?: IncreasePositionAmounts; decreaseAmounts?: DecreasePositionAmounts; }): number | undefined
Parameters
isSwap: boolean- Whether this is a swap operationisIncrease: boolean- Whether this is an increase position operationswapAmounts?: SwapAmounts- Swap amounts data containing swap strategyincreaseAmounts?: IncreasePositionAmounts- Increase position amounts datadecreaseAmounts?: DecreasePositionAmounts- Decrease position amounts data
Calculates the number of swaps required for a trade operation based on the trade type and swap strategy.
import { getSwapCount } from "@gmx-ui/sdk/utils/trade";
import { SwapAmounts } from "@gmx-ui/sdk/types/trade";
const swapAmounts: SwapAmounts = {
swapStrategy: {
swapPathStats: {
swapPath: ["0x123...", "0x456..."],
},
},
};
const swapCount = getSwapCount({
isSwap: true,
isIncrease: false,
swapAmounts,
});
createTradeFlags
createTradeFlags(tradeType: TradeType, tradeMode: TradeMode): TradeFlags
Parameters
tradeType: TradeType- The type of trade (Long, Short, or Swap)tradeMode: TradeMode- The mode of trade (Market, Limit, StopMarket, Trigger, or Twap)
Creates a comprehensive set of boolean flags that describe the characteristics of a trade operation.
import { createTradeFlags } from "@gmx-ui/sdk/utils/trade";
import { TradeType, TradeMode } from "@gmx-ui/sdk/types/trade";
const tradeFlags = createTradeFlags(TradeType.Long, TradeMode.Market);
// Returns: { isLong: true, isShort: false, isSwap: false, isPosition: true, isIncrease: true, isMarket: true, isLimit: false, isTrigger: false, isTwap: false }