Skip to main content

executionFee

This module provides utilities for calculating execution fees and estimating gas limits for various GMX protocol operations. It handles fee calculations for orders, deposits, withdrawals, and batch operations while considering network-specific gas costs and limits.

Methods

getExecutionFee

  • getExecutionFee(chainId: number, gasLimits: GasLimitsConfig, tokensData: TokensData, estimatedGasLimit: bigint, gasPrice: bigint, oraclePriceCount: bigint, numberOfParts?: number): ExecutionFee | undefined

Parameters

  • chainId: number - The blockchain network ID
  • gasLimits: GasLimitsConfig - Gas limit configuration for the chain
  • tokensData: TokensData - Token data including native token information
  • estimatedGasLimit: bigint - Estimated gas limit for the operation
  • gasPrice: bigint - Current gas price
  • oraclePriceCount: bigint - Number of oracle prices required
  • numberOfParts?: number - Optional number of parts for multi-part operations

Calculates the execution fee for a GMX operation, including gas costs and fee validation.

import { getExecutionFee } from "@gmx-ui/sdk/utils/fees";

const executionFee = getExecutionFee(
42161, // Arbitrum
gasLimits,
tokensData,
500000n, // estimated gas
1000000000n, // gas price in wei
2n, // oracle price count
1 // single operation
);

if (executionFee) {
console.log("Fee USD:", executionFee.feeUsd);
console.log("Fee Token Amount:", executionFee.feeTokenAmount);
console.log("Is High Fee:", executionFee.isFeeHigh);
}

estimateRelayerGasLimit

  • estimateRelayerGasLimit(params: RelayerGasParams): bigint

Parameters

  • params: RelayerGasParams - Object containing gas estimation parameters including token permits, fee swaps, oracle prices, and transaction payload

Estimates the total gas limit required for relayer operations including swaps, oracle updates, and permits.

import { estimateRelayerGasLimit } from "@gmx-ui/sdk/utils/fees";

const gasLimit = estimateRelayerGasLimit({
gasLimits,
tokenPermitsCount: 1,
feeSwapsCount: 2,
feeExternalCallsGasLimit: 50000n,
oraclePriceCount: 3,
transactionPayloadGasLimit: 200000n,
l1GasLimit: 100000n
});

approximateL1GasBuffer

  • approximateL1GasBuffer(params: { l1Reference: L1ExpressOrderGasReference; sizeOfData: bigint }): bigint

Parameters

  • l1Reference: L1ExpressOrderGasReference - Reference gas data for L1 operations
  • sizeOfData: bigint - Size of the data being processed

Approximates the L1 gas buffer needed based on data size using logarithmic scaling.

import { approximateL1GasBuffer } from "@gmx-ui/sdk/utils/fees";

const l1GasLimit = approximateL1GasBuffer({
l1Reference: {
gasLimit: 100000n,
sizeOfData: 1000n
},
sizeOfData: 2000n
});

estimateBatchGasLimit

  • estimateBatchGasLimit(params: BatchGasParams): bigint

Parameters

  • params: BatchGasParams - Object containing counts for create, update, cancel operations and external calls gas limit

Estimates gas limit for batch operations including order creation, updates, and cancellations.

import { estimateBatchGasLimit } from "@gmx-ui/sdk/utils/fees";

const batchGasLimit = estimateBatchGasLimit({
gasLimits,
createOrdersCount: 3,
updateOrdersCount: 1,
cancelOrdersCount: 2,
externalCallsGasLimit: 75000n,
isGmxAccount: true
});

estimateBatchMinGasPaymentTokenAmount

  • estimateBatchMinGasPaymentTokenAmount(params: BatchPaymentParams): bigint

Parameters

  • params: BatchPaymentParams - Object containing chain ID, tokens, gas parameters, and operation counts

Estimates the minimum gas payment token amount required for batch operations.

import { estimateBatchMinGasPaymentTokenAmount } from "@gmx-ui/sdk/utils/fees";

const minPayment = estimateBatchMinGasPaymentTokenAmount({
chainId: 42161,
gasPaymentToken: usdcToken,
isGmxAccount: false,
relayFeeToken: ethToken,
gasPrice: 1000000000n,
gasLimits,
l1Reference: undefined,
tokensData,
createOrdersCount: 2,
updateOrdersCount: 0,
cancelOrdersCount: 1,
executionFeeAmount: undefined
});

estimateExecuteIncreaseOrderGasLimit

  • estimateExecuteIncreaseOrderGasLimit(gasLimits: GasLimitsConfig, order: { swapsCount?: number; callbackGasLimit?: bigint }): bigint

Parameters

  • gasLimits: GasLimitsConfig - Gas limit configuration
  • order: object - Order parameters including swap count and callback gas limit

Estimates gas limit for executing increase position orders.

import { estimateExecuteIncreaseOrderGasLimit } from "@gmx-ui/sdk/utils/fees";

const gasLimit = estimateExecuteIncreaseOrderGasLimit(gasLimits, {
swapsCount: 2,
callbackGasLimit: 50000n
});

estimateExecuteDecreaseOrderGasLimit

  • estimateExecuteDecreaseOrderGasLimit(gasLimits: GasLimitsConfig, order: { swapsCount: number; callbackGasLimit?: bigint; decreaseSwapType?: DecreasePositionSwapType }): bigint

Parameters

  • gasLimits: GasLimitsConfig - Gas limit configuration
  • order: object - Order parameters including swap count, callback gas limit, and decrease swap type

Estimates gas limit for executing decrease position orders.

import { estimateExecuteDecreaseOrderGasLimit } from "@gmx-ui/sdk/utils/fees";
import { DecreasePositionSwapType } from "@gmx-ui/sdk/types/orders";

const gasLimit = estimateExecuteDecreaseOrderGasLimit(gasLimits, {
swapsCount: 1,
callbackGasLimit: 30000n,
decreaseSwapType: DecreasePositionSwapType.SwapCollateralTokenToPnlToken
});

estimateExecuteSwapOrderGasLimit

  • estimateExecuteSwapOrderGasLimit(gasLimits: GasLimitsConfig, order: { swapsCount: number; callbackGasLimit?: bigint }): bigint

Parameters

  • gasLimits: GasLimitsConfig - Gas limit configuration
  • order: object - Order parameters including swap count and callback gas limit

Estimates gas limit for executing swap orders.

import { estimateExecuteSwapOrderGasLimit } from "@gmx-ui/sdk/utils/fees";

const gasLimit = estimateExecuteSwapOrderGasLimit(gasLimits, {
swapsCount: 3,
callbackGasLimit: 40000n
});

estimateExecuteDepositGasLimit

  • estimateExecuteDepositGasLimit(gasLimits: GasLimitsConfig, deposit: { longTokenSwapsCount?: number; shortTokenSwapsCount?: number; callbackGasLimit?: bigint }): bigint

Parameters

  • gasLimits: GasLimitsConfig - Gas limit configuration
  • deposit: object - Deposit parameters including token swap counts and callback gas limit

Estimates gas limit for executing GM token deposits.

import { estimateExecuteDepositGasLimit } from "@gmx-ui/sdk/utils/fees";

const gasLimit = estimateExecuteDepositGasLimit(gasLimits, {
longTokenSwapsCount: 1,
shortTokenSwapsCount: 1,
callbackGasLimit: 25000n
});

estimateExecuteGlvDepositGasLimit

  • estimateExecuteGlvDepositGasLimit(gasLimits: GasLimitsConfig, params: { isMarketTokenDeposit: boolean; marketsCount: bigint; initialLongTokenAmount: bigint; initialShortTokenAmount: bigint }): bigint

Parameters

  • gasLimits: GasLimitsConfig - Gas limit configuration
  • params: object - GLV deposit parameters including market token flag, markets count, and token amounts

Estimates gas limit for executing GLV deposits.

import { estimateExecuteGlvDepositGasLimit } from "@gmx-ui/sdk/utils/fees";

const gasLimit = estimateExecuteGlvDepositGasLimit(gasLimits, {
isMarketTokenDeposit: false,
marketsCount: 3n,
initialLongTokenAmount: 1000000n,
initialShortTokenAmount: 2000000n
});

estimateExecuteGlvWithdrawalGasLimit

  • estimateExecuteGlvWithdrawalGasLimit(gasLimits: GasLimitsConfig, params: { marketsCount: bigint }): bigint

Parameters

  • gasLimits: GasLimitsConfig - Gas limit configuration
  • params: object - GLV withdrawal parameters including markets count

Estimates gas limit for executing GLV withdrawals.

import { estimateExecuteGlvWithdrawalGasLimit } from "@gmx-ui/sdk/utils/fees";

const gasLimit = estimateExecuteGlvWithdrawalGasLimit(gasLimits, {
marketsCount: 2n
});

estimateExecuteWithdrawalGasLimit

  • estimateExecuteWithdrawalGasLimit(gasLimits: GasLimitsConfig, withdrawal: { callbackGasLimit?: bigint }): bigint

Parameters

  • gasLimits: GasLimitsConfig - Gas limit configuration
  • withdrawal: object - Withdrawal parameters including callback gas limit

Estimates gas limit for executing GM token withdrawals.

import { estimateExecuteWithdrawalGasLimit } from "@gmx-ui/sdk/utils/fees";

const gasLimit = estimateExecuteWithdrawalGasLimit(gasLimits, {
callbackGasLimit: 35000n
});

estimateExecuteShiftGasLimit

  • estimateExecuteShiftGasLimit(gasLimits: GasLimitsConfig, shift: { callbackGasLimit?: bigint }): bigint

Parameters

  • gasLimits: GasLimitsConfig - Gas limit configuration
  • shift: object - Shift parameters including callback gas limit

Estimates gas limit for executing position shifts between markets.

import { estimateExecuteShiftGasLimit } from "@gmx-ui/sdk/utils/fees";

const gasLimit = estimateExecuteShiftGasLimit(gasLimits, {
callbackGasLimit: 20000n
});