523 lines
17 KiB
Plaintext
523 lines
17 KiB
Plaintext
# Orders
|
|
|
|
## How it works
|
|
|
|
Orders on Drift go through a multi-stage matching process. When you place an order, it enters a **JIT auction** (Just-In-Time) where market makers can compete to fill it at better prices. If no one fills during the auction, the order either executes against the **DLOB** (Decentralized Limit Order Book) or the **AMM** as a fallback. This design ensures you get the best price from multiple liquidity sources.
|
|
|
|
Orders are stored in your user account and have both an onchain order ID and an optional user-assigned ID for tracking. When an order fills, your position updates automatically, and you can query fill events to see execution details.
|
|
|
|
## Order Types
|
|
|
|
| Type | Description |
|
|
|---|---|
|
|
| `MARKET` | Executes immediately. Goes through a JIT auction first (controlled by `auctionStartPrice`, `auctionEndPrice`, `auctionDuration`), then fills against the DLOB or AMM. |
|
|
| `LIMIT` | Rests on the DLOB at a fixed price until filled or canceled. Set `postOnly: PostOnlyParams.MUST_POST_ONLY` to guarantee maker status and avoid crossing the spread. |
|
|
| `ORACLE` | Like a market order, but auction prices and the resting limit price are expressed as **offsets from the oracle price** (not absolute prices). Useful for market makers who want tight spreads without hardcoding prices. |
|
|
| `TRIGGER_MARKET` | A stop/take-profit market order. Executes as a market order when the oracle price crosses `triggerPrice` in the specified `triggerCondition` direction. |
|
|
| `TRIGGER_LIMIT` | A stop/take-profit limit order. Same trigger mechanism as `TRIGGER_MARKET`, but executes as a limit order at `price` once triggered. |
|
|
|
|
### Post-Only Params
|
|
|
|
When placing limit orders, you can control maker/taker behavior with `postOnly`:
|
|
|
|
| Value | Behavior |
|
|
|---|---|
|
|
| `PostOnlyParams.NONE` | Order can be maker or taker (default) |
|
|
| `PostOnlyParams.MUST_POST_ONLY` | Transaction fails if the order would cross the spread |
|
|
| `PostOnlyParams.TRY_POST_ONLY` | Order is silently skipped (not placed) if it would cross; tx succeeds |
|
|
| `PostOnlyParams.SLIDE` | Order price is adjusted one tick inside the spread to guarantee maker status |
|
|
|
|
## SDK Usage
|
|
|
|
This page focuses on placing and canceling orders via `DriftClient`. For concise examples we use helper builders like `getMarketOrderParams(...)`.
|
|
|
|
### Build Market Order Params
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="getMarketOrderParams" type="function">
|
|
```js
|
|
import { BN, BASE_PRECISION, MarketType, PositionDirection, getMarketOrderParams } from "@drift-labs/sdk";
|
|
|
|
const orderParams = getMarketOrderParams({
|
|
marketIndex: 0,
|
|
marketType: MarketType.PERP,
|
|
direction: PositionDirection.LONG,
|
|
baseAssetAmount: new BN(1).mul(BASE_PRECISION), // 1 SOL (in 1e9 precision)
|
|
});
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
### Build Order Params (Generic Helper)
|
|
|
|
If you want a single helper that works for limit/market/oracle/trigger, use `getOrderParams(...)`.
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="getOrderParams" type="function">
|
|
```js
|
|
import { getOrderParams, OrderType, PositionDirection } from "@drift-labs/sdk";
|
|
|
|
const orderParams = getOrderParams({
|
|
orderType: OrderType.LIMIT,
|
|
marketIndex: 0,
|
|
direction: PositionDirection.LONG,
|
|
baseAssetAmount: driftClient.convertToPerpPrecision(1), // 1 base unit → BN(1e9)
|
|
price: driftClient.convertToPricePrecision(21.23), // $21.23 → BN(21_230_000)
|
|
});
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
### Place a Perp Order
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="placePerpOrder" type="method" owner="DriftClient">
|
|
```js
|
|
// Assumes `driftClient` is subscribed.
|
|
const txSig = await driftClient.placePerpOrder(
|
|
getMarketOrderParams({
|
|
marketIndex: 0,
|
|
direction: PositionDirection.LONG,
|
|
baseAssetAmount: new BN(1).mul(BASE_PRECISION),
|
|
})
|
|
);
|
|
console.log(txSig);
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
### Place a Spot Order
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="placeSpotOrder" type="method" owner="DriftClient">
|
|
```js
|
|
import { OrderType, PositionDirection } from "@drift-labs/sdk";
|
|
|
|
await driftClient.placeSpotOrder({
|
|
orderType: OrderType.LIMIT,
|
|
marketIndex: 1, // e.g. SOL spot
|
|
direction: PositionDirection.LONG,
|
|
baseAssetAmount: driftClient.convertToSpotPrecision(1, 1), // 1 SOL
|
|
price: driftClient.convertToPricePrecision(150),
|
|
});
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
### Place Multiple Orders
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="placeOrders" type="method" owner="DriftClient">
|
|
```js
|
|
await driftClient.placeOrders([
|
|
{
|
|
orderType: OrderType.LIMIT,
|
|
marketType: MarketType.PERP,
|
|
marketIndex: 0,
|
|
direction: PositionDirection.LONG,
|
|
baseAssetAmount: driftClient.convertToPerpPrecision(1),
|
|
price: driftClient.convertToPricePrecision(21.23),
|
|
},
|
|
{
|
|
orderType: OrderType.LIMIT,
|
|
marketType: MarketType.PERP,
|
|
marketIndex: 0,
|
|
direction: PositionDirection.SHORT,
|
|
baseAssetAmount: driftClient.convertToPerpPrecision(1),
|
|
oraclePriceOffset: driftClient.convertToPricePrecision(0.05).toNumber(),
|
|
},
|
|
]);
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
### Oracle / Auction-Style Orders
|
|
|
|
Oracle orders have prices that track the oracle feed with an offset. They go through a JIT auction before execution, with auction prices that gradually converge from the start offset to the end offset relative to oracle.
|
|
|
|
**Important:** For `OrderType.ORACLE`, `auctionStartPrice`, `auctionEndPrice`, and `oraclePriceOffset` are all **offsets** from the oracle price (in PRICE_PRECISION, 1e6), not absolute prices.
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="Oracle order" type="example">
|
|
```js
|
|
import { BN, OrderType, PositionDirection, PRICE_PRECISION } from "@drift-labs/sdk";
|
|
|
|
const marketIndex = 18;
|
|
|
|
// Offsets are relative to oracle price (in PRICE_PRECISION)
|
|
// For a long: auction starts at a better (lower) price and ends at a worse (higher) price
|
|
const auctionStartPrice = PRICE_PRECISION.muln(-5).divn(10); // -$0.50 below oracle
|
|
const auctionEndPrice = PRICE_PRECISION.muln(5).divn(10); // +$0.50 above oracle
|
|
|
|
const orderParams = {
|
|
orderType: OrderType.ORACLE,
|
|
baseAssetAmount: driftClient.convertToPerpPrecision(10),
|
|
direction: PositionDirection.LONG,
|
|
marketIndex,
|
|
auctionStartPrice,
|
|
auctionEndPrice,
|
|
oraclePriceOffset: driftClient.convertToPricePrecision(0.30).toNumber(), // +$0.30 from oracle
|
|
auctionDuration: 30, // slots
|
|
};
|
|
|
|
await driftClient.placePerpOrder(orderParams);
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
### Cancel Orders
|
|
|
|
Cancel a specific order by its onchain order ID.
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="cancelOrder" type="method" owner="DriftClient">
|
|
```js
|
|
await driftClient.cancelOrder(1);
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
Cancel multiple specific orders by their onchain order IDs in a single transaction.
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="cancelOrdersByIds" type="method" owner="DriftClient">
|
|
```js
|
|
await driftClient.cancelOrdersByIds([1, 2, 3]);
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
Cancel all orders matching the given market and direction filters. Pass `null` for any filter to match all. Omit all parameters to cancel every open order.
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="cancelOrders" type="method" owner="DriftClient">
|
|
```js
|
|
import { MarketType, PositionDirection } from "@drift-labs/sdk";
|
|
|
|
// Cancel all long perp orders on market 0
|
|
await driftClient.cancelOrders(MarketType.PERP, 0, PositionDirection.LONG);
|
|
|
|
// Cancel all orders across all markets
|
|
await driftClient.cancelOrders(null, null, null);
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
### Cancel and Place (Atomic)
|
|
|
|
Atomically cancels existing orders and places new ones in a single transaction. This is the preferred approach for market makers who need to replace quotes without risk of being filled on stale orders in the gap between a separate cancel and re-quote.
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="cancelAndPlaceOrders" type="method" owner="DriftClient">
|
|
```js
|
|
await driftClient.cancelAndPlaceOrders(
|
|
{ marketType: MarketType.PERP, marketIndex: 0 },
|
|
[
|
|
{
|
|
orderType: OrderType.LIMIT,
|
|
marketIndex: 0,
|
|
direction: PositionDirection.LONG,
|
|
baseAssetAmount: driftClient.convertToPerpPrecision(1),
|
|
price: driftClient.convertToPricePrecision(21.23),
|
|
},
|
|
]
|
|
);
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
### Modify Orders
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="modifyOrder" type="method" owner="DriftClient">
|
|
```js
|
|
await driftClient.modifyOrder({
|
|
orderId: 1,
|
|
newBaseAmount: driftClient.convertToPerpPrecision(2),
|
|
});
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="modifyOrderByUserOrderId" type="method" owner="DriftClient">
|
|
```js
|
|
await driftClient.modifyOrderByUserOrderId({
|
|
userOrderId: 1,
|
|
newBaseAmount: driftClient.convertToPerpPrecision(2),
|
|
});
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
### Trigger Orders (Stop / Take-Profit)
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="Trigger order" type="example">
|
|
```js
|
|
import { OrderTriggerCondition, OrderType, PositionDirection } from "@drift-labs/sdk";
|
|
|
|
const orderParams = {
|
|
orderType: OrderType.TRIGGER_MARKET,
|
|
marketIndex: 0,
|
|
direction: PositionDirection.SHORT,
|
|
baseAssetAmount: driftClient.convertToPerpPrecision(1),
|
|
triggerPrice: driftClient.convertToPricePrecision(95),
|
|
triggerCondition: OrderTriggerCondition.BELOW,
|
|
};
|
|
|
|
await driftClient.placePerpOrder(orderParams);
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
### Instruction Builders (Advanced)
|
|
|
|
Higher-level methods like `placePerpOrder()` build, sign, and send a transaction in one call. Instruction (IX) builders give you the raw `TransactionInstruction` objects so you can:
|
|
|
|
- **Set a custom compute budget** with priority fees for faster inclusion
|
|
- **Batch multiple instructions** into a single transaction (e.g., cancel + place atomically)
|
|
- **Use Address Lookup Tables (ALTs)** to fit more accounts into a transaction
|
|
- **Compose with other programs** (e.g., add a memo or call another protocol in the same tx)
|
|
|
|
#### Complete Example: Batching IXs with Compute Budget
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="IX batching example" type="example">
|
|
```js
|
|
import { ComputeBudgetProgram } from "@solana/web3.js";
|
|
import {
|
|
MarketType, PositionDirection, OrderType, getOrderParams,
|
|
} from "@drift-labs/sdk";
|
|
|
|
// 1. Build individual instructions
|
|
const cancelIx = await driftClient.getCancelOrdersIx(
|
|
MarketType.PERP, // marketType (null to cancel all types)
|
|
0, // marketIndex (null to cancel across all markets)
|
|
null // direction (null to cancel both sides)
|
|
);
|
|
|
|
const placeIx = await driftClient.getPlacePerpOrderIx(
|
|
getOrderParams({
|
|
orderType: OrderType.LIMIT,
|
|
marketIndex: 0,
|
|
direction: PositionDirection.LONG,
|
|
baseAssetAmount: driftClient.convertToPerpPrecision(1),
|
|
price: driftClient.convertToPricePrecision(21.0),
|
|
})
|
|
);
|
|
|
|
// 2. Add compute budget instructions for priority fees
|
|
const computeUnitPrice = ComputeBudgetProgram.setComputeUnitPrice({
|
|
microLamports: 50_000, // priority fee in micro-lamports per CU
|
|
});
|
|
const computeUnitLimit = ComputeBudgetProgram.setComputeUnitLimit({
|
|
units: 400_000, // max compute units for the transaction
|
|
});
|
|
|
|
// 3. Build a versioned transaction with all instructions
|
|
const tx = await driftClient.txSender.getVersionedTransaction(
|
|
[computeUnitLimit, computeUnitPrice, cancelIx, placeIx],
|
|
[], // lookup table accounts (AddressLookupTableAccount[])
|
|
driftClient.wallet.publicKey
|
|
);
|
|
|
|
// 4. Send the transaction
|
|
const { txSig } = await driftClient.txSender.sendVersionedTransaction(
|
|
tx,
|
|
[],
|
|
driftClient.opts
|
|
);
|
|
console.log("Batch tx:", txSig);
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
#### Individual IX Builders
|
|
|
|
**getPlacePerpOrderIx** builds an instruction to place a perp order.
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="getPlacePerpOrderIx" type="method" owner="DriftClient">
|
|
```js
|
|
// Params:
|
|
// orderParams: OptionalOrderParams - same params as placePerpOrder()
|
|
// subAccountId?: number - defaults to active subaccount
|
|
const ix = await driftClient.getPlacePerpOrderIx(
|
|
getOrderParams({
|
|
orderType: OrderType.LIMIT,
|
|
marketIndex: 0,
|
|
direction: PositionDirection.LONG,
|
|
baseAssetAmount: driftClient.convertToPerpPrecision(1),
|
|
price: driftClient.convertToPricePrecision(21.0),
|
|
})
|
|
);
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
**getPlaceSpotOrderIx** builds an instruction to place a spot order.
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="getPlaceSpotOrderIx" type="method" owner="DriftClient">
|
|
```js
|
|
// Params:
|
|
// orderParams: OptionalOrderParams - same params as placeSpotOrder()
|
|
// subAccountId?: number - defaults to active subaccount
|
|
const ix = await driftClient.getPlaceSpotOrderIx({
|
|
orderType: OrderType.LIMIT,
|
|
marketIndex: 1,
|
|
direction: PositionDirection.LONG,
|
|
baseAssetAmount: driftClient.convertToSpotPrecision(1, 1),
|
|
price: driftClient.convertToPricePrecision(150),
|
|
});
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
**getCancelOrdersIx** builds an instruction to cancel orders matching the given filters. Pass `null` for any filter to match all.
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="getCancelOrdersIx" type="method" owner="DriftClient">
|
|
```js
|
|
import { MarketType, PositionDirection } from "@drift-labs/sdk";
|
|
|
|
// Params:
|
|
// marketType: MarketType | null - filter by PERP or SPOT (null = all)
|
|
// marketIndex: number | null - filter by market index (null = all)
|
|
// direction: PositionDirection | null - filter by LONG or SHORT (null = both)
|
|
// subAccountId?: number - defaults to active subaccount
|
|
|
|
// Cancel all perp orders on market 0
|
|
const ix = await driftClient.getCancelOrdersIx(MarketType.PERP, 0, null);
|
|
|
|
// Cancel ALL orders across all markets
|
|
const ixAll = await driftClient.getCancelOrdersIx(null, null, null);
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
**getFillPerpOrderIx** builds an instruction to fill another user's perp order (used by filler/keeper bots).
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="getFillPerpOrderIx" type="method" owner="DriftClient">
|
|
```js
|
|
// Params:
|
|
// userAccountPublicKey: PublicKey - the taker's user account address
|
|
// userAccount: UserAccount - the taker's deserialized user account
|
|
// order: { marketIndex, orderId } - the order to fill
|
|
// makerInfo?: MakerInfo | MakerInfo[] - optional maker(s) to match against
|
|
// referrerInfo?: ReferrerInfo - optional referrer for fee sharing
|
|
// fillerSubAccountId?: number - filler's subaccount
|
|
|
|
const takerPubkey = takerUser.userAccountPublicKey;
|
|
const takerAccount = takerUser.getUserAccount();
|
|
const order = takerAccount.orders[0]; // the order to fill
|
|
|
|
const ix = await driftClient.getFillPerpOrderIx(
|
|
takerPubkey,
|
|
takerAccount,
|
|
{ marketIndex: order.marketIndex, orderId: order.orderId }
|
|
);
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
**getFillSpotOrderIx** builds an instruction to fill another user's spot order.
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="getFillSpotOrderIx" type="method" owner="DriftClient">
|
|
```js
|
|
// Same pattern as getFillPerpOrderIx but for spot markets.
|
|
const ix = await driftClient.getFillSpotOrderIx(
|
|
takerPubkey,
|
|
takerAccount,
|
|
{ marketIndex: order.marketIndex, orderId: order.orderId }
|
|
);
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
**getTriggerOrderIx** builds an instruction to trigger a conditional order (stop-loss or take-profit) that has met its trigger condition.
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="getTriggerOrderIx" type="method" owner="DriftClient">
|
|
```js
|
|
// Params:
|
|
// userAccountPublicKey: PublicKey - the user whose order to trigger
|
|
// userAccount: UserAccount - the deserialized user account
|
|
// order: Order - the full order object (must have trigger condition met)
|
|
// fillerPublicKey?: PublicKey - optional, defaults to your user account
|
|
|
|
const userPubkey = targetUser.userAccountPublicKey;
|
|
const userAccount = targetUser.getUserAccount();
|
|
const triggerOrder = userAccount.orders.find(
|
|
(o) => o.orderType.triggerMarket !== undefined || o.orderType.triggerLimit !== undefined
|
|
);
|
|
|
|
const ix = await driftClient.getTriggerOrderIx(
|
|
userPubkey,
|
|
userAccount,
|
|
triggerOrder
|
|
);
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
**getRevertFillIx** builds an instruction to revert a fill (used by filler bots when a fill was invalid).
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="getRevertFillIx" type="method" owner="DriftClient">
|
|
```js
|
|
// Params:
|
|
// fillerPublicKey?: PublicKey - defaults to your user account
|
|
const ix = await driftClient.getRevertFillIx();
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
**getSettlePNLsIxs** builds instructions to settle PnL for one or more users across multiple markets. Returns an array of instructions (one per user per market).
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="getSettlePNLsIxs" type="method" owner="DriftClient">
|
|
```js
|
|
// Params:
|
|
// users: Array of { settleeUserAccountPublicKey, settleeUserAccount }
|
|
// marketIndexes: number[] - perp market indexes to settle
|
|
|
|
const user = driftClient.getUser();
|
|
const ixs = await driftClient.getSettlePNLsIxs(
|
|
[
|
|
{
|
|
settleeUserAccountPublicKey: user.userAccountPublicKey,
|
|
settleeUserAccount: user.getUserAccount(),
|
|
},
|
|
],
|
|
[0, 1] // settle PnL on perp markets 0 and 1
|
|
);
|
|
// ixs is an array of TransactionInstruction, one per user per market
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|
|
|
|
**getJupiterSwapIxV6** builds a Jupiter swap instruction routed through Drift.
|
|
|
|
<SDKDoc>
|
|
<TypeScript name="getJupiterSwapIxV6" type="method" owner="DriftClient">
|
|
```js
|
|
// Params:
|
|
// inMarketIndex: number - spot market index of the input token
|
|
// outMarketIndex: number - spot market index of the output token
|
|
// amount: BN - amount of input token (in spot precision)
|
|
// slippageBps: number - max slippage in basis points
|
|
|
|
const ix = await driftClient.getJupiterSwapIxV6({
|
|
inMarketIndex: 0, // e.g. USDC
|
|
outMarketIndex: 1, // e.g. SOL
|
|
amount: driftClient.convertToSpotPrecision(0, 10), // 10 USDC
|
|
slippageBps: 50, // 0.5% max slippage
|
|
});
|
|
```
|
|
</TypeScript>
|
|
</SDKDoc>
|