Skip to Content
DevelopersDrift SDKDeposits & Withdrawals

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

Convert a human-readable token amount into the onchain precision units required for spot instructions.

// marketIndex 0 is commonly USDC const marketIndex = 0; const amount = driftClient.convertToSpotPrecision(marketIndex, 100); // 100 USDC (example)
Method DriftClient.convertToSpotPrecisionReference ↗
ParameterTypeRequired
marketIndex
number
Yes
amount
any
Yes
Returns
BN

Get your wallet’s associated token account for a given spot market mint.

const marketIndex = 0; const ata = await driftClient.getAssociatedTokenAccount(marketIndex); console.log(ata.toBase58());
Method DriftClient.getAssociatedTokenAccountReference ↗
ParameterTypeRequired
marketIndex
number
Yes
useNative
boolean
No
tokenProgram
PublicKey
No
authority
PublicKey
No
allowOwnerOffCurve
boolean
No
Returns
Promise<PublicKey>

Deposit

Deposit tokens from your wallet ATA into your Drift subaccount’s spot balance.

const marketIndex = 0; const amount = driftClient.convertToSpotPrecision(marketIndex, 100); const associatedTokenAccount = await driftClient.getAssociatedTokenAccount(marketIndex); await driftClient.deposit(amount, marketIndex, associatedTokenAccount);
Method DriftClient.depositReference ↗
ParameterTypeRequired
amount
any
to deposit
Yes
marketIndex
number
spot market index to deposit into
Yes
associatedTokenAccount
PublicKey
can be the wallet public key if using native sol
Yes
subAccountId
number
subaccountId to deposit
No
reduceOnly
boolean
if true, deposit must not increase account risk
No
txParams
TxParams
transaction parameters
No
initSwiftAccount
boolean
if true, initialize a swift account for the user
No
overrides
{ authority?: PublicKey; }
allows overriding authority for the deposit transaction
No
Returns
Promise<string>

Withdraw

Withdraw tokens from your Drift spot balance back to your wallet ATA (subject to free collateral checks).

const marketIndex = 0; const amount = driftClient.convertToSpotPrecision(marketIndex, 100); const associatedTokenAccount = await driftClient.getAssociatedTokenAccount(marketIndex); await driftClient.withdraw(amount, marketIndex, associatedTokenAccount);
Method DriftClient.withdrawReference ↗
ParameterTypeRequired
amount
any
Yes
marketIndex
number
Yes
associatedTokenAddress
PublicKey
the token account to withdraw to. can be the wallet public key if using native sol
Yes
reduceOnly
boolean
No
subAccountId
number
No
txParams
TxParams
No
updateFuel
boolean
No
Returns
Promise<string>

Spot rates (borrow / lend)

Calculate the current lending APY-style rate paid to depositors for a spot market.

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 ↗
ParameterTypeRequired
bank
SpotMarketAccount
Yes
delta
any
No
currentUtilization
any
No
Returns
BN

Calculate the current borrow rate charged to borrowers for a spot market.

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 ↗
ParameterTypeRequired
bank
SpotMarketAccount
Yes
delta
any
No
currentUtilization
any
No
Returns
BN
Last updated on