Skip to Content

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 ↗
Parameters:
NameTypeDefault
paramsOmit<OptionalOrderParams, "orderType">
Returns:
OptionalOrderParams

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 ↗
Parameters:
NameTypeDefault
optionalOrderParamsOptionalOrderParams
overridingParamsRecord<string, any>
Returns:
OrderParams

Precision helpers for orders

const size = driftClient.convertToPerpPrecision(1); // 1 base unit (BN) console.log(size.toString());
Method DriftClient.convertToPerpPrecisionReference ↗
Parameters:
NameTypeDefault
amountany
Returns:
BN
const price = driftClient.convertToPricePrecision(21.23); // price BN console.log(price.toString());
Method DriftClient.convertToPricePrecisionReference ↗
Parameters:
NameTypeDefault
amountany
Returns:
BN

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 ↗
Parameters:
NameTypeDefault
orderParamsOptionalOrderParams
txParamsTxParams
subAccountIdnumber
isolatedPositionDepositAmountany
Returns:
Promise<string>

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 ↗
Parameters:
NameTypeDefault
orderParamsOptionalOrderParams
subAccountIdnumber
depositToTradeArgs{ isMakingNewAccount: boolean; depositMarketIndex: number; }
Returns:
Promise<TransactionInstruction>
const ix = await driftClient.getPlaceSpotOrderIx(orderParams);
Method DriftClient.getPlaceSpotOrderIxReference ↗
Parameters:
NameTypeDefault
orderParamsOptionalOrderParams
subAccountIdnumber
Returns:
Promise<TransactionInstruction>
const ix = await driftClient.getCancelOrdersIx(marketType, marketIndex, direction);
Method DriftClient.getCancelOrdersIxReference ↗
Parameters:
NameTypeDefault
marketTypeMarketType
marketIndexnumber
directionPositionDirection
subAccountIdnumber
Returns:
Promise<TransactionInstruction>
const ix = await driftClient.getFillPerpOrderIx(userAccountPublicKey, userAccount, order, makerInfos);
Method DriftClient.getFillPerpOrderIxReference ↗
Parameters:
NameTypeDefault
userAccountPublicKeyPublicKey
userAccountUserAccount
orderPick<Order, "marketIndex" | "orderId">
makerInfoMakerInfo | MakerInfo[]
referrerInfoReferrerInfo
fillerSubAccountIdnumber
isSignedMsgboolean
fillerAuthorityPublicKey
hasBuilderFeeboolean
Returns:
Promise<TransactionInstruction>
const ix = await driftClient.getFillSpotOrderIx(userAccountPublicKey, userAccount, order, makerInfos);
Method DriftClient.getFillSpotOrderIxReference ↗
Parameters:
NameTypeDefault
userAccountPublicKeyPublicKey
userAccountUserAccount
orderPick<Order, "marketIndex" | "orderId">
fulfillmentConfigSerumV3FulfillmentConfigAccount | PhoenixV1FulfillmentConfigAccount | OpenbookV2FulfillmentConfigAccount
makerInfoMakerInfo | MakerInfo[]
referrerInfoReferrerInfo
fillerPublicKeyPublicKey
Returns:
Promise<TransactionInstruction>
const ix = await driftClient.getTriggerOrderIx(userAccountPublicKey, userAccount, order);
Method DriftClient.getTriggerOrderIxReference ↗
Parameters:
NameTypeDefault
userAccountPublicKeyPublicKey
userAccountUserAccount
orderOrder
fillerPublicKeyPublicKey
Returns:
Promise<TransactionInstruction>
const ix = await driftClient.getRevertFillIx();
Method DriftClient.getRevertFillIxReference ↗
Parameters:
NameTypeDefault
fillerPublicKeyPublicKey
Returns:
Promise<TransactionInstruction>
const ixs = await driftClient.getSettlePNLsIxs(userAccountPublicKey, marketIndexes);
Method DriftClient.getSettlePNLsIxsReference ↗
Parameters:
NameTypeDefault
users{ settleeUserAccountPublicKey: PublicKey; settleeUserAccount: UserAccount; }[]
marketIndexesnumber[]
revenueShareEscrowMapRevenueShareEscrowMap
Returns:
Promise<TransactionInstruction[]>
const ix = await driftClient.getJupiterSwapIxV6({ inMarketIndex: 0, outMarketIndex: 1, amount: driftClient.convertToSpotPrecision(0, 10), slippageBps: 50, });
Method DriftClient.getJupiterSwapIxV6Reference ↗
Parameters:
NameTypeDefault
__0{ jupiterClient: JupiterClient; outMarketIndex: number; inMarketIndex: number; outAssociatedTokenAccount?: PublicKey; inAssociatedTokenAccount?: PublicKey; ... 6 more ...; userAccountPublicKey?: PublicKey; }
Returns:
Promise<{ ixs: TransactionInstruction[]; lookupTables: AddressLookupTableAccount[]; }>

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 ↗
Parameters:
NameTypeDefault
orderParamsOptionalOrderParams
txParamsTxParams
subAccountIdnumber
Returns:
Promise<string>

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 ↗
Parameters:
NameTypeDefault
paramsOrderParams[]
txParamsTxParams
subAccountIdnumber
optionalIxsTransactionInstruction[]
isolatedPositionDepositAmountany
Returns:
Promise<string>

Cancel orders

await driftClient.cancelOrder(1);
Method DriftClient.cancelOrderReference ↗
Parameters:
NameTypeDefault
orderIdnumber
txParamsTxParams
subAccountIdnumber
overrides{ withdrawIsolatedDepositAmount?: any; }
Returns:
Promise<string>
await driftClient.cancelOrdersByIds([1]);
Method DriftClient.cancelOrdersByIdsReference ↗
Parameters:
NameTypeDefault
orderIdsnumber[]

The order ids to cancel.

txParamsTxParams

The transaction parameters.

subAccountIdnumber

The sub account id to cancel the orders for.

userUser

The user to cancel the orders for. If provided, it will be prioritized over the subAccountId.

overrides{ authority?: PublicKey; }
Returns:
Promise<string>
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 ↗
Parameters:
NameTypeDefault
marketTypeMarketType
marketIndexnumber
directionPositionDirection
txParamsTxParams
subAccountIdnumber
Returns:
Promise<string>

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 ↗
Parameters:
NameTypeDefault
cancelOrderParams{ marketType?: MarketType; marketIndex?: number; direction?: PositionDirection; }
placeOrderParamsOrderParams[]
txParamsTxParams
subAccountIdnumber
Returns:
Promise<string>

Modify orders

await driftClient.modifyOrder({ orderId: 1, newBaseAssetAmount: driftClient.convertToPerpPrecision(2), });
Method DriftClient.modifyOrderReference ↗
Parameters:
NameTypeDefault
orderParams{ orderId: number; newDirection?: PositionDirection; newBaseAmount?: any; newLimitPrice?: any; newOraclePriceOffset?: number; newTriggerPrice?: any; newTriggerCondition?: OrderTriggerCondition; ... 7 more ...; policy?: number; }
txParamsTxParams
subAccountIdnumber
Returns:
Promise<string>
await driftClient.modifyOrderByUserOrderId({ userOrderId: 1, newBaseAssetAmount: driftClient.convertToPerpPrecision(2), });
Method DriftClient.modifyOrderByUserOrderIdReference ↗
Parameters:
NameTypeDefault
orderParams{ userOrderId: number; newDirection?: PositionDirection; newBaseAmount?: any; newLimitPrice?: any; newOraclePriceOffset?: number; newTriggerPrice?: any; newTriggerCondition?: OrderTriggerCondition; ... 7 more ...; maxTs?: any; }
txParamsTxParams
subAccountIdnumber
Returns:
Promise<string>

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