fees
This module provides utilities for calculating various fees in the GMX protocol, including swap fees, position fees, funding fees, borrowing fees, and price impact calculations. It also includes helper functions for working with fee items and swap statistics.
Methods
getSwapFee
getSwapFee(marketInfo, swapAmount, balanceWasImproved, isAtomicSwap): bigint
Parameters
marketInfo: MarketInfo- Market information containing fee factorsswapAmount: bigint- Amount being swapped in token unitsbalanceWasImproved: boolean- Whether the swap improves market balanceisAtomicSwap: boolean- Whether this is an atomic swap
Calculates the swap fee for a given swap amount based on market conditions and swap type.
import { getSwapFee } from "@gmx-ui/sdk/utils/fees";
const swapFee = getSwapFee(
marketInfo,
1000000000000000000n, // 1 token
true,
false
);
getPositionFee
getPositionFee(marketInfo, sizeDeltaUsd, balanceWasImproved, referralInfo, uiFeeFactor): object
Parameters
marketInfo: MarketInfo- Market information containing fee factorssizeDeltaUsd: bigint- Position size change in USDbalanceWasImproved: boolean- Whether the position improves market balancereferralInfo?: { totalRebateFactor: bigint; discountFactor: bigint }- Referral discount informationuiFeeFactor?: bigint- UI fee factor
Calculates position fees including discounts and rebates from referral programs.
import { getPositionFee } from "@gmx-ui/sdk/utils/fees";
const feeResult = getPositionFee(
marketInfo,
1000000000000000000000n, // $1000 USD
false,
{ totalRebateFactor: 5000n, discountFactor: 3000n },
100n
);
// Returns: { positionFeeUsd, discountUsd, totalRebateUsd, uiFeeUsd }
getFundingFactorPerPeriod
getFundingFactorPerPeriod(marketInfo, isLong, periodInSeconds): bigint
Parameters
marketInfo: MarketInfo- Market information containing funding dataisLong: boolean- Whether calculating for long positionsperiodInSeconds: number- Time period in seconds
Calculates the funding factor for a specific time period based on market conditions.
import { getFundingFactorPerPeriod } from "@gmx-ui/sdk/utils/fees";
const fundingFactor = getFundingFactorPerPeriod(
marketInfo,
true,
3600 // 1 hour
);
getFundingFeeRateUsd
getFundingFeeRateUsd(marketInfo, isLong, sizeInUsd, periodInSeconds): bigint
Parameters
marketInfo: MarketInfo- Market information containing funding dataisLong: boolean- Whether calculating for long positionssizeInUsd: bigint- Position size in USDperiodInSeconds: number- Time period in seconds
Calculates the funding fee rate in USD for a position over a specific time period.
import { getFundingFeeRateUsd } from "@gmx-ui/sdk/utils/fees";
const fundingFeeRate = getFundingFeeRateUsd(
marketInfo,
true,
1000000000000000000000n, // $1000 USD
86400 // 24 hours
);
getBorrowingFactorPerPeriod
getBorrowingFactorPerPeriod(marketInfo, isLong, periodInSeconds): bigint
Parameters
marketInfo: MarketInfo- Market information containing borrowing factorsisLong: boolean- Whether calculating for long positionsperiodInSeconds: number- Time period in seconds
Calculates the borrowing factor for a specific time period.
import { getBorrowingFactorPerPeriod } from "@gmx-ui/sdk/utils/fees";
const borrowingFactor = getBorrowingFactorPerPeriod(
marketInfo,
false,
3600 // 1 hour
);
getBorrowingFeeRateUsd
getBorrowingFeeRateUsd(marketInfo, isLong, sizeInUsd, periodInSeconds): bigint
Parameters
marketInfo: MarketInfo- Market information containing borrowing factorsisLong: boolean- Whether calculating for long positionssizeInUsd: bigint- Position size in USDperiodInSeconds: number- Time period in seconds
Calculates the borrowing fee rate in USD for a position over a specific time period.
import { getBorrowingFeeRateUsd } from "@gmx-ui/sdk/utils/fees";
const borrowingFeeRate = getBorrowingFeeRateUsd(
marketInfo,
false,
1000000000000000000000n, // $1000 USD
86400 // 24 hours
);
getIsHighPriceImpact
getIsHighPriceImpact(positionPriceImpact, swapPriceImpact): boolean
Parameters
positionPriceImpact?: FeeItem- Price impact from position changesswapPriceImpact?: FeeItem- Price impact from swaps
Determines if the combined price impact exceeds the high price impact threshold.
import { getIsHighPriceImpact } from "@gmx-ui/sdk/utils/fees";
const isHighImpact = getIsHighPriceImpact(
{ deltaUsd: -5000000000000000000n, bps: 500n, precisePercentage: 50000000000000000n },
{ deltaUsd: -2000000000000000000n, bps: 200n, precisePercentage: 20000000000000000n }
);
getFeeItem
getFeeItem(feeDeltaUsd, basis, opts): FeeItem | undefined
Parameters
feeDeltaUsd?: bigint- Fee amount in USDbasis?: bigint- Basis amount for percentage calculationsopts?: { shouldRoundUp?: boolean }- Options for rounding behavior
Creates a FeeItem object with delta USD, basis points, and precise percentage.
import { getFeeItem } from "@gmx-ui/sdk/utils/fees";
const feeItem = getFeeItem(
1000000000000000000n, // $1 USD
100000000000000000000n, // $100 USD basis
{ shouldRoundUp: true }
);
// Returns: { deltaUsd, bps, precisePercentage }
getTotalFeeItem
getTotalFeeItem(feeItems): FeeItem
Parameters
feeItems: (FeeItem | undefined)[]- Array of fee items to sum
Calculates the total of multiple fee items by summing their components.
import { getTotalFeeItem } from "@gmx-ui/sdk/utils/fees";
const totalFee = getTotalFeeItem([
{ deltaUsd: 1000000000000000000n, bps: 100n, precisePercentage: 10000000000000000n },
{ deltaUsd: 2000000000000000000n, bps: 200n, precisePercentage: 20000000000000000n },
undefined
]);
// Returns: { deltaUsd: 3000000000000000000n, bps: 300n, precisePercentage: 30000000000000000n }
getTotalSwapVolumeFromSwapStats
getTotalSwapVolumeFromSwapStats(swapSteps): bigint
Parameters
swapSteps?: SwapStats[]- Array of swap statistics
Calculates the total swap volume from an array of swap statistics.
import { getTotalSwapVolumeFromSwapStats } from "@gmx-ui/sdk/utils/fees";
const totalVolume = getTotalSwapVolumeFromSwapStats([
{ usdIn: 1000000000000000000000n },
{ usdIn: 2000000000000000000000n }
]);
// Returns: 3000000000000000000000n