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
The markets utility module exports functions organized into six categories: display names, prices and PnL, liquidity and open interest, market classification, leverage, and data assembly.
Market display names
getMarketFullName
getMarketFullName(p: { longToken: Token; shortToken: Token; indexToken: Token; isSpotOnly: boolean }): string
Returns the full display name of a market combining the index name and pool name.
import { getMarketFullName } from "@gmx-io/sdk/utils/markets";
const fullName = getMarketFullName({
longToken: ethToken,
shortToken: usdcToken,
indexToken: ethToken,
isSpotOnly: false,
});
// "ETH/USD [ETH-USDC]"
getMarketIndexName
getMarketIndexName(p: ({ indexToken: Token } | { glvToken: Token }) & { isSpotOnly: boolean }): string
Returns the index name portion of a market name (for example, "ETH/USD" or "SWAP-ONLY").
getMarketBaseName
getMarketBaseName(p: ({ indexToken: Token } | { glvToken: Token }) & { isSpotOnly: boolean }): string
Returns the base token name from a market (for example, "ETH").
getMarketPoolName
getMarketPoolName(p: { longToken: Token; shortToken: Token }, separator?: string): string
Returns the pool name combining long and short token symbols. Default separator is "-".
import { getMarketPoolName } from "@gmx-io/sdk/utils/markets";
getMarketPoolName({ longToken: ethToken, shortToken: usdcToken }); // "ETH-USDC"
getMarketPoolName({ longToken: ethToken, shortToken: usdcToken }, "/"); // "ETH/USDC"
getMarketAddressByName
getMarketAddressByName(marketsInfoData: MarketsInfoData, name: string): string | undefined
Looks up a market token address by its full name string (for example, "ETH/USD [WETH-USDC]"). Returns undefined if not found.
Prices and PnL
getContractMarketPrices
getContractMarketPrices(tokensData: TokensData, market: Market): ContractMarketPrices | undefined
Returns contract-formatted prices for market tokens, or undefined if token price data is missing.
import { getContractMarketPrices } from "@gmx-io/sdk/utils/markets";
const contractPrices = getContractMarketPrices(tokensData, market);
if (contractPrices) {
console.log(contractPrices.indexTokenPrice);
}
getPriceForPnl
getPriceForPnl(prices: TokenPrices, isLong: boolean, maximize: boolean): bigint
Returns the appropriate price (min or max) for PnL calculations based on position direction.
getMarketPnl
getMarketPnl(marketInfo: MarketInfo, isLong: boolean, forMaxPoolValue: boolean): bigint
Returns the aggregate PnL for the long or short side of a market.
getMarket24Stats
getMarket24Stats(dayPriceCandle: DayPriceCandle): { open24h, high24h, low24h, close24h, priceChange24h, priceChangePercent24hBps }
Computes 24-hour OHLC stats and price change in basis points from a day price candle.
getMarketTicker
getMarketTicker(marketInfo: MarketInfo, dayPriceCandle: DayPriceCandle): MarketTicker
Returns a comprehensive market ticker including prices, open interest, liquidity, funding rates, and borrowing rates.
Liquidity and open interest
getPoolUsdWithoutPnl
getPoolUsdWithoutPnl(marketInfo: MarketInfo, isLong: boolean, priceType: "minPrice" | "maxPrice" | "midPrice"): bigint
Returns the USD value of the long or short pool excluding PnL.
getCappedPoolPnl
getCappedPoolPnl(p: { marketInfo: MarketInfo; poolUsd: bigint; poolPnl: bigint; isLong: boolean }): bigint
Returns the pool PnL capped at the market's maximum allowed PnL ratio.
getAvailableUsdLiquidityForCollateral
getAvailableUsdLiquidityForCollateral(marketInfo: MarketInfo, isLong: boolean): bigint
Returns available USD liquidity for collateral on the specified side.
getReservedUsd
getReservedUsd(marketInfo: MarketInfo, isLong: boolean): bigint
Returns the reserved USD amount for the long or short side.
getOpenInterestUsd
getOpenInterestUsd(marketInfo: MarketInfo, isLong: boolean): bigint
Returns the open interest in USD for the long or short side.
getOpenInterestInTokens
getOpenInterestInTokens(marketInfo: MarketInfo, isLong: boolean): bigint
Returns the open interest in tokens for the long or short side.
getOpenInterestForBalance
getOpenInterestForBalance(marketInfo: MarketInfo, isLong: boolean): bigint
Returns the open interest used for balance calculations. Uses token-denominated interest converted to USD when useOpenInterestInTokensForBalance is set.
getOiUsdFromRawValues
getOiUsdFromRawValues(rawValues: { longInterestUsingLongToken, longInterestUsingShortToken, shortInterestUsingLongToken, shortInterestUsingShortToken }, marketDivisor: bigint): { longInterestUsd, shortInterestUsd }
Computes aggregate long/short USD open interest from raw on-chain split values, applying the market divisor.
getOiInTokensFromRawValues
getOiInTokensFromRawValues(rawValues: {...}, marketDivisor: bigint): { longInterestInTokens, shortInterestInTokens }
Computes aggregate long/short token open interest from raw on-chain split values.