- REST under
/api/v1/margin/:parent-margin-account(+/collateral/transfer-in,/transfer-out),collateral/available,collateral/transfer,risk-buckets/*,sub-accounts/{marginAccountId}/collateral/transfer-in/transfer-out - SDK
sdk.marginAccounts(React:usePositions()for position state); gRPCMarginAccountsService - WebSocket
balances,movements, rawpositions:{tradingPairId},liquidations:{tradingPairId}
Collateral Model
Account Fields
maxRemovableCollateral and availableOrderCollateral are absent on parent rows and fallback reads, and availableOrderCollateral briefly after a transfer. Treat undefined as unavailable, not zero.
PENDING_LIQUIDATION: maintenance margin breached; new orders rejected while positions unwind. BAD_DEBT: post-liquidation deficit, equity may be negative; no new positions or transfers. Alert on both; poll position risk for live UI.
listMarginAccounts() (paged by nextPageToken) returns a row per margin account, closed ones flagged, plus a row per risk bucket with riskBucketId, marginMode, tradingPairId, strategyKey. A state filter matches riskBucketState, dropping the parent row.
Transfers
- No position-level margin method exists: a position’s margin is its bucket’s collateral, moved only to and from the parent.
- A bucket’s
withdrawableCollateral= allocation + realized PnL − funding paid − unrealized losses − initial margin; zero unlessNORMAL. - Parent → spot (and a withdrawal with source
margin) moves only unallocated collateral: whatever sits in a risk bucket, cross included, must be released to the parent first. A flat cross bucket keeps its allocation and realized PnL for its next position, so after a cross close the parent’swithdrawableCollateralis lower by that amount. Past the unallocated amount,400: when the cross allocation alone covers the gap,release the cross risk bucket's collateral to the margin account firston a transfer (release withtransferCollateralFromMarginAccountandmarginMode: "CROSS") and, on a withdrawal,source=margin withdrawal cannot withdraw cross risk bucket collateral; withdraw it with source=cross_risk_bucketwhen the cross bucket alone holds the whole amount (otherwise the release message); the isolated-bucket message when the isolated allocations together cover it,amount exceeds the margin account's withdrawable collateral of <amount>when it needs both, and the ordinary shortfall message past the whole balance. - A withdrawal with source
cross_risk_bucketreleases the amount from the default margin account’s cross risk bucket and withdraws it on-chain in one step, up to the cross bucket’swithdrawableCollateral. A refused request moves nothing. transferMarginCollateralruns a route in one step, up to three moves:{ marginAccountId, riskBucketId }to{ wallet: true }releases the bucket, moves to the default account and transfers out. A refused request moves nothing. Sub-accounts reach the wallet through the default account.- Every transfer takes an optional
idempotencyKey(shared with order create): a matching retry within 15 minutes returns the first result. See Retrying a transfer safely.
applyToPositionMargin (isolated bucket with a position): omitted or true commits the amount to position margin — leverage drops, initial margin required rises by it, free collateral is unchanged. false keeps it spendable; use false in loops targeting a free-collateral level, or they never converge. Both equally improve the liquidation cushion; positionMarginCredited reports the outcome.
getAvailableCollateral({ asset }) returns walletAvailable, walletLocked (open orders, pending withdrawals), marginTransferable (max into margin) and marginAvailableCollateral (max out, = withdrawableCollateral). Never size from total spot balance.
Pre-Trade Simulation
Simulation, notavailableOrderCollateral, is the per-order authority. An order is charged the increase in the bucket’s worst-case reserve plus fees (a reducing order needs less or none), at per-market, per-bracket rates. A leverage above the order’s riskTiers bracket is rejected, not capped; read getPerpMarketConfig().riskTiers first. See simulation rules.
Margin Sub-Accounts
One margin account per key per application;default (the parent) is the only one deposits, withdrawals and wallet transfers touch. A loss in one sub-account never draws on another.
- Keys: up to 64 letters, digits,
_ - . :. - Open sub-accounts are capped by weighted 14-day volume tier (10 rising to 50, or an admin override); lifetime distinct keys at three times that. Re-opening a closed key never counts toward the lifetime cap.
- Name a sub-account by
marginAccountIdor by its key asstrategyKey; for per-strategy accounting give each strategy its own sub-account (risk buckets are scoped by pair only). Risk-bucket methods takemarginAccountId. Reads:getMarginAccountPnl(id),sdk.portfolio.getMargin({ includeAccounts: true }). - Close needs no position and no resting, conditional or TWAP order; buckets close and collateral returns to
default. A bucket in liquidation or bad debt blocks it. Re-creating the key re-opens it. - Private order events carry
marginAccountId; a delegated agent with one id inallowedMarginAccountIdstrades only that account.
Movements
getParentMarginAccountMovements({ pageSize, movementType }) lists collateral transfers. PnL, funding, fees and on-chain margin deposits/withdrawals (referenceType margin_account_deposit / margin_account_withdrawal) are in user movements and funding history.
