priceImpact
This module provides utilities for calculating price impact in GMX protocol operations, including position trades and swaps. It handles price impact calculations based on pool imbalances, virtual inventories, and various impact factors.
Methods
getPriceImpactByAcceptablePrice
getPriceImpactByAcceptablePrice(p: { sizeDeltaUsd: bigint; acceptablePrice: bigint; indexPrice: bigint; isLong: boolean; isIncrease: boolean }): { priceImpactDeltaUsd: bigint; priceImpactDeltaAmount: bigint; priceDelta: bigint; acceptablePriceDeltaBps: bigint }
Parameters
p: object- Parameters object containing trade detailssizeDeltaUsd: bigint- Size delta in USDacceptablePrice: bigint- Acceptable price for the tradeindexPrice: bigint- Current index priceisLong: boolean- Whether the position is longisIncrease: boolean- Whether this is an increase operation
Calculates price impact based on the difference between acceptable price and current index price.
import { getPriceImpactByAcceptablePrice } from "@gmx-ui/sdk/utils/fees";
const priceImpact = getPriceImpactByAcceptablePrice({
sizeDeltaUsd: 1000000000000000000000000000000n, // $1000
acceptablePrice: 2000000000000000000000000000000n, // $2000
indexPrice: 1950000000000000000000000000000n, // $1950
isLong: true,
isIncrease: true
});
console.log(priceImpact.priceImpactDeltaUsd);
applySwapImpactWithCap
applySwapImpactWithCap(marketInfo: MarketInfo, token: TokenData, priceImpactDeltaUsd: bigint): { impactDeltaAmount: bigint; cappedDiffUsd: bigint }
Parameters
marketInfo: MarketInfo- Market informationtoken: TokenData- Token datapriceImpactDeltaUsd: bigint- Price impact delta in USD
Applies swap impact with capping based on available impact pool amounts.
import { applySwapImpactWithCap } from "@gmx-ui/sdk/utils/fees";
const result = applySwapImpactWithCap(
marketInfo,
tokenData,
500000000000000000000000000000n // $500 impact
);
console.log(result.impactDeltaAmount);
console.log(result.cappedDiffUsd);
getCappedPositionImpactUsd
getCappedPositionImpactUsd(marketInfo: MarketInfo, sizeDeltaUsd: bigint, isLong: boolean, isIncrease: boolean, opts?: { fallbackToZero?: boolean; shouldCapNegativeImpact?: boolean }): { priceImpactDeltaUsd: bigint; balanceWasImproved: boolean }
Parameters
marketInfo: MarketInfo- Market informationsizeDeltaUsd: bigint- Size delta in USDisLong: boolean- Whether the position is longisIncrease: boolean- Whether this is an increase operationopts: object- Optional parameters for calculation behavior
Calculates position price impact with capping based on maximum impact factors.
import { getCappedPositionImpactUsd } from "@gmx-ui/sdk/utils/fees";
const impact = getCappedPositionImpactUsd(
marketInfo,
1000000000000000000000000000000n, // $1000
true,
true,
{ fallbackToZero: true }
);
console.log(impact.priceImpactDeltaUsd);
console.log(impact.balanceWasImproved);
capPositionImpactUsdByMaxImpactPool
capPositionImpactUsdByMaxImpactPool(marketInfo: MarketInfo, positionImpactDeltaUsd: bigint): bigint
Parameters
marketInfo: MarketInfo- Market informationpositionImpactDeltaUsd: bigint- Position impact delta in USD
Caps position impact based on available impact pool amount.
import { capPositionImpactUsdByMaxImpactPool } from "@gmx-ui/sdk/utils/fees";
const cappedImpact = capPositionImpactUsdByMaxImpactPool(
marketInfo,
750000000000000000000000000000n // $750 impact
);
console.log(cappedImpact);
capPositionImpactUsdByMaxPriceImpactFactor
capPositionImpactUsdByMaxPriceImpactFactor(marketInfo: MarketInfo, sizeDeltaUsd: bigint, positionImpactDeltaUsd: bigint): bigint
Parameters
marketInfo: MarketInfo- Market informationsizeDeltaUsd: bigint- Size delta in USDpositionImpactDeltaUsd: bigint- Position impact delta in USD
Caps position impact based on maximum price impact factor.
import { capPositionImpactUsdByMaxPriceImpactFactor } from "@gmx-ui/sdk/utils/fees";
const cappedImpact = capPositionImpactUsdByMaxPriceImpactFactor(
marketInfo,
1000000000000000000000000000000n, // $1000 size
500000000000000000000000000000n // $500 impact
);
console.log(cappedImpact);
getMaxPositionImpactFactors
getMaxPositionImpactFactors(marketInfo: MarketInfo): { maxPositiveImpactFactor: bigint; maxNegativeImpactFactor: bigint }
Parameters
marketInfo: MarketInfo- Market information
Gets maximum position impact factors for positive and negative impacts.
import { getMaxPositionImpactFactors } from "@gmx-ui/sdk/utils/fees";
const factors = getMaxPositionImpactFactors(marketInfo);
console.log(factors.maxPositiveImpactFactor);
console.log(factors.maxNegativeImpactFactor);
getPriceImpactForPosition
getPriceImpactForPosition(marketInfo: MarketInfo, sizeDeltaUsd: bigint, isLong: boolean, opts?: { fallbackToZero?: boolean }): { priceImpactDeltaUsd: bigint; balanceWasImproved: boolean }
Parameters
marketInfo: MarketInfo- Market informationsizeDeltaUsd: bigint- Size delta in USDisLong: boolean- Whether the position is longopts: object- Optional parameters for calculation behavior
Calculates price impact for position operations considering open interest and virtual inventory.
import { getPriceImpactForPosition } from "@gmx-ui/sdk/utils/fees";
const impact = getPriceImpactForPosition(
marketInfo,
1000000000000000000000000000000n, // $1000
true,
{ fallbackToZero: true }
);
console.log(impact.priceImpactDeltaUsd);
console.log(impact.balanceWasImproved);
getProportionalPendingImpactValues
getProportionalPendingImpactValues({ sizeInUsd, pendingImpactAmount, sizeDeltaUsd, indexToken }: { sizeInUsd: bigint; pendingImpactAmount: bigint; sizeDeltaUsd: bigint; indexToken: TokenData }): { proportionalPendingImpactDeltaAmount: bigint; proportionalPendingImpactDeltaUsd: bigint }
Parameters
sizeInUsd: bigint- Current position size in USDpendingImpactAmount: bigint- Pending impact amountsizeDeltaUsd: bigint- Size delta in USDindexToken: TokenData- Index token data
Calculates proportional pending impact values based on position size changes.
import { getProportionalPendingImpactValues } from "@gmx-ui/sdk/utils/fees";
const impact = getProportionalPendingImpactValues({
sizeInUsd: 5000000000000000000000000000000n, // $5000
pendingImpactAmount: 100000000000000000n, // 0.1 tokens
sizeDeltaUsd: 1000000000000000000000000000000n, // $1000
indexToken: tokenData
});
console.log(impact.proportionalPendingImpactDeltaAmount);
console.log(impact.proportionalPendingImpactDeltaUsd);
getPriceImpactForSwap
getPriceImpactForSwap(marketInfo: MarketInfo, tokenA: TokenData, tokenB: TokenData, usdDeltaTokenA: bigint, usdDeltaTokenB: bigint, opts?: { fallbackToZero?: boolean }): { priceImpactDeltaUsd: bigint; balanceWasImproved: boolean }
Parameters
marketInfo: MarketInfo- Market informationtokenA: TokenData- First token datatokenB: TokenData- Second token datausdDeltaTokenA: bigint- USD delta for token AusdDeltaTokenB: bigint- USD delta for token Bopts: object- Optional parameters for calculation behavior
Calculates price impact for swap operations between two tokens.
import { getPriceImpactForSwap } from "@gmx-ui/sdk/utils/fees";
const impact = getPriceImpactForSwap(
marketInfo,
tokenAData,
tokenBData,
1000000000000000000000000000000n, // $1000 token A
-1000000000000000000000000000000n, // -$1000 token B
{ fallbackToZero: true }
);
console.log(impact.priceImpactDeltaUsd);
console.log(impact.balanceWasImproved);
getNextPoolAmountsParams
getNextPoolAmountsParams(p: { longToken: TokenData; shortToken: TokenData; longPoolAmount: bigint; shortPoolAmount: bigint; longDeltaUsd: bigint; shortDeltaUsd: bigint }): { longPoolUsd: bigint; shortPoolUsd: bigint; nextLongPoolUsd: bigint; nextShortPoolUsd: bigint }
Parameters
p: object- Parameters object containing pool informationlongToken: TokenData- Long token datashortToken: TokenData- Short token datalongPoolAmount: bigint- Current long pool amountshortPoolAmount: bigint- Current short pool amountlongDeltaUsd: bigint- Long delta in USDshortDeltaUsd: bigint- Short delta in USD
Calculates current and next pool amounts in USD for impact calculations.
import { getNextPoolAmountsParams } from "@gmx-ui/sdk/utils/fees";
const poolParams = getNextPoolAmountsParams({
longToken: longTokenData,
shortToken: shortTokenData,
longPoolAmount: 1000000000000000000n, // 1 token
shortPoolAmount: 2000000000000000000n, // 2 tokens
longDeltaUsd: 500000000000000000000000000000n, // $500
shortDeltaUsd: -250000000000000000000000000000n // -$250
});
console.log(poolParams.longPoolUsd);
console.log(poolParams.nextLongPoolUsd);
getPriceImpactUsd
getPriceImpactUsd(p: { currentLongUsd: bigint; currentShortUsd: bigint; nextLongUsd: bigint; nextShortUsd: bigint; factorPositive: bigint; factorNegative: bigint; exponentFactor: bigint; fallbackToZero?: boolean }): { priceImpactDeltaUsd: bigint; balanceWasImproved: boolean }
Parameters
p: object- Parameters object containing USD amounts and factorscurrentLongUsd: bigint- Current long USD amountcurrentShortUsd: bigint- Current short USD amountnextLongUsd: bigint- Next long USD amountnextShortUsd: bigint- Next short USD amountfactorPositive: bigint- Positive impact factorfactorNegative: bigint- Negative impact factorexponentFactor: bigint- Exponent factor for calculationsfallbackToZero?: boolean- Whether to fallback to zero on errors
Core function that calculates price impact based on pool balance changes.
import { getPriceImpactUsd } from "@gmx-ui/sdk/utils/fees";
const impact = getPriceImpactUsd({
currentLongUsd: 10000000000000000000000000000000n, // $10,000
currentShortUsd: 8000000000000000000000000000000n, // $8,000
nextLongUsd: 11000000000000000000000000000000n, // $11,000
nextShortUsd: 8000000000000000000000000000000n, // $8,000
factorPositive: 5000000000000000000000000n, // 0.0005%
factorNegative: 5000000000000000000000000n, // 0.0005%
exponentFactor: 2000000000000000000000000000000n, // 2.0
fallbackToZero: true
});
console.log(impact.priceImpactDeltaUsd);
console.log(impact.balanceWasImproved);
calculateImpactForSameSideRebalance
calculateImpactForSameSideRebalance(p: { currentDiff: bigint; nextDiff: bigint; hasPositiveImpact: boolean; factor: bigint; exponentFactor: bigint }): bigint
Parameters
p: object- Parameters object for same-side rebalance calculationcurrentDiff: bigint- Current difference between poolsnextDiff: bigint- Next difference between poolshasPositiveImpact: boolean- Whether the impact is positivefactor: bigint- Impact factor to applyexponentFactor: bigint- Exponent factor for calculations
Calculates impact when rebalancing occurs on the same side of the pool.
import { calculateImpactForSameSideRebalance } from "@gmx-ui/sdk/utils/fees";
const impact = calculateImpactForSameSideRebalance({
currentDiff: 2000000000000000000000000000000n, // $2000 difference
nextDiff: 1500000000000000000000000000000n, // $1500 difference
hasPositiveImpact: true,
factor: 5000000000000000000000000n, // 0.0005%
exponentFactor: 2000000000000000000000000000000n // 2.0
});
console.log(impact);
calculateImpactForCrossoverRebalance
calculateImpactForCrossoverRebalance(p: { currentDiff: bigint; nextDiff: bigint; factorPositive: bigint; factorNegative: bigint; exponentFactor: bigint }): bigint
Parameters
p: object- Parameters object for crossover rebalance calculationcurrentDiff: bigint- Current difference between poolsnextDiff: bigint- Next difference between poolsfactorPositive: bigint- Positive impact factorfactorNegative: bigint- Negative impact factorexponentFactor: bigint- Exponent factor for calculations
Calculates impact when rebalancing crosses over from one side to the other.
import { calculateImpactForCrossoverRebalance } from "@gmx-ui/sdk/utils/fees";
const impact = calculateImpactForCrossoverRebalance({
currentDiff: 1000000000000000000000000000000n, // $1000 difference
nextDiff: 500000000000000000000000000000n, // $500 difference (opposite side)
factorPositive: 5000000000000000000000000n, // 0.0005%
factorNegative: 5000000000000000000000000n, // 0.0005%
exponentFactor: 2000000000000000000000000000000n // 2.0
});
console.log(impact);
applyImpactFactor
applyImpactFactor(diff: bigint, factor: bigint, exponent: bigint): bigint
Parameters
diff: bigint- Difference value to apply factor tofactor: bigint- Impact factorexponent: bigint- Exponent for the calculation
Applies impact factor with exponential calculation to a difference value.
import { applyImpactFactor } from "@gmx-ui/sdk/utils/fees";
const result = applyImpactFactor(
1000000000000000000000000000000n, // $1000 difference
5000000000000000000000000n, // 0.0005% factor
2000000000000000000000000000000n // 2.0 exponent
);
console.log(result);
getCappedPriceImpactPercentageFromFees
getCappedPriceImpactPercentageFromFees({ fees, isSwap }: { fees: TradeFees | undefined; isSwap: boolean }): bigint | undefined
Parameters
fees: TradeFees | undefined- Trade fees objectisSwap: boolean- Whether this is a swap operation
Extracts capped price impact percentage from trade fees.
import { getCappedPriceImpactPercentageFromFees } from "@gmx-ui/sdk/utils/fees";
const impactPercentage = getCappedPriceImpactPercentageFromFees({
fees: tradeFees,
isSwap: false
});
console.log(impactPercentage);