deltadefi 1.1.4

The Rust SDK for DeltaDeFi
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
//! Market Stream Module
//!
//! This module provides WebSocket streaming functionality for real-time market data.
//! It enables subscribing to market depth, price changes, recent trades, and OHLC data.
//!
//! ## Automatic Reconnection
//!
//! All market streams support automatic reconnection with exponential backoff.
//! Use `subscribe_*_with_reconnect` methods for resilient connections.
//!
//! ## Example with Reconnection
//!
//! ```rust,ignore
//! use deltadefi::{DeltaDeFi, Stage, ReconnectConfig};
//!
//! let client = DeltaDeFi::new("api-key".to_string(), Stage::Staging, None)?;
//!
//! let config = ReconnectConfig::default().with_max_retries(10);
//! let (mut handle, mut receiver) = client.market_stream
//!     .subscribe_depth_with_reconnect("ADAUSDM", None, Some(config)).await?;
//!
//! while let Some(event) = receiver.recv().await {
//!     match event {
//!         MarketStreamEvent::Message(msg) => println!("Market data: {:?}", msg),
//!         MarketStreamEvent::Connected => println!("Connected to market stream!"),
//!         MarketStreamEvent::Reconnecting { attempt, delay_ms } => {
//!             println!("Reconnecting attempt {} in {}ms", attempt, delay_ms);
//!         }
//!         MarketStreamEvent::Disconnected { reason } => println!("Disconnected: {}", reason),
//!         MarketStreamEvent::MaxRetriesExceeded => break,
//!     }
//! }
//! ```

use std::time::Duration;

use futures_util::{SinkExt, StreamExt};
use tokio::sync::mpsc;
use tokio::time::timeout;
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
use whisky::WError;

use crate::client::stream::ReconnectConfig;
use crate::responses::stream::{
    MarketDepthMessage, MarketPriceMessage, MarketStreamMessage, OhlcMessage, Trade,
};

/// Events emitted by market streams, including connection lifecycle events.
#[derive(Debug, Clone)]
pub enum MarketStreamEvent {
    /// A message was received from the stream.
    Message(MarketStreamMessage),
    /// Successfully connected (or reconnected) to the stream.
    Connected,
    /// Connection was lost, attempting to reconnect.
    Reconnecting {
        /// Current attempt number (1-indexed)
        attempt: u32,
        /// Delay before this attempt (milliseconds)
        delay_ms: u64,
    },
    /// Disconnected from the stream.
    Disconnected {
        /// Reason for disconnection
        reason: String,
    },
    /// Maximum reconnection attempts exceeded.
    MaxRetriesExceeded,
}

/// Handle for controlling an active market stream connection.
///
/// This handle can be used to close the stream connection gracefully.
/// The connection will also be closed when this handle is dropped.
#[derive(Debug)]
pub struct MarketStreamHandle {
    close_tx: Option<mpsc::Sender<()>>,
}

impl MarketStreamHandle {
    /// Close the stream connection gracefully.
    pub async fn close(&mut self) {
        if let Some(tx) = self.close_tx.take() {
            let _ = tx.send(()).await;
        }
    }

    /// Check if the stream is still active.
    pub fn is_active(&self) -> bool {
        self.close_tx.is_some()
    }
}

impl Drop for MarketStreamHandle {
    fn drop(&mut self) {
        if let Some(tx) = self.close_tx.take() {
            let _ = tx.try_send(());
        }
    }
}

/// Internal result type for connection attempts
enum ConnectionResult {
    UserClosed,
    /// Connection error. The boolean indicates whether connection was ever established.
    Error(String, bool),
    ReceiverDropped,
}

/// Market data stream client for receiving real-time market updates.
///
/// This client manages WebSocket connections to the DeltaDeFi market streaming API
/// and provides parsed messages through async channels.
///
/// # Available Streams
///
/// - **Depth Stream**: Order book depth updates
/// - **Price Stream**: Market price changes
/// - **Recent Trades Stream**: Recent trade executions
/// - **OHLC/Graph Stream**: Candlestick chart data
///
/// # Example
///
/// ```rust,ignore
/// use deltadefi::{DeltaDeFi, Stage};
///
/// let client = DeltaDeFi::new("api-key".to_string(), Stage::Staging, None)?;
///
/// // Subscribe to market depth for ADAUSDM
/// let (mut handle, mut receiver) = client.market_stream
///     .subscribe_depth("ADAUSDM", None).await?;
///
/// while let Some(message) = receiver.recv().await {
///     if let MarketStreamMessage::Depth(depth) = message {
///         println!("Bids: {:?}, Asks: {:?}", depth.bids.len(), depth.asks.len());
///     }
/// }
///
/// handle.close().await;
/// ```
pub struct MarketStream {
    ws_url: String,
}

impl MarketStream {
    /// Create a new MarketStream instance.
    pub fn new(ws_url: String) -> Self {
        MarketStream { ws_url }
    }

    // ========================================================================
    // Basic subscription methods (no reconnection)
    // ========================================================================

    /// Subscribe to the market depth stream for a symbol.
    ///
    /// Receives order book depth updates showing bids and asks.
    /// This is the simple API without automatic reconnection.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol (e.g., "ADAUSDM")
    /// * `buffer_size` - Size of the message buffer (default: 100)
    ///
    /// # Returns
    ///
    /// A tuple containing the stream handle and message receiver.
    pub async fn subscribe_depth(
        &self,
        symbol: &str,
        buffer_size: Option<usize>,
    ) -> Result<(MarketStreamHandle, mpsc::Receiver<MarketStreamMessage>), WError> {
        let ws_endpoint = format!("{}/market/ws/depth/{}", self.ws_url, symbol);
        self.connect_and_stream(ws_endpoint, buffer_size, |json| {
            match serde_json::from_str::<MarketDepthMessage>(json) {
                Ok(msg) => MarketStreamMessage::Depth(msg),
                Err(_) => MarketStreamMessage::Unknown(json.to_string()),
            }
        })
        .await
    }

    /// Subscribe to the market price stream for a symbol.
    ///
    /// Receives price updates when trades occur.
    /// This is the simple API without automatic reconnection.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol (e.g., "ADAUSDM")
    /// * `buffer_size` - Size of the message buffer (default: 100)
    ///
    /// # Returns
    ///
    /// A tuple containing the stream handle and message receiver.
    pub async fn subscribe_price(
        &self,
        symbol: &str,
        buffer_size: Option<usize>,
    ) -> Result<(MarketStreamHandle, mpsc::Receiver<MarketStreamMessage>), WError> {
        let ws_endpoint = format!("{}/market/ws/market-price/{}", self.ws_url, symbol);
        self.connect_and_stream(ws_endpoint, buffer_size, |json| {
            match serde_json::from_str::<MarketPriceMessage>(json) {
                Ok(msg) => MarketStreamMessage::Price(msg),
                Err(_) => MarketStreamMessage::Unknown(json.to_string()),
            }
        })
        .await
    }

    /// Subscribe to the recent trades stream for a symbol.
    ///
    /// Receives notifications about recent trade executions.
    /// This is the simple API without automatic reconnection.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol (e.g., "ADAUSDM")
    /// * `buffer_size` - Size of the message buffer (default: 100)
    ///
    /// # Returns
    ///
    /// A tuple containing the stream handle and message receiver.
    pub async fn subscribe_recent_trades(
        &self,
        symbol: &str,
        buffer_size: Option<usize>,
    ) -> Result<(MarketStreamHandle, mpsc::Receiver<MarketStreamMessage>), WError> {
        let ws_endpoint = format!("{}/market/ws/recent-trade/{}", self.ws_url, symbol);
        self.connect_and_stream(ws_endpoint, buffer_size, |json| {
            match serde_json::from_str::<Vec<Trade>>(json) {
                Ok(trades) => MarketStreamMessage::RecentTrades(trades),
                Err(_) => MarketStreamMessage::Unknown(json.to_string()),
            }
        })
        .await
    }

    /// Subscribe to the OHLC/graph stream for a symbol.
    ///
    /// Receives candlestick chart data updates.
    /// This is the simple API without automatic reconnection.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol (e.g., "ADAUSDM")
    /// * `interval` - Time interval (e.g., "5m", "15m", "30m", "1h", "1d")
    /// * `buffer_size` - Size of the message buffer (default: 100)
    ///
    /// # Returns
    ///
    /// A tuple containing the stream handle and message receiver.
    pub async fn subscribe_ohlc(
        &self,
        symbol: &str,
        interval: &str,
        buffer_size: Option<usize>,
    ) -> Result<(MarketStreamHandle, mpsc::Receiver<MarketStreamMessage>), WError> {
        let ws_endpoint = format!("{}/market/ws/graph/{}/{}", self.ws_url, symbol, interval);
        self.connect_and_stream(ws_endpoint, buffer_size, |json| {
            match serde_json::from_str::<OhlcMessage>(json) {
                Ok(msg) => MarketStreamMessage::Ohlc(msg),
                Err(_) => MarketStreamMessage::Unknown(json.to_string()),
            }
        })
        .await
    }

    /// Internal method to connect and stream messages (no reconnection).
    async fn connect_and_stream<F>(
        &self,
        ws_endpoint: String,
        buffer_size: Option<usize>,
        parser: F,
    ) -> Result<(MarketStreamHandle, mpsc::Receiver<MarketStreamMessage>), WError>
    where
        F: Fn(&str) -> MarketStreamMessage + Send + 'static,
    {
        let buffer = buffer_size.unwrap_or(100);
        let (message_tx, message_rx) = mpsc::channel::<MarketStreamMessage>(buffer);
        let (close_tx, mut close_rx) = mpsc::channel::<()>(1);

        // Connect to WebSocket with timeout
        let connect_timeout = Duration::from_secs(30);
        let (ws_stream, _response) = timeout(connect_timeout, connect_async(&ws_endpoint))
            .await
            .map_err(|_| WError::new("MarketStream", "Connection timeout"))?
            .map_err(|e| WError::new("MarketStream", &format!("Connection failed: {}", e)))?;

        let (mut write, mut read) = ws_stream.split();

        // Spawn background task to handle WebSocket messages
        tokio::spawn(async move {
            loop {
                tokio::select! {
                    // Check for close signal
                    _ = close_rx.recv() => {
                        let _ = write.send(Message::Close(None)).await;
                        break;
                    }
                    // Handle incoming messages
                    msg = read.next() => {
                        match msg {
                            Some(Ok(Message::Text(text))) => {
                                let stream_msg = parser(&text);
                                if message_tx.send(stream_msg).await.is_err() {
                                    let _ = write.send(Message::Close(None)).await;
                                    break;
                                }
                            }
                            Some(Ok(Message::Ping(data))) => {
                                if write.send(Message::Pong(data)).await.is_err() {
                                    break;
                                }
                            }
                            Some(Ok(Message::Close(_))) => {
                                break;
                            }
                            Some(Err(_)) => {
                                break;
                            }
                            None => {
                                break;
                            }
                            _ => {
                                // Ignore other message types
                            }
                        }
                    }
                }
            }
        });

        let handle = MarketStreamHandle {
            close_tx: Some(close_tx),
        };

        Ok((handle, message_rx))
    }

    // ========================================================================
    // Subscription methods with automatic reconnection
    // ========================================================================

    /// Subscribe to depth stream with automatic reconnection.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol (e.g., "ADAUSDM")
    /// * `buffer_size` - Size of the message buffer (default: 100)
    /// * `reconnect_config` - Configuration for reconnection behavior
    pub async fn subscribe_depth_with_reconnect(
        &self,
        symbol: &str,
        buffer_size: Option<usize>,
        reconnect_config: Option<ReconnectConfig>,
    ) -> Result<(MarketStreamHandle, mpsc::Receiver<MarketStreamEvent>), WError> {
        let ws_endpoint = format!("{}/market/ws/depth/{}", self.ws_url, symbol);
        self.connect_and_stream_with_reconnect(ws_endpoint, buffer_size, reconnect_config, |json| {
            match serde_json::from_str::<MarketDepthMessage>(json) {
                Ok(msg) => MarketStreamMessage::Depth(msg),
                Err(_) => MarketStreamMessage::Unknown(json.to_string()),
            }
        })
        .await
    }

    /// Subscribe to price stream with automatic reconnection.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol (e.g., "ADAUSDM")
    /// * `buffer_size` - Size of the message buffer (default: 100)
    /// * `reconnect_config` - Configuration for reconnection behavior
    pub async fn subscribe_price_with_reconnect(
        &self,
        symbol: &str,
        buffer_size: Option<usize>,
        reconnect_config: Option<ReconnectConfig>,
    ) -> Result<(MarketStreamHandle, mpsc::Receiver<MarketStreamEvent>), WError> {
        let ws_endpoint = format!("{}/market/ws/market-price/{}", self.ws_url, symbol);
        self.connect_and_stream_with_reconnect(ws_endpoint, buffer_size, reconnect_config, |json| {
            match serde_json::from_str::<MarketPriceMessage>(json) {
                Ok(msg) => MarketStreamMessage::Price(msg),
                Err(_) => MarketStreamMessage::Unknown(json.to_string()),
            }
        })
        .await
    }

    /// Subscribe to recent trades stream with automatic reconnection.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol (e.g., "ADAUSDM")
    /// * `buffer_size` - Size of the message buffer (default: 100)
    /// * `reconnect_config` - Configuration for reconnection behavior
    pub async fn subscribe_recent_trades_with_reconnect(
        &self,
        symbol: &str,
        buffer_size: Option<usize>,
        reconnect_config: Option<ReconnectConfig>,
    ) -> Result<(MarketStreamHandle, mpsc::Receiver<MarketStreamEvent>), WError> {
        let ws_endpoint = format!("{}/market/ws/recent-trade/{}", self.ws_url, symbol);
        self.connect_and_stream_with_reconnect(ws_endpoint, buffer_size, reconnect_config, |json| {
            match serde_json::from_str::<Vec<Trade>>(json) {
                Ok(trades) => MarketStreamMessage::RecentTrades(trades),
                Err(_) => MarketStreamMessage::Unknown(json.to_string()),
            }
        })
        .await
    }

    /// Subscribe to OHLC stream with automatic reconnection.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol (e.g., "ADAUSDM")
    /// * `interval` - Time interval (e.g., "5m", "15m", "30m", "1h", "1d")
    /// * `buffer_size` - Size of the message buffer (default: 100)
    /// * `reconnect_config` - Configuration for reconnection behavior
    pub async fn subscribe_ohlc_with_reconnect(
        &self,
        symbol: &str,
        interval: &str,
        buffer_size: Option<usize>,
        reconnect_config: Option<ReconnectConfig>,
    ) -> Result<(MarketStreamHandle, mpsc::Receiver<MarketStreamEvent>), WError> {
        let ws_endpoint = format!("{}/market/ws/graph/{}/{}", self.ws_url, symbol, interval);
        self.connect_and_stream_with_reconnect(ws_endpoint, buffer_size, reconnect_config, |json| {
            match serde_json::from_str::<OhlcMessage>(json) {
                Ok(msg) => MarketStreamMessage::Ohlc(msg),
                Err(_) => MarketStreamMessage::Unknown(json.to_string()),
            }
        })
        .await
    }

    /// Internal method to connect and stream with automatic reconnection.
    async fn connect_and_stream_with_reconnect<F>(
        &self,
        ws_endpoint: String,
        buffer_size: Option<usize>,
        reconnect_config: Option<ReconnectConfig>,
        parser: F,
    ) -> Result<(MarketStreamHandle, mpsc::Receiver<MarketStreamEvent>), WError>
    where
        F: Fn(&str) -> MarketStreamMessage + Send + Sync + 'static,
    {
        let buffer = buffer_size.unwrap_or(100);
        let config = reconnect_config.unwrap_or_default();
        let (event_tx, event_rx) = mpsc::channel::<MarketStreamEvent>(buffer);
        let (close_tx, close_rx) = mpsc::channel::<()>(1);

        // Spawn the reconnecting stream task
        tokio::spawn(Self::run_reconnecting_stream(
            ws_endpoint,
            config,
            event_tx,
            close_rx,
            parser,
        ));

        let handle = MarketStreamHandle {
            close_tx: Some(close_tx),
        };

        Ok((handle, event_rx))
    }

    /// Internal: Run a single WebSocket connection, returning why it ended.
    async fn run_single_connection<F>(
        ws_endpoint: &str,
        connect_timeout: Duration,
        event_tx: &mpsc::Sender<MarketStreamEvent>,
        close_rx: &mut mpsc::Receiver<()>,
        parser: &F,
    ) -> ConnectionResult
    where
        F: Fn(&str) -> MarketStreamMessage,
    {
        // Attempt to connect with timeout
        let connect_result = timeout(connect_timeout, connect_async(ws_endpoint)).await;

        let ws_stream = match connect_result {
            Ok(Ok((stream, _response))) => stream,
            Ok(Err(e)) => {
                return ConnectionResult::Error(format!("Connection failed: {}", e), false);
            }
            Err(_) => {
                return ConnectionResult::Error("Connection timeout".to_string(), false);
            }
        };

        // Notify connected
        if event_tx.send(MarketStreamEvent::Connected).await.is_err() {
            return ConnectionResult::ReceiverDropped;
        }

        let (mut write, mut read) = ws_stream.split();

        // Message loop
        loop {
            tokio::select! {
                // Check for close signal
                _ = close_rx.recv() => {
                    let _ = write.send(Message::Close(None)).await;
                    return ConnectionResult::UserClosed;
                }
                // Handle incoming messages
                msg = read.next() => {
                    match msg {
                        Some(Ok(Message::Text(text))) => {
                            let stream_msg = parser(&text);
                            let event = MarketStreamEvent::Message(stream_msg);
                            if event_tx.send(event).await.is_err() {
                                let _ = write.send(Message::Close(None)).await;
                                return ConnectionResult::ReceiverDropped;
                            }
                        }
                        Some(Ok(Message::Ping(data))) => {
                            if write.send(Message::Pong(data)).await.is_err() {
                                return ConnectionResult::Error("Failed to send pong".to_string(), true);
                            }
                        }
                        Some(Ok(Message::Close(frame))) => {
                            let reason = frame
                                .map(|f| f.reason.to_string())
                                .unwrap_or_else(|| "Server closed connection".to_string());
                            return ConnectionResult::Error(reason, true);
                        }
                        Some(Err(e)) => {
                            return ConnectionResult::Error(format!("WebSocket error: {}", e), true);
                        }
                        None => {
                            return ConnectionResult::Error("Stream ended unexpectedly".to_string(), true);
                        }
                        _ => {
                            // Ignore other message types
                        }
                    }
                }
            }
        }
    }

    /// Internal: Run the reconnecting stream loop.
    async fn run_reconnecting_stream<F>(
        ws_endpoint: String,
        config: ReconnectConfig,
        event_tx: mpsc::Sender<MarketStreamEvent>,
        mut close_rx: mpsc::Receiver<()>,
        parser: F,
    ) where
        F: Fn(&str) -> MarketStreamMessage + Send + Sync + 'static,
    {
        let connect_timeout = Duration::from_millis(config.connect_timeout_ms);
        let mut attempt: u32 = 0;

        loop {
            // Check for close signal before attempting connection
            if close_rx.try_recv().is_ok() {
                break;
            }

            let result = Self::run_single_connection(
                &ws_endpoint,
                connect_timeout,
                &event_tx,
                &mut close_rx,
                &parser,
            )
            .await;

            match result {
                ConnectionResult::UserClosed => {
                    break;
                }
                ConnectionResult::ReceiverDropped => {
                    break;
                }
                ConnectionResult::Error(reason, was_connected) => {
                    // Reset attempt counter if we had a successful connection
                    // This ensures fresh backoff after a working session ends
                    if was_connected {
                        attempt = 0;
                    }

                    // Send disconnected event
                    let _ = event_tx
                        .send(MarketStreamEvent::Disconnected {
                            reason: reason.clone(),
                        })
                        .await;

                    // Check if we should retry
                    if !config.should_retry(attempt) {
                        let _ = event_tx.send(MarketStreamEvent::MaxRetriesExceeded).await;
                        break;
                    }

                    // Calculate delay and notify
                    let delay_ms = config.delay_for_attempt(attempt);
                    attempt += 1;

                    let _ = event_tx
                        .send(MarketStreamEvent::Reconnecting { attempt, delay_ms })
                        .await;

                    // Wait for delay, but allow early exit on close signal
                    tokio::select! {
                        _ = close_rx.recv() => {
                            break;
                        }
                        _ = tokio::time::sleep(Duration::from_millis(delay_ms)) => {
                            // Continue to retry
                        }
                    }
                }
            }
        }
    }

    /// Subscribe to depth stream with a callback function.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol
    /// * `callback` - Async function called for each message. Return `false` to stop.
    pub async fn subscribe_depth_with_callback<F, Fut>(
        &self,
        symbol: &str,
        mut callback: F,
    ) -> Result<(), WError>
    where
        F: FnMut(MarketStreamMessage) -> Fut,
        Fut: std::future::Future<Output = bool>,
    {
        let (mut handle, mut receiver) = self.subscribe_depth(symbol, None).await?;

        while let Some(message) = receiver.recv().await {
            if !callback(message).await {
                break;
            }
        }

        handle.close().await;
        Ok(())
    }

    /// Subscribe to price stream with a callback function.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol
    /// * `callback` - Async function called for each message. Return `false` to stop.
    pub async fn subscribe_price_with_callback<F, Fut>(
        &self,
        symbol: &str,
        mut callback: F,
    ) -> Result<(), WError>
    where
        F: FnMut(MarketStreamMessage) -> Fut,
        Fut: std::future::Future<Output = bool>,
    {
        let (mut handle, mut receiver) = self.subscribe_price(symbol, None).await?;

        while let Some(message) = receiver.recv().await {
            if !callback(message).await {
                break;
            }
        }

        handle.close().await;
        Ok(())
    }

    /// Subscribe to recent trades stream with a callback function.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol
    /// * `callback` - Async function called for each message. Return `false` to stop.
    pub async fn subscribe_recent_trades_with_callback<F, Fut>(
        &self,
        symbol: &str,
        mut callback: F,
    ) -> Result<(), WError>
    where
        F: FnMut(MarketStreamMessage) -> Fut,
        Fut: std::future::Future<Output = bool>,
    {
        let (mut handle, mut receiver) = self.subscribe_recent_trades(symbol, None).await?;

        while let Some(message) = receiver.recv().await {
            if !callback(message).await {
                break;
            }
        }

        handle.close().await;
        Ok(())
    }

    /// Subscribe to OHLC stream with a callback function.
    ///
    /// # Arguments
    ///
    /// * `symbol` - Trading pair symbol
    /// * `interval` - Time interval (e.g., "5m", "1h")
    /// * `callback` - Async function called for each message. Return `false` to stop.
    pub async fn subscribe_ohlc_with_callback<F, Fut>(
        &self,
        symbol: &str,
        interval: &str,
        mut callback: F,
    ) -> Result<(), WError>
    where
        F: FnMut(MarketStreamMessage) -> Fut,
        Fut: std::future::Future<Output = bool>,
    {
        let (mut handle, mut receiver) = self.subscribe_ohlc(symbol, interval, None).await?;

        while let Some(message) = receiver.recv().await {
            if !callback(message).await {
                break;
            }
        }

        handle.close().await;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_market_depth_parsing() {
        let json = r#"{
            "timestamp": 1704067200000,
            "bids": [
                {"price": 0.45, "quantity": 1000.0},
                {"price": 0.44, "quantity": 2000.0}
            ],
            "asks": [
                {"price": 0.46, "quantity": 500.0},
                {"price": 0.47, "quantity": 1500.0}
            ]
        }"#;

        let msg: MarketDepthMessage = serde_json::from_str(json).unwrap();
        assert_eq!(msg.timestamp, 1704067200000);
        assert_eq!(msg.bids.len(), 2);
        assert_eq!(msg.asks.len(), 2);
        assert_eq!(msg.bids[0].price, 0.45);
        assert_eq!(msg.bids[0].quantity, 1000.0);
    }

    #[test]
    fn test_market_price_parsing() {
        let json = r#"{"price": 0.456789}"#;

        let msg: MarketPriceMessage = serde_json::from_str(json).unwrap();
        assert_eq!(msg.price, 0.456789);
    }

    #[test]
    fn test_recent_trades_parsing() {
        let json = r#"[
            {
                "order_id": "order-123",
                "timestamp": "2024-01-01T00:00:00Z",
                "symbol": "ADAUSDM",
                "price": 0.45,
                "amount": 100.0,
                "side": "buy"
            }
        ]"#;

        let trades: Vec<Trade> = serde_json::from_str(json).unwrap();
        assert_eq!(trades.len(), 1);
        assert_eq!(trades[0].order_id, "order-123");
        assert_eq!(trades[0].symbol, "ADAUSDM");
        assert_eq!(trades[0].side, "buy");
    }

    #[test]
    fn test_ohlc_parsing() {
        let json = r#"{
            "t": 1704067200,
            "s": "ADAUSDM",
            "o": 0.45,
            "h": 0.48,
            "l": 0.44,
            "c": 0.47,
            "v": 10000.5
        }"#;

        let msg: OhlcMessage = serde_json::from_str(json).unwrap();
        assert_eq!(msg.timestamp, 1704067200);
        assert_eq!(msg.symbol, "ADAUSDM");
        assert_eq!(msg.open, 0.45);
        assert_eq!(msg.high, 0.48);
        assert_eq!(msg.low, 0.44);
        assert_eq!(msg.close, 0.47);
        assert_eq!(msg.volume, 10000.5);
    }

    #[test]
    fn test_market_stream_message_helpers() {
        let depth = MarketStreamMessage::Depth(MarketDepthMessage {
            timestamp: 0,
            bids: vec![],
            asks: vec![],
        });
        assert!(depth.is_depth());
        assert!(!depth.is_price());

        let price = MarketStreamMessage::Price(MarketPriceMessage { price: 0.5 });
        assert!(price.is_price());
        assert!(!price.is_depth());

        let trades = MarketStreamMessage::RecentTrades(vec![]);
        assert!(trades.is_recent_trades());

        let ohlc = MarketStreamMessage::Ohlc(OhlcMessage {
            timestamp: 0,
            symbol: "TEST".to_string(),
            open: 0.0,
            high: 0.0,
            low: 0.0,
            close: 0.0,
            volume: 0.0,
        });
        assert!(ohlc.is_ohlc());

        let unknown = MarketStreamMessage::Unknown("test".to_string());
        assert!(unknown.is_unknown());
    }
}