Skip to main content

increase

This module provides utilities for calculating increase position amounts, prices, and next position values for GMX trading operations. It handles different trading strategies including leverage by size, leverage by collateral, and independent position management.

Methods

getIncreasePositionAmounts

  • getIncreasePositionAmounts(params): IncreasePositionAmounts

Parameters

  • marketInfo: MarketInfo - Market information containing trading parameters
  • indexToken: TokenData - The index token for the position
  • initialCollateralToken: TokenData - The initial collateral token
  • collateralToken: TokenData - The final collateral token after swaps
  • isLong: boolean - Whether this is a long position
  • initialCollateralAmount?: bigint - Amount of initial collateral
  • position?: PositionInfo - Existing position information
  • externalSwapQuote?: ExternalSwapQuote - External swap quote if using external swaps
  • indexTokenAmount?: bigint - Amount of index tokens
  • leverage?: bigint - Desired leverage in basis points
  • triggerPrice?: bigint - Trigger price for limit orders
  • limitOrderType?: IncreasePositionAmounts["limitOrderType"] - Type of limit order
  • fixedAcceptablePriceImpactBps?: bigint - Fixed acceptable price impact in basis points
  • acceptablePriceImpactBuffer?: number - Buffer for acceptable price impact
  • userReferralInfo?: UserReferralInfo - User referral information for fee calculations
  • strategy: "leverageBySize" | "leverageByCollateral" | "independent" - Trading strategy
  • findSwapPath: FindSwapPath - Function to find optimal swap paths
  • uiFeeFactor: bigint - UI fee factor
  • marketsInfoData?: MarketsInfoData - Markets information data
  • chainId: number - Chain ID
  • externalSwapQuoteParams?: ExternalSwapQuoteParams - Parameters for external swap quotes
  • isSetAcceptablePriceImpactEnabled: boolean - Whether acceptable price impact is enabled

Calculates comprehensive amounts for increasing a position including collateral requirements, fees, price impacts, and swap details based on the specified strategy.

import { getIncreasePositionAmounts } from "@gmx-ui/sdk/utils/trade";

const amounts = getIncreasePositionAmounts({
marketInfo,
indexToken,
initialCollateralToken,
collateralToken,
isLong: true,
initialCollateralAmount: 1000000000000000000n, // 1 token
leverage: 200000n, // 20x leverage (20 * 10000)
strategy: "leverageByCollateral",
findSwapPath,
uiFeeFactor: 5n, // 0.05%
chainId: 42161,
userReferralInfo: undefined,
isSetAcceptablePriceImpactEnabled: true
});

console.log("Size delta USD:", amounts.sizeDeltaUsd);
console.log("Collateral delta USD:", amounts.collateralDeltaUsd);
console.log("Position fee USD:", amounts.positionFeeUsd);

getTokensRatio

  • getTokensRatio(params): { markRatio: TokensRatio; triggerRatio?: TokensRatio }

Parameters

  • fromToken: TokenData - Source token
  • toToken: TokenData - Target token
  • triggerRatioValue: bigint - Trigger ratio value
  • markPrice: bigint - Current mark price

Calculates token ratios for trigger orders, returning both mark ratio and trigger ratio.

import { getTokensRatio } from "@gmx-ui/sdk/utils/trade";

const ratios = getTokensRatio({
fromToken: ethToken,
toToken: usdcToken,
triggerRatioValue: 2000000000000000000000n, // 2000 USDC per ETH
markPrice: 1950000000000000000000n // 1950 USDC per ETH
});

console.log("Mark ratio:", ratios.markRatio.ratio);
console.log("Trigger ratio:", ratios.triggerRatio?.ratio);

leverageBySizeValues

  • leverageBySizeValues(params): { collateralDeltaUsd: bigint; collateralDeltaAmount: bigint; baseCollateralUsd: bigint; baseCollateralAmount: bigint }

Parameters

  • collateralToken: TokenData - Collateral token information
  • leverage: bigint - Leverage in basis points
  • sizeDeltaUsd: bigint - Size delta in USD
  • collateralPrice: bigint - Collateral token price
  • positionFeeUsd: bigint - Position fee in USD
  • borrowingFeeUsd: bigint - Borrowing fee in USD
  • uiFeeUsd: bigint - UI fee in USD
  • swapUiFeeUsd: bigint - Swap UI fee in USD
  • fundingFeeUsd: bigint - Funding fee in USD

Calculates collateral requirements when using leverage by size strategy.

import { leverageBySizeValues } from "@gmx-ui/sdk/utils/trade";

const values = leverageBySizeValues({
collateralToken: usdcToken,
leverage: 200000n, // 20x
sizeDeltaUsd: 10000000000000000000000n, // $10,000
collateralPrice: 1000000000000000000n, // $1
positionFeeUsd: 10000000000000000000n, // $10
borrowingFeeUsd: 5000000000000000000n, // $5
uiFeeUsd: 2000000000000000000n, // $2
swapUiFeeUsd: 1000000000000000000n, // $1
fundingFeeUsd: 3000000000000000000n // $3
});

console.log("Collateral delta USD:", values.collateralDeltaUsd);
console.log("Base collateral amount:", values.baseCollateralAmount);

getIncreasePositionPrices

  • getIncreasePositionPrices(params): { indexPrice: bigint; initialCollateralPrice: bigint; collateralPrice: bigint; triggerThresholdType?: TriggerThresholdType; triggerPrice?: bigint }

Parameters

  • triggerPrice?: bigint - Trigger price for limit orders
  • indexToken: TokenData - Index token information
  • initialCollateralToken: TokenData - Initial collateral token
  • collateralToken: TokenData - Final collateral token
  • isLong: boolean - Whether this is a long position
  • limitOrderType?: IncreasePositionAmounts["limitOrderType"] - Type of limit order

Determines the appropriate prices to use for position calculations based on order type and trigger conditions.

import { getIncreasePositionPrices } from "@gmx-ui/sdk/utils/trade";

const prices = getIncreasePositionPrices({
triggerPrice: 2000000000000000000000n, // $2000
indexToken: ethToken,
initialCollateralToken: usdcToken,
collateralToken: usdcToken,
isLong: true,
limitOrderType: "LimitIncrease"
});

console.log("Index price:", prices.indexPrice);
console.log("Trigger threshold type:", prices.triggerThresholdType);

getNextPositionValuesForIncreaseTrade

  • getNextPositionValuesForIncreaseTrade(params): NextPositionValues

Parameters

  • existingPosition?: PositionInfo - Current position information
  • marketInfo: MarketInfo - Market information
  • collateralToken: TokenData - Collateral token
  • positionPriceImpactDeltaUsd: bigint - Price impact delta in USD
  • sizeDeltaUsd: bigint - Size increase in USD
  • sizeDeltaInTokens: bigint - Size increase in tokens
  • collateralDeltaUsd: bigint - Collateral increase in USD
  • collateralDeltaAmount: bigint - Collateral increase in token amount
  • indexPrice: bigint - Current index price
  • isLong: boolean - Whether this is a long position
  • showPnlInLeverage: boolean - Whether to include PnL in leverage calculation
  • minCollateralUsd: bigint - Minimum collateral requirement
  • userReferralInfo?: UserReferralInfo - User referral information

Calculates the projected position values after an increase trade is executed.

import { getNextPositionValuesForIncreaseTrade } from "@gmx-ui/sdk/utils/trade";

const nextValues = getNextPositionValuesForIncreaseTrade({
existingPosition: currentPosition,
marketInfo,
collateralToken: usdcToken,
positionPriceImpactDeltaUsd: -5000000000000000000n, // -$5 impact
sizeDeltaUsd: 5000000000000000000000n, // $5000 increase
sizeDeltaInTokens: 2500000000000000000n, // 2.5 ETH
collateralDeltaUsd: 500000000000000000000n, // $500 collateral
collateralDeltaAmount: 500000000n, // 500 USDC
indexPrice: 2000000000000000000000n, // $2000
isLong: true,
showPnlInLeverage: true,
minCollateralUsd: 10000000000000000000n, // $10 minimum
userReferralInfo: undefined
});

console.log("Next size USD:", nextValues.nextSizeUsd);
console.log("Next leverage:", nextValues.nextLeverage);
console.log("Next liquidation price:", nextValues.nextLiqPrice);