Skip to main content

Order Structure

Every order in Drift has a comprehensive structure tracking its state and parameters:
sdk/src/types.ts

Order Types

Drift supports several order types with different execution characteristics:
sdk/src/types.ts

Market Orders

Market orders execute immediately at the best available price:
  • Fill against the AMM or matching limit orders
  • May use Dutch auction for better price discovery
  • No price limit (except max slippage from auction)
  • Default auction duration: configured per market
Market orders use a brief Dutch auction by default, starting at a premium/discount to the oracle price and converging toward the oracle price over time.

Limit Orders

Limit orders specify a maximum/minimum price:
  • Long: Fill at price or better (lower)
  • Short: Fill at price or better (higher)
  • Rest on the orderbook if not immediately fillable
  • Can be maker or taker depending on execution
Example:

Trigger Orders

Trigger orders activate when a condition is met: Trigger Market: Becomes a market order when triggered Trigger Limit: Becomes a limit order when triggered
sdk/src/types.ts
Example Stop-Loss:
Example Take-Profit:

Oracle Orders

Oracle orders use an offset from the oracle price:
The effective limit price is: oraclePrice + oraclePriceOffset This allows orders to automatically track the oracle price.

Order Parameters

When placing an order, you specify parameters:
sdk/src/types.ts

Post-Only Orders

sdk/src/types.ts
  • MUST_POST_ONLY: Transaction fails if order would immediately fill
  • TRY_POST_ONLY: Order is skipped if it would immediately fill
  • SLIDE: Price is adjusted to guarantee maker execution

Reduce-Only Orders

Reduce-only orders can only decrease position size:
  • Cannot open a new position
  • Cannot increase existing position
  • Cannot flip position direction
  • Commonly used for stop-loss and take-profit orders

Immediate-or-Cancel (IOC)

IOC orders attempt to fill immediately and cancel any unfilled portion:
sdk/src/types.ts
Set via immediateOrCancel: true or in bitFlags.

Time-in-Force

Orders can have an expiration time:
Expired orders are automatically cancelled.

Order Status

sdk/src/types.ts

Order Execution

Dutch Auction Mechanism

Most orders use a Dutch auction for better price discovery:
  1. Auction Start: Order starts at a premium (buy) or discount (sell)
  2. Price Improvement: Price moves toward oracle price over time
  3. Auction End: After auctionDuration slots, fills at oracle price
  4. Early Fill: Can fill at improved price during auction
Auction Price Calculation:
sdk/src/math/auction.ts
The auction price interpolates between auctionStartPrice and auctionEndPrice based on slots elapsed.

Order Matching

Orders can be filled through multiple sources:
  1. DLOB Matching: Against resting limit orders
  2. AMM Fill: Against the protocol AMM
  3. JIT (Just-in-Time) Auction: Keeper provides liquidity during auction
  4. External DEX: Through Serum, Phoenix, or OpenBook v2
sdk/src/types.ts

Fill Price Priority

When matching orders, the best price for the taker is selected:
  • For buys: lowest price wins
  • For sells: highest price wins
  • Considers all available liquidity sources
  • Auction mechanism provides time for price competition

Scale Orders

Place multiple orders across a price range:
sdk/src/types.ts
Example:
This creates 5 buy orders at 95,95, 93.75, 92.50,92.50, 91.25, and $90, each for 2 SOL.

Order Events

Order actions emit events:
sdk/src/types.ts

Order Management

Modifying Orders

Orders can be modified with certain constraints:
sdk/src/types.ts
  • MustModify: Transaction fails if order cannot be modified
  • ExcludePreviousFill: Ignore fills that occurred in the same slot

Cancelling Orders

Orders can be cancelled:
  • By the user at any time
  • Automatically when expired (maxTs reached)
  • When user is liquidated (risk-increasing orders)
  • When reduce-only conditions are violated

Protected Maker Mode

Markets can enable protected maker mode for certain users:
sdk/src/types.ts
Protected makers get price adjustments to ensure maker status and better fill prices.

Next Steps

Positions

See how orders create and modify positions

Markets

Understand market mechanics for order execution

Margin System

Learn margin requirements for orders

SDK Guide

Implementation guide for placing orders