markets
This module provides utilities for working with GMX markets, including market naming, pricing, liquidity calculations, and PnL computations. It handles market metadata, pool calculations, leverage limits, and various market-related operations.
Methods
getMarketFullName
getMarketFullName(p: { longToken: Token; shortToken: Token; indexToken: Token; isSpotOnly: boolean }): string
Parameters
p: { longToken: Token; shortToken: Token; indexToken: Token; isSpotOnly: boolean }- Market configuration object containing the tokens and spot-only flag
Returns the full display name of a market combining the index name and pool name.
import { getMarketFullName } from "@gmx-ui/sdk/utils/markets";
const fullName = getMarketFullName({
longToken: ethToken,
shortToken: usdcToken,
indexToken: ethToken,
isSpotOnly: false
});
// Returns: "ETH/USD [ETH-USDC]"
getMarketIndexName
getMarketIndexName(p: ({ indexToken: Token } | { glvToken: Token }) & { isSpotOnly: boolean }): string
Parameters
p: ({ indexToken: Token } | { glvToken: Token }) & { isSpotOnly: boolean }- Market configuration with either index token or GLV token and spot-only flag
Returns the index name portion of a market name.
import { getMarketIndexName } from "@gmx-ui/sdk/utils/markets";
const indexName = getMarketIndexName({
indexToken: ethToken,
isSpotOnly: false
});
// Returns: "ETH/USD"
const spotIndexName = getMarketIndexName({
indexToken: ethToken,
isSpotOnly: true
});
// Returns: "SWAP-ONLY"
getMarketBaseName
getMarketBaseName(p: ({ indexToken: Token } | { glvToken: Token }) & { isSpotOnly: boolean }): string
Parameters
p: ({ indexToken: Token } | { glvToken: Token }) & { isSpotOnly: boolean }- Market configuration with either index token or GLV token and spot-only flag
Returns the base name of a market token.
import { getMarketBaseName } from "@gmx-ui/sdk/utils/markets";
const baseName = getMarketBaseName({
indexToken: ethToken,
isSpotOnly: false
});
// Returns: "ETH"
getMarketPoolName
getMarketPoolName(p: { longToken: Token; shortToken: Token }, separator?: string): string
Parameters
p: { longToken: Token; shortToken: Token }- Pool configuration with long and short tokensseparator: string- Optional separator character (defaults to "-")
Returns the pool name combining long and short token symbols.
import { getMarketPoolName } from "@gmx-ui/sdk/utils/markets";
const poolName = getMarketPoolName({
longToken: ethToken,
shortToken: usdcToken
});
// Returns: "ETH-USDC"
const poolNameWithSlash = getMarketPoolName({
longToken: ethToken,
shortToken: usdcToken
}, "/");
// Returns: "ETH/USDC"
getContractMarketPrices
getContractMarketPrices(tokensData: TokensData, market: Market): ContractMarketPrices | undefined
Parameters
tokensData: TokensData- Token data mappingmarket: Market- Market configuration
Returns contract-formatted prices for market tokens or undefined if tokens are missing.
import { getContractMarketPrices } from "@gmx-ui/sdk/utils/markets";
const contractPrices = getContractMarketPrices(tokensData, market);
if (contractPrices) {
console.log(contractPrices.indexTokenPrice);
console.log(contractPrices.longTokenPrice);
console.log(contractPrices.shortTokenPrice);
}
getTokenPoolType
getTokenPoolType(marketInfo: { longToken: Token; shortToken: Token }, tokenAddress: string): "long" | "short" | undefined
Parameters
marketInfo: { longToken: Token; shortToken: Token }- Market information with long and short tokenstokenAddress: string- Address of the token to check
Determines whether a token belongs to the long or short pool. Returns "long" for single token backed markets.
import { getTokenPoolType } from "@gmx-ui/sdk/utils/markets";
const poolType = getTokenPoolType(marketInfo, tokenAddress);
if (poolType === "long") {
console.log("Token belongs to long pool");
} else if (poolType === "short") {
console.log("Token belongs to short pool");
}
getPoolUsdWithoutPnl
getPoolUsdWithoutPnl(marketInfo: MarketInfo, isLong: boolean, priceType: "minPrice" | "maxPrice" | "midPrice"): bigint
Parameters
marketInfo: MarketInfo- Complete market informationisLong: boolean- Whether to get long or short pool valuepriceType: "minPrice" | "maxPrice" | "midPrice"- Price type to use for calculation
Returns the USD value of a pool without including PnL.
import { getPoolUsdWithoutPnl } from "@gmx-ui/sdk/utils/markets";
const longPoolUsd = getPoolUsdWithoutPnl(marketInfo, true, "midPrice");
const shortPoolUsd = getPoolUsdWithoutPnl(marketInfo, false, "minPrice");
getCappedPoolPnl
getCappedPoolPnl(p: { marketInfo: MarketInfo; poolUsd: bigint; poolPnl: bigint; isLong: boolean }): bigint
Parameters
p: { marketInfo: MarketInfo; poolUsd: bigint; poolPnl: bigint; isLong: boolean }- Parameters object containing market info, pool USD value, pool PnL, and long/short flag
Returns the pool PnL capped at the maximum allowed PnL for the market.
import { getCappedPoolPnl } from "@gmx-ui/sdk/utils/markets";
const cappedPnl = getCappedPoolPnl({
marketInfo,
poolUsd: 1000000n,
poolPnl: 50000n,
isLong: true
});
getMaxLeverageByMinCollateralFactor
getMaxLeverageByMinCollateralFactor(minCollateralFactor: bigint | undefined): number
Calculates the maximum leverage based on minimum collateral factor.
import { getMaxLeverageByMinCollateralFactor } from "@gmx-ui/sdk/utils/markets";
const maxLeverage = getMaxLeverageByMinCollateralFactor(500000000000000000000000000000n);
console.log(`Max leverage: ${maxLeverage / 10000}x`);
getMaxAllowedLeverageByMinCollateralFactor
getMaxAllowedLeverageByMinCollateralFactor(minCollateralFactor: bigint | undefined): number
Calculates the maximum allowed leverage (half of theoretical maximum) based on minimum collateral factor.
import { getMaxAllowedLeverageByMinCollateralFactor } from "@gmx-ui/sdk/utils/markets";
const maxAllowedLeverage = getMaxAllowedLeverageByMinCollateralFactor(500000000000000000000000000000n);
console.log(`Max allowed leverage: ${maxAllowedLeverage / 10000}x`);
getOppositeCollateral
getOppositeCollateral(marketInfo: MarketInfo, tokenAddress: string): Token | undefined
Parameters
marketInfo: MarketInfo- Complete market informationtokenAddress: string- Address of the current collateral token
Returns the opposite collateral token for a given token address.
import { getOppositeCollateral } from "@gmx-ui/sdk/utils/markets";
const oppositeToken = getOppositeCollateral(marketInfo, ethToken.address);
if (oppositeToken) {
console.log(`Opposite collateral: ${oppositeToken.symbol}`);
}
getAvailableUsdLiquidityForCollateral
getAvailableUsdLiquidityForCollateral(marketInfo: MarketInfo, isLong: boolean): bigint
Parameters
marketInfo: MarketInfo- Complete market informationisLong: boolean- Whether to get liquidity for long or short side
Returns the available USD liquidity for collateral on the specified side.
import { getAvailableUsdLiquidityForCollateral } from "@gmx-ui/sdk/utils/markets";
const longLiquidity = getAvailableUsdLiquidityForCollateral(marketInfo, true);
const shortLiquidity = getAvailableUsdLiquidityForCollateral(marketInfo, false);
getReservedUsd
getReservedUsd(marketInfo: MarketInfo, isLong: boolean): bigint
Parameters
marketInfo: MarketInfo- Complete market informationisLong: boolean- Whether to get reserved USD for long or short side
Returns the reserved USD amount for the specified side of the market.
import { getReservedUsd } from "@gmx-ui/sdk/utils/markets";
const longReservedUsd = getReservedUsd(marketInfo, true);
const shortReservedUsd = getReservedUsd(marketInfo, false);
getMarketDivisor
getMarketDivisor({ longTokenAddress, shortTokenAddress }: { longTokenAddress: string; shortTokenAddress: string }): bigint
Parameters
{ longTokenAddress, shortTokenAddress }: { longTokenAddress: string; shortTokenAddress: string }- Object containing long and short token addresses
Returns the market divisor (2n for same token markets, 1n for different token markets).
import { getMarketDivisor } from "@gmx-ui/sdk/utils/markets";
const divisor = getMarketDivisor({
longTokenAddress: "0x123...",
shortTokenAddress: "0x456..."
});
getMarketPnl
getMarketPnl(marketInfo: MarketInfo, isLong: boolean, forMaxPoolValue: boolean): bigint
Parameters
marketInfo: MarketInfo- Complete market informationisLong: boolean- Whether to calculate PnL for long or short sideforMaxPoolValue: boolean- Whether to calculate for maximum pool value
Returns the PnL for the specified side of the market.
import { getMarketPnl } from "@gmx-ui/sdk/utils/markets";
const longPnl = getMarketPnl(marketInfo, true, false);
const shortPnl = getMarketPnl(marketInfo, false, true);
getOpenInterestUsd
getOpenInterestUsd(marketInfo: MarketInfo, isLong: boolean): bigint
Parameters
marketInfo: MarketInfo- Complete market informationisLong: boolean- Whether to get open interest for long or short side
Returns the open interest in USD for the specified side.
import { getOpenInterestUsd } from "@gmx-ui/sdk/utils/markets";
const longOpenInterest = getOpenInterestUsd(marketInfo, true);
const shortOpenInterest = getOpenInterestUsd(marketInfo, false);
getOpenInterestInTokens
getOpenInterestInTokens(marketInfo: MarketInfo, isLong: boolean): bigint
Parameters
marketInfo: MarketInfo- Complete market informationisLong: boolean- Whether to get open interest for long or short side
Returns the open interest in tokens for the specified side.
import { getOpenInterestInTokens } from "@gmx-ui/sdk/utils/markets";
const longOpenInterestTokens = getOpenInterestInTokens(marketInfo, true);
const shortOpenInterestTokens = getOpenInterestInTokens(marketInfo, false);
getPriceForPnl
getPriceForPnl(prices: TokenPrices, isLong: boolean, maximize: boolean): bigint
Parameters
prices: TokenPrices- Token price informationisLong: boolean- Whether calculating for long or short positionmaximize: boolean- Whether to maximize the PnL calculation
Returns the appropriate price for PnL calculations based on position type and maximization preference.
import { getPriceForPnl } from "@gmx-ui/sdk/utils/markets";
const priceForLongPnl = getPriceForPnl(tokenPrices, true, true);
const priceForShortPnl = getPriceForPnl(tokenPrices, false, false);
getIsMarketAvailableForExpressSwaps
getIsMarketAvailableForExpressSwaps(marketInfo: MarketInfo): boolean
Parameters
marketInfo: MarketInfo- Complete market information
Returns whether a market is available for express swaps by checking if all tokens have price feed providers.
import { getIsMarketAvailableForExpressSwaps } from "@gmx-ui/sdk/utils/markets";
const isAvailable = getIsMarketAvailableForExpressSwaps(marketInfo);
if (isAvailable) {
console.log("Market supports express swaps");
}