Markets, Oracles, and Positions
These are the most common read-path helpers you’ll use to power bots, dashboards, and risk logic.
Market accounts
const marketIndex = 0;
const spotMarket = driftClient.getSpotMarketAccount(marketIndex);
console.log(spotMarket?.marketIndex);Method DriftClient.getSpotMarketAccountReference ↗| Name | Type | Default |
|---|---|---|
marketIndex | number |
const marketIndex = 0;
const perpMarket = driftClient.getPerpMarketAccount(marketIndex);
console.log(perpMarket?.marketIndex);Method DriftClient.getPerpMarketAccountReference ↗| Name | Type | Default |
|---|---|---|
marketIndex | number |
const spotMarkets = driftClient.getSpotMarketAccounts();
console.log(spotMarkets.length);Method DriftClient.getSpotMarketAccountsReference ↗This function does not accept any parameters.
const perpMarkets = driftClient.getPerpMarketAccounts();
console.log(perpMarkets.length);Method DriftClient.getPerpMarketAccountsReference ↗This function does not accept any parameters.
Oracle price
const marketIndex = 0;
const oracle = driftClient.getOracleDataForPerpMarket(marketIndex);
console.log(oracle.price.toString());Method DriftClient.getOracleDataForPerpMarketReference ↗| Name | Type | Default |
|---|---|---|
marketIndex | number |
const marketIndex = 0;
const oracle = driftClient.getOracleDataForSpotMarket(marketIndex);
console.log(oracle.price.toString());Method DriftClient.getOracleDataForSpotMarketReference ↗| Name | Type | Default |
|---|---|---|
marketIndex | number |
const marketIndex = 0;
const oracle = driftClient.getMMOracleDataForPerpMarket(marketIndex);
console.log(oracle.price.toString());Method DriftClient.getMMOracleDataForPerpMarketReference ↗| Name | Type | Default |
|---|---|---|
marketIndex | number |
Positions and balances
const spotPosition = driftClient.getSpotPosition(0);
console.log(spotPosition);Method DriftClient.getSpotPositionReference ↗| Name | Type | Default |
|---|---|---|
marketIndex | number | |
subAccountId | number |
const perpPosition = driftClient.getPerpPosition(0);
console.log(perpPosition);Method DriftClient.getPerpPositionReference ↗TypeScript docs unavailable for
getPerpPosition.Protocol state
const state = driftClient.getStateAccount();
console.log(state);Method DriftClient.getStateAccountReference ↗This function does not accept any parameters.
Orderbook & DLOB (local)
import { OrderSubscriber } from "@drift-labs/sdk";Class OrderSubscriberReference ↗import { DLOBSubscriber } from "@drift-labs/sdk";Class DLOBSubscriberReference ↗import { SlotSubscriber } from "@drift-labs/sdk";Class SlotSubscriberReference ↗const slotSubscriber = new SlotSubscriber(connection);
await slotSubscriber.subscribe();
const orderSubscriber = new OrderSubscriber({
driftClient,
subscriptionConfig: { type: "websocket" },
fastDecode: true,
decodeData: true,
});
await orderSubscriber.subscribe();
const dlobSubscriber = new DLOBSubscriber({
driftClient,
dlobSource: orderSubscriber,
slotSource: slotSubscriber,
updateFrequency: 1000,
});
await dlobSubscriber.subscribe();import { DLOB } from "@drift-labs/sdk";Class DLOBReference ↗import { UserMap } from "@drift-labs/sdk";Class UserMapReference ↗Last updated on