# Order Processing System A multi-threaded system for processing crypto orders with WebSocket event handling and order state management. ## Architecture Overview ``` ┌────────────────┐ │ Process Flow │ └───────┬────────┘ │ ┌──────────────┴───────────────┐ ▼ ▼ ▼ ┌────────────┐ ┌─────────────┐ ┌─────────────┐ │ Prepare │ │ Process │ │ Inventory │ │ Thread │ │ Thread │ │ Thread │ └─────┬──────┘ └──────┬──────┘ └──────┬──────┘ │ │ │ │ │ │ Order Prep Transaction WebSocket & Validation Processing Monitoring ``` ## Quick Reference ```rust // Initialize system and process orders let result = process_orders( Arc::new(orderbook), Arc::new(provider), orders ).await; ``` ## Core Components ### Data Structures ```rust struct OrderRequest { mm_order_id: i32, order: Order, tx_request: TransactionRequest, } struct OrderPending { mm_order_id: i32, order: Order, tx_hash: Option>, } struct SuccessfulOrder { mm_order_id: i32, order: Order, kuru_order_id: U256, } ``` ### Main Processing Threads 1. **Prepare Thread** (`prepare_order_thread`) - Prepares orders for submission - Handles gas price updates - Retries failed transactions 2. **Process Thread** (`process_tx_thread`) - Submits transactions to blockchain - Tracks transaction status - Handles failed transactions 3. **Inventory Thread** (`inventory_thread`) - Manages WebSocket connections - Processes order events - Updates order statuses ### Communication Channels ```rust // Main channels tx_sender: Order preparation → Transaction processing order_id_sender: Transaction processing → Inventory failed_tx_sender: Failed transactions → Retry system // Control channel stop_signal: Global stop mechanism ``` ## WebSocket Events ```rust enum SocketEvents { OrderCreated, Trade, OrdersCancelled, } ``` ## Usage Example ```rust // Create shared components let orderbook = Arc::new(Orderbook::new(address, provider.clone())?); let provider = Arc::new(provider); // Prepare orders let orders = vec![ Order::Limit(limit_order), Order::Market(market_order), ]; // Process orders let result = process_orders( orderbook, provider, orders ).await; ``` ## Error Handling - Transaction failures trigger automatic retries - WebSocket disconnections are handled with reconnection logic - Failed orders are tracked and can be resubmitted - All errors are logged for monitoring ## Notes - Asynchronous processing for high throughput - Automatic gas price adjustments - Real-time order status updates - Thread-safe order management - Graceful shutdown handling - Built-in retry mechanism for failed transactions