quantoxide 0.5.4

Rust framework for developing, backtesting, and deploying Bitcoin futures trading strategies.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use std::{
    fmt,
    sync::{Arc, Mutex},
};

use async_trait::async_trait;
use tokio::{
    sync::broadcast::{self, error::RecvError},
    time,
};

use lnm_sdk::{api_v2::WebSocketClient, api_v3::RestClient};

use crate::{
    db::Database,
    shared::Lookback,
    sync::config::{SyncConfig, SyncControllerConfig},
    tui::{
        TuiControllerShutdown,
        error::{Result as TuiResult, TuiError},
    },
    util::AbortOnDropHandle,
};

use super::{
    error::{Result, SyncError},
    process::{SyncProcess, error::SyncProcessFatalError},
    state::{SyncReader, SyncReceiver, SyncStatus, SyncStatusManager, SyncTransmitter, SyncUpdate},
};

/// Controller for managing and monitoring a running synchronization process.
///
/// `SyncController` provides an interface to monitor the status of a sync process and perform
/// graceful shutdown operations. It holds a handle to the running sync task and coordinates
/// shutdown signals.
#[derive(Debug)]
pub struct SyncController {
    config: SyncControllerConfig,
    handle: Mutex<Option<AbortOnDropHandle<()>>>,
    shutdown_tx: broadcast::Sender<()>,
    status_manager: Arc<SyncStatusManager>,
}

impl SyncController {
    fn new(
        config: &SyncConfig,
        handle: AbortOnDropHandle<()>,
        shutdown_tx: broadcast::Sender<()>,
        status_manager: Arc<SyncStatusManager>,
    ) -> Arc<Self> {
        Arc::new(Self {
            config: config.into(),
            handle: Mutex::new(Some(handle)),
            shutdown_tx,
            status_manager,
        })
    }

    /// Returns a [`SyncReader`] interface for accessing sync status and updates.
    pub fn reader(&self) -> Arc<dyn SyncReader> {
        self.status_manager.clone()
    }

    /// Returns the [`SyncMode`] of the sync process.
    pub fn mode(&self) -> SyncMode {
        self.status_manager.mode()
    }

    /// Creates a new [`SyncReceiver`] for subscribing to sync status updates.
    pub fn update_receiver(&self) -> SyncReceiver {
        self.status_manager.update_receiver()
    }

    /// Returns the current [`SyncStatus`] as a snapshot.
    pub fn status_snapshot(&self) -> SyncStatus {
        self.status_manager.status_snapshot()
    }

    fn try_consume_handle(&self) -> Option<AbortOnDropHandle<()>> {
        self.handle
            .lock()
            .expect("`SyncController` mutex can't be poisoned")
            .take()
    }

    /// Tries to perform a clean shutdown of the sync process and consumes the task handle. If a
    /// clean shutdown fails, the process is aborted.
    ///
    /// This method can only be called once per controller instance.
    ///
    /// Returns an error if the process had to be aborted, or if it the handle was already consumed.
    pub async fn shutdown(&self) -> Result<()> {
        let Some(mut handle) = self.try_consume_handle() else {
            return Err(SyncError::SyncAlreadyShutdown);
        };

        if handle.is_finished() {
            let status = self.status_manager.status_snapshot();
            return Err(SyncError::SyncAlreadyTerminated(status));
        }

        self.status_manager.update(SyncStatus::ShutdownInitiated);

        let shutdown_send_res = self.shutdown_tx.send(()).map_err(|e| {
            handle.abort();
            SyncProcessFatalError::SendShutdownSignalFailed(e)
        });

        let shutdown_res = match shutdown_send_res {
            Ok(_) => {
                tokio::select! {
                    join_res = &mut handle => {
                        join_res.map_err(SyncProcessFatalError::SyncProcessTaskJoin)
                    }
                    _ = time::sleep(self.config.shutdown_timeout()) => {
                        handle.abort();
                        Err(SyncProcessFatalError::ShutdownTimeout)
                    }
                }
            }
            Err(e) => Err(e),
        };

        if let Err(e) = shutdown_res {
            let e_ref = Arc::new(e);
            self.status_manager.update(e_ref.clone().into());

            return Err(SyncError::SyncShutdownFailed(e_ref));
        }

        self.status_manager.update(SyncStatus::Shutdown);
        Ok(())
    }

    /// Waits until the sync process has stopped and returns the final status.
    ///
    /// This method blocks until the sync process reaches a stopped state, either through graceful
    /// shutdown or termination.
    pub async fn until_stopped(&self) -> SyncStatus {
        let mut sync_rx = self.update_receiver();

        let status = self.status_snapshot();
        if status.is_stopped() {
            return status;
        }

        loop {
            match sync_rx.recv().await {
                Ok(sync_update) => {
                    if let SyncUpdate::Status(status) = sync_update
                        && status.is_stopped()
                    {
                        return status;
                    }
                }
                Err(RecvError::Lagged(_)) => {
                    let status = self.status_snapshot();
                    if status.is_stopped() {
                        return status;
                    }
                }
                Err(RecvError::Closed) => return self.status_snapshot(),
            }
        }
    }
}

#[async_trait]
impl TuiControllerShutdown for SyncController {
    async fn tui_shutdown(&self) -> TuiResult<()> {
        self.shutdown().await.map_err(TuiError::SyncShutdownFailed)
    }
}

/// Synchronization mode that determines how price data is fetched and maintained.
///
/// The sync mode controls which data sources are used and how far back in time to fetch historical
/// data.
#[derive(Debug, Clone, Copy)]
pub enum SyncMode {
    /// Backfill mode: only fetches historical price data from REST API.
    ///
    /// This mode does not maintain live price feeds and is suitable for populating historical data
    /// in batch.
    Backfill,
    /// Live mode: maintains real-time price feeds via WebSocket.
    ///
    /// Includes a candle configuration to also fetch recent historical data before starting the
    /// live feed.
    Live(Option<Lookback>),
    /// Full mode: combines both backfill and live synchronization.
    ///
    /// Fetches complete historical data and then maintains real-time price feeds.
    Full,
}

impl SyncMode {
    /// Creates a backfill-only sync mode.
    pub fn backfill() -> Self {
        SyncMode::Backfill
    }

    /// Creates a live sync mode without historical lookback.
    pub fn live_no_lookback() -> Self {
        SyncMode::Live(None)
    }

    /// Creates a live sync mode with a specified lookback configuration.
    pub fn live_with_lookback(lookback: Lookback) -> Self {
        SyncMode::Live(Some(lookback))
    }

    /// Creates a full sync mode (both backfill and live).
    pub fn full() -> Self {
        SyncMode::Full
    }

    /// Returns whether this mode includes an active live price feed.
    ///
    /// Returns `true` for `Live` and `Full` modes, `false` for `Backfill`.
    pub fn live_feed_active(&self) -> bool {
        match self {
            SyncMode::Backfill => false,
            SyncMode::Live(_) => true,
            SyncMode::Full => true,
        }
    }
}

impl fmt::Display for SyncMode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            SyncMode::Backfill => write!(f, "Backfill"),
            SyncMode::Live(lookback) => match lookback {
                Some(lookback) => write!(f, "Live (lookback: {})", lookback),
                None => write!(f, "Live"),
            },
            SyncMode::Full => write!(f, "Full"),
        }
    }
}

pub(super) enum SyncModeInt {
    Backfill {
        api_rest: Arc<RestClient>,
    },
    LiveNoLookback {
        api_rest: Arc<RestClient>,
        api_ws: Arc<WebSocketClient>,
    },
    LiveWithLookback {
        api_rest: Arc<RestClient>,
        api_ws: Arc<WebSocketClient>,
        lookback: Lookback,
    },
    Full {
        api_rest: Arc<RestClient>,
        api_ws: Arc<WebSocketClient>,
    },
}

impl From<&SyncModeInt> for SyncMode {
    fn from(value: &SyncModeInt) -> Self {
        match value {
            SyncModeInt::Backfill { .. } => Self::Backfill,
            SyncModeInt::LiveNoLookback {
                api_rest: _,
                api_ws: _,
            } => Self::Live(None),
            SyncModeInt::LiveWithLookback {
                api_rest: _,
                api_ws: _,
                lookback,
            } => Self::Live(Some(*lookback)),
            SyncModeInt::Full { .. } => Self::Full,
        }
    }
}

/// Builder for configuring and starting a synchronization engine.
///
/// `SyncEngine` encapsulates the configuration, database connection, API clients, and sync mode.
/// The sync process is spawned when [`start`](Self::start) is called, and a [`SyncController`] is
/// returned for monitoring and management.
pub struct SyncEngine {
    config: SyncConfig,
    db: Arc<Database>,
    mode_int: SyncModeInt,
    status_manager: Arc<SyncStatusManager>,
    update_tx: SyncTransmitter,
}

impl SyncEngine {
    fn with_mode_int(
        config: impl Into<SyncConfig>,
        db: Arc<Database>,
        mode_int: SyncModeInt,
    ) -> Self {
        let (update_tx, _) = broadcast::channel::<SyncUpdate>(1_000);

        let mode = (&mode_int).into();
        let status_manager = SyncStatusManager::new(mode, update_tx.clone());

        Self {
            config: config.into(),
            db,
            mode_int,
            status_manager,
            update_tx,
        }
    }

    pub(crate) fn live_no_lookback(
        config: impl Into<SyncConfig>,
        db: Arc<Database>,
        api_rest: Arc<RestClient>,
        api_ws: Arc<WebSocketClient>,
    ) -> Self {
        let mode_int = SyncModeInt::LiveNoLookback { api_rest, api_ws };

        Self::with_mode_int(config, db, mode_int)
    }

    pub(crate) fn live_with_lookback(
        config: impl Into<SyncConfig>,
        db: Arc<Database>,
        api_rest: Arc<RestClient>,
        api_ws: Arc<WebSocketClient>,
        lookback: Lookback,
    ) -> Self {
        let mode_int = SyncModeInt::LiveWithLookback {
            api_rest,
            api_ws,
            lookback,
        };

        Self::with_mode_int(config, db, mode_int)
    }

    pub(crate) fn full(
        config: impl Into<SyncConfig>,
        db: Arc<Database>,
        api_rest: Arc<RestClient>,
        api_ws: Arc<WebSocketClient>,
    ) -> Self {
        let mode_int = SyncModeInt::Full { api_rest, api_ws };

        Self::with_mode_int(config, db, mode_int)
    }

    /// Creates a new sync engine with the specified configuration and mode.
    ///
    /// This constructor automatically initializes the required API clients based on the sync mode.
    pub fn new(
        config: impl Into<SyncConfig>,
        db: Arc<Database>,
        api_domain: impl ToString,
        mode: SyncMode,
    ) -> Result<Self> {
        let config: SyncConfig = config.into();
        let domain = api_domain.to_string();

        let api_rest = RestClient::new(&config, domain.clone()).map_err(SyncError::RestApiInit)?;
        let api_ws = WebSocketClient::new(&config, domain);

        let mode = match mode {
            SyncMode::Backfill => SyncModeInt::Backfill { api_rest },
            SyncMode::Live(lookback) => match lookback {
                Some(lookback) => SyncModeInt::LiveWithLookback {
                    api_rest,
                    api_ws,
                    lookback,
                },
                None => SyncModeInt::LiveNoLookback { api_rest, api_ws },
            },
            SyncMode::Full => SyncModeInt::Full { api_rest, api_ws },
        };

        Ok(Self::with_mode_int(config, db, mode))
    }

    /// Returns a reader interface for accessing sync status and updates.
    pub fn reader(&self) -> Arc<dyn SyncReader> {
        self.status_manager.clone()
    }

    /// Creates a new receiver for subscribing to sync status updates.
    pub fn update_receiver(&self) -> SyncReceiver {
        self.status_manager.update_receiver()
    }

    /// Returns the current synchronization status as a snapshot.
    pub fn status_snapshot(&self) -> SyncStatus {
        self.status_manager.status_snapshot()
    }

    /// Starts the synchronization process and returns a [`SyncController`] for managing it.
    ///
    /// This consumes the engine and spawns the sync task in the background.
    pub fn start(self) -> Arc<SyncController> {
        let (shutdown_tx, _) = broadcast::channel::<()>(1);

        let handle = SyncProcess::spawn(
            &self.config,
            self.db,
            self.mode_int,
            shutdown_tx.clone(),
            self.status_manager.clone(),
            self.update_tx,
        );

        SyncController::new(&self.config, handle, shutdown_tx, self.status_manager)
    }
}