Skip to Content
DevelopersVelocity SDKDeposits & Withdrawals

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 ↗
ParameterTypeRequired
marketIndex
number
Spot market index whose token decimals determine the scale factor.
Yes
amount
any
UI 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 ↗
ParameterTypeRequired
marketIndex
number
Spot market index whose mint the token account is for.
Yes
useNative
boolean
If `true` (default) and the market is wrapped SOL, return `authority` directly instead of an ATA.
No
tokenProgram
PublicKey
Token program the mint belongs to; defaults to the classic SPL Token program.
No
authority
PublicKey
Wallet the token account belongs to; defaults to `this.wallet.publicKey`.
No
allowOwnerOffCurve
boolean
Passed 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 ↗
ParameterTypeRequired
amount
any
Amount to deposit, in the spot market's own token precision (`10^mint.decimals`, e.g. QUOTE_PRECISION (1e6) for USDC).
Yes
marketIndex
number
Spot market index to deposit into.
Yes
associatedTokenAccount
PublicKey
Source token account; can be the wallet's own public key when depositing native SOL into the wrapped-SOL market (see `getAssociatedTokenAccount`).
Yes
subAccountId
number
Sub-account id to credit; defaults to `this.activeSubAccountId`.
No
reduceOnly
boolean
If `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
TxParams
Optional transaction parameters; `computeUnits` is always forced to `800_000`.
No
initSwiftAccount
boolean
If `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 ↗
ParameterTypeRequired
amount
any
Amount to withdraw, in the spot market's own token precision (`10^mint.decimals`, e.g. QUOTE_PRECISION (1e6) for USDC).
Yes
marketIndex
number
Spot market index to withdraw from.
Yes
associatedTokenAddress
PublicKey
the token account to withdraw to. can be the wallet public key if using native sol
Yes
reduceOnly
boolean
If `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
number
Sub-account id to withdraw from; defaults to `this.activeSubAccountId`.
No
txParams
TxParams
Optional 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 ↗

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.

ParameterTypeRequired
bank
SpotMarketAccount
The spot market account
Yes
delta
any
Optional hypothetical change in token amount; positive adds to deposits, negative adds to borrows (see `calculateUtilization`)
No
currentUtilization
any
Precomputed 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 ↗

Alias for `calculateInterestRate` (annualized borrow rate).

ParameterTypeRequired
bank
SpotMarketAccount
The spot market account
Yes
delta
any
Optional hypothetical change in token amount (see `calculateUtilization`)
No
currentUtilization
any
Precomputed utilization, `SPOT_MARKET_UTILIZATION_PRECISION` (1e6)
No
Returns
BN
Last updated on