Purpose and Responsibilities
The Orderbook service acts as the protocol’s frontend interface:- Order Validation: Validates orders before accepting them, checking signatures, balances, token approvals, and order parameters
- Order Storage: Persists valid orders to PostgreSQL for retrieval by the Autopilot service
- Quote Generation: Provides price quotes for trading pairs using multiple price estimation backends
- Order Queries: Enables users to retrieve order status, trade history, and auction data
- App Data Registry: Stores and retrieves order metadata and IPFS content
API Overview
The Orderbook exposes a comprehensive REST API with endpoints for:POST /api/v1/orders- Submit new ordersPOST /api/v1/quote- Request trading quotesGET /api/v1/orders/{uid}- Retrieve order detailsGET /api/v1/account/{owner}/orders- List user ordersGET /api/v1/trades- Query historical tradesGET /api/v1/auction- Get current auction dataDELETE /api/v1/orders/{uid}- Cancel orders
For complete API documentation, see the OpenAPI specification available at the
/docs endpoint when running the service.Order Validation and Storage
Validation Process
The Orderbook performs extensive validation before accepting orders:- Signature verification: EIP-712, EIP-1271, or PreSign validation
- Balance checks: Ensures user has sufficient sell token balance
- Token approvals: Verifies allowance for the settlement contract
- Banned users: Checks against Chainalysis oracle and custom deny lists
- Token restrictions: Blocks unsupported or malicious tokens
- Order validity period: Enforces minimum and maximum validity durations
- Gas estimation: Validates orders won’t exceed gas limits
- Hooks validation: Verifies pre/post-interaction hooks are safe
Storage Architecture
Orders are stored in PostgreSQL with the following structure:- Primary database: Write operations go to the main database
- Read replica: Optional read replica for query operations to reduce load
- Indexed fields: Order UID, owner address, creation timestamp, status
PostgreSQL Integration
The service connects to PostgreSQL for persistent storage:- Order storage and retrieval
- Trade history tracking
- Native price caching
- App data registry
- Solver competition results
- Order event logging
Fee Estimation and Quoting
The Orderbook provides sophisticated quoting capabilities:Quote Types
Optimal Quotes: Full verification with best available pricesPrice Estimation
Quotes are generated using multiple price estimators:- External solvers: Query solver APIs for best prices
- Native price estimator: Estimates token prices in native currency (ETH)
- Gas price estimator: Calculates current gas costs
- Balance verification: Ensures user can execute the quote
Volume Fees
The service supports volume-based fee structures:Configuration Options
Command-Line Arguments
Key configuration parameters fromcrates/orderbook/src/arguments.rs:
| Argument | Environment Variable | Default | Description |
|---|---|---|---|
--bind-address | BIND_ADDRESS | 0.0.0.0:8080 | HTTP server address |
--db-write-url | DB_WRITE_URL | postgresql:// | Write database URL |
--db-read-url | DB_READ_URL | Same as write | Read replica URL |
--config | CONFIG | Required | Path to TOML config file |
Configuration File
The service requires a TOML configuration file with settings for:Running the Service
Basic Setup
Environment Variables
Docker
Key Dependencies
The Orderbook service relies on several key components:Internal Crates
shared: Common utilities for pricing, liquidity, and gas estimationdatabase: PostgreSQL abstraction and migrationsmodel: Order and API data modelscontracts: Smart contract bindings (Settlement, WETH, Signatures)price_estimation: Price estimation factory and backendsorder_validation: Order validation logic
External Services
- PostgreSQL: Primary data store
- Ethereum RPC node: Blockchain state queries
- Price estimation drivers: External solver APIs for quotes
- IPFS gateway: Optional app data storage
- Chainalysis oracle: Optional banned address detection
Smart Contracts
Monitoring and Observability
Metrics
The service exposes Prometheus metrics on the default metrics port:Logging
Structured logging with configurable levels:Health Checks
The service provides health check endpoints for monitoring:- Database connectivity check on startup
- Regular order insertion/retrieval tests
- Price estimator availability
Performance Considerations
Optimization tips:- Use a read replica for high query volumes
- Enable fast quoter for price-insensitive queries
- Configure appropriate quote timeouts
- Tune price estimator cache settings
- Monitor database query performance
Common Issues
Database Connection Errors
Ensure PostgreSQL is running and accessible:Order Validation Failures
Check logs for specific validation errors. Common issues:- Insufficient token balance
- Missing token approval
- Invalid signature
- Order validity period too short
