Skip to main content

hypercall_db/traits/
orders.rs

1//! Order persistence traits.
2//!
3//! Sync because order writes happen in the engine event loop (blocking context).
4//! Async order reads for the API go through `AnalyticsReader`.
5
6use anyhow::Result;
7use hypercall_types::{OrderUpdateMessage, WalletAddress};
8use std::collections::HashMap;
9
10use crate::{FillSideEffects, OrderInfoRecord, PersistOrderAction, PersistOrderInfo};
11
12/// Read-only order queries.
13pub trait OrderReader: Send + Sync {
14    /// Load materialized order infos, optionally filtered by wallet.
15    fn get_order_infos_sync(&self, wallet: Option<&WalletAddress>) -> Result<Vec<OrderInfoRecord>>;
16    /// Batch lookup of client_ids by order_ids (sync path).
17    fn get_client_ids_by_order_ids_sync(
18        &self,
19        order_ids: &[i64],
20    ) -> Result<HashMap<i64, Option<String>>>;
21    /// Return which of the given order IDs are in a terminal state.
22    fn get_terminal_order_ids_sync(&self, order_ids: &[i64]) -> Result<Vec<i64>>;
23    /// MAX(order_id) + 1 for sequence initialization.
24    fn get_max_order_id_sync(&self) -> Result<u64>;
25    /// MAX(trade_id) + 1 for sequence initialization.
26    fn get_max_trade_id_sync(&self) -> Result<u64>;
27}
28
29/// Order mutations.
30pub trait OrderWriter: OrderReader {
31    /// Persist static order info (upsert on order_id).
32    fn persist_order_info_sync(&self, info: &PersistOrderInfo) -> Result<()>;
33
34    /// Persist an order action audit trail entry.
35    fn persist_order_action_sync(&self, action: &PersistOrderAction) -> Result<()>;
36
37    /// Persist an order status update + materialize into order_infos.
38    fn persist_order_update_sync(&self, update: &OrderUpdateMessage) -> Result<()>;
39
40    /// Persist a fill atomically with trades + ledger side effects.
41    fn persist_fill_with_side_effects_sync(
42        &self,
43        fill: &hypercall_types::Fill,
44        side_effects: &FillSideEffects,
45    ) -> Result<(bool, bool)>;
46
47    /// Batch cancel orders for settlement.
48    fn batch_cancel_orders_for_settlement_sync(
49        &self,
50        order_ids: &[i64],
51        timestamp_ms: i64,
52    ) -> Result<usize>;
53
54    /// Cancel orphaned orders by symbol.
55    fn cancel_orphaned_orders_by_symbols_sync(&self, symbols: &[String]) -> Result<usize>;
56}