Deposits & Withdrawals
How it works
Velocity 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 the quote asset, 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 the quote asset, 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.
Spot markets on Velocity are collateral/borrow-lend only — they no longer support order-book trading (see Orders). Deposits, withdrawals, and internal transfers are unaffected by that change.
SDK Usage
Deposits/withdrawals move tokens between your wallet token accounts and your Velocity 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 the protocol's quote asset (dUSDT on devnet, USDC on mainnet-beta)
const marketIndex = 0;
const amount = velocityClient.convertToSpotPrecision(marketIndex, 100); // 100 units (example)Method VelocityClient.convertToSpotPrecisionReference ↗
Method VelocityClient.convertToSpotPrecisionReference ↗| Parameter | Type | Required |
|---|---|---|
marketIndex | numberSpot market index whose token decimals determine the scale factor. | Yes |
amount | anyUI amount to convert. | Yes |
| Returns |
|---|
BN |
Get your wallet’s associated token account for a given spot market mint.
const marketIndex = 0;
const ata = await velocityClient.getAssociatedTokenAccount(marketIndex);
console.log(ata.toBase58());Method VelocityClient.getAssociatedTokenAccountReference ↗
Method VelocityClient.getAssociatedTokenAccountReference ↗| Parameter | Type | Required |
|---|---|---|
marketIndex | numberSpot market index whose mint the token account is for. | Yes |
useNative | booleanIf `true` (default) and the market is wrapped SOL, return `authority` directly
instead of an ATA. | No |
tokenProgram | PublicKeyToken program the mint belongs to; defaults to the classic SPL Token program. | No |
authority | PublicKeyWallet the token account belongs to; defaults to `this.wallet.publicKey`. | No |
allowOwnerOffCurve | booleanPassed through to `getAssociatedTokenAddress`; set `true` for PDA owners. | No |
| Returns |
|---|
Promise<PublicKey> |
Deposit
Deposit tokens from your wallet ATA into your Velocity subaccount’s spot balance.
const marketIndex = 0;
const amount = velocityClient.convertToSpotPrecision(marketIndex, 100);
const associatedTokenAccount = await velocityClient.getAssociatedTokenAccount(marketIndex);
await velocityClient.deposit(amount, marketIndex, associatedTokenAccount);Method VelocityClient.depositReference ↗
Method VelocityClient.depositReference ↗| Parameter | Type | Required |
|---|---|---|
amount | anyAmount to deposit, in the spot market's own token precision (`10^mint.decimals`,
e.g. QUOTE_PRECISION (1e6) for USDC). | Yes |
marketIndex | numberSpot market index to deposit into. | Yes |
associatedTokenAccount | PublicKeySource token account; can be the wallet's own public key when
depositing native SOL into the wrapped-SOL market (see `getAssociatedTokenAccount`). | Yes |
subAccountId | numberSub-account id to credit; defaults to `this.activeSubAccountId`. | No |
reduceOnly | booleanIf `true`, caps the deposit so it cannot exceed what's needed to fully repay
an existing borrow (never opens/increases a deposit position); defaults to `false`. | No |
txParams | TxParamsOptional transaction parameters; `computeUnits` is always forced to `800_000`. | No |
initSwiftAccount | booleanIf `true`, also initializes the signer's `SignedMsgUserOrders` account
first if it doesn't exist; defaults to `false`. | No |
overrides | { authority?: PublicKey; } | No |
| Returns |
|---|
Promise<string> |
Withdraw
Withdraw tokens from your Velocity spot balance back to your wallet ATA (subject to free collateral checks).
const marketIndex = 0;
const amount = velocityClient.convertToSpotPrecision(marketIndex, 100);
const associatedTokenAccount = await velocityClient.getAssociatedTokenAccount(marketIndex);
await velocityClient.withdraw(amount, marketIndex, associatedTokenAccount);Method VelocityClient.withdrawReference ↗
Method VelocityClient.withdrawReference ↗| Parameter | Type | Required |
|---|---|---|
amount | anyAmount to withdraw, in the spot market's own token precision (`10^mint.decimals`,
e.g. QUOTE_PRECISION (1e6) for USDC). | Yes |
marketIndex | numberSpot market index to withdraw from. | Yes |
associatedTokenAddress | PublicKeythe token account to withdraw to. can be the wallet public key if using native sol | Yes |
reduceOnly | booleanIf `true`, caps the withdrawal so it can never open/increase a borrow; the
on-chain amount is clamped to `min(requested, max withdrawable within margin, existing deposit)`. | No |
subAccountId | numberSub-account id to withdraw from; defaults to `this.activeSubAccountId`. | No |
txParams | TxParamsOptional compute-unit/priority-fee overrides for the transaction. | 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 "@velocity-exchange/sdk";
const spotMarket = velocityClient.getSpotMarketAccount(0);
const rate = calculateDepositRate(spotMarket);
console.log(convertToNumber(rate, SPOT_MARKET_RATE_PRECISION));Function calculateDepositRateReference ↗
Function calculateDepositRateReference ↗Calculates the annualized deposit interest rate for a spot market, mirroring `calculate_deposit_rate` (velocity-rs). Lenders receive the borrow rate net of the insurance fund and protocol fee carveouts (`ifFeeFactor` + `protocolFeeFactor`, both `PERCENTAGE_PRECISION`), scaled down by utilization since only borrowed deposits earn interest.
| Parameter | Type | Required |
|---|---|---|
bank | SpotMarketAccountThe spot market account | Yes |
delta | anyOptional hypothetical change in token amount; positive adds to deposits,
negative adds to borrows (see `calculateUtilization`) | No |
currentUtilization | anyPrecomputed utilization, `SPOT_MARKET_UTILIZATION_PRECISION`
(1e6); if omitted it is derived from `bank` and `delta` | No |
| Returns |
|---|
BN |
Calculate the current borrow rate charged to borrowers for a spot market.
import { SPOT_MARKET_RATE_PRECISION, calculateBorrowRate, convertToNumber } from "@velocity-exchange/sdk";
const spotMarket = velocityClient.getSpotMarketAccount(0);
const rate = calculateBorrowRate(spotMarket);
console.log(convertToNumber(rate, SPOT_MARKET_RATE_PRECISION));Function calculateBorrowRateReference ↗
Function calculateBorrowRateReference ↗Alias for `calculateInterestRate` (annualized borrow rate).
| Parameter | Type | Required |
|---|---|---|
bank | SpotMarketAccountThe spot market account | Yes |
delta | anyOptional hypothetical change in token amount (see `calculateUtilization`) | No |
currentUtilization | anyPrecomputed utilization, `SPOT_MARKET_UTILIZATION_PRECISION` (1e6) | No |
| Returns |
|---|
BN |