Skip to main content

sandbox_quant/portfolio/
sync.rs

1use crate::error::exchange_error::ExchangeError;
2use crate::exchange::facade::ExchangeFacade;
3use crate::portfolio::store::PortfolioStateStore;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub struct SyncReport {
7    pub positions: usize,
8    pub open_order_groups: usize,
9    pub balances: usize,
10}
11
12#[derive(Debug, Default)]
13pub struct PortfolioSyncService;
14
15impl PortfolioSyncService {
16    /// Refreshes the authoritative portfolio snapshot from the exchange.
17    ///
18    /// Example:
19    /// - stale local store with outdated positions
20    /// - exchange returns fresh snapshot
21    /// - store is overwritten and marked `Fresh`
22    pub fn refresh_authoritative<E: ExchangeFacade<Error = ExchangeError>>(
23        &self,
24        exchange: &E,
25        store: &mut PortfolioStateStore,
26    ) -> Result<SyncReport, ExchangeError> {
27        store.refresh_from_exchange(exchange)?;
28        Ok(SyncReport {
29            positions: store.snapshot.positions.len(),
30            open_order_groups: store.snapshot.open_orders.len(),
31            balances: store.snapshot.balances.len(),
32        })
33    }
34
35    pub fn mark_market_data_stale(&self, store: &mut PortfolioStateStore) {
36        store.mark_market_data_stale();
37    }
38
39    pub fn mark_account_state_stale(&self, store: &mut PortfolioStateStore) {
40        store.mark_account_state_stale();
41    }
42
43    pub fn mark_reconciliation_stale(&self, store: &mut PortfolioStateStore) {
44        store.mark_reconciliation_stale();
45    }
46}