Skip to main content
MarginAccounts holds all trader collateral (USDC) and manages per-position margin locking. When a position opens, the required initial margin is locked from the trader’s deposited collateral. When a position closes, margin is unlocked and PnL is settled.

Contract Relationships

Read Functions

collateralBalance

function collateralBalance(address account) external view returns (uint256)
Returns total deposited collateral for an account (locked + available).

imLockedTotal

function imLockedTotal(address account) external view returns (uint256)
Returns total locked initial margin across all of an account’s open positions.

imLockedByPosition

function imLockedByPosition(uint256 positionId) external view returns (uint256)
Returns locked margin for a specific position.

globalImLocked

function globalImLocked() external view returns (uint256)
Returns total locked margin across all accounts in the system.

availableBalance

function availableBalance(address account) external view returns (uint256)
Returns free collateral: collateralBalance - imLockedTotal. This is the maximum amount the trader can withdraw or allocate to new positions.

equity

function equity(address account) external view returns (int256)
Returns account equity: collateral plus unrealized PnL across all open positions. Can be negative if unrealized losses exceed collateral.

collateralToken

function collateralToken() external view returns (address)
Returns the address of the collateral ERC-20 token (USDC).

collateralDecimals

function collateralDecimals() external view returns (uint8)
Returns the decimal places of the collateral token (6 for USDC).

Write Functions

deposit

function deposit(uint256 amount) external
Deposits USDC into the caller’s margin account. Requires prior ERC-20 approval.
NameTypeDescription
amountuint256USDC amount to deposit (6 decimals)

withdraw

function withdraw(uint256 amount) external
Withdraws available (unlocked) USDC from the caller’s margin account.
NameTypeDescription
amountuint256USDC amount to withdraw (6 decimals)
Validation checks:
#CheckRevert Error
1amount > 0ZeroAmount
2amount <= availableBalanceWithdrawalExceedsAvailable

Protocol Functions

These functions are called by PositionManager and SettlementEngine — not directly by traders.

lockMargin

function lockMargin(address account, uint256 positionId, uint256 amount) external
Locks initial margin from free collateral when a position is opened.

settlePosition

function settlePosition(address account, uint256 positionId, uint256 marginToRelease, int256 pnl, uint256 fee) external
Unlocks margin and applies PnL when a position is fully or partially closed. For partial closes, marginToRelease is proportional to the reduction.

transferOut

function transferOut(address to, uint256 amount) external
Transfers USDC out of MarginAccounts during settlement (e.g., trader losses to PoolVault).

addMarginToPosition

function addMarginToPosition(address account, uint256 positionId, uint256 amount) external
Moves free collateral into a position’s locked margin (called via PositionManager.addPositionMargin).

removeMarginFromPosition

function removeMarginFromPosition(address account, uint256 positionId, uint256 amount) external
Releases locked margin back to free collateral (called via PositionManager.removePositionMargin).

collectFee

function collectFee(address account, uint256 amount) external
Collects a fee from the account’s free (available) collateral.

collectFeeFromCollateral

function collectFeeFromCollateral(address account, uint256 amount) external
Collects a fee from total collateral, ignoring locked margin checks. Used for oracle fees during settlement when all margin may be locked.

Events

EventWhen Emitted
CollateralDepositedUSDC deposited into margin account
CollateralWithdrawnUSDC withdrawn from margin account
MarginLockedInitial margin locked for a new position
MarginUnlockedMargin released on position close
PnlSettledPnL applied to account after settlement
BadDebtTrader loss exceeds locked margin (pool absorbs shortfall)
MarginAddedToPositionAdditional margin locked for existing position
MarginRemovedFromPositionMargin removed from existing position
FeeCollectedFee deducted from account collateral

Margin Model

IM/MM factors and liquidation thresholds.

Position Manager

Opening positions and margin adjustments.

Settlement Engine

Settlement, liquidation, and PnL flows.