> ## Documentation Index
> Fetch the complete documentation index at: https://docs.glam.systems/llms.txt
> Use this file to discover all available pages before exploring further.

# Drift

> Trade spot and perpetual markets and deposit to Drift Vaults

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:

```typescript theme={null}
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:

```typescript theme={null}
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:

```typescript theme={null}
// 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:

```typescript theme={null}
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:

```typescript theme={null}
// 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:

```typescript theme={null}
const txSig = await glamClient.drift.settlePnl(
  0, // market index
  0, // sub-account ID
);
```

### Configure User

Update margin and trading settings:

```typescript theme={null}
// 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:

```typescript theme={null}
const txSig = await glamClient.drift.deleteUser(0);
```

## Drift Vaults

### Initialize Depositor

Initialize a vault depositor account before depositing:

```typescript theme={null}
const driftVault = new PublicKey("DriftVlt111111111111111111111111");

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

### Deposit

Deposit assets into a Drift Vault:

```typescript theme={null}
const txSig = await glamClient.driftVaults.deposit(
  driftVault,
  new BN(100_000_000), // amount
);
```

### Withdraw

Drift Vault withdrawals follow a request-withdraw-claim flow:

```typescript theme={null}
// 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);
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.