bot-engine 0.1.0

Trading bot engine: order manager, inventory, event routing
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! Account syncer: syncs account state snapshots to upstream API.
//!
//! This module provides async HTTP syncing of clearinghouse state snapshots
//! to an external API for Arbitrage and other snapshot-based strategies.

use bot_core::{now_ms, AccountState};
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::time::Duration;

use crate::performance_metrics::PerformanceMetricsSnapshot;

/// Configuration for account syncing
#[derive(Debug, Clone)]
pub struct AccountSyncerConfig {
    /// Bot ID for upstream API
    pub bot_id: String,
    /// Upstream API base URL (e.g., `<https://api.example.com/bot-api>`)
    pub upstream_url: String,
    /// Sync interval in milliseconds (default: 10000)
    pub sync_interval_ms: u64,
    /// HTTP timeout in seconds
    pub timeout_secs: u64,
    /// Maximum retries for failed syncs
    pub max_retries: u32,
    /// Initial retry delay in milliseconds
    pub retry_delay_ms: u64,
    /// Optional shared secret for upstream sync auth.
    pub sync_secret: Option<String>,
}

impl Default for AccountSyncerConfig {
    fn default() -> Self {
        Self {
            bot_id: String::new(),
            upstream_url: String::new(),
            sync_interval_ms: 10_000,
            timeout_secs: 10,
            max_retries: 3,
            retry_delay_ms: 1000,
            sync_secret: None,
        }
    }
}

/// Request payload for clearinghouse state sync API
#[derive(Debug, Clone, Serialize)]
pub struct ClearingHouseStateRequest {
    /// Account value as a decimal string.
    pub account_value: String,
    /// Unrealized PnL as a decimal string.
    pub unrealized_pnl: String,
    /// Position payloads.
    pub positions: Vec<PositionInfo>,
    /// Timestamp in seconds.
    pub ts: i64,
    /// Whether this request marks bot shutdown.
    pub stop_bot: bool,
    /// Shutdown reason when `stop_bot` is true.
    pub stop_reason: String,
    /// Optional metadata for strategy-specific data
    #[serde(skip_serializing_if = "Option::is_none")]
    pub metadata: Option<serde_json::Value>,
}

/// Position information for sync
#[derive(Debug, Clone, Serialize)]
pub struct PositionInfo {
    /// Instrument ID.
    pub instrument_id: String,
    /// Position quantity string.
    pub qty: String,
    /// Entry price string.
    pub entry_px: String,
    /// Unrealized PnL string.
    pub unrealized_pnl: String,
}

/// Response from clearinghouse state sync API
#[derive(Debug, Clone, Deserialize)]
pub struct SyncResponse {
    /// Upstream-calculated PnL.
    pub pnl: f64,
}

/// Error types for account syncing
#[derive(Debug, thiserror::Error)]
pub enum SyncError {
    /// HTTP request failed after construction.
    #[error("HTTP request failed: {0}")]
    Http(String),

    /// Network transport failure.
    #[error("Network error: {0}")]
    Network(String),

    /// Response parsing failed.
    #[error("Parse error: {0}")]
    Parse(String),

    /// Upstream API returned a non-success status.
    #[error("API error: status={status}, body={body}")]
    Api {
        /// HTTP status code.
        status: u16,
        /// Response body.
        body: String,
    },

    /// Invalid syncer configuration.
    #[error("Configuration error: {0}")]
    Config(String),

    /// Retry policy was exhausted.
    #[error("Max retries exceeded")]
    MaxRetries,
}

impl From<reqwest::Error> for SyncError {
    fn from(e: reqwest::Error) -> Self {
        if e.is_timeout() {
            SyncError::Network("Request timeout".to_string())
        } else if e.is_connect() {
            SyncError::Network(format!("Connection failed: {}", e))
        } else {
            SyncError::Http(e.to_string())
        }
    }
}

/// Result of a sync operation
#[derive(Debug, Clone)]
pub struct SyncResult {
    /// Whether the upstream sync succeeded.
    pub success: bool,
    /// Upstream-calculated PnL, if returned.
    pub pnl: Option<f64>,
}

/// Account syncer - handles syncing clearinghouse state to upstream API
pub struct AccountSyncer {
    config: AccountSyncerConfig,
    client: Client,
    /// Last successful sync timestamp
    last_sync_ts: i64,
    /// Last sync PnL
    last_pnl: Option<f64>,
    /// Latest performance metrics to include in upstream metadata
    metrics_snapshot: Option<PerformanceMetricsSnapshot>,
}

impl AccountSyncer {
    /// Create a new account syncer with the given configuration
    pub fn new(config: AccountSyncerConfig) -> Result<Self, SyncError> {
        if config.bot_id.is_empty() {
            return Err(SyncError::Config("bot_id is required".to_string()));
        }
        if config.upstream_url.is_empty() {
            return Err(SyncError::Config("upstream_url is required".to_string()));
        }

        let client = Client::builder()
            .timeout(Duration::from_secs(config.timeout_secs))
            .build()
            .map_err(|e| SyncError::Http(e.to_string()))?;

        tracing::info!("[AccountSyncer] Initialized for bot_id={}", config.bot_id);

        Ok(Self {
            config,
            client,
            last_sync_ts: 0,
            last_pnl: None,
            metrics_snapshot: None,
        })
    }

    /// Check if it's time to sync (based on interval)
    pub fn should_sync(&self) -> bool {
        let now = now_ms();
        now - self.last_sync_ts >= self.config.sync_interval_ms as i64
    }

    /// Get the last known PnL
    pub fn last_pnl(&self) -> Option<f64> {
        self.last_pnl
    }

    /// Set the latest performance metrics snapshot for sync metadata.
    pub fn set_metrics_snapshot(&mut self, snapshot: Option<PerformanceMetricsSnapshot>) {
        self.metrics_snapshot = snapshot;
    }

    /// Sync account state to upstream API
    ///
    /// Returns the PnL from the upstream API on success
    pub async fn sync(
        &mut self,
        account_state: &AccountState,
        stop_bot: bool,
        stop_reason: &str,
    ) -> Result<SyncResult, SyncError> {
        let now = now_ms();

        // Convert AccountState to request format
        let positions: Vec<PositionInfo> = account_state
            .positions
            .iter()
            .map(|p| PositionInfo {
                instrument_id: p.instrument.0.clone(),
                qty: p.qty.to_string(),
                entry_px: p
                    .avg_entry_px
                    .map(|px| px.0.to_string())
                    .unwrap_or_else(|| "0".to_string()),
                unrealized_pnl: p
                    .unrealized_pnl
                    .map(|pnl| pnl.to_string())
                    .unwrap_or_else(|| "0".to_string()),
            })
            .collect();

        let request = ClearingHouseStateRequest {
            account_value: account_state
                .account_value
                .map(|v| v.to_string())
                .unwrap_or_else(|| "0".to_string()),
            unrealized_pnl: account_state
                .unrealized_pnl
                .map(|pnl| pnl.to_string())
                .unwrap_or_else(|| "0".to_string()),
            positions,
            ts: now / 1000, // seconds
            stop_bot,
            stop_reason: stop_reason.to_string(),
            metadata: self.metadata_payload(),
        };

        tracing::info!(
            "[AccountSyncer] Syncing account state: positions={}, pnl={:?}",
            account_state.positions.len(),
            account_state.unrealized_pnl
        );

        // Execute with retry
        let response = self.execute_with_retry(&request).await?;

        // Update state
        self.last_sync_ts = now;
        self.last_pnl = Some(response.pnl);

        tracing::info!("[AccountSyncer] Sync successful: pnl={:.4}", response.pnl);

        Ok(SyncResult {
            success: true,
            pnl: Some(response.pnl),
        })
    }

    fn metadata_payload(&self) -> Option<serde_json::Value> {
        self.metrics_snapshot.as_ref().map(|snapshot| {
            serde_json::json!({
                "performance_metrics": snapshot
            })
        })
    }

    /// Execute sync request with retry logic
    async fn execute_with_retry(
        &self,
        request: &ClearingHouseStateRequest,
    ) -> Result<SyncResponse, SyncError> {
        let url = format!(
            "{}/sync-clearingHouseState/{}",
            self.config.upstream_url.trim_end_matches('/'),
            self.config.bot_id
        );

        let mut last_error: Option<SyncError> = None;
        let mut delay_ms = self.config.retry_delay_ms;

        for attempt in 1..=self.config.max_retries {
            tracing::debug!(
                "[AccountSyncer] Sync attempt {}/{} to {}",
                attempt,
                self.config.max_retries,
                url
            );

            match self.execute_request(&url, request).await {
                Ok(response) => return Ok(response),
                Err(e) => {
                    tracing::warn!(
                        "[AccountSyncer] Sync attempt {}/{} failed: {}",
                        attempt,
                        self.config.max_retries,
                        e
                    );
                    last_error = Some(e);

                    if attempt < self.config.max_retries {
                        tracing::debug!("[AccountSyncer] Retrying in {}ms...", delay_ms);
                        tokio::time::sleep(Duration::from_millis(delay_ms)).await;
                        delay_ms *= 2; // exponential backoff
                    }
                }
            }
        }

        Err(last_error.unwrap_or(SyncError::MaxRetries))
    }

    /// Execute a single sync request
    async fn execute_request(
        &self,
        url: &str,
        request: &ClearingHouseStateRequest,
    ) -> Result<SyncResponse, SyncError> {
        let mut request_builder = self
            .client
            .post(url)
            .header("Content-Type", "application/json");
        if let Some(secret) = self.config.sync_secret.as_deref().filter(|s| !s.is_empty()) {
            request_builder = request_builder.header("x-bot-sync-secret", secret);
        }
        let response = request_builder.json(request).send().await?;

        let status = response.status();
        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(SyncError::Api {
                status: status.as_u16(),
                body,
            });
        }

        let sync_response: SyncResponse = response
            .json()
            .await
            .map_err(|e| SyncError::Parse(e.to_string()))?;

        Ok(sync_response)
    }

    /// Perform final sync on shutdown (with stop_bot=true)
    pub async fn shutdown_sync(
        &mut self,
        account_state: &AccountState,
        stop_reason: &str,
    ) -> Result<SyncResult, SyncError> {
        tracing::info!(
            "[AccountSyncer] Performing shutdown sync with reason: {}",
            stop_reason
        );
        self.sync(account_state, true, stop_reason).await
    }
}

// Implement AccountSync trait for drop-in substitution with MockAccountSyncer
#[async_trait::async_trait]
impl crate::sync_traits::AccountSync for AccountSyncer {
    fn should_sync(&self) -> bool {
        AccountSyncer::should_sync(self)
    }

    fn last_pnl(&self) -> Option<f64> {
        AccountSyncer::last_pnl(self)
    }

    fn set_metrics_snapshot(
        &mut self,
        snapshot: Option<crate::performance_metrics::PerformanceMetricsSnapshot>,
    ) {
        AccountSyncer::set_metrics_snapshot(self, snapshot)
    }

    async fn sync(
        &mut self,
        account_state: &AccountState,
        stop_bot: bool,
        stop_reason: &str,
    ) -> Result<crate::sync_traits::AccountSyncResult, SyncError> {
        let result = AccountSyncer::sync(self, account_state, stop_bot, stop_reason).await?;
        Ok(crate::sync_traits::AccountSyncResult {
            success: result.success,
            pnl: result.pnl,
        })
    }

    async fn shutdown_sync(
        &mut self,
        account_state: &AccountState,
        stop_reason: &str,
    ) -> Result<crate::sync_traits::AccountSyncResult, SyncError> {
        let result = AccountSyncer::shutdown_sync(self, account_state, stop_reason).await?;
        Ok(crate::sync_traits::AccountSyncResult {
            success: result.success,
            pnl: result.pnl,
        })
    }
}

#[cfg(test)]

mod tests {
    use super::*;
    use crate::performance_metrics::{
        PerformanceBenchmark, PerformanceMetrics, PerformanceMetricsSnapshot,
    };

    fn make_metrics_snapshot() -> PerformanceMetricsSnapshot {
        PerformanceMetricsSnapshot {
            schema_version: 1,
            mode: "paper".to_string(),
            scope: "current_run".to_string(),
            run_started_at_ms: Some(1),
            metrics: PerformanceMetrics {
                period_return_pct: Some(1.0),
                apr_pct: Some(365.0),
                sharpe: Some(1.5),
                max_drawdown_pct: Some(0.5),
                max_drawdown_usdc: "5".to_string(),
                win_rate_pct: Some(100.0),
                closed_trade_count: 1,
                winning_trade_count: 1,
                losing_trade_count: 0,
                fill_count: 2,
                total_fees: "0.2".to_string(),
                total_volume: "200".to_string(),
                net_pnl: "10".to_string(),
                fee_drag_pct: Some(0.02),
            },
            benchmark: PerformanceBenchmark {
                start_ts_ms: Some(1),
                end_ts_ms: Some(2),
                duration_ms: Some(1),
                quote_count: 2,
                starting_balance_usdc: Some("1000".to_string()),
                ending_balance_usdc: Some("1010".to_string()),
                instrument: Some("BTC-PERP".to_string()),
            },
            latest_equity: None,
        }
    }

    #[test]
    fn test_config_validation() {
        // Empty bot_id should fail
        let config = AccountSyncerConfig {
            bot_id: String::new(),
            upstream_url: "http://test.com".to_string(),
            ..Default::default()
        };
        assert!(AccountSyncer::new(config).is_err());

        // Empty upstream_url should fail
        let config = AccountSyncerConfig {
            bot_id: "test-bot".to_string(),
            upstream_url: String::new(),
            ..Default::default()
        };
        assert!(AccountSyncer::new(config).is_err());

        // Valid config should succeed
        let config = AccountSyncerConfig {
            bot_id: "test-bot".to_string(),
            upstream_url: "http://test.com".to_string(),
            ..Default::default()
        };
        assert!(AccountSyncer::new(config).is_ok());
    }

    #[test]
    fn test_metadata_payload_includes_performance_metrics() {
        let config = AccountSyncerConfig {
            bot_id: "test-bot".to_string(),
            upstream_url: "http://test.com".to_string(),
            ..Default::default()
        };
        let mut syncer = AccountSyncer::new(config).unwrap();
        syncer.set_metrics_snapshot(Some(make_metrics_snapshot()));

        let metadata = syncer.metadata_payload().expect("metadata");
        assert_eq!(
            metadata["performance_metrics"]["metrics"]["net_pnl"],
            serde_json::json!("10")
        );
        assert_eq!(
            metadata["performance_metrics"]["mode"],
            serde_json::json!("paper")
        );
    }
}