quicknode-hyperliquid-sdk 0.1.9

Hyperliquid SDK for Rust - Simple, performant trading client. HyperCore, HyperEVM, WebSocket and gRPC streams.
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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
//! gRPC streaming client for Hyperliquid.
//!
//! Provides low-latency real-time data streaming via gRPC.
//! Authentication is via x-token header with your QuickNode API token.
//!
//! Example:
//! ```ignore
//! use hyperliquid_sdk::GRPCStream;
//!
//! let mut stream = GRPCStream::new(Some("https://your-endpoint.quiknode.pro/TOKEN".to_string()));
//! stream.trades(&["BTC", "ETH"], |data| {
//!     println!("Trade: {:?}", data);
//! });
//! stream.start().await?;
//! ```

use parking_lot::RwLock;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::mpsc;
use tokio::time::sleep;
use tonic::metadata::MetadataValue;
use tonic::transport::{Channel, ClientTlsConfig};
use tonic::Request;

use crate::error::Result;
use crate::stream::ConnectionState;

// Include generated protobuf code
pub mod proto {
    tonic::include_proto!("hyperliquid");
}

use proto::streaming_client::StreamingClient;
use proto::block_streaming_client::BlockStreamingClient;
use proto::order_book_streaming_client::OrderBookStreamingClient;
use proto::{
    FilterValues, L2BookRequest, L4BookRequest, Ping, PingRequest, StreamSubscribe,
    SubscribeRequest, Timestamp,
};

// ══════════════════════════════════════════════════════════════════════════════
// gRPC Constants
// ══════════════════════════════════════════════════════════════════════════════

const GRPC_PORT: u16 = 10000;
const INITIAL_RECONNECT_DELAY: Duration = Duration::from_secs(1);
const MAX_RECONNECT_DELAY: Duration = Duration::from_secs(60);
const RECONNECT_BACKOFF_FACTOR: f64 = 2.0;
const KEEPALIVE_TIME: Duration = Duration::from_secs(30);
const KEEPALIVE_TIMEOUT: Duration = Duration::from_secs(10);

// ══════════════════════════════════════════════════════════════════════════════
// gRPC Stream Types
// ══════════════════════════════════════════════════════════════════════════════

/// gRPC stream types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum GRPCStreamType {
    Trades,
    Orders,
    BookUpdates,
    Twap,
    Events,
    Blocks,
    WriterActions,
    L2Book,
    L4Book,
}

impl GRPCStreamType {
    /// Get the stream name
    pub fn as_str(&self) -> &'static str {
        match self {
            GRPCStreamType::Trades => "trades",
            GRPCStreamType::Orders => "orders",
            GRPCStreamType::BookUpdates => "book_updates",
            GRPCStreamType::Twap => "twap",
            GRPCStreamType::Events => "events",
            GRPCStreamType::Blocks => "blocks",
            GRPCStreamType::WriterActions => "writer_actions",
            GRPCStreamType::L2Book => "l2_book",
            GRPCStreamType::L4Book => "l4_book",
        }
    }

    /// Convert to proto enum value
    fn to_proto(&self) -> i32 {
        match self {
            GRPCStreamType::Trades => 1,
            GRPCStreamType::Orders => 2,
            GRPCStreamType::BookUpdates => 3,
            GRPCStreamType::Twap => 4,
            GRPCStreamType::Events => 5,
            GRPCStreamType::Blocks => 6,
            GRPCStreamType::WriterActions => 7,
            GRPCStreamType::L2Book => 0,
            GRPCStreamType::L4Book => 0,
        }
    }
}

// ══════════════════════════════════════════════════════════════════════════════
// gRPC Subscription
// ══════════════════════════════════════════════════════════════════════════════

/// A gRPC subscription handle
#[derive(Debug, Clone)]
pub struct GRPCSubscription {
    pub id: u32,
    pub stream_type: GRPCStreamType,
}

// ══════════════════════════════════════════════════════════════════════════════
// gRPC Stream Configuration
// ══════════════════════════════════════════════════════════════════════════════

/// gRPC stream configuration
#[derive(Clone)]
pub struct GRPCStreamConfig {
    pub endpoint: Option<String>,
    pub reconnect: bool,
    pub max_reconnect_attempts: Option<u32>,
    pub keepalive_interval: Duration,
    pub keepalive_timeout: Duration,
}

impl Default for GRPCStreamConfig {
    fn default() -> Self {
        Self {
            endpoint: None,
            reconnect: true,
            max_reconnect_attempts: None,
            keepalive_interval: KEEPALIVE_TIME,
            keepalive_timeout: KEEPALIVE_TIMEOUT,
        }
    }
}

// ══════════════════════════════════════════════════════════════════════════════
// gRPC Subscription Info
// ══════════════════════════════════════════════════════════════════════════════

struct GRPCSubscriptionInfo {
    stream_type: GRPCStreamType,
    coins: Vec<String>,
    users: Vec<String>,
    coin: Option<String>,
    n_levels: Option<u32>,
    n_sig_figs: Option<u32>,
}

// ══════════════════════════════════════════════════════════════════════════════
// gRPC Stream
// ══════════════════════════════════════════════════════════════════════════════

/// gRPC stream client for Hyperliquid real-time data
pub struct GRPCStream {
    config: GRPCStreamConfig,
    host: String,
    token: String,
    state: Arc<RwLock<ConnectionState>>,
    running: Arc<AtomicBool>,
    reconnect_attempts: Arc<AtomicU32>,
    subscription_id: Arc<AtomicU32>,
    subscriptions: Arc<RwLock<HashMap<u32, GRPCSubscriptionInfo>>>,
    callbacks: Arc<RwLock<HashMap<u32, Box<dyn Fn(Value) + Send + Sync>>>>,
    on_error: Option<Arc<dyn Fn(String) + Send + Sync>>,
    on_close: Option<Arc<dyn Fn() + Send + Sync>>,
    on_connect: Option<Arc<dyn Fn() + Send + Sync>>,
    on_reconnect: Option<Arc<dyn Fn(u32) + Send + Sync>>,
    on_state_change: Option<Arc<dyn Fn(ConnectionState) + Send + Sync>>,
    stop_tx: Option<mpsc::Sender<()>>,
}

impl GRPCStream {
    /// Create a new gRPC stream client
    pub fn new(endpoint: Option<String>) -> Self {
        let (host, token) = endpoint
            .as_ref()
            .map(|ep| parse_endpoint(ep))
            .unwrap_or_default();

        Self {
            config: GRPCStreamConfig {
                endpoint,
                ..Default::default()
            },
            host,
            token,
            state: Arc::new(RwLock::new(ConnectionState::Disconnected)),
            running: Arc::new(AtomicBool::new(false)),
            reconnect_attempts: Arc::new(AtomicU32::new(0)),
            subscription_id: Arc::new(AtomicU32::new(0)),
            subscriptions: Arc::new(RwLock::new(HashMap::new())),
            callbacks: Arc::new(RwLock::new(HashMap::new())),
            on_error: None,
            on_close: None,
            on_connect: None,
            on_reconnect: None,
            on_state_change: None,
            stop_tx: None,
        }
    }

    /// Configure stream options
    pub fn configure(mut self, config: GRPCStreamConfig) -> Self {
        if let Some(ref ep) = config.endpoint {
            let (host, token) = parse_endpoint(ep);
            self.host = host;
            self.token = token;
        }
        self.config = config;
        self
    }

    /// Set error callback
    pub fn on_error<F>(mut self, f: F) -> Self
    where
        F: Fn(String) + Send + Sync + 'static,
    {
        self.on_error = Some(Arc::new(f));
        self
    }

    /// Set close callback
    pub fn on_close<F>(mut self, f: F) -> Self
    where
        F: Fn() + Send + Sync + 'static,
    {
        self.on_close = Some(Arc::new(f));
        self
    }

    /// Set connect callback
    pub fn on_connect<F>(mut self, f: F) -> Self
    where
        F: Fn() + Send + Sync + 'static,
    {
        self.on_connect = Some(Arc::new(f));
        self
    }

    /// Set reconnect callback
    pub fn on_reconnect<F>(mut self, f: F) -> Self
    where
        F: Fn(u32) + Send + Sync + 'static,
    {
        self.on_reconnect = Some(Arc::new(f));
        self
    }

    /// Set state change callback
    pub fn on_state_change<F>(mut self, f: F) -> Self
    where
        F: Fn(ConnectionState) + Send + Sync + 'static,
    {
        self.on_state_change = Some(Arc::new(f));
        self
    }

    /// Get current connection state
    pub fn state(&self) -> ConnectionState {
        *self.state.read()
    }

    /// Check if connected
    pub fn connected(&self) -> bool {
        *self.state.read() == ConnectionState::Connected
    }

    fn set_state(&self, state: ConnectionState) {
        let mut s = self.state.write();
        if *s != state {
            *s = state;
            if let Some(ref cb) = self.on_state_change {
                cb(state);
            }
        }
    }

    fn next_subscription_id(&self) -> u32 {
        self.subscription_id.fetch_add(1, Ordering::SeqCst)
    }

    // ──────────────────────────────────────────────────────────────────────────
    // Subscriptions
    // ──────────────────────────────────────────────────────────────────────────

    /// Subscribe to trades
    pub fn trades<F>(&mut self, coins: &[&str], callback: F) -> GRPCSubscription
    where
        F: Fn(Value) + Send + Sync + 'static,
    {
        let id = self.next_subscription_id();
        self.subscriptions.write().insert(
            id,
            GRPCSubscriptionInfo {
                stream_type: GRPCStreamType::Trades,
                coins: coins.iter().map(|s| s.to_string()).collect(),
                users: vec![],
                coin: None,
                n_levels: None,
                n_sig_figs: None,
            },
        );
        self.callbacks.write().insert(id, Box::new(callback));

        GRPCSubscription {
            id,
            stream_type: GRPCStreamType::Trades,
        }
    }

    /// Subscribe to orders
    pub fn orders<F>(&mut self, coins: &[&str], callback: F) -> GRPCSubscription
    where
        F: Fn(Value) + Send + Sync + 'static,
    {
        let id = self.next_subscription_id();
        self.subscriptions.write().insert(
            id,
            GRPCSubscriptionInfo {
                stream_type: GRPCStreamType::Orders,
                coins: coins.iter().map(|s| s.to_string()).collect(),
                users: vec![],
                coin: None,
                n_levels: None,
                n_sig_figs: None,
            },
        );
        self.callbacks.write().insert(id, Box::new(callback));

        GRPCSubscription {
            id,
            stream_type: GRPCStreamType::Orders,
        }
    }

    /// Subscribe to book updates
    pub fn book_updates<F>(&mut self, coins: &[&str], callback: F) -> GRPCSubscription
    where
        F: Fn(Value) + Send + Sync + 'static,
    {
        let id = self.next_subscription_id();
        self.subscriptions.write().insert(
            id,
            GRPCSubscriptionInfo {
                stream_type: GRPCStreamType::BookUpdates,
                coins: coins.iter().map(|s| s.to_string()).collect(),
                users: vec![],
                coin: None,
                n_levels: None,
                n_sig_figs: None,
            },
        );
        self.callbacks.write().insert(id, Box::new(callback));

        GRPCSubscription {
            id,
            stream_type: GRPCStreamType::BookUpdates,
        }
    }

    /// Subscribe to L2 order book
    pub fn l2_book<F>(&mut self, coin: &str, callback: F) -> GRPCSubscription
    where
        F: Fn(Value) + Send + Sync + 'static,
    {
        self.l2_book_with_options(coin, 20, None, callback)
    }

    /// Subscribe to L2 order book with options
    pub fn l2_book_with_options<F>(
        &mut self,
        coin: &str,
        n_levels: u32,
        n_sig_figs: Option<u32>,
        callback: F,
    ) -> GRPCSubscription
    where
        F: Fn(Value) + Send + Sync + 'static,
    {
        let id = self.next_subscription_id();
        self.subscriptions.write().insert(
            id,
            GRPCSubscriptionInfo {
                stream_type: GRPCStreamType::L2Book,
                coins: vec![],
                users: vec![],
                coin: Some(coin.to_string()),
                n_levels: Some(n_levels),
                n_sig_figs,
            },
        );
        self.callbacks.write().insert(id, Box::new(callback));

        GRPCSubscription {
            id,
            stream_type: GRPCStreamType::L2Book,
        }
    }

    /// Subscribe to L4 order book (individual orders with OIDs)
    pub fn l4_book<F>(&mut self, coin: &str, callback: F) -> GRPCSubscription
    where
        F: Fn(Value) + Send + Sync + 'static,
    {
        let id = self.next_subscription_id();
        self.subscriptions.write().insert(
            id,
            GRPCSubscriptionInfo {
                stream_type: GRPCStreamType::L4Book,
                coins: vec![],
                users: vec![],
                coin: Some(coin.to_string()),
                n_levels: None,
                n_sig_figs: None,
            },
        );
        self.callbacks.write().insert(id, Box::new(callback));

        GRPCSubscription {
            id,
            stream_type: GRPCStreamType::L4Book,
        }
    }

    /// Subscribe to blocks
    pub fn blocks<F>(&mut self, callback: F) -> GRPCSubscription
    where
        F: Fn(Value) + Send + Sync + 'static,
    {
        let id = self.next_subscription_id();
        self.subscriptions.write().insert(
            id,
            GRPCSubscriptionInfo {
                stream_type: GRPCStreamType::Blocks,
                coins: vec![],
                users: vec![],
                coin: None,
                n_levels: None,
                n_sig_figs: None,
            },
        );
        self.callbacks.write().insert(id, Box::new(callback));

        GRPCSubscription {
            id,
            stream_type: GRPCStreamType::Blocks,
        }
    }

    /// Subscribe to TWAP updates
    pub fn twap<F>(&mut self, coins: &[&str], callback: F) -> GRPCSubscription
    where
        F: Fn(Value) + Send + Sync + 'static,
    {
        let id = self.next_subscription_id();
        self.subscriptions.write().insert(
            id,
            GRPCSubscriptionInfo {
                stream_type: GRPCStreamType::Twap,
                coins: coins.iter().map(|s| s.to_string()).collect(),
                users: vec![],
                coin: None,
                n_levels: None,
                n_sig_figs: None,
            },
        );
        self.callbacks.write().insert(id, Box::new(callback));

        GRPCSubscription {
            id,
            stream_type: GRPCStreamType::Twap,
        }
    }

    /// Subscribe to events
    pub fn events<F>(&mut self, callback: F) -> GRPCSubscription
    where
        F: Fn(Value) + Send + Sync + 'static,
    {
        let id = self.next_subscription_id();
        self.subscriptions.write().insert(
            id,
            GRPCSubscriptionInfo {
                stream_type: GRPCStreamType::Events,
                coins: vec![],
                users: vec![],
                coin: None,
                n_levels: None,
                n_sig_figs: None,
            },
        );
        self.callbacks.write().insert(id, Box::new(callback));

        GRPCSubscription {
            id,
            stream_type: GRPCStreamType::Events,
        }
    }

    /// Subscribe to writer actions
    pub fn writer_actions<F>(&mut self, callback: F) -> GRPCSubscription
    where
        F: Fn(Value) + Send + Sync + 'static,
    {
        let id = self.next_subscription_id();
        self.subscriptions.write().insert(
            id,
            GRPCSubscriptionInfo {
                stream_type: GRPCStreamType::WriterActions,
                coins: vec![],
                users: vec![],
                coin: None,
                n_levels: None,
                n_sig_figs: None,
            },
        );
        self.callbacks.write().insert(id, Box::new(callback));

        GRPCSubscription {
            id,
            stream_type: GRPCStreamType::WriterActions,
        }
    }

    /// Unsubscribe
    pub fn unsubscribe(&mut self, subscription: &GRPCSubscription) {
        self.subscriptions.write().remove(&subscription.id);
        self.callbacks.write().remove(&subscription.id);
    }

    // ──────────────────────────────────────────────────────────────────────────
    // Lifecycle
    // ──────────────────────────────────────────────────────────────────────────

    /// Start the stream in background (non-blocking)
    pub fn start(&mut self) -> Result<()> {
        if self.running.load(Ordering::SeqCst) {
            return Ok(());
        }

        self.running.store(true, Ordering::SeqCst);

        let (stop_tx, stop_rx) = mpsc::channel(1);
        self.stop_tx = Some(stop_tx);

        let host = self.host.clone();
        let token = self.token.clone();
        let state = self.state.clone();
        let running = self.running.clone();
        let reconnect_attempts = self.reconnect_attempts.clone();
        let subscriptions = self.subscriptions.clone();
        let callbacks = self.callbacks.clone();
        let config = self.config.clone();
        let on_error = self.on_error.clone();
        let on_close = self.on_close.clone();
        let on_connect = self.on_connect.clone();
        let on_reconnect = self.on_reconnect.clone();
        let on_state_change = self.on_state_change.clone();

        tokio::spawn(async move {
            Self::run_loop(
                host,
                token,
                state,
                running,
                reconnect_attempts,
                subscriptions,
                callbacks,
                config,
                on_error,
                on_close,
                on_connect,
                on_reconnect,
                on_state_change,
                stop_rx,
            )
            .await;
        });

        Ok(())
    }

    /// Run the stream (blocking)
    pub async fn run(&mut self) -> Result<()> {
        self.start()?;

        while self.running.load(Ordering::SeqCst) {
            sleep(Duration::from_millis(100)).await;
        }

        Ok(())
    }

    /// Stop the stream
    pub fn stop(&mut self) {
        self.running.store(false, Ordering::SeqCst);
        if let Some(tx) = self.stop_tx.take() {
            let _ = tx.try_send(());
        }
        self.set_state(ConnectionState::Disconnected);

        if let Some(ref cb) = self.on_close {
            cb();
        }
    }

    /// Ping the server
    pub async fn ping(&self) -> bool {
        if self.host.is_empty() {
            return false;
        }

        let target = format!("https://{}:{}", self.host, GRPC_PORT);

        let channel = match Channel::from_shared(target)
            .unwrap()
            .tls_config(ClientTlsConfig::new().with_native_roots())
            .unwrap()
            .connect()
            .await
        {
            Ok(c) => c,
            Err(_) => return false,
        };

        let token: MetadataValue<_> = self.token.parse().unwrap();
        let mut client =
            StreamingClient::with_interceptor(channel, move |mut req: Request<()>| {
                req.metadata_mut()
                    .insert("x-token", token.clone());
                Ok(req)
            });

        match client.ping(PingRequest { count: 1 }).await {
            Ok(resp) => resp.into_inner().count == 1,
            Err(_) => false,
        }
    }

    #[allow(clippy::too_many_arguments)]
    async fn run_loop(
        host: String,
        token: String,
        state: Arc<RwLock<ConnectionState>>,
        running: Arc<AtomicBool>,
        reconnect_attempts: Arc<AtomicU32>,
        subscriptions: Arc<RwLock<HashMap<u32, GRPCSubscriptionInfo>>>,
        callbacks: Arc<RwLock<HashMap<u32, Box<dyn Fn(Value) + Send + Sync>>>>,
        config: GRPCStreamConfig,
        on_error: Option<Arc<dyn Fn(String) + Send + Sync>>,
        on_close: Option<Arc<dyn Fn() + Send + Sync>>,
        _on_connect: Option<Arc<dyn Fn() + Send + Sync>>,
        on_reconnect: Option<Arc<dyn Fn(u32) + Send + Sync>>,
        on_state_change: Option<Arc<dyn Fn(ConnectionState) + Send + Sync>>,
        mut stop_rx: mpsc::Receiver<()>,
    ) {
        let mut backoff = INITIAL_RECONNECT_DELAY;

        while running.load(Ordering::SeqCst) {
            // Check for stop signal
            if stop_rx.try_recv().is_ok() {
                break;
            }

            // Update state
            {
                let mut s = state.write();
                if *s == ConnectionState::Reconnecting {
                    if let Some(ref cb) = on_reconnect {
                        cb(reconnect_attempts.load(Ordering::SeqCst));
                    }
                }
                *s = ConnectionState::Connecting;
            }
            if let Some(ref cb) = on_state_change {
                cb(ConnectionState::Connecting);
            }

            // Try to connect and stream
            let result = Self::connect_and_stream(
                &host,
                &token,
                &subscriptions,
                &callbacks,
                &running,
                &mut stop_rx,
            )
            .await;

            if let Err(e) = result {
                if let Some(ref cb) = on_error {
                    cb(e.to_string());
                }
            }

            if !running.load(Ordering::SeqCst) {
                break;
            }

            if !config.reconnect {
                break;
            }

            let attempts = reconnect_attempts.fetch_add(1, Ordering::SeqCst) + 1;
            if let Some(max) = config.max_reconnect_attempts {
                if attempts >= max {
                    break;
                }
            }

            {
                *state.write() = ConnectionState::Reconnecting;
            }
            if let Some(ref cb) = on_state_change {
                cb(ConnectionState::Reconnecting);
            }

            // Wait before reconnecting
            tokio::select! {
                _ = sleep(backoff) => {}
                _ = stop_rx.recv() => { break; }
            }

            backoff = Duration::from_secs_f64(
                (backoff.as_secs_f64() * RECONNECT_BACKOFF_FACTOR).min(MAX_RECONNECT_DELAY.as_secs_f64())
            );
        }

        {
            *state.write() = ConnectionState::Disconnected;
        }
        if let Some(ref cb) = on_state_change {
            cb(ConnectionState::Disconnected);
        }
        if let Some(ref cb) = on_close {
            cb();
        }
    }

    async fn connect_and_stream(
        host: &str,
        token: &str,
        subscriptions: &Arc<RwLock<HashMap<u32, GRPCSubscriptionInfo>>>,
        callbacks: &Arc<RwLock<HashMap<u32, Box<dyn Fn(Value) + Send + Sync>>>>,
        running: &Arc<AtomicBool>,
        stop_rx: &mut mpsc::Receiver<()>,
    ) -> Result<()> {
        if host.is_empty() {
            return Err(crate::error::Error::ConfigError("No gRPC endpoint configured".to_string()));
        }

        let target = format!("https://{}:{}", host, GRPC_PORT);

        // Create channel with TLS using native system root certificates
        // (like Python's ssl_channel_credentials() and TypeScript's grpc.credentials.createSsl())
        let channel = Channel::from_shared(target)
            .map_err(|e| crate::error::Error::NetworkError(e.to_string()))?
            .tls_config(ClientTlsConfig::new().with_native_roots())
            .map_err(|e: tonic::transport::Error| crate::error::Error::NetworkError(e.to_string()))?
            .connect()
            .await
            .map_err(|e| crate::error::Error::NetworkError(format!("Failed to connect: {}", e)))?;

        // Get subscriptions snapshot
        let subs: Vec<(u32, GRPCSubscriptionInfo)> = {
            let guard = subscriptions.read();
            guard
                .iter()
                .map(|(k, v)| {
                    (
                        *k,
                        GRPCSubscriptionInfo {
                            stream_type: v.stream_type,
                            coins: v.coins.clone(),
                            users: v.users.clone(),
                            coin: v.coin.clone(),
                            n_levels: v.n_levels,
                            n_sig_figs: v.n_sig_figs,
                        },
                    )
                })
                .collect()
        };

        // Start each subscription stream
        let mut handles = Vec::new();
        for (sub_id, sub_info) in subs {
            let channel = channel.clone();
            let token = token.to_string();
            let callbacks = callbacks.clone();
            let running = running.clone();

            let handle = tokio::spawn(async move {
                match sub_info.stream_type {
                    GRPCStreamType::L2Book => {
                        Self::stream_l2_book(channel, &token, sub_id, &sub_info, &callbacks, &running).await;
                    }
                    GRPCStreamType::L4Book => {
                        Self::stream_l4_book(channel, &token, sub_id, &sub_info, &callbacks, &running).await;
                    }
                    GRPCStreamType::Blocks => {
                        Self::stream_blocks(channel, &token, sub_id, &callbacks, &running).await;
                    }
                    _ => {
                        Self::stream_data(channel, &token, sub_id, &sub_info, &callbacks, &running).await;
                    }
                }
            });
            handles.push(handle);
        }

        // Wait for stop signal or any stream to end
        loop {
            tokio::select! {
                _ = stop_rx.recv() => { break; }
                _ = sleep(Duration::from_secs(1)) => {
                    if !running.load(Ordering::SeqCst) {
                        break;
                    }
                    // Check if any handles finished
                    let mut all_done = true;
                    for h in &handles {
                        if !h.is_finished() {
                            all_done = false;
                            break;
                        }
                    }
                    if all_done && !handles.is_empty() {
                        break;
                    }
                }
            }
        }

        Ok(())
    }

    async fn stream_data(
        channel: Channel,
        token: &str,
        sub_id: u32,
        sub_info: &GRPCSubscriptionInfo,
        callbacks: &Arc<RwLock<HashMap<u32, Box<dyn Fn(Value) + Send + Sync>>>>,
        running: &Arc<AtomicBool>,
    ) {
        let token_value: MetadataValue<_> = token.parse().unwrap();
        let mut client = StreamingClient::with_interceptor(channel, move |mut req: Request<()>| {
            req.metadata_mut().insert("x-token", token_value.clone());
            Ok(req)
        });

        // Build subscribe request
        let mut filters = HashMap::new();
        if !sub_info.coins.is_empty() {
            filters.insert(
                "coin".to_string(),
                FilterValues {
                    values: sub_info.coins.clone(),
                },
            );
        }
        if !sub_info.users.is_empty() {
            filters.insert(
                "user".to_string(),
                FilterValues {
                    values: sub_info.users.clone(),
                },
            );
        }

        let subscribe_req = SubscribeRequest {
            request: Some(proto::subscribe_request::Request::Subscribe(StreamSubscribe {
                stream_type: sub_info.stream_type.to_proto(),
                filters,
                filter_name: String::new(),
            })),
        };

        // Create bidirectional stream
        let (tx, rx) = tokio::sync::mpsc::channel(16);
        let outbound = tokio_stream::wrappers::ReceiverStream::new(rx);

        // Send initial subscribe
        if tx.send(subscribe_req).await.is_err() {
            return;
        }

        // Start ping task
        let tx_ping = tx.clone();
        let running_ping = running.clone();
        tokio::spawn(async move {
            loop {
                sleep(Duration::from_secs(30)).await;
                if !running_ping.load(Ordering::SeqCst) {
                    break;
                }
                let ping_req = SubscribeRequest {
                    request: Some(proto::subscribe_request::Request::Ping(Ping {
                        timestamp: chrono::Utc::now().timestamp_millis(),
                    })),
                };
                if tx_ping.send(ping_req).await.is_err() {
                    break;
                }
            }
        });

        // Call StreamData
        let response = match client.stream_data(outbound).await {
            Ok(r) => r,
            Err(e) => {
                tracing::error!("StreamData error: {}", e);
                return;
            }
        };

        let mut inbound = response.into_inner();

        while running.load(Ordering::SeqCst) {
            match inbound.message().await {
                Ok(Some(update)) => {
                    if let Some(proto::subscribe_update::Update::Data(data)) = update.update {
                        // Parse the JSON data
                        if let Ok(parsed) = serde_json::from_str::<Value>(&data.data) {
                            // Extract events if present
                            if let Some(events) = parsed.get("events").and_then(|e| e.as_array()) {
                                for event in events {
                                    if let Some(arr) = event.as_array() {
                                        if arr.len() >= 2 {
                                            let user = arr[0].as_str().unwrap_or("");
                                            if let Some(event_data) = arr[1].as_object() {
                                                let mut data_with_meta = serde_json::Map::new();
                                                for (k, v) in event_data {
                                                    data_with_meta.insert(k.clone(), v.clone());
                                                }
                                                data_with_meta.insert("_block_number".to_string(), Value::Number(data.block_number.into()));
                                                data_with_meta.insert("_timestamp".to_string(), Value::Number(data.timestamp.into()));
                                                data_with_meta.insert("_user".to_string(), Value::String(user.to_string()));

                                                if let Some(cb) = callbacks.read().get(&sub_id) {
                                                    cb(Value::Object(data_with_meta));
                                                }
                                            }
                                        }
                                    }
                                }
                            } else {
                                // No events, return raw data
                                let mut data_with_meta = parsed.as_object().cloned().unwrap_or_default();
                                data_with_meta.insert("_block_number".to_string(), Value::Number(data.block_number.into()));
                                data_with_meta.insert("_timestamp".to_string(), Value::Number(data.timestamp.into()));

                                if let Some(cb) = callbacks.read().get(&sub_id) {
                                    cb(Value::Object(data_with_meta));
                                }
                            }
                        }
                    }
                }
                Ok(None) => break,
                Err(e) => {
                    tracing::error!("Stream error: {}", e);
                    break;
                }
            }
        }
    }

    async fn stream_blocks(
        channel: Channel,
        token: &str,
        sub_id: u32,
        callbacks: &Arc<RwLock<HashMap<u32, Box<dyn Fn(Value) + Send + Sync>>>>,
        running: &Arc<AtomicBool>,
    ) {
        let token_value: MetadataValue<_> = token.parse().unwrap();
        let mut client = BlockStreamingClient::with_interceptor(channel, move |mut req: Request<()>| {
            req.metadata_mut().insert("x-token", token_value.clone());
            Ok(req)
        });

        let request = Timestamp {
            timestamp: chrono::Utc::now().timestamp_millis(),
        };

        let response = match client.stream_blocks(request).await {
            Ok(r) => r,
            Err(e) => {
                tracing::error!("StreamBlocks error: {}", e);
                return;
            }
        };

        let mut stream = response.into_inner();

        while running.load(Ordering::SeqCst) {
            match stream.message().await {
                Ok(Some(block)) => {
                    if let Ok(data) = serde_json::from_str::<Value>(&block.data_json) {
                        if let Some(cb) = callbacks.read().get(&sub_id) {
                            cb(data);
                        }
                    }
                }
                Ok(None) => break,
                Err(e) => {
                    tracing::error!("Block stream error: {}", e);
                    break;
                }
            }
        }
    }

    async fn stream_l2_book(
        channel: Channel,
        token: &str,
        sub_id: u32,
        sub_info: &GRPCSubscriptionInfo,
        callbacks: &Arc<RwLock<HashMap<u32, Box<dyn Fn(Value) + Send + Sync>>>>,
        running: &Arc<AtomicBool>,
    ) {
        let token_value: MetadataValue<_> = token.parse().unwrap();
        let mut client = OrderBookStreamingClient::with_interceptor(channel, move |mut req: Request<()>| {
            req.metadata_mut().insert("x-token", token_value.clone());
            Ok(req)
        });

        let request = L2BookRequest {
            coin: sub_info.coin.clone().unwrap_or_default(),
            n_levels: sub_info.n_levels.unwrap_or(20),
            n_sig_figs: sub_info.n_sig_figs,
            mantissa: None,
        };

        let response = match client.stream_l2_book(request).await {
            Ok(r) => r,
            Err(e) => {
                tracing::error!("StreamL2Book error: {}", e);
                return;
            }
        };

        let mut stream = response.into_inner();

        while running.load(Ordering::SeqCst) {
            match stream.message().await {
                Ok(Some(update)) => {
                    let bids: Vec<Value> = update
                        .bids
                        .iter()
                        .map(|l| serde_json::json!([l.px, l.sz, l.n]))
                        .collect();
                    let asks: Vec<Value> = update
                        .asks
                        .iter()
                        .map(|l| serde_json::json!([l.px, l.sz, l.n]))
                        .collect();

                    let data = serde_json::json!({
                        "coin": update.coin,
                        "time": update.time,
                        "block_number": update.block_number,
                        "bids": bids,
                        "asks": asks,
                    });

                    if let Some(cb) = callbacks.read().get(&sub_id) {
                        cb(data);
                    }
                }
                Ok(None) => break,
                Err(e) => {
                    tracing::error!("L2 book stream error: {}", e);
                    break;
                }
            }
        }
    }

    async fn stream_l4_book(
        channel: Channel,
        token: &str,
        sub_id: u32,
        sub_info: &GRPCSubscriptionInfo,
        callbacks: &Arc<RwLock<HashMap<u32, Box<dyn Fn(Value) + Send + Sync>>>>,
        running: &Arc<AtomicBool>,
    ) {
        let token_value: MetadataValue<_> = token.parse().unwrap();
        let mut client = OrderBookStreamingClient::with_interceptor(channel, move |mut req: Request<()>| {
            req.metadata_mut().insert("x-token", token_value.clone());
            Ok(req)
        });

        let request = L4BookRequest {
            coin: sub_info.coin.clone().unwrap_or_default(),
        };

        let response = match client.stream_l4_book(request).await {
            Ok(r) => r,
            Err(e) => {
                tracing::error!("StreamL4Book error: {}", e);
                return;
            }
        };

        let mut stream = response.into_inner();

        while running.load(Ordering::SeqCst) {
            match stream.message().await {
                Ok(Some(update)) => {
                    let data = if let Some(proto::l4_book_update::Update::Snapshot(snapshot)) = update.update {
                        let bids: Vec<Value> = snapshot.bids.iter().map(l4_order_to_json).collect();
                        let asks: Vec<Value> = snapshot.asks.iter().map(l4_order_to_json).collect();

                        serde_json::json!({
                            "type": "snapshot",
                            "coin": snapshot.coin,
                            "time": snapshot.time,
                            "height": snapshot.height,
                            "bids": bids,
                            "asks": asks,
                        })
                    } else if let Some(proto::l4_book_update::Update::Diff(diff)) = update.update {
                        let diff_data: Value = serde_json::from_str(&diff.data).unwrap_or(Value::Null);
                        serde_json::json!({
                            "type": "diff",
                            "time": diff.time,
                            "height": diff.height,
                            "data": diff_data,
                        })
                    } else {
                        continue;
                    };

                    if let Some(cb) = callbacks.read().get(&sub_id) {
                        cb(data);
                    }
                }
                Ok(None) => break,
                Err(e) => {
                    tracing::error!("L4 book stream error: {}", e);
                    break;
                }
            }
        }
    }
}

// ══════════════════════════════════════════════════════════════════════════════
// Helper Functions
// ══════════════════════════════════════════════════════════════════════════════

fn parse_endpoint(url: &str) -> (String, String) {
    let parsed = match url::Url::parse(url) {
        Ok(u) => u,
        Err(_) => return (String::new(), String::new()),
    };

    let host = parsed.host_str().unwrap_or("").to_string();

    // Extract token from path
    let path_parts: Vec<&str> = parsed.path().trim_matches('/').split('/').collect();
    let mut token = String::new();
    for part in path_parts {
        if !part.is_empty()
            && part != "info"
            && part != "hypercore"
            && part != "evm"
            && part != "nanoreth"
            && part != "ws"
        {
            token = part.to_string();
            break;
        }
    }

    (host, token)
}

fn l4_order_to_json(order: &proto::L4Order) -> Value {
    serde_json::json!({
        "user": order.user,
        "coin": order.coin,
        "side": order.side,
        "limit_px": order.limit_px,
        "sz": order.sz,
        "oid": order.oid,
        "timestamp": order.timestamp,
        "trigger_condition": order.trigger_condition,
        "is_trigger": order.is_trigger,
        "trigger_px": order.trigger_px,
        "is_position_tpsl": order.is_position_tpsl,
        "reduce_only": order.reduce_only,
        "order_type": order.order_type,
        "tif": order.tif,
        "cloid": order.cloid,
    })
}