DriftClient
TheDriftClient 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
DriftClientConfig
required
Configuration object for initializing the DriftClient
Subscription Methods
subscribe
Subscribes to Drift Protocol state and user accounts.Promise<boolean>
Returns
true if subscription was successfulunsubscribe
Unsubscribes from all accounts and cleans up resources.fetchAccounts
Forces the account subscriber to fetch fresh account data from RPC.User Account Management
initializeUserAccount
Initializes a new user account with optional referrer information.number
default:0
Sub-account ID to initialize
string
Account name (defaults to “Main Account” for sub-account 0)
ReferrerInfo
Referrer information for fee sharing
TxParams
Transaction parameters (compute units, priority fees)
Promise<[TransactionSignature, PublicKey]>
Returns a tuple of [transaction signature, user account public key]
deleteUser
Deletes a user account and reclaims rent.number
default:0
Sub-account ID to delete
TxParams
Transaction parameters
Promise<TransactionSignature>
Transaction signature
getUser
Returns a User instance for the specified sub-account.number
Sub-account ID (defaults to activeSubAccountId)
PublicKey
Authority public key (defaults to wallet public key)
User
User instance for the specified account
getUserAccount
Returns the UserAccount data for the specified sub-account.number
Sub-account ID (defaults to activeSubAccountId)
PublicKey
Authority public key
UserAccount | undefined
User account data or undefined if not found
switchActiveUser
Switches the active sub-account.number
required
New active sub-account ID
PublicKey
Authority for the new active account
Market Data Methods
getPerpMarketAccount
Returns perpetual market account data.number
required
Perpetual market index
PerpMarketAccount | undefined
Perpetual market account data
getSpotMarketAccount
Returns spot market account data.number
required
Spot market index
SpotMarketAccount | undefined
Spot market account data
getOraclePriceDataAndSlot
Returns oracle price data with the slot number.PublicKey
required
Oracle account public key
OracleSource
required
Oracle source type (e.g., Pyth, Switchboard)
DataAndSlot<OraclePriceData> | undefined
Oracle price data with slot number
State Methods
getStateAccount
Returns the Drift protocol state account.StateAccount
Drift protocol state containing global parameters
getStatePublicKey
Returns the public key of the state account.Promise<PublicKey>
State account public key
Wallet Management
updateWallet
Updates the wallet used for signing transactions and re-subscribes to user accounts.IWallet
required
New wallet adapter
number[]
Sub-account IDs to subscribe to
number
New active sub-account ID
boolean
Whether to include delegated accounts
Map<string, number[]>
Map of authority addresses to sub-account IDs
Promise<boolean>
Returns
true if wallet update and re-subscription was successfulHelper Methods
convertToSpotPrecision
Converts an amount to the correct precision for a spot market.number
required
Spot market index
BN | number
required
Amount to convert
BN
Amount in spot market precision
convertToPerpPrecision
Converts an amount to perpetual market precision (1e9).BN | number
required
Amount to convert
BN
Amount in BASE_PRECISION (1e9)
convertToPricePrecision
Converts an amount to price precision (1e6).BN | number
required
Amount to convert
BN
Amount in PRICE_PRECISION (1e6)
Properties
Connection
Solana RPC connection instance
IWallet
Wallet adapter for signing transactions
Program
Anchor program instance for Drift Protocol
PublicKey
Current authority public key
number
Currently active sub-account ID
boolean
Whether the client is currently subscribed to accounts