Skip to main content

Overview

OrderParams defines the complete configuration for placing orders on Drift Protocol. The SDK provides helper functions to create specific order types with sensible defaults.

OrderParams Type

orderType
OrderType
required
The type of order to place.OrderType variants:
  • OrderType.LIMIT - Limit order with specified price
  • OrderType.MARKET - Market order executed at best available price
  • OrderType.TRIGGER_MARKET - Triggers a market order when condition is met
  • OrderType.TRIGGER_LIMIT - Triggers a limit order when condition is met
  • OrderType.ORACLE - Order priced relative to oracle price
marketType
MarketType
required
Market type for the order.MarketType variants:
  • MarketType.PERP - Perpetual futures market
  • MarketType.SPOT - Spot market
direction
PositionDirection
required
Direction of the order.PositionDirection variants:
  • PositionDirection.LONG - Buy/long position
  • PositionDirection.SHORT - Sell/short position
baseAssetAmount
BN
required
Amount of base asset to trade, in base precision (typically 1e9 for most markets).
price
BN
required
Limit price for the order in PRICE_PRECISION (1e6). Set to 0 for market orders.
marketIndex
number
required
Index of the market to trade on.
userOrderId
number
default:"0"
User-defined order ID for tracking. Must be unique per user.
reduceOnly
boolean
default:"false"
If true, order can only reduce existing position, not increase or flip it.
postOnly
PostOnlyParams
default:"PostOnlyParams.NONE"
Post-only constraint for the order.PostOnlyParams variants:
  • PostOnlyParams.NONE - No post-only constraint
  • PostOnlyParams.MUST_POST_ONLY - Transaction fails if order can’t be post-only
  • PostOnlyParams.TRY_POST_ONLY - Order not placed if it can’t be post-only
  • PostOnlyParams.SLIDE - Price adjusted to be post-only if needed
bitFlags
number
default:"0"
Bit flags for order options.OrderParamsBitFlag values:
  • OrderParamsBitFlag.ImmediateOrCancel (1) - Cancel unfilled portion immediately
  • OrderParamsBitFlag.UpdateHighLeverageMode (2) - Update high leverage mode
triggerPrice
BN | null
default:"null"
Trigger price for trigger orders in PRICE_PRECISION (1e6). Required for TRIGGER_MARKET and TRIGGER_LIMIT orders.
triggerCondition
OrderTriggerCondition
default:"OrderTriggerCondition.ABOVE"
Condition for trigger activation.OrderTriggerCondition variants:
  • OrderTriggerCondition.ABOVE - Trigger when price goes above trigger price
  • OrderTriggerCondition.BELOW - Trigger when price goes below trigger price
  • OrderTriggerCondition.TRIGGERED_ABOVE - Trigger condition above has been met
  • OrderTriggerCondition.TRIGGERED_BELOW - Trigger condition below has been met
oraclePriceOffset
number | null
default:"null"
Price offset from oracle price in basis points. Used for floating limit orders.
auctionDuration
number | null
default:"null"
Duration of the auction phase in slots. During auction, order transitions from taking to resting.
maxTs
BN | null
default:"null"
Maximum timestamp (Unix seconds) for order validity. Order expires after this time.
auctionStartPrice
BN | null
default:"null"
Starting price for auction in PRICE_PRECISION (1e6).
auctionEndPrice
BN | null
default:"null"
Ending price for auction in PRICE_PRECISION (1e6).

Helper Functions

getOrderParams

Creates an OrderParams object with default values merged with provided parameters.
import { getOrderParams } from '@drift-labs/sdk';

const orderParams = getOrderParams({
  orderType: OrderType.LIMIT,
  marketIndex: 0,
  direction: PositionDirection.LONG,
  baseAssetAmount: new BN(1000000000), // 1 SOL
  price: new BN(50000000), // $50
});
Parameters:
  • optionalOrderParams (OptionalOrderParams) - Order parameters to set
  • overridingParams (Record<string, any>) - Additional parameters to override
Returns: Complete OrderParams object

getLimitOrderParams

Creates parameters for a limit order.
import { getLimitOrderParams } from '@drift-labs/sdk';

const limitOrder = getLimitOrderParams({
  marketIndex: 0,
  direction: PositionDirection.LONG,
  baseAssetAmount: new BN(1000000000),
  price: new BN(50000000),
});
Parameters:
  • params - Order parameters excluding orderType
  • params.price (BN) - Required limit price
Returns: OptionalOrderParams with orderType set to LIMIT

getMarketOrderParams

Creates parameters for a market order.
import { getMarketOrderParams } from '@drift-labs/sdk';

const marketOrder = getMarketOrderParams({
  marketIndex: 0,
  direction: PositionDirection.LONG,
  baseAssetAmount: new BN(1000000000),
});
Parameters:
  • params - Order parameters excluding orderType
Returns: OptionalOrderParams with orderType set to MARKET

getTriggerMarketOrderParams

Creates parameters for a trigger market order (stop loss or take profit).
import { getTriggerMarketOrderParams } from '@drift-labs/sdk';

const stopLoss = getTriggerMarketOrderParams({
  marketIndex: 0,
  direction: PositionDirection.SHORT,
  baseAssetAmount: new BN(1000000000),
  triggerCondition: OrderTriggerCondition.BELOW,
  triggerPrice: new BN(45000000), // $45
});
Parameters:
  • params - Order parameters excluding orderType
  • params.triggerCondition (OrderTriggerCondition) - Required trigger condition
  • params.triggerPrice (BN) - Required trigger price
Returns: OptionalOrderParams with orderType set to TRIGGER_MARKET

getTriggerLimitOrderParams

Creates parameters for a trigger limit order.
import { getTriggerLimitOrderParams } from '@drift-labs/sdk';

const triggerLimit = getTriggerLimitOrderParams({
  marketIndex: 0,
  direction: PositionDirection.SHORT,
  baseAssetAmount: new BN(1000000000),
  triggerCondition: OrderTriggerCondition.BELOW,
  triggerPrice: new BN(45000000),
  price: new BN(44500000), // Limit price after trigger
});
Parameters:
  • params - Order parameters excluding orderType
  • params.triggerCondition (OrderTriggerCondition) - Required trigger condition
  • params.triggerPrice (BN) - Required trigger price
  • params.price (BN) - Required limit price after trigger
Returns: OptionalOrderParams with orderType set to TRIGGER_LIMIT

Scale Orders

Scale orders allow placing multiple limit orders distributed across a price range.

ScaleOrderParams

marketType
MarketType
required
Market type for the orders.
direction
PositionDirection
required
Direction of the orders.
marketIndex
number
required
Index of the market.
totalBaseAssetAmount
BN
required
Total base asset amount to distribute across all orders.
startPrice
BN
required
Starting price for the scale in PRICE_PRECISION (1e6).
endPrice
BN
required
Ending price for the scale in PRICE_PRECISION (1e6).
orderCount
number
required
Number of orders to place (min 2, max 32). Total open orders cannot exceed 32.
sizeDistribution
SizeDistribution
required
How to distribute sizes across orders.SizeDistribution variants:
  • SizeDistribution.FLAT - Equal size for all orders
  • SizeDistribution.ASCENDING - Smallest at start price, largest at end price
  • SizeDistribution.DESCENDING - Largest at start price, smallest at end price
reduceOnly
boolean
required
Whether orders should be reduce-only.
postOnly
PostOnlyParams
required
Post-only setting for all orders.
bitFlags
number
required
Bit flags for the orders.
maxTs
BN | null
required
Maximum timestamp for orders to be valid.

Utility Functions

isUpdateHighLeverageMode

Checks if the UpdateHighLeverageMode bit flag is set.
import { isUpdateHighLeverageMode, OrderParamsBitFlag } from '@drift-labs/sdk';

const bitFlags = OrderParamsBitFlag.UpdateHighLeverageMode;
const isHighLeverage = isUpdateHighLeverageMode(bitFlags); // true
Parameters:
  • bitFlags (number) - Bit flags to check
Returns: boolean - True if UpdateHighLeverageMode flag is set

Default Values

The SDK provides default values for all optional order parameters:
export const DefaultOrderParams: OrderParams = {
  orderType: OrderType.MARKET,
  marketType: MarketType.PERP,
  userOrderId: 0,
  direction: PositionDirection.LONG,
  baseAssetAmount: ZERO,
  price: ZERO,
  marketIndex: 0,
  reduceOnly: false,
  postOnly: PostOnlyParams.NONE,
  bitFlags: 0,
  triggerPrice: null,
  triggerCondition: OrderTriggerCondition.ABOVE,
  oraclePriceOffset: null,
  auctionDuration: null,
  maxTs: null,
  auctionStartPrice: null,
  auctionEndPrice: null,
};

OptionalOrderParams

All fields from OrderParams are optional except:
  • orderType
  • marketIndex
  • baseAssetAmount
  • direction

ModifyOrderParams

Used for modifying existing orders. All OrderParams fields are optional and nullable, plus:
policy
ModifyOrderPolicy
Policy for order modification.ModifyOrderPolicy enum:
  • ModifyOrderPolicy.MustModify (1) - Transaction fails if order cannot be modified
  • ModifyOrderPolicy.ExcludePreviousFill (2) - Exclude previously filled amount

See Also

  • Position Management - Calculate position metrics and PnL
  • DLOB - Decentralized Limit Order Book for order matching

Build docs developers (and LLMs) love