Skip to main content

DriftClient

The DriftClient class is the primary interface for interacting with Drift Protocol. It provides methods for managing user accounts, executing trades, handling deposits/withdrawals, and subscribing to protocol state.

Constructor

new DriftClient(config: DriftClientConfig)
config
DriftClientConfig
required
Configuration object for initializing the DriftClient

Subscription Methods

subscribe

Subscribes to Drift Protocol state and user accounts.
await driftClient.subscribe(): Promise<boolean>
return
Promise<boolean>
Returns true if subscription was successful

unsubscribe

Unsubscribes from all accounts and cleans up resources.
await driftClient.unsubscribe(): Promise<void>

fetchAccounts

Forces the account subscriber to fetch fresh account data from RPC.
await driftClient.fetchAccounts(): Promise<void>

User Account Management

initializeUserAccount

Initializes a new user account with optional referrer information.
await driftClient.initializeUserAccount(
  subAccountId?: number,
  name?: string,
  referrerInfo?: ReferrerInfo,
  txParams?: TxParams
): Promise<[TransactionSignature, PublicKey]>
subAccountId
number
default:0
Sub-account ID to initialize
name
string
Account name (defaults to “Main Account” for sub-account 0)
referrerInfo
ReferrerInfo
Referrer information for fee sharing
txParams
TxParams
Transaction parameters (compute units, priority fees)
return
Promise<[TransactionSignature, PublicKey]>
Returns a tuple of [transaction signature, user account public key]

deleteUser

Deletes a user account and reclaims rent.
await driftClient.deleteUser(
  subAccountId?: number,
  txParams?: TxParams
): Promise<TransactionSignature>
subAccountId
number
default:0
Sub-account ID to delete
txParams
TxParams
Transaction parameters
return
Promise<TransactionSignature>
Transaction signature

getUser

Returns a User instance for the specified sub-account.
driftClient.getUser(
  subAccountId?: number,
  authority?: PublicKey
): User
subAccountId
number
Sub-account ID (defaults to activeSubAccountId)
authority
PublicKey
Authority public key (defaults to wallet public key)
return
User
User instance for the specified account

getUserAccount

Returns the UserAccount data for the specified sub-account.
driftClient.getUserAccount(
  subAccountId?: number,
  authority?: PublicKey
): UserAccount | undefined
subAccountId
number
Sub-account ID (defaults to activeSubAccountId)
authority
PublicKey
Authority public key
return
UserAccount | undefined
User account data or undefined if not found

switchActiveUser

Switches the active sub-account.
await driftClient.switchActiveUser(
  subAccountId: number,
  authority?: PublicKey
): Promise<void>
subAccountId
number
required
New active sub-account ID
authority
PublicKey
Authority for the new active account

Market Data Methods

getPerpMarketAccount

Returns perpetual market account data.
driftClient.getPerpMarketAccount(
  marketIndex: number
): PerpMarketAccount | undefined
marketIndex
number
required
Perpetual market index
return
PerpMarketAccount | undefined
Perpetual market account data

getSpotMarketAccount

Returns spot market account data.
driftClient.getSpotMarketAccount(
  marketIndex: number
): SpotMarketAccount | undefined
marketIndex
number
required
Spot market index
return
SpotMarketAccount | undefined
Spot market account data

getOraclePriceDataAndSlot

Returns oracle price data with the slot number.
driftClient.getOraclePriceDataAndSlot(
  oraclePublicKey: PublicKey,
  oracleSource: OracleSource
): DataAndSlot<OraclePriceData> | undefined
oraclePublicKey
PublicKey
required
Oracle account public key
oracleSource
OracleSource
required
Oracle source type (e.g., Pyth, Switchboard)
return
DataAndSlot<OraclePriceData> | undefined
Oracle price data with slot number

State Methods

getStateAccount

Returns the Drift protocol state account.
driftClient.getStateAccount(): StateAccount
return
StateAccount
Drift protocol state containing global parameters

getStatePublicKey

Returns the public key of the state account.
await driftClient.getStatePublicKey(): Promise<PublicKey>
return
Promise<PublicKey>
State account public key

Wallet Management

updateWallet

Updates the wallet used for signing transactions and re-subscribes to user accounts.
await driftClient.updateWallet(
  newWallet: IWallet,
  subAccountIds?: number[],
  activeSubAccountId?: number,
  includeDelegates?: boolean,
  authoritySubaccountMap?: Map<string, number[]>
): Promise<boolean>
newWallet
IWallet
required
New wallet adapter
subAccountIds
number[]
Sub-account IDs to subscribe to
activeSubAccountId
number
New active sub-account ID
includeDelegates
boolean
Whether to include delegated accounts
authoritySubaccountMap
Map<string, number[]>
Map of authority addresses to sub-account IDs
return
Promise<boolean>
Returns true if wallet update and re-subscription was successful

Helper Methods

convertToSpotPrecision

Converts an amount to the correct precision for a spot market.
driftClient.convertToSpotPrecision(
  marketIndex: number,
  amount: BN | number
): BN
marketIndex
number
required
Spot market index
amount
BN | number
required
Amount to convert
return
BN
Amount in spot market precision

convertToPerpPrecision

Converts an amount to perpetual market precision (1e9).
driftClient.convertToPerpPrecision(
  amount: BN | number
): BN
amount
BN | number
required
Amount to convert
return
BN
Amount in BASE_PRECISION (1e9)

convertToPricePrecision

Converts an amount to price precision (1e6).
driftClient.convertToPricePrecision(
  amount: BN | number
): BN
amount
BN | number
required
Amount to convert
return
BN
Amount in PRICE_PRECISION (1e6)

Properties

connection
Connection
Solana RPC connection instance
wallet
IWallet
Wallet adapter for signing transactions
program
Program
Anchor program instance for Drift Protocol
authority
PublicKey
Current authority public key
activeSubAccountId
number
Currently active sub-account ID
isSubscribed
boolean
Whether the client is currently subscribed to accounts

Usage Example

import { DriftClient, Wallet } from '@drift-labs/sdk';
import { Connection, PublicKey } from '@solana/web3.js';

// Initialize connection and wallet
const connection = new Connection('https://api.mainnet-beta.solana.com');
const wallet = new Wallet(keypair);

// Create DriftClient instance
const driftClient = new DriftClient({
  connection,
  wallet,
  env: 'mainnet-beta',
  txVersion: 0,
});

// Subscribe to accounts
await driftClient.subscribe();

// Get perpetual market data
const perpMarket = driftClient.getPerpMarketAccount(0);
console.log('SOL-PERP Oracle Price:', perpMarket.amm.lastOraclePrice);

// Get user account
const user = driftClient.getUser();
const userAccount = user.getUserAccount();

// Unsubscribe when done
await driftClient.unsubscribe();

Build docs developers (and LLMs) love