Skip to main content

orders

This module provides utilities for working with GMX orders, including type checking functions, order information processing, and order classification helpers. It handles both swap orders and position orders, including TWAP (Time-Weighted Average Price) orders.

Methods

isMarketOrderType

  • isMarketOrderType(orderType: OrderType): boolean

Checks if the order type is a market order (executed immediately at current market price).

import { isMarketOrderType } from "@gmx-ui/sdk/utils/orders";
import { OrderType } from "@gmx-ui/sdk/types/orders";

const isMarket = isMarketOrderType(OrderType.MarketIncrease);
// Returns: true

isLimitOrderType

  • isLimitOrderType(orderType: OrderType): boolean

Checks if the order type is a limit order (executed when price reaches specified level).

import { isLimitOrderType } from "@gmx-ui/sdk/utils/orders";
import { OrderType } from "@gmx-ui/sdk/types/orders";

const isLimit = isLimitOrderType(OrderType.LimitIncrease);
// Returns: true

isTriggerDecreaseOrderType

  • isTriggerDecreaseOrderType(orderType: OrderType): boolean

Checks if the order type is a trigger decrease order (stop loss or limit decrease).

import { isTriggerDecreaseOrderType } from "@gmx-ui/sdk/utils/orders";
import { OrderType } from "@gmx-ui/sdk/types/orders";

const isTriggerDecrease = isTriggerDecreaseOrderType(OrderType.StopLossDecrease);
// Returns: true

isDecreaseOrderType

  • isDecreaseOrderType(orderType: OrderType): boolean

Checks if the order type is any decrease order (market, limit, or stop loss decrease).

import { isDecreaseOrderType } from "@gmx-ui/sdk/utils/orders";
import { OrderType } from "@gmx-ui/sdk/types/orders";

const isDecrease = isDecreaseOrderType(OrderType.MarketDecrease);
// Returns: true

isIncreaseOrderType

  • isIncreaseOrderType(orderType: OrderType): orderType is OrderType.MarketIncrease | OrderType.LimitIncrease | OrderType.StopIncrease

Checks if the order type is any increase order with type narrowing.

import { isIncreaseOrderType } from "@gmx-ui/sdk/utils/orders";
import { OrderType } from "@gmx-ui/sdk/types/orders";

const isIncrease = isIncreaseOrderType(OrderType.LimitIncrease);
// Returns: true

isSwapOrderType

  • isSwapOrderType(orderType: OrderType): boolean

Checks if the order type is a swap order (market or limit swap).

import { isSwapOrderType } from "@gmx-ui/sdk/utils/orders";
import { OrderType } from "@gmx-ui/sdk/types/orders";

const isSwap = isSwapOrderType(OrderType.MarketSwap);
// Returns: true

isLimitSwapOrderType

  • isLimitSwapOrderType(orderType: OrderType): boolean

Checks if the order type is specifically a limit swap order.

import { isLimitSwapOrderType } from "@gmx-ui/sdk/utils/orders";
import { OrderType } from "@gmx-ui/sdk/types/orders";

const isLimitSwap = isLimitSwapOrderType(OrderType.LimitSwap);
// Returns: true

isLiquidationOrderType

  • isLiquidationOrderType(orderType: OrderType): boolean

Checks if the order type is a liquidation order.

import { isLiquidationOrderType } from "@gmx-ui/sdk/utils/orders";
import { OrderType } from "@gmx-ui/sdk/types/orders";

const isLiquidation = isLiquidationOrderType(OrderType.Liquidation);
// Returns: true

isStopLossOrderType

  • isStopLossOrderType(orderType: OrderType): boolean

Checks if the order type is specifically a stop loss order.

import { isStopLossOrderType } from "@gmx-ui/sdk/utils/orders";
import { OrderType } from "@gmx-ui/sdk/types/orders";

const isStopLoss = isStopLossOrderType(OrderType.StopLossDecrease);
// Returns: true

isLimitDecreaseOrderType

  • isLimitDecreaseOrderType(orderType: OrderType): boolean

Checks if the order type is specifically a limit decrease order.

import { isLimitDecreaseOrderType } from "@gmx-ui/sdk/utils/orders";
import { OrderType } from "@gmx-ui/sdk/types/orders";

const isLimitDecrease = isLimitDecreaseOrderType(OrderType.LimitDecrease);
// Returns: true

isLimitIncreaseOrderType

  • isLimitIncreaseOrderType(orderType: OrderType): boolean

Checks if the order type is specifically a limit increase order.

import { isLimitIncreaseOrderType } from "@gmx-ui/sdk/utils/orders";
import { OrderType } from "@gmx-ui/sdk/types/orders";

const isLimitIncrease = isLimitIncreaseOrderType(OrderType.LimitIncrease);
// Returns: true

isStopIncreaseOrderType

  • isStopIncreaseOrderType(orderType: OrderType): boolean

Checks if the order type is specifically a stop increase order.

import { isStopIncreaseOrderType } from "@gmx-ui/sdk/utils/orders";
import { OrderType } from "@gmx-ui/sdk/types/orders";

const isStopIncrease = isStopIncreaseOrderType(OrderType.StopIncrease);
// Returns: true

isTwapOrder

  • isTwapOrder<T extends OrderParams>(orderInfo: T): orderInfo is Extract<T, { isTwap: true }>

Checks if the order is a TWAP (Time-Weighted Average Price) order with type narrowing.

import { isTwapOrder } from "@gmx-ui/sdk/utils/orders";

const isTwap = isTwapOrder(orderInfo);
if (isTwap) {
// orderInfo is now typed as a TWAP order
console.log(orderInfo.orders.length);
}

isTwapSwapOrder

  • isTwapSwapOrder(orderInfo: OrderInfo): orderInfo is TwapOrderInfo<SwapOrderInfo>

Checks if the order is a TWAP swap order with type narrowing.

import { isTwapSwapOrder } from "@gmx-ui/sdk/utils/orders";

const isTwapSwap = isTwapSwapOrder(orderInfo);
if (isTwapSwap) {
// orderInfo is now typed as a TWAP swap order
console.log(orderInfo.initialCollateralToken.symbol);
}

isTwapPositionOrder

  • isTwapPositionOrder(orderInfo: OrderInfo): orderInfo is TwapOrderInfo<PositionOrderInfo>

Checks if the order is a TWAP position order with type narrowing.

import { isTwapPositionOrder } from "@gmx-ui/sdk/utils/orders";

const isTwapPosition = isTwapPositionOrder(orderInfo);
if (isTwapPosition) {
// orderInfo is now typed as a TWAP position order
console.log(orderInfo.marketInfo.name);
}

isSwapOrder

  • isSwapOrder(orderInfo: OrderInfo): orderInfo is SwapOrderInfo

Checks if the order is a swap order with type narrowing.

import { isSwapOrder } from "@gmx-ui/sdk/utils/orders";

const isSwap = isSwapOrder(orderInfo);
if (isSwap) {
// orderInfo is now typed as a swap order
console.log(orderInfo.triggerRatio);
}

isPositionOrder

  • isPositionOrder(orderInfo: OrderInfo): orderInfo is PositionOrderInfo

Checks if the order is a position order with type narrowing.

import { isPositionOrder } from "@gmx-ui/sdk/utils/orders";

const isPosition = isPositionOrder(orderInfo);
if (isPosition) {
// orderInfo is now typed as a position order
console.log(orderInfo.acceptablePrice);
}

getOrderKeys

  • getOrderKeys(order: OrderInfo): string[]

Gets all order keys for an order. Returns multiple keys for TWAP orders, single key for regular orders.

import { getOrderKeys } from "@gmx-ui/sdk/utils/orders";

const keys = getOrderKeys(orderInfo);
// For TWAP orders: ["key1", "key2", "key3"]
// For regular orders: ["key1"]

getOrderInfo

  • getOrderInfo(p: { marketsInfoData: MarketsInfoData; tokensData: TokensData; wrappedNativeToken: Token; order: Order }): OrderInfo | undefined

Parameters

  • marketsInfoData: MarketsInfoData - Market information data
  • tokensData: TokensData - Token information data
  • wrappedNativeToken: Token - Wrapped native token information
  • order: Order - Raw order data from contract

Processes raw order data and returns enriched order information with calculated fields.

import { getOrderInfo } from "@gmx-ui/sdk/utils/orders";

const orderInfo = getOrderInfo({
marketsInfoData,
tokensData,
wrappedNativeToken,
order: rawOrder
});

if (orderInfo) {
console.log(orderInfo.triggerPrice);
}

isVisibleOrder

  • isVisibleOrder(orderType: OrderType): boolean

Checks if the order type should be visible in the UI.

import { isVisibleOrder } from "@gmx-ui/sdk/utils/orders";
import { OrderType } from "@gmx-ui/sdk/types/orders";

const shouldShow = isVisibleOrder(OrderType.LimitIncrease);
// Returns: true

isOrderForPosition

  • isOrderForPosition(order: OrderInfo, positionKey: string): order is PositionOrderInfo

Checks if an order belongs to a specific position identified by position key.

import { isOrderForPosition } from "@gmx-ui/sdk/utils/orders";

const belongsToPosition = isOrderForPosition(orderInfo, positionKey);
if (belongsToPosition) {
// Order is confirmed to be for this position
console.log("Order matches position");
}

isOrderForPositionByData

  • isOrderForPositionByData(order: OrderInfo, { account, marketAddress, collateralAddress, isLong }: { account: string; marketAddress: string; collateralAddress: string; isLong: boolean }): order is PositionOrderInfo

Parameters

  • order: OrderInfo - Order information to check
  • account: string - Account address
  • marketAddress: string - Market contract address
  • collateralAddress: string - Collateral token address
  • isLong: boolean - Whether position is long

Checks if an order belongs to a specific position identified by position data.

import { isOrderForPositionByData } from "@gmx-ui/sdk/utils/orders";

const belongsToPosition = isOrderForPositionByData(orderInfo, {
account: "0x123...",
marketAddress: "0x456...",
collateralAddress: "0x789...",
isLong: true
});

getOrderTradeboxKey

  • getOrderTradeboxKey(order: OrderInfo): string

Generates a unique key for the order to be used in tradebox UI components.

import { getOrderTradeboxKey } from "@gmx-ui/sdk/utils/orders";

const tradeboxKey = getOrderTradeboxKey(orderInfo);
// For position orders: "POSITION-account:market:collateral:isLong"
// For swap orders: "SWAP-account:fromToken:toToken"