Skip to main content
GLAM integrates with Drift Protocol for spot trading, perpetual trading, and Drift Vaults. The SDK provides two sub-clients:
  • drift — Drift Protocol v2 (spot and perp trading)
  • driftVaults — Drift Vaults (managed vault deposits)

Drift Protocol

Initialize User

Before trading on Drift, initialize a Drift user account for the vault:
import { GlamClient } from "@glamsystems/glam-sdk";

const glamClient = new GlamClient();

// Initialize with sub-account ID 0
const txSig = await glamClient.drift.initialize(0);

Deposit

Deposit assets into a Drift spot market:
import { BN } from "@coral-xyz/anchor";

// Deposit 100 USDC to spot market index 0
const txSig = await glamClient.drift.deposit(
  new BN(100_000_000), // amount in base units
  0, // market index
  0, // sub-account ID
);

Withdraw

Withdraw assets from a Drift spot market:
// Withdraw 50 USDC from spot market index 0
const txSig = await glamClient.drift.withdraw(
  new BN(50_000_000),
  0, // market index
  0, // sub-account ID
);

Place Order

Place a spot or perpetual order:
const txSig = await glamClient.drift.placeOrder(
  {
    orderType: { limit: {} },
    marketType: { perp: {} },
    marketIndex: 0, // SOL-PERP
    direction: { long: {} },
    baseAssetAmount: new BN(1_000_000_000), // 1 SOL
    price: new BN(150_000_000), // $150
  },
  0, // sub-account ID
);

Cancel Orders

Cancel orders by market or by specific order IDs:
// Cancel all perp orders for SOL-PERP market
const txSig = await glamClient.drift.cancelOrders(
  { perp: {} }, // market type
  0, // market index
  null, // direction (null = all)
  0, // sub-account ID
);

// Cancel specific orders by ID
const txSig = await glamClient.drift.cancelOrdersByIds(
  [1, 2, 3], // order IDs
  0, // sub-account ID
);

Settle PnL

Settle profit and loss for a perpetual market position:
const txSig = await glamClient.drift.settlePnl(
  0, // market index
  0, // sub-account ID
);

Configure User

Update margin and trading settings:
// Set custom margin ratio (max leverage)
await glamClient.drift.updateUserCustomMarginRatio(
  5000, // DRIFT_MARGIN_PRECISION = 10,000; 5000 = 2x leverage
  0, // sub-account ID
);

// Enable margin trading
await glamClient.drift.updateUserMarginTradingEnabled(true, 0);

// Set a delegate for the Drift user
await glamClient.drift.updateUserDelegate(
  new PublicKey("Delegate1111111111111111111111111"),
  0,
);

Delete User

Delete a Drift user account when no longer needed:
const txSig = await glamClient.drift.deleteUser(0);

Drift Vaults

Initialize Depositor

Initialize a vault depositor account before depositing:
const driftVault = new PublicKey("DriftVlt111111111111111111111111");

const txSig = await glamClient.driftVaults.initializeVaultDepositor(
  driftVault,
);

Deposit

Deposit assets into a Drift Vault:
const txSig = await glamClient.driftVaults.deposit(
  driftVault,
  new BN(100_000_000), // amount
);

Withdraw

Drift Vault withdrawals follow a request-withdraw-claim flow:
// 1. Request withdrawal
await glamClient.driftVaults.requestWithdraw(
  driftVault,
  new BN(50_000_000), // amount
);

// 2. Cancel if needed
await glamClient.driftVaults.cancelWithdrawRequest(driftVault);

// 3. Or complete the withdrawal after the period elapses
await glamClient.driftVaults.withdraw(driftVault);