Setup
The examples below use placeholders like <RPC_URL> and <KEYPAIR_PATH>.
Program addresses
The Drift program id is environment-specific. In most cases you’ll use the default program id baked into the SDK config for devnet or mainnet-beta.
Wallet / authentication
To interact with Solana you need a keypair. One common approach for bots is to point ANCHOR_WALLET (or your own env var) at a JSON keypair file.
solana-keygen new --outfile ~/.config/solana/my-keypair.json
export ANCHOR_WALLET=~/.config/solana/my-keypair.jsonInstall
npm i @drift-labs/sdkNumeric types (BN) and precision helpers
The SDK represents most on-chain numbers as integers using BN-style big numbers. Use helper functions + precision constants to convert into human units when needed.
import { BN, PRICE_PRECISION, convertToNumber } from "@drift-labs/sdk";
const raw = new BN(10_500_000); // e.g. 10.5 with 1e6 precision
const human = convertToNumber(raw, PRICE_PRECISION);
console.log(human);Function convertToNumberReference ↗| Name | Type | Default |
|---|---|---|
bigNumber | any | |
precision | any |
import { PRICE_PRECISION } from "@drift-labs/sdk";Variable PRICE_PRECISIONReference ↗PRICE_PRECISION.Create a Drift client
At a minimum you provide a Solana connection, a wallet, and the env. Then call subscribe() to start receiving account updates.
import { loadKeypair } from "@drift-labs/sdk";
const keypair = loadKeypair("<KEYPAIR_PATH>");Function loadKeypairReference ↗| Name | Type | Default |
|---|---|---|
privateKey | string |
import { Wallet, loadKeypair } from "@drift-labs/sdk";
const wallet = new Wallet(loadKeypair("<KEYPAIR_PATH>"));Class WalletReference ↗import { Connection } from "@solana/web3.js";
import { DriftClient, Wallet, loadKeypair } from "@drift-labs/sdk";
const connection = new Connection("<RPC_URL>", "confirmed");
const wallet = new Wallet(loadKeypair("<KEYPAIR_PATH>"));
const driftClient = new DriftClient({
connection,
wallet,
env: "mainnet-beta",
});
await driftClient.subscribe();Class DriftClientReference ↗await driftClient.subscribe();Method DriftClient.subscribeReference ↗// Force-refresh all tracked accounts from RPC.
await driftClient.fetchAccounts();Method DriftClient.fetchAccountsReference ↗await driftClient.unsubscribe();Method DriftClient.unsubscribeReference ↗Account subscriptions (websocket vs polling)
For most bots, websocket subscriptions are the easiest way to keep markets and users up to date. For read-only workflows or when you need tighter control over RPC load, you can switch to polling with a BulkAccountLoader.
import { BulkAccountLoader } from "@drift-labs/sdk";Class BulkAccountLoaderReference ↗import { BulkAccountLoader } from "@drift-labs/sdk";
const accountLoader = new BulkAccountLoader(connection, "confirmed", 0);
const driftClient = new DriftClient({
connection,
wallet,
env: "mainnet-beta",
accountSubscription: {
type: "polling",
accountLoader,
},
// Optional: explicitly list markets/oracles to load.
// perpMarketIndexes: [0, 1],
// spotMarketIndexes: [0],
// oracleInfos: [{ publicKey: <ORACLE_PUBKEY>, source: <ORACLE_SOURCE> }],
});Multiple subaccounts
if (!driftClient.hasUser(1)) {
await driftClient.addUser(1);
}Method DriftClient.hasUserReference ↗| Name | Type | Default |
|---|---|---|
subAccountId | number | |
authority | PublicKey |
await driftClient.addUser(1);Method DriftClient.addUserReference ↗| Name | Type | Default |
|---|---|---|
subAccountId | number | |
authority | PublicKey | |
userAccount | UserAccount |
Program accounts (advanced)
import { DRIFT_PROGRAM_ID } from "@drift-labs/sdk";Variable DRIFT_PROGRAM_IDReference ↗import { getHighLeverageModeConfigPublicKey } from "@drift-labs/sdk";
const pda = getHighLeverageModeConfigPublicKey(driftClient.program.programId);
const config = await driftClient.program.account.highLeverageModeConfig.fetch(pda);Function getHighLeverageModeConfigPublicKeyReference ↗| Name | Type | Default |
|---|---|---|
programId | PublicKey |
import { initialize } from "@drift-labs/sdk";
const sdkConfig = initialize({ env: "mainnet-beta" });
console.log(sdkConfig.DRIFT_PROGRAM_ID);Function initializeReference ↗| Name | Type | Default |
|---|---|---|
props | { env: DriftEnv; overrideEnv?: Partial<DriftConfig>; } |