Deposits & Withdrawals
How it works
Drift uses a cross-margin system where all your deposits serve as collateral for all your positions across perp and spot markets. When you deposit tokens (like USDC, SOL, or other supported assets), they’re added to your user account’s spot balances. These deposits can be used to back perp positions, and you can borrow against them to increase leverage.
Each deposit in a spot market earns interest from borrowers, while borrows accrue interest charges. Spot balances are tracked with precision (typically 1e6 for USDC, 1e9 for SOL) and can be positive (deposits) or negative (borrows). Your total collateral value is calculated by summing all deposits (weighted by asset weights) and subtracting borrows and unrealized perp losses.
Withdrawals require sufficient free collateral, you can’t withdraw funds that are backing open positions or would put your account below minimum margin requirements. The SDK handles token account derivation and precision conversion automatically.
SDK Usage
Deposits/withdrawals move tokens between your wallet token accounts and your Drift subaccount’s spot position.
Converting amounts and deriving the token account
// marketIndex 0 is commonly USDC
const marketIndex = 0;
const amount = driftClient.convertToSpotPrecision(marketIndex, 100); // 100 USDC (example)Method DriftClient.convertToSpotPrecisionReference ↗| Name | Type | Default |
|---|---|---|
marketIndex | number | |
amount | any |
const marketIndex = 0;
const ata = await driftClient.getAssociatedTokenAccount(marketIndex);
console.log(ata.toBase58());Method DriftClient.getAssociatedTokenAccountReference ↗| Name | Type | Default |
|---|---|---|
marketIndex | number | |
useNative | boolean | |
tokenProgram | PublicKey | |
authority | PublicKey | |
allowOwnerOffCurve | boolean |
Deposit
const marketIndex = 0;
const amount = driftClient.convertToSpotPrecision(marketIndex, 100);
const associatedTokenAccount = await driftClient.getAssociatedTokenAccount(marketIndex);
await driftClient.deposit(amount, marketIndex, associatedTokenAccount);Method DriftClient.depositReference ↗Withdraw
const marketIndex = 0;
const amount = driftClient.convertToSpotPrecision(marketIndex, 100);
const associatedTokenAccount = await driftClient.getAssociatedTokenAccount(marketIndex);
await driftClient.withdraw(amount, marketIndex, associatedTokenAccount);Method DriftClient.withdrawReference ↗Spot rates (borrow / lend)
import { SPOT_MARKET_RATE_PRECISION, calculateDepositRate, convertToNumber } from "@drift-labs/sdk";
const spotMarket = driftClient.getSpotMarketAccount(0);
const rate = calculateDepositRate(spotMarket);
console.log(convertToNumber(rate, SPOT_MARKET_RATE_PRECISION));Function calculateDepositRateReference ↗| Name | Type | Default |
|---|---|---|
bank | SpotMarketAccount | |
delta | any | |
currentUtilization | any |
import { SPOT_MARKET_RATE_PRECISION, calculateBorrowRate, convertToNumber } from "@drift-labs/sdk";
const spotMarket = driftClient.getSpotMarketAccount(0);
const rate = calculateBorrowRate(spotMarket);
console.log(convertToNumber(rate, SPOT_MARKET_RATE_PRECISION));Function calculateBorrowRateReference ↗| Name | Type | Default |
|---|---|---|
bank | SpotMarketAccount | |
delta | any | |
currentUtilization | any |