Margin
SportsPerp uses a two-point margin model familiar from every major perpetual DEX: an initial margin required to open a position, and a maintenance margin below which liquidation mechanisms engage. Both are expressed as a percentage of position size.
Initial margin
Initial margin is implicit in the leverage you choose. At 5x leverage, you post 20% of the notional position size as collateral; at 2x, you post 50%.
initial_margin = position_size / leverageThe maximum leverage available to you at open time is the minimum of three caps:
| Cap | Source | See |
|---|---|---|
| Market max leverage | Per-market parameter, 5x at launch | Contract Specifications |
| Tier max leverage | Scales down as your position grows relative to per-market effective_oi (= max(market_total_oi, initial_capacity)) | Margin Tiers |
| Confidence multiplier | Scales down as oracle uncertainty rises | This page, below |
So a trader on a 5x-max market who places a position sized at 30% of effective_oi (tier 4 → 2x) during a high-confidence-interval moment (0.4x multiplier) gets an effective max of min(5, 2, 0.4 × 5) = 0.8x. In practice, 0.8x will fall below the MIN_POSITION_SIZE check and the trade will be rejected. This is the protocol protecting both the trader and the protocol from opening a position that’s unlikely to end well.
Maintenance margin
Maintenance margin is the floor below which liquidation triggers. It is 20%, hard-coded as MAINTENANCE_MARGIN_BPS = 2000 in math/margin.rs.
The check is performed on every trade, every funding application, and every liquidation call:
margin_ratio = (collateral + unrealized_pnl) × 10000 / position_size
= effective_collateral × 10000 / position_sizemargin_ratio > 2000(> 20%) → position is healthy2000 ≥ margin_ratio > 1333(20% ≥ ratio > 13.33%) → Layer 1 partial liquidation eligiblemargin_ratio ≤ 1333(≤ 13.33%, which is 2/3 of maintenance) → Layer 2 backstop (or Layer 3 ADL if insurance depleted)
The 20% floor is chosen to give a partial liquidation room to work: closing 20% of a position at margin-ratio 20% restores the ratio meaningfully before the backstop threshold is hit. See Three-Layer Cascade for the full cascade.
Effective collateral
Effective collateral = deposited collateral + unrealized PnL (floored at 0).
// programs/obv-perps/src/math/margin.rs
pub fn effective_collateral(collateral: u64, pnl: i64) -> u64 {
let effective = (collateral as i64).saturating_add(pnl);
if effective <= 0 { 0 } else { effective as u64 }
}This is what the margin-ratio check uses. Consequences:
- Winning positions get easier to carry. A +20% PnL effectively doubles your maintenance-margin headroom.
- Losing positions accumulate risk even without drawdown. Funding payments reduce collateral. A position with modest negative PnL and a long holding period can enter the liquidation zone purely from funding.
- Maintenance is computed in real-time from mark price, not entry price. As the market moves, so does your margin ratio, continuously.
Confidence-based leverage scaling
The protocol automatically reduces max leverage when oracle confidence widens — typically during live matches when data is in flight, or in the first minutes after a fresh OBV update when models haven’t converged.
| Oracle confidence (bps) | Multiplier | Effect on 5x market |
|---|---|---|
| < 300 | 1.0x | 5x max |
| 300 – 500 | 0.8x | 4x max |
| 500 – 800 | 0.6x | 3x max |
| 800 – 1000 | 0.4x | 2x max |
| > 1000 | 0 (blocked) | Trading halted |
Implementation: confidence_leverage_multiplier in math/margin.rs.
This is not a governance parameter — it is a protocol invariant. A trader cannot override it. When in doubt the protocol de-risks automatically rather than requiring manual intervention.
Adding and removing collateral
Two instructions let you manage margin on an open position without closing it:
add_collateral— deposit more USDC to reduce leverage or reclaim headroom.withdraw_collateral— pull USDC out, subject to the maintenance-margin check not being violated.
Every margin transfer also resets the anti-manipulation baseline (last_margin_transfer_collateral). This matters for liquidation protection — a position can only be liquidated while profitable if it has lost ≥ 18.3% of effective collateral vs this baseline. See Three-Layer Cascade.
Fixed-point integer math
All margin arithmetic uses u64 and u128 integers with explicit scales — no floats. Overflow is checked on every multiplication. A trader can reproduce any margin computation bit-for-bit from open-source inputs using the @sportsperp/sdk math helpers, which mirror the Rust implementation.
Further reading
- Margin Tiers — how position size relative to per-market
effective_oicaps your leverage. - Entry Price & PnL — how PnL enters the effective-collateral calculation.
- Liquidations — what happens when the margin check fails.