Skip to main content

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 details
    • sizeDeltaUsd: bigint - Size delta in USD
    • acceptablePrice: bigint - Acceptable price for the trade
    • indexPrice: bigint - Current index price
    • isLong: boolean - Whether the position is long
    • isIncrease: 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 information
  • token: TokenData - Token data
  • priceImpactDeltaUsd: 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 information
  • sizeDeltaUsd: bigint - Size delta in USD
  • isLong: boolean - Whether the position is long
  • isIncrease: boolean - Whether this is an increase operation
  • opts: 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 information
  • positionImpactDeltaUsd: 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 information
  • sizeDeltaUsd: bigint - Size delta in USD
  • positionImpactDeltaUsd: 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 information
  • sizeDeltaUsd: bigint - Size delta in USD
  • isLong: boolean - Whether the position is long
  • opts: 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 USD
  • pendingImpactAmount: bigint - Pending impact amount
  • sizeDeltaUsd: bigint - Size delta in USD
  • indexToken: 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 information
  • tokenA: TokenData - First token data
  • tokenB: TokenData - Second token data
  • usdDeltaTokenA: bigint - USD delta for token A
  • usdDeltaTokenB: bigint - USD delta for token B
  • opts: 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 information
    • longToken: TokenData - Long token data
    • shortToken: TokenData - Short token data
    • longPoolAmount: bigint - Current long pool amount
    • shortPoolAmount: bigint - Current short pool amount
    • longDeltaUsd: bigint - Long delta in USD
    • shortDeltaUsd: 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 factors
    • currentLongUsd: bigint - Current long USD amount
    • currentShortUsd: bigint - Current short USD amount
    • nextLongUsd: bigint - Next long USD amount
    • nextShortUsd: bigint - Next short USD amount
    • factorPositive: bigint - Positive impact factor
    • factorNegative: bigint - Negative impact factor
    • exponentFactor: bigint - Exponent factor for calculations
    • fallbackToZero?: 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 calculation
    • currentDiff: bigint - Current difference between pools
    • nextDiff: bigint - Next difference between pools
    • hasPositiveImpact: boolean - Whether the impact is positive
    • factor: bigint - Impact factor to apply
    • exponentFactor: 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 calculation
    • currentDiff: bigint - Current difference between pools
    • nextDiff: bigint - Next difference between pools
    • factorPositive: bigint - Positive impact factor
    • factorNegative: bigint - Negative impact factor
    • exponentFactor: 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 to
  • factor: bigint - Impact factor
  • exponent: 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 object
  • isSwap: 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);