CLOB Architecture
Balance Management
Fund custody and balance tracking in the CLOB system
Balance Management
The BalanceManager contract handles all fund custody and balance tracking within the CLOB system.
Balance States
Funds in the CLOB system exist in two states:
| State | Description |
|---|---|
| Available | Funds deposited but not committed to orders |
| Locked | Funds committed to open orders |
Deposits
Token Deposits
- Approve the BalanceManager to spend your tokens
- Call
deposit()through the CLOBRouter - Funds become available in your CLOB balance
ETH Deposits
For native ETH:
- Call
depositETH()with value - ETH is wrapped to WETH automatically
- WETH becomes available in your CLOB balance
Withdrawals
Call withdraw() through the CLOBRouter to move funds back to your wallet.
Important: Only available (unlocked) funds can be withdrawn. To withdraw locked funds, cancel the associated orders first.
Fund Locking
When you place an order:
- Required funds move from available to locked
- Locked funds are reserved for that specific order
When you cancel an order:
- Locked funds return to available
- You can then withdraw or use for new orders
Trade Settlement
When orders match:
- Locked funds transfer between buyer and seller
- Fees are deducted automatically
- Counterparty receives funds in their available balance
Fee Structure
| Fee Type | When Applied |
|---|---|
| Maker Fee | Order adds liquidity (resting order) |
| Taker Fee | Order removes liquidity (crossing order) |
Fees are deducted during settlement. The fee receiver accumulates fees from both sides of each trade.
Balance Queries
Available Balance
function getBalance(address user, address currency)
external view returns (uint256)Returns funds available for new orders or withdrawal.
Locked Balance
function getLockedBalance(
address user,
address operator,
address currency
) external view returns (uint256)Returns funds locked by a specific OrderBook.
Total Balance
A user's total funds = available + sum(locked across all pools)
Security
Fund Isolation
- Each user's funds are tracked separately
- Locked funds are isolated per OrderBook
- No cross-user fund access
Authorization
| Operation | Authorized Callers |
|---|---|
deposit | CLOBRouter |
withdraw | CLOBRouter |
lock | OrderBook |
unlock | OrderBook |
transferLockedFrom | OrderBook |
Error Handling
| Error | Cause | Resolution |
|---|---|---|
InsufficientBalance | Not enough available funds | Deposit more or cancel orders |
ZeroAmount | Attempted zero-value operation | Use non-zero amount |
UnauthorizedCaller | Caller not authorized | Use CLOBRouter |
TransferError | Token transfer failed | Check token approval/balance |
NativeTransferFailed | ETH transfer failed | Check recipient can receive ETH |
Events
| Event | Description |
|---|---|
Deposit | Funds deposited |
Withdrawal | Funds withdrawn |
Lock | Funds locked for order |
Unlock | Funds unlocked |
TransferFrom | Settlement transfer |
TransferLockedFrom | Locked fund transfer |