2026-03-16 01:09:32 +03:00

104 lines
3.7 KiB
Plaintext

# 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.
<SDKDoc>
<TypeScript name="convertToSpotPrecision" type="method" owner="DriftClient">
```js
// marketIndex 0 is commonly USDC
const marketIndex = 0;
const amount = driftClient.convertToSpotPrecision(marketIndex, 100); // 100 USDC (example)
```
</TypeScript>
</SDKDoc>
Get your wallet's associated token account for a given spot market mint.
<SDKDoc>
<TypeScript name="getAssociatedTokenAccount" type="method" owner="DriftClient">
```js
const marketIndex = 0;
const ata = await driftClient.getAssociatedTokenAccount(marketIndex);
console.log(ata.toBase58());
```
</TypeScript>
</SDKDoc>
### Deposit
Deposit tokens from your wallet ATA into your Drift subaccount's spot balance.
<SDKDoc>
<TypeScript name="deposit" type="method" owner="DriftClient">
```js
const marketIndex = 0;
const amount = driftClient.convertToSpotPrecision(marketIndex, 100);
const associatedTokenAccount = await driftClient.getAssociatedTokenAccount(marketIndex);
await driftClient.deposit(amount, marketIndex, associatedTokenAccount);
```
</TypeScript>
</SDKDoc>
### Withdraw
Withdraw tokens from your Drift spot balance back to your wallet ATA (subject to free collateral checks).
<SDKDoc>
<TypeScript name="withdraw" type="method" owner="DriftClient">
```js
const marketIndex = 0;
const amount = driftClient.convertToSpotPrecision(marketIndex, 100);
const associatedTokenAccount = await driftClient.getAssociatedTokenAccount(marketIndex);
await driftClient.withdraw(amount, marketIndex, associatedTokenAccount);
```
</TypeScript>
</SDKDoc>
### Spot rates (borrow / lend)
Calculate the current lending APY-style rate paid to depositors for a spot market.
<SDKDoc>
<TypeScript name="calculateDepositRate" type="function">
```js
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));
```
</TypeScript>
</SDKDoc>
Calculate the current borrow rate charged to borrowers for a spot market.
<SDKDoc>
<TypeScript name="calculateBorrowRate" type="function">
```js
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));
```
</TypeScript>
</SDKDoc>