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 parametersindexToken: TokenData- The index token for the positioninitialCollateralToken: TokenData- The initial collateral tokencollateralToken: TokenData- The final collateral token after swapsisLong: boolean- Whether this is a long positioninitialCollateralAmount?: bigint- Amount of initial collateralposition?: PositionInfo- Existing position informationexternalSwapQuote?: ExternalSwapQuote- External swap quote if using external swapsindexTokenAmount?: bigint- Amount of index tokensleverage?: bigint- Desired leverage in basis pointstriggerPrice?: bigint- Trigger price for limit orderslimitOrderType?: IncreasePositionAmounts["limitOrderType"]- Type of limit orderfixedAcceptablePriceImpactBps?: bigint- Fixed acceptable price impact in basis pointsacceptablePriceImpactBuffer?: number- Buffer for acceptable price impactuserReferralInfo?: UserReferralInfo- User referral information for fee calculationsstrategy: "leverageBySize" | "leverageByCollateral" | "independent"- Trading strategyfindSwapPath: FindSwapPath- Function to find optimal swap pathsuiFeeFactor: bigint- UI fee factormarketsInfoData?: MarketsInfoData- Markets information datachainId: number- Chain IDexternalSwapQuoteParams?: ExternalSwapQuoteParams- Parameters for external swap quotesisSetAcceptablePriceImpactEnabled: 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 tokentoToken: TokenData- Target tokentriggerRatioValue: bigint- Trigger ratio valuemarkPrice: 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 informationleverage: bigint- Leverage in basis pointssizeDeltaUsd: bigint- Size delta in USDcollateralPrice: bigint- Collateral token pricepositionFeeUsd: bigint- Position fee in USDborrowingFeeUsd: bigint- Borrowing fee in USDuiFeeUsd: bigint- UI fee in USDswapUiFeeUsd: bigint- Swap UI fee in USDfundingFeeUsd: 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 ordersindexToken: TokenData- Index token informationinitialCollateralToken: TokenData- Initial collateral tokencollateralToken: TokenData- Final collateral tokenisLong: boolean- Whether this is a long positionlimitOrderType?: 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 informationmarketInfo: MarketInfo- Market informationcollateralToken: TokenData- Collateral tokenpositionPriceImpactDeltaUsd: bigint- Price impact delta in USDsizeDeltaUsd: bigint- Size increase in USDsizeDeltaInTokens: bigint- Size increase in tokenscollateralDeltaUsd: bigint- Collateral increase in USDcollateralDeltaAmount: bigint- Collateral increase in token amountindexPrice: bigint- Current index priceisLong: boolean- Whether this is a long positionshowPnlInLeverage: boolean- Whether to include PnL in leverage calculationminCollateralUsd: bigint- Minimum collateral requirementuserReferralInfo?: 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);