Skip to main content

bot_engine/
sync_traits.rs

1//! Syncer traits for testing abstraction.
2//!
3//! These traits allow substituting real syncers with mock implementations
4//! for paper trading and testing scenarios.
5
6use bot_core::{AccountState, Fill};
7use rust_decimal::Decimal;
8
9use crate::account_syncer::SyncError;
10use crate::performance_metrics::PerformanceMetricsSnapshot;
11
12/// Common result for sync operations (trait-level abstraction)
13#[derive(Debug, Clone)]
14pub struct TradeSyncResult {
15    /// Whether the sync succeeded.
16    pub success: bool,
17    /// Upstream-calculated PnL, if returned.
18    pub pnl: Option<f64>,
19}
20
21/// Trait for trade-based syncing (Grid, MM strategies)
22///
23/// Syncers implementing this trait accept fills and sync them to upstream.
24#[async_trait::async_trait]
25pub trait TradeSync: Send + Sync {
26    /// Add a fill to the pending queue
27    fn add_fill(&mut self, fill: Fill);
28
29    /// Check if it's time to sync based on interval
30    fn should_sync(&self) -> bool;
31
32    /// Get count of pending fills
33    fn pending_count(&self) -> usize;
34
35    /// Get last known PnL
36    fn last_pnl(&self) -> Option<f64>;
37
38    /// Set the latest performance metrics snapshot to include in sync metadata.
39    fn set_metrics_snapshot(&mut self, _snapshot: Option<PerformanceMetricsSnapshot>) {}
40
41    /// Sync pending fills to upstream
42    async fn sync(
43        &mut self,
44        current_price: Option<Decimal>,
45        stop_bot: bool,
46        stop_reason: &str,
47    ) -> Result<TradeSyncResult, SyncError>;
48
49    /// Perform final sync on shutdown
50    async fn shutdown_sync(
51        &mut self,
52        current_price: Option<Decimal>,
53        stop_reason: &str,
54    ) -> Result<TradeSyncResult, SyncError>;
55}
56
57/// Common result for account sync operations
58#[derive(Debug, Clone)]
59pub struct AccountSyncResult {
60    /// Whether the sync succeeded.
61    pub success: bool,
62    /// Upstream-calculated PnL, if returned.
63    pub pnl: Option<f64>,
64}
65
66/// Trait for account snapshot syncing (Arbitrage strategies)
67///
68/// Syncers implementing this trait accept full account state snapshots.
69#[async_trait::async_trait]
70pub trait AccountSync: Send + Sync {
71    /// Check if it's time to sync based on interval
72    fn should_sync(&self) -> bool;
73
74    /// Get last known PnL
75    fn last_pnl(&self) -> Option<f64>;
76
77    /// Set the latest performance metrics snapshot to include in sync metadata.
78    fn set_metrics_snapshot(&mut self, _snapshot: Option<PerformanceMetricsSnapshot>) {}
79
80    /// Sync account state to upstream
81    async fn sync(
82        &mut self,
83        account_state: &AccountState,
84        stop_bot: bool,
85        stop_reason: &str,
86    ) -> Result<AccountSyncResult, SyncError>;
87
88    /// Perform final sync on shutdown
89    async fn shutdown_sync(
90        &mut self,
91        account_state: &AccountState,
92        stop_reason: &str,
93    ) -> Result<AccountSyncResult, SyncError>;
94}