Users
Most write actions in Drift are done through DriftClient. For account-level reads (positions, orders, health), you’ll typically use a User.
Initialize a user account
// Assumes you already constructed and subscribed `driftClient`.
const [txSig, userAccountPublicKey] = await driftClient.initializeUserAccount(0, "my-account");
console.log(txSig, userAccountPublicKey.toBase58());Method DriftClient.initializeUserAccountReference ↗| Name | Type | Default |
|---|---|---|
subAccountId | number | |
name | string | |
referrerInfo | ReferrerInfo | |
txParams | TxParams |
Get the next subaccount id
const nextId = driftClient.getNextSubAccountId();
console.log(nextId);Method DriftClient.getNextSubAccountIdReference ↗This function does not accept any parameters.
Get a User and subscribe
import { User } from "@drift-labs/sdk";
const user = driftClient.getUser(); // convenience helper
await user.subscribe();Class UserReference ↗const user = driftClient.getUser(); // active subaccountMethod DriftClient.getUserReference ↗| Name | Type | Default |
|---|---|---|
subAccountId | number | |
authority | PublicKey |
// Fetch a specific subaccount.
const user = driftClient.getUser(1);Method DriftClient.getUserReference ↗| Name | Type | Default |
|---|---|---|
subAccountId | number | |
authority | PublicKey |
const account = driftClient.getUser().getUserAccount();
console.log(account);Method User.getUserAccountReference ↗This function does not accept any parameters.
const account = driftClient.getUserAccount(); // active subaccount
console.log(account);Method DriftClient.getUserAccountReference ↗| Name | Type | Default |
|---|---|---|
subAccountId | number | |
authority | PublicKey |
const user = driftClient.getUser(0);
await user.fetchAccounts();Method User.fetchAccountsReference ↗This function does not accept any parameters.
Derived addresses (user account)
const userAccountPublicKey = await driftClient.getUserAccountPublicKey(0);
console.log(userAccountPublicKey.toBase58());Method DriftClient.getUserAccountPublicKeyReference ↗| Name | Type | Default |
|---|---|---|
subAccountId | number | |
authority | PublicKey |
Query user state (orders / positions)
import { BN } from "@drift-labs/sdk";
const user = driftClient.getUser();
const tokenAmount = user.getTokenAmount(0);
const isDeposit = tokenAmount.gte(new BN(0));
const isBorrow = tokenAmount.lt(new BN(0));
console.log({ tokenAmount: tokenAmount.toString(), isDeposit, isBorrow });Method User.getTokenAmountReference ↗| Name | Type | Default |
|---|---|---|
marketIndex | number |
const position = driftClient.getUser().getPerpPosition(0);
console.log(position?.baseAssetAmount.toString());Method User.getPerpPositionReference ↗| Name | Type | Default |
|---|---|---|
marketIndex | number |
const order = driftClient.getUser().getOrder(1);
console.log(order);Method User.getOrderReference ↗| Name | Type | Default |
|---|---|---|
orderId | number |
const order = driftClient.getUser().getOrderByUserOrderId(1);
console.log(order);Method User.getOrderByUserOrderIdReference ↗| Name | Type | Default |
|---|---|---|
userOrderId | number |
const orders = driftClient.getUser().getOpenOrders();
console.log(orders.length);Method User.getOpenOrdersReference ↗This function does not accept any parameters.
Switch active subaccount
// Switch to subaccount 1.
driftClient.switchActiveUser(1);Method DriftClient.switchActiveUserReference ↗| Name | Type | Default |
|---|---|---|
subAccountId | number | |
authority | PublicKey |
Update user settings (delegate / margin)
import { PublicKey } from "@solana/web3.js";
await driftClient.updateUserDelegate(new PublicKey("<DELEGATE_PUBKEY>"), 0);Method DriftClient.updateUserDelegateReference ↗| Name | Type | Default |
|---|---|---|
delegate | PublicKey | |
subAccountId | number |
await driftClient.updateUserMarginTradingEnabled(true, 0);Method DriftClient.updateUserMarginTradingEnabledReference ↗| Name | Type | Default |
|---|---|---|
updates | { marginTradingEnabled: boolean; subAccountId: number; }[] |
import { MARGIN_PRECISION } from "@drift-labs/sdk";
await driftClient.updateUserCustomMarginRatio([
{ marginRatio: MARGIN_PRECISION, subAccountId: 0 },
]);Method DriftClient.updateUserCustomMarginRatioReference ↗| Name | Type | Default |
|---|---|---|
updates | { marginRatio: number; subAccountId: number; }[] | |
txParams | TxParams |
Delete a user account
If a subaccount has no assets/liabilities, it can be deleted to reclaim rent.
await driftClient.deleteUser(1);Method DriftClient.deleteUserReference ↗| Name | Type | Default |
|---|---|---|
subAccountId | number | |
txParams | TxParams |
Last updated on