Orders
This page focuses on placing and canceling orders via DriftClient. For concise examples we use helper builders like getMarketOrderParams(...).
Build market order params
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)
});Function getMarketOrderParamsReference ↗| Name | Type | Default |
|---|---|---|
params | Omit<OptionalOrderParams, "orderType"> |
Build order params (generic helper)
If you want a single helper that works for limit/market/oracle/trigger, use getOrderParams(...).
import { getOrderParams, OrderType, PositionDirection } from "@drift-labs/sdk";
const orderParams = getOrderParams({
orderType: OrderType.LIMIT,
marketIndex: 0,
direction: PositionDirection.LONG,
baseAssetAmount: driftClient.convertToPerpPrecision(1),
price: driftClient.convertToPricePrecision(21.23),
});Function getOrderParamsReference ↗| Name | Type | Default |
|---|---|---|
optionalOrderParams | OptionalOrderParams | |
overridingParams | Record<string, any> |
Precision helpers for orders
const size = driftClient.convertToPerpPrecision(1); // 1 base unit (BN)
console.log(size.toString());Method DriftClient.convertToPerpPrecisionReference ↗| Name | Type | Default |
|---|---|---|
amount | any |
const price = driftClient.convertToPricePrecision(21.23); // price BN
console.log(price.toString());Method DriftClient.convertToPricePrecisionReference ↗| Name | Type | Default |
|---|---|---|
amount | any |
Place a perp order
// 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);Method DriftClient.placePerpOrderReference ↗| Name | Type | Default |
|---|---|---|
orderParams | OptionalOrderParams | |
txParams | TxParams | |
subAccountId | number | |
isolatedPositionDepositAmount | any |
Oracle / auction-style orders (example shape)
// Example shape from older docs: auction params + oracle offset.
// This is a usage pattern example; verify fields against the `OrderParams` type.
const oraclePrice = driftClient.getOracleDataForPerpMarket(18).price;
const auctionStartPrice = oraclePrice.neg().divn(1000);
const auctionEndPrice = oraclePrice.divn(1000);
const orderParams = {
orderType: OrderType.ORACLE,
baseAssetAmount: driftClient.convertToPerpPrecision(10),
direction: PositionDirection.LONG,
marketIndex: 18,
auctionStartPrice,
auctionEndPrice,
oraclePriceOffset: oraclePrice.divn(500).toNumber(),
auctionDuration: 30,
};
await driftClient.placePerpOrder(orderParams);Instruction builders (advanced)
Use ix builders when you need custom compute budgets, ALT usage, or batching.
const ix = await driftClient.getPlacePerpOrderIx(orderParams);Method DriftClient.getPlacePerpOrderIxReference ↗| Name | Type | Default |
|---|---|---|
orderParams | OptionalOrderParams | |
subAccountId | number | |
depositToTradeArgs | { isMakingNewAccount: boolean; depositMarketIndex: number; } |
const ix = await driftClient.getPlaceSpotOrderIx(orderParams);Method DriftClient.getPlaceSpotOrderIxReference ↗| Name | Type | Default |
|---|---|---|
orderParams | OptionalOrderParams | |
subAccountId | number |
const ix = await driftClient.getCancelOrdersIx(marketType, marketIndex, direction);Method DriftClient.getCancelOrdersIxReference ↗| Name | Type | Default |
|---|---|---|
marketType | MarketType | |
marketIndex | number | |
direction | PositionDirection | |
subAccountId | number |
const ix = await driftClient.getFillPerpOrderIx(userAccountPublicKey, userAccount, order, makerInfos);Method DriftClient.getFillPerpOrderIxReference ↗const ix = await driftClient.getFillSpotOrderIx(userAccountPublicKey, userAccount, order, makerInfos);Method DriftClient.getFillSpotOrderIxReference ↗const ix = await driftClient.getTriggerOrderIx(userAccountPublicKey, userAccount, order);Method DriftClient.getTriggerOrderIxReference ↗| Name | Type | Default |
|---|---|---|
userAccountPublicKey | PublicKey | |
userAccount | UserAccount | |
order | Order | |
fillerPublicKey | PublicKey |
const ix = await driftClient.getRevertFillIx();Method DriftClient.getRevertFillIxReference ↗| Name | Type | Default |
|---|---|---|
fillerPublicKey | PublicKey |
const ixs = await driftClient.getSettlePNLsIxs(userAccountPublicKey, marketIndexes);Method DriftClient.getSettlePNLsIxsReference ↗| Name | Type | Default |
|---|---|---|
users | { settleeUserAccountPublicKey: PublicKey; settleeUserAccount: UserAccount; }[] | |
marketIndexes | number[] | |
revenueShareEscrowMap | RevenueShareEscrowMap |
const ix = await driftClient.getJupiterSwapIxV6({
inMarketIndex: 0,
outMarketIndex: 1,
amount: driftClient.convertToSpotPrecision(0, 10),
slippageBps: 50,
});Method DriftClient.getJupiterSwapIxV6Reference ↗Place a spot order
// Example uses a simplified object; adjust fields to your strategy.
await driftClient.placeSpotOrder({
marketIndex: 1,
direction: PositionDirection.LONG,
// ... order fields (amount/price/etc)
});Method DriftClient.placeSpotOrderReference ↗| Name | Type | Default |
|---|---|---|
orderParams | OptionalOrderParams | |
txParams | TxParams | |
subAccountId | number |
Place multiple orders
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(),
},
]);Method DriftClient.placeOrdersReference ↗| Name | Type | Default |
|---|---|---|
params | OrderParams[] | |
txParams | TxParams | |
subAccountId | number | |
optionalIxs | TransactionInstruction[] | |
isolatedPositionDepositAmount | any |
Cancel orders
await driftClient.cancelOrder(1);Method DriftClient.cancelOrderReference ↗| Name | Type | Default |
|---|---|---|
orderId | number | |
txParams | TxParams | |
subAccountId | number | |
overrides | { withdrawIsolatedDepositAmount?: any; } |
await driftClient.cancelOrdersByIds([1]);Method DriftClient.cancelOrdersByIdsReference ↗import { MarketType, PositionDirection } from "@drift-labs/sdk";
// Cancels orders matching the filters. To cancel all orders, omit parameters.
await driftClient.cancelOrders(MarketType.PERP, 0, PositionDirection.LONG);Method DriftClient.cancelOrdersReference ↗| Name | Type | Default |
|---|---|---|
marketType | MarketType | |
marketIndex | number | |
direction | PositionDirection | |
txParams | TxParams | |
subAccountId | number |
Cancel and place (atomic)
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),
},
]
);Method DriftClient.cancelAndPlaceOrdersReference ↗| Name | Type | Default |
|---|---|---|
cancelOrderParams | { marketType?: MarketType; marketIndex?: number; direction?: PositionDirection; } | |
placeOrderParams | OrderParams[] | |
txParams | TxParams | |
subAccountId | number |
Modify orders
await driftClient.modifyOrder({
orderId: 1,
newBaseAssetAmount: driftClient.convertToPerpPrecision(2),
});Method DriftClient.modifyOrderReference ↗await driftClient.modifyOrderByUserOrderId({
userOrderId: 1,
newBaseAssetAmount: driftClient.convertToPerpPrecision(2),
});Method DriftClient.modifyOrderByUserOrderIdReference ↗Trigger orders (stop / take-profit)
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);Oracle / auction orders (shape)
import { OrderType, PositionDirection } from "@drift-labs/sdk";
const oracle = driftClient.getOracleDataForPerpMarket(0);
const orderParams = {
orderType: OrderType.ORACLE,
marketIndex: 0,
direction: PositionDirection.LONG,
baseAssetAmount: driftClient.convertToPerpPrecision(1),
auctionStartPrice: oracle.price.muln(9950).divn(10000),
auctionEndPrice: oracle.price.muln(10050).divn(10000),
auctionDuration: 50,
oraclePriceOffset: driftClient.convertToPricePrecision(0.05).toNumber(),
};
await driftClient.placePerpOrder(orderParams);Last updated on