Backend Operations
Off-chain validation, keeper coordination, and server operations
Backend operations complement the on-chain protocol with off-chain validation, data indexing, and keeper coordination.
Architecture Overview
The backend consists of multiple specialized servers:
| Server | Purpose | Public |
|---|---|---|
| API Server | REST API for market data and account info | Yes |
| WebSocket Server | Real-time market data streams | Yes |
| RFQ Server | Request-for-quote handling | Yes (with auth) |
| Operator Server | Transaction relay and matching | No |
| Keeper Server | Automated liquidations and settlements | No |
See Backend Architecture for detailed server descriptions.
Pre-Trade Margin Validation
Before any trade is submitted on-chain, the backend validates:
- Portfolio Health Check: Verify post-trade equity ≥ initial margin
- Stress Test: Ensure portfolio survives 4-corner stress scenarios
- Deposit Sufficiency: Confirm sufficient deposit for premium obligations
Validation Flow
User → Submit Order → Backend Validation → On-Chain Submission
↓
[Check Failed]
↓
Return Error (No Gas Spent)This prevents users from spending gas on transactions that would revert.
Margin Check Endpoint
// POST /helpers/validate-trade
{
"portfolioId": "123",
"trade": {
"seriesId": "...",
"size": "1000000000000000000",
"isBuy": true
}
}Response includes:
preTradeHealth: Current portfolio statepostTradeHealth: Projected state after tradeisValid: Whether trade passes validationerrors: Specific validation failures
Settlement Readiness Checks
The backend continuously monitors portfolios for settlement readiness:
- Identify Expiring Positions: Find positions within 24 hours of expiry
- Calculate Obligations: Determine worst-case settlement obligations
- Check Cash Coverage: Verify deposited cash ≥ obligations
- Trigger Liquidation: Alert keepers if liquidation is needed
Liquidation Alerts
When a portfolio fails settlement readiness:
// Alert payload
{
"portfolioId": "123",
"reason": "INSUFFICIENT_CASH",
"obligation": "8000000000", // $80,000 in USDC decimals
"cash": "2000000000", // $20,000 in USDC decimals
"expiry": "1736409600",
"recommendedAction": "LIQUIDATE_TO_TARGET"
}Keeper Coordination
The backend coordinates keeper operations without controlling them:
- Price Monitoring: Track oracle prices and identify snapshot opportunities
- Liquidation Opportunities: Broadcast liquidatable portfolios to registered keepers
- Settlement Triggers: Monitor expiry times and alert keepers
Keeper Registration
Keepers register with the backend to receive:
- Real-time liquidation opportunities
- Settlement timing alerts
- Gas price recommendations
Data Indexing
The indexer (Ponder) provides:
- Portfolio State: Current positions, balances, and health
- Historical Data: Past trades, settlements, and liquidations
- Market Data: Orderbook state, recent trades, oracle prices
Query Patterns
Common indexed queries:
# Get portfolio positions
query {
portfolio(id: "123") {
positions {
series {
symbol
expiry
strike
}
optionBalance
entryMark
}
collateral
health
}
}Error Handling
Backend validation errors prevent on-chain reverts:
| Error Code | Cause | Resolution |
|---|---|---|
INSUFFICIENT_MARGIN | Post-trade equity < IM | Deposit more collateral or reduce position size |
STRESS_TEST_FAILED | 4-corner scenario loss exceeds threshold | Reduce position concentration |
INSUFFICIENT_DEPOSIT | Deposit < premium obligation | Deposit more USDC |
INVALID_SERIES | Series does not exist | Check series ID |
EXPIRED_SERIES | Series already expired | Cannot trade expired options |
Related
- Keepers - Keeper operations guide
- Market Makers - MMM operations
- Backend Architecture - Server details
- API Reference - Endpoint documentation