digdigdig3 0.3.23

Unified async Rust API for 47 exchange connectors (REST + WebSocket). The core layer — pure ExchangeHub + connectors. Higher-level builder, persistence, replay, OB tracker live in `digdigdig3-station`.
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
//! BitMEX WebSocket frame parsers and REST response parsers.
//!
//! All parsers receive the full frame Value.  BitMEX push format:
//! ```json
//! {"table": "<topic>", "action": "partial"|"insert"|"update"|"delete", "data": [...]}
//! ```
//! Delta frames carry only changed fields — parsers use `?` / `continue` when a
//! required field is absent in a specific row (that row is silently skipped).

use chrono::DateTime;
use serde_json::Value;

use crate::core::types::{
    FundingRate, Kline, Liquidation, PublicTrade, SettlementEvent,
    StreamEvent, TradeSide, WebSocketError, WebSocketResult,
};
use crate::core::{ExchangeError, ExchangeResult};

// ─────────────────────────────────────────────────────────────────────────────
// Shared helpers
// ─────────────────────────────────────────────────────────────────────────────

/// Extract the "data" array from a BitMEX frame.
fn frame_data(raw: &Value) -> WebSocketResult<&Vec<Value>> {
    raw.get("data")
        .and_then(Value::as_array)
        .ok_or_else(|| WebSocketError::Parse("bitmex: frame missing 'data' array".into()))
}

/// Parse an ISO 8601 timestamp string to milliseconds since epoch.
fn iso_to_ms(s: &str) -> Option<i64> {
    DateTime::parse_from_rfc3339(s)
        .ok()
        .map(|dt| dt.timestamp_millis())
}

// Shared wasm-safe wall-clock helper.
use crate::core::utils::now_ms;

// ─────────────────────────────────────────────────────────────────────────────
// instrument channel — PredictedFunding
// ─────────────────────────────────────────────────────────────────────────────

/// Parse `instrument:{sym}` frame → `StreamEvent::PredictedFunding`.
///
/// BitMEX sends `indicativeFundingRate` (NOT `estimatedFundingRate`) as the
/// predicted next funding rate.  Delta updates include only changed fields, so
/// rows without `indicativeFundingRate` are silently skipped — that is normal
/// behaviour and is NOT an error.
pub fn parse_predicted_funding(raw: &Value) -> WebSocketResult<StreamEvent> {
    let data = frame_data(raw)?;

    for item in data {
        let symbol = match item.get("symbol").and_then(Value::as_str) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => continue,
        };

        let predicted_rate = match item.get("indicativeFundingRate").and_then(Value::as_f64) {
            Some(r) => r,
            None => continue, // field absent in this delta row
        };

        let next_funding_time = item
            .get("fundingTimestamp")
            .and_then(Value::as_str)
            .and_then(iso_to_ms)
            .unwrap_or(0);

        let timestamp = item
            .get("timestamp")
            .and_then(Value::as_str)
            .and_then(iso_to_ms)
            .unwrap_or_else(now_ms);

        // Return on first row that carries the field.  BitMEX perpetuals like
        // XBTUSD emit one-item data arrays on most updates.
        return Ok(StreamEvent::PredictedFunding {
            symbol,
            predicted: crate::core::types::PredictedFunding {
                predicted_rate,
                next_funding_time,
                timestamp,
            },
        });
    }

    Err(WebSocketError::FieldAbsent(
        "bitmex instrument: no row contained indicativeFundingRate".into(),
    ))
}

// ─────────────────────────────────────────────────────────────────────────────
// instrument channel — FundingRate
// ─────────────────────────────────────────────────────────────────────────────

/// Parse `instrument:{sym}` frame → `StreamEvent::FundingRate`.
///
/// `fundingRate` is the locked rate for the current 8h period.  It changes
/// once per period at 04:00 / 12:00 / 20:00 UTC.
pub fn parse_funding_rate(raw: &Value) -> WebSocketResult<StreamEvent> {
    let data = frame_data(raw)?;

    for item in data {
        let symbol = match item.get("symbol").and_then(Value::as_str) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => continue,
        };

        let rate = match item.get("fundingRate").and_then(Value::as_f64) {
            Some(r) => r,
            None => continue,
        };

        let next_funding_time = item
            .get("fundingTimestamp")
            .and_then(Value::as_str)
            .and_then(iso_to_ms);

        let timestamp = item
            .get("timestamp")
            .and_then(Value::as_str)
            .and_then(iso_to_ms)
            .unwrap_or_else(now_ms);

        return Ok(StreamEvent::FundingRate {
            symbol,
            funding: crate::core::types::FundingRate {
                rate,
                next_funding_time,
                timestamp,
                ..Default::default()
            },
        });
    }

    Err(WebSocketError::FieldAbsent(
        "bitmex instrument: no row contained fundingRate".into(),
    ))
}

// ─────────────────────────────────────────────────────────────────────────────
// instrument channel — MarkPrice
// ─────────────────────────────────────────────────────────────────────────────

/// Parse `instrument:{sym}` frame → `StreamEvent::MarkPrice`.
pub fn parse_mark_price(raw: &Value) -> WebSocketResult<StreamEvent> {
    let data = frame_data(raw)?;

    for item in data {
        let symbol = match item.get("symbol").and_then(Value::as_str) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => continue,
        };

        let mark_price = match item.get("markPrice").and_then(Value::as_f64) {
            Some(p) => p,
            None => continue,
        };

        let index_price = item.get("indexPrice").and_then(Value::as_f64);

        // BitMEX-specific: indicative settlement price and indicative funding rate
        // (predicted values for the next funding/settlement cycle).
        let indicative_settle_price = item
            .get("indicativeSettlePrice")
            .and_then(Value::as_f64);
        let indicative_funding_rate = item
            .get("indicativeFundingRate")
            .and_then(Value::as_f64);

        let timestamp = item
            .get("timestamp")
            .and_then(Value::as_str)
            .and_then(iso_to_ms)
            .unwrap_or_else(now_ms);

        return Ok(StreamEvent::MarkPrice {
            symbol,
            mark: crate::core::types::MarkPrice {
                mark_price,
                index_price,
                indicative_settle_price,
                indicative_funding_rate,
                timestamp,
                ..Default::default()
            },
        });
    }

    Err(WebSocketError::FieldAbsent(
        "bitmex instrument: no row contained markPrice".into(),
    ))
}

// ─────────────────────────────────────────────────────────────────────────────
// instrument channel — OpenInterest
// ─────────────────────────────────────────────────────────────────────────────

/// Parse `instrument:{sym}` frame → `StreamEvent::OpenInterestUpdate`.
///
/// BitMEX `instrument` channel carries `openInterest` (number of open contracts)
/// and `openValue` (value in satoshis).  Like all instrument fields, these appear
/// only in delta rows that actually changed — rows without `openInterest` are
/// silently skipped so we never emit a bogus 0.
pub fn parse_open_interest(raw: &Value) -> WebSocketResult<StreamEvent> {
    let data = frame_data(raw)?;

    for item in data {
        let symbol = match item.get("symbol").and_then(Value::as_str) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => continue,
        };

        let open_interest = match item.get("openInterest").and_then(Value::as_f64) {
            Some(oi) => oi,
            None => continue, // field absent in this delta row — normal for partial updates
        };

        let open_interest_value = item.get("openValue").and_then(Value::as_f64);

        let timestamp = item
            .get("timestamp")
            .and_then(Value::as_str)
            .and_then(iso_to_ms)
            .unwrap_or_else(now_ms);

        return Ok(StreamEvent::OpenInterestUpdate {
            symbol,
            open_interest: crate::core::types::OpenInterest {
                open_interest,
                open_interest_value,
                timestamp,
                ..Default::default()
            },
        });
    }

    Err(WebSocketError::FieldAbsent(
        "bitmex instrument: no row contained openInterest".into(),
    ))
}

// ─────────────────────────────────────────────────────────────────────────────
// instrument channel — IndexPrice
// ─────────────────────────────────────────────────────────────────────────────

/// Parse `instrument:{sym}` frame → `StreamEvent::IndexPrice`.
pub fn parse_index_price(raw: &Value) -> WebSocketResult<StreamEvent> {
    let data = frame_data(raw)?;

    for item in data {
        let symbol = match item.get("symbol").and_then(Value::as_str) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => continue,
        };

        // BitMEX uses `indicativeSettlePrice` as the index / spot reference price.
        // `indexPrice` is also available on some instruments.
        let price = match item
            .get("indexPrice")
            .or_else(|| item.get("indicativeSettlePrice"))
            .and_then(Value::as_f64)
        {
            Some(p) => p,
            None => continue,
        };

        let timestamp = item
            .get("timestamp")
            .and_then(Value::as_str)
            .and_then(iso_to_ms)
            .unwrap_or_else(now_ms);

        return Ok(StreamEvent::IndexPrice {
            symbol,
            index_price: crate::core::types::IndexPrice { price, timestamp, ..Default::default() },
        });
    }

    Err(WebSocketError::FieldAbsent(
        "bitmex instrument: no row contained indexPrice or indicativeSettlePrice".into(),
    ))
}

// ─────────────────────────────────────────────────────────────────────────────
// trade channel
// ─────────────────────────────────────────────────────────────────────────────

/// Parse `trade:{sym}` frame → `StreamEvent::Trade`.
///
/// BitMEX trade frames always use `"action": "insert"`.  Data is an array —
/// we return on the first valid row (caller dispatches once per frame).
pub fn parse_trade(raw: &Value) -> WebSocketResult<StreamEvent> {
    use crate::core::types::PublicTrade;

    let data = frame_data(raw)?;

    for item in data {
        let symbol = match item.get("symbol").and_then(Value::as_str) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => continue,
        };

        let price = match item.get("price").and_then(Value::as_f64) {
            Some(p) => p,
            None => continue,
        };

        let quantity = item
            .get("size")
            .and_then(Value::as_f64)
            .unwrap_or(0.0);

        let side = item
            .get("side")
            .and_then(Value::as_str)
            .map(|s| if s == "Buy" { TradeSide::Buy } else { TradeSide::Sell })
            .unwrap_or(TradeSide::Buy);

        let timestamp = item
            .get("timestamp")
            .and_then(Value::as_str)
            .and_then(iso_to_ms)
            .unwrap_or_else(now_ms);

        let trade_id = item
            .get("trdMatchID")
            .and_then(Value::as_str)
            .unwrap_or("")
            .to_string();

        let gross_value = item.get("grossValue").and_then(Value::as_f64);
        let home_notional = item.get("homeNotional").and_then(Value::as_f64);
        let foreign_notional = item.get("foreignNotional").and_then(Value::as_f64);
        let tick_direction = item
            .get("tickDirection")
            .and_then(Value::as_str)
            .map(|s| s.to_string());
        let trade_type = item
            .get("trdType")
            .and_then(Value::as_str)
            .map(|s| s.to_string());
        let pool = item
            .get("pool")
            .and_then(Value::as_str)
            .map(|s| s.to_string());

        let trade = PublicTrade {
            id: trade_id,
            price,
            quantity,
            side,
            timestamp,
            gross_value,
            home_notional,
            foreign_notional,
            tick_direction,
            trade_type,
            pool,
            ..Default::default()
        };

        return Ok(StreamEvent::Trade { symbol, trade });
    }

    Err(WebSocketError::Parse("bitmex trade: empty or invalid data array".into()))
}

// ─────────────────────────────────────────────────────────────────────────────
// quote channel
// ─────────────────────────────────────────────────────────────────────────────

/// Parse `quote:{sym}` frame → `StreamEvent::Ticker`.
///
/// BitMEX `quote` channel provides best bid/ask + sizes.
pub fn parse_quote(raw: &Value) -> WebSocketResult<StreamEvent> {
    use crate::core::types::Ticker;

    let data = frame_data(raw)?;

    for item in data {
        let symbol = match item.get("symbol").and_then(Value::as_str) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => continue,
        };

        let bid_price = item.get("bidPrice").and_then(Value::as_f64);
        let ask_price = item.get("askPrice").and_then(Value::as_f64);

        // BitMEX quote channel also carries top-of-book sizes.
        let bid_qty = item.get("bidSize").and_then(Value::as_f64);
        let ask_qty = item.get("askSize").and_then(Value::as_f64);

        let timestamp = item
            .get("timestamp")
            .and_then(Value::as_str)
            .and_then(iso_to_ms)
            .unwrap_or_else(now_ms);

        let ticker = Ticker {
            last_price: bid_price.or(ask_price).unwrap_or(0.0),
            bid_price,
            ask_price,
            bid_qty,
            ask_qty,
            high_24h: None,
            low_24h: None,
            volume_24h: None,
            quote_volume_24h: None,
            price_change_24h: None,
            price_change_percent_24h: None,
            timestamp, ..Default::default()
        };

        return Ok(StreamEvent::Ticker { symbol, ticker });
    }

    Err(WebSocketError::Parse("bitmex quote: empty or invalid data array".into()))
}

// ─────────────────────────────────────────────────────────────────────────────
// liquidation channel
// ─────────────────────────────────────────────────────────────────────────────

/// Parse `liquidation` (global) frame → `StreamEvent::Liquidation`.
pub fn parse_liquidation(raw: &Value) -> WebSocketResult<StreamEvent> {
    let data = frame_data(raw)?;

    for item in data {
        let symbol = match item.get("symbol").and_then(Value::as_str) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => continue,
        };

        let price = match item.get("price").and_then(Value::as_f64) {
            Some(p) => p,
            None => continue,
        };

        let quantity = item
            .get("leavesQty")
            .and_then(Value::as_f64)
            .unwrap_or(0.0);

        // BitMEX `side` on a liquidation is the direction of the liquidation order
        // (opposite to the position that was liquidated).
        let side = item
            .get("side")
            .and_then(Value::as_str)
            .map(|s| if s == "Buy" { TradeSide::Buy } else { TradeSide::Sell })
            .unwrap_or(TradeSide::Sell);

        // BitMEX WS liquidation carries the exchange order ID.
        let order_id = item
            .get("orderID")
            .and_then(Value::as_str)
            .filter(|s| !s.is_empty())
            .map(|s| s.to_string());

        let sym = symbol;
        return Ok(StreamEvent::Liquidation {
            symbol: sym.clone(),
            liquidation: crate::core::types::Liquidation {
                symbol: sym,
                side,
                price,
                quantity,
                order_id,
                value: None,
                timestamp: now_ms(),
                ..Default::default()
            },
        });
    }

    Err(WebSocketError::Parse("bitmex liquidation: empty or invalid data array".into()))
}

// ─────────────────────────────────────────────────────────────────────────────
// funding channel — settlement events
// ─────────────────────────────────────────────────────────────────────────────

/// Parse `funding:{sym}` frame → `StreamEvent::FundingSettlement`.
///
/// Published at each 8-hour funding settlement (04:00, 12:00, 20:00 UTC).
pub fn parse_funding_settled(raw: &Value) -> WebSocketResult<StreamEvent> {
    let data = frame_data(raw)?;

    for item in data {
        let symbol = match item.get("symbol").and_then(Value::as_str) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => continue,
        };

        let settled_rate = match item.get("fundingRate").and_then(Value::as_f64) {
            Some(r) => r,
            None => continue,
        };

        let settlement_time = item
            .get("timestamp")
            .and_then(Value::as_str)
            .and_then(iso_to_ms)
            .unwrap_or(0);

        let timestamp = settlement_time;

        return Ok(StreamEvent::FundingSettlement {
            symbol,
            settlement: crate::core::types::FundingSettlement {
                settled_rate,
                settlement_time,
                timestamp,
            },
        });
    }

    Err(WebSocketError::Parse("bitmex funding: empty or invalid data array".into()))
}

// ─────────────────────────────────────────────────────────────────────────────
// settlement channel — SettlementEvent
// ─────────────────────────────────────────────────────────────────────────────

/// Parse `settlement` (global) frame → `StreamEvent::SettlementEvent`.
///
/// BitMEX publishes this channel when a futures or options contract settles
/// at expiry.  Each row carries: `symbol`, `settlementType` ("Settlement"/
/// "Funding"), `settledPrice`, `taxBase`, `taxRate`, `timestamp`.
pub fn parse_settlement_event(raw: &Value) -> WebSocketResult<StreamEvent> {
    let data = frame_data(raw)?;

    for item in data {
        let symbol = match item.get("symbol").and_then(Value::as_str) {
            Some(s) if !s.is_empty() => s.to_string(),
            _ => continue,
        };

        let settlement_price = match item.get("settledPrice").and_then(Value::as_f64) {
            Some(p) => p,
            None => continue,
        };

        let settlement_time = item
            .get("timestamp")
            .and_then(Value::as_str)
            .and_then(iso_to_ms)
            .unwrap_or(0);

        let timestamp = settlement_time;

        let settlement_type = item
            .get("settlementType")
            .and_then(Value::as_str)
            .filter(|s| !s.is_empty())
            .map(|s| s.to_string());

        let settled_price = item.get("settledPrice").and_then(Value::as_f64);
        let tax_base = item.get("taxBase").and_then(Value::as_f64);
        let tax_rate = item.get("taxRate").and_then(Value::as_f64);

        return Ok(StreamEvent::SettlementEvent {
            symbol: symbol.clone(),
            settlement: SettlementEvent {
                settlement_price,
                settlement_time,
                timestamp,
                symbol: Some(symbol),
                settlement_type,
                settled_price,
                tax_base,
                tax_rate,
            },
        });
    }

    Err(WebSocketError::Parse(
        "bitmex settlement: empty or invalid data array".into(),
    ))
}

// ─────────────────────────────────────────────────────────────────────────────
// REST parsers
// ─────────────────────────────────────────────────────────────────────────────

/// Parse `GET /api/v1/trade` response → `Vec<PublicTrade>`.
///
/// BitMEX REST trade response is a JSON array (not wrapped).
/// Each element: `{timestamp, symbol, side, size, price, trdMatchID, …}`.
/// The `timestamp` field is an ISO-8601 string — converted to epoch ms.
pub fn parse_rest_recent_trades(v: &Value) -> ExchangeResult<Vec<PublicTrade>> {
    let arr = v
        .as_array()
        .ok_or_else(|| ExchangeError::Parse("bitmex recent_trades: expected array".into()))?;

    let trades = arr
        .iter()
        .filter_map(|item| {
            let price = item.get("price")?.as_f64()?;
            let quantity = item.get("size")?.as_f64().unwrap_or(0.0);
            let side = item
                .get("side")
                .and_then(Value::as_str)
                .map(|s| if s == "Buy" { TradeSide::Buy } else { TradeSide::Sell })
                .unwrap_or(TradeSide::Buy);
            let timestamp = item
                .get("timestamp")
                .and_then(Value::as_str)
                .and_then(iso_to_ms)
                .unwrap_or(0);
            let id = item
                .get("trdMatchID")
                .and_then(Value::as_str)
                .unwrap_or("")
                .to_string();
            let gross_value = item.get("grossValue").and_then(Value::as_f64);
            let home_notional = item.get("homeNotional").and_then(Value::as_f64);
            let foreign_notional = item.get("foreignNotional").and_then(Value::as_f64);
            let tick_direction = item
                .get("tickDirection")
                .and_then(Value::as_str)
                .map(|s| s.to_string());
            let trade_type = item
                .get("trdType")
                .and_then(Value::as_str)
                .map(|s| s.to_string());
            let pool = item
                .get("pool")
                .and_then(Value::as_str)
                .map(|s| s.to_string());
            Some(PublicTrade {
                id,
                price,
                quantity,
                side,
                timestamp,
                gross_value,
                home_notional,
                foreign_notional,
                tick_direction,
                trade_type,
                pool,
                ..Default::default()
            })
        })
        .collect();

    Ok(trades)
}

/// Parse `GET /api/v1/trade/bucketed` response → `Vec<Kline>`.
///
/// BitMEX bucket `timestamp` = **end** of the period.
/// `open_time` = `timestamp − bin_size_ms` so callers get the period open time.
///
/// `bin_size_ms` must be the millisecond duration of the requested `binSize`
/// (use `endpoints::bin_size_duration_ms`). Results arrive newest-first
/// when `reverse=true`; this function reverses to oldest-first.
pub fn parse_rest_klines(v: &Value, bin_size_ms: i64) -> ExchangeResult<Vec<Kline>> {
    let arr = v
        .as_array()
        .ok_or_else(|| ExchangeError::Parse("bitmex klines: expected array".into()))?;

    let mut klines: Vec<Kline> = arr
        .iter()
        .filter_map(|item| {
            // timestamp = end of bucket (ISO-8601 string)
            let close_ts = item
                .get("timestamp")
                .and_then(Value::as_str)
                .and_then(iso_to_ms)?;
            let open_time = close_ts - bin_size_ms;

            let open  = item.get("open")?.as_f64()?;
            let high  = item.get("high")?.as_f64()?;
            let low   = item.get("low")?.as_f64()?;
            let close = item.get("close")?.as_f64()?;
            // BitMEX "volume" = foreignNotional (USD notional for XBTUSD).
            // homeNotional (BTC) is the volume in base asset.
            let volume = item
                .get("homeNotional")
                .and_then(Value::as_f64)
                .unwrap_or_else(|| item.get("volume").and_then(Value::as_f64).unwrap_or(0.0));
            let quote_volume = item.get("foreignNotional").and_then(Value::as_f64);
            let trades = item.get("trades").and_then(Value::as_u64);
            let vwap = item.get("vwap").and_then(Value::as_f64);
            // lastSize = size of the last trade in the bucket (BitMEX-specific).
            let last_size = item.get("lastSize").and_then(Value::as_f64);

            Some(Kline {
                open_time,
                open,
                high,
                low,
                close,
                volume,
                quote_volume,
                close_time: Some(close_ts),
                trades,
                vwap,
                last_size,
                ..Default::default()
            })
        })
        .collect();

    // `reverse=true` → newest first from API; flip to oldest-first for callers.
    klines.reverse();

    Ok(klines)
}

/// Parse `GET /api/v1/funding` response → `Vec<FundingRate>`.
///
/// Each element: `{timestamp, symbol, fundingRate, fundingRateDaily, …}`.
/// The `timestamp` field is an ISO-8601 string — converted to epoch ms.
pub fn parse_rest_funding_rate_history(v: &Value) -> ExchangeResult<Vec<FundingRate>> {
    let arr = v
        .as_array()
        .ok_or_else(|| ExchangeError::Parse("bitmex funding_history: expected array".into()))?;

    let rates = arr
        .iter()
        .filter_map(|item| {
            let rate = item.get("fundingRate")?.as_f64()?;
            let timestamp = item
                .get("timestamp")
                .and_then(Value::as_str)
                .and_then(iso_to_ms)
                .unwrap_or(0);
            let symbol = item
                .get("symbol")
                .and_then(Value::as_str)
                .filter(|s| !s.is_empty())
                .map(|s| s.to_string());
            Some(FundingRate {
                rate,
                next_funding_time: None,
                timestamp,
                symbol,
                ..Default::default()
            })
        })
        .collect();

    Ok(rates)
}

/// Parse `GET /api/v1/liquidation` response → `Vec<Liquidation>`.
///
/// BitMEX liquidation endpoint returns `[]` when there are no recent forced
/// closes — that is normal; an empty result is NOT an error.
/// Each element: `{orderID, symbol, side, price, leavesQty}`.
/// NOTE: `side` on a BitMEX liquidation order is the direction of the
/// forced-close (Buy = short being liquidated; Sell = long being liquidated).
/// We invert to `Liquidation.side` convention where `Buy` means a long
/// position was liquidated.
pub fn parse_rest_liquidation_history(v: &Value, symbol: &str) -> ExchangeResult<Vec<Liquidation>> {
    let arr = v
        .as_array()
        .ok_or_else(|| ExchangeError::Parse("bitmex liquidation: expected array".into()))?;

    let liq: Vec<Liquidation> = arr
        .iter()
        .filter_map(|item| {
            let price    = item.get("price")?.as_f64()?;
            let quantity = item.get("leavesQty")?.as_f64().unwrap_or(0.0);
            // BitMEX: side = direction of the liquidation ORDER (opposite to position).
            // "Buy"  liq order → short position was liquidated → our side = Sell.
            // "Sell" liq order → long  position was liquidated → our side = Buy.
            let side = item
                .get("side")
                .and_then(Value::as_str)
                .map(|s| if s == "Buy" { TradeSide::Sell } else { TradeSide::Buy })
                .unwrap_or(TradeSide::Sell);
            // BitMEX liquidation REST rows DO carry an orderID field.
            let order_id = item
                .get("orderID")
                .and_then(Value::as_str)
                .filter(|s| !s.is_empty())
                .map(|s| s.to_string());
            // BitMEX liquidation REST rows do NOT carry a timestamp field —
            // the endpoint is intended as a snapshot of the current liquidation
            // queue, not a historical log. We use 0 as sentinel.
            Some(Liquidation {
                symbol: symbol.to_string(),
                side,
                price,
                quantity,
                order_id,
                timestamp: 0,
                value: None,
                ..Default::default()
            })
        })
        .collect();

    Ok(liq)
}

// ─────────────────────────────────────────────────────────────────────────────
// Tests
// ─────────────────────────────────────────────────────────────────────────────

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

    fn instrument_frame(symbol: &str, indicative_rate: f64, funding_ts: &str, ts: &str) -> Value {
        serde_json::json!({
            "table": "instrument",
            "action": "update",
            "data": [{
                "symbol": symbol,
                "indicativeFundingRate": indicative_rate,
                "fundingTimestamp": funding_ts,
                "timestamp": ts
            }]
        })
    }

    #[test]
    fn parse_open_interest_extracts_oi_and_value() {
        let frame = serde_json::json!({
            "table": "instrument",
            "action": "update",
            "data": [{
                "symbol": "XBTUSD",
                "openInterest": 123456789_u64,
                "openValue": 8765432100_u64,
                "timestamp": "2024-01-01T12:00:00.000Z"
            }]
        });
        let event = parse_open_interest(&frame).expect("should parse OpenInterestUpdate");
        match event {
            StreamEvent::OpenInterestUpdate { symbol, open_interest } => {
                assert_eq!(symbol, "XBTUSD");
                assert!((open_interest.open_interest - 123_456_789.0).abs() < 1.0);
                assert!(open_interest.open_interest_value.is_some());
                assert!((open_interest.open_interest_value.unwrap() - 8_765_432_100.0).abs() < 1.0);
                assert!(open_interest.timestamp > 0);
            }
            other => panic!("expected OpenInterestUpdate, got {:?}", other),
        }
    }

    #[test]
    fn parse_open_interest_missing_field_returns_field_absent() {
        // Partial-update frame without openInterest — must not emit OI=0.
        let frame = serde_json::json!({
            "table": "instrument",
            "action": "update",
            "data": [{"symbol": "XBTUSD", "markPrice": 45200.0, "timestamp": "2024-01-01T07:45:00.000Z"}]
        });
        let err = parse_open_interest(&frame).expect_err("should return FieldAbsent");
        assert!(
            matches!(err, WebSocketError::FieldAbsent(_)),
            "expected FieldAbsent, got {:?}", err
        );
    }

    #[test]
    fn parse_predicted_funding_extracts_indicative_rate() {
        let frame = instrument_frame(
            "XBTUSD",
            0.000085,
            "2024-01-01T08:00:00.000Z",
            "2024-01-01T07:45:00.123Z",
        );

        let event = parse_predicted_funding(&frame).expect("should parse PredictedFunding");
        match event {
            StreamEvent::PredictedFunding {
                symbol,
                predicted,
            } => {
                assert_eq!(symbol, "XBTUSD");
                assert!((predicted.predicted_rate - 0.000085).abs() < 1e-12, "rate mismatch");
                assert!(predicted.next_funding_time > 0, "next_funding_time must be set");
                assert!(predicted.timestamp > 0, "timestamp must be set");
            }
            other => panic!("expected PredictedFunding, got {:?}", other),
        }
    }

    #[test]
    fn parse_predicted_funding_skips_row_without_field() {
        // Delta frame without indicativeFundingRate — should return FieldAbsent
        let frame = serde_json::json!({
            "table": "instrument",
            "action": "update",
            "data": [{"symbol": "XBTUSD", "markPrice": 45200.0, "timestamp": "2024-01-01T07:45:00.000Z"}]
        });
        let err = parse_predicted_funding(&frame).expect_err("should return FieldAbsent");
        assert!(
            matches!(err, WebSocketError::FieldAbsent(_)),
            "expected FieldAbsent, got {:?}", err
        );
    }

    #[test]
    fn parse_funding_rate_extracts_funding_rate_field() {
        let frame = serde_json::json!({
            "table": "instrument",
            "action": "update",
            "data": [{
                "symbol": "XBTUSD",
                "fundingRate": 0.0001,
                "fundingTimestamp": "2024-01-01T08:00:00.000Z",
                "timestamp": "2024-01-01T04:00:00.000Z"
            }]
        });
        let event = parse_funding_rate(&frame).expect("should parse FundingRate");
        match event {
            StreamEvent::FundingRate { symbol, funding } => {
                assert_eq!(symbol, "XBTUSD");
                assert!((funding.rate - 0.0001).abs() < 1e-12);
                assert!(funding.next_funding_time.is_some());
            }
            other => panic!("expected FundingRate, got {:?}", other),
        }
    }

    #[test]
    fn parse_trade_extracts_price_qty_side() {
        let frame = serde_json::json!({
            "table": "trade",
            "action": "insert",
            "data": [{
                "symbol": "XBTUSD",
                "side": "Buy",
                "size": 100,
                "price": 45200.0,
                "trdMatchID": "abc-123",
                "timestamp": "2024-01-01T00:00:00.123Z"
            }]
        });
        let event = parse_trade(&frame).expect("should parse Trade");
        match event {
            StreamEvent::Trade { symbol, trade } => {
                assert_eq!(symbol, "XBTUSD");
                assert!((trade.price - 45200.0).abs() < 1e-6);
                assert_eq!(trade.side, TradeSide::Buy);
                assert_eq!(trade.id, "abc-123");
            }
            other => panic!("expected Trade, got {:?}", other),
        }
    }

    #[test]
    fn parse_funding_settled_extracts_settled_rate() {
        let frame = serde_json::json!({
            "table": "funding",
            "action": "insert",
            "data": [{
                "symbol": "XBTUSD",
                "fundingRate": 0.0001,
                "fundingInterval": "2000-01-01T08:00:00.000Z",
                "fundingRateDaily": 0.0003,
                "timestamp": "2024-01-01T08:00:00.000Z"
            }]
        });
        let event = parse_funding_settled(&frame).expect("should parse FundingSettlement");
        match event {
            StreamEvent::FundingSettlement { symbol, settlement } => {
                assert_eq!(symbol, "XBTUSD");
                assert!((settlement.settled_rate - 0.0001).abs() < 1e-12);
            }
            other => panic!("expected FundingSettlement, got {:?}", other),
        }
    }

    // ─── REST parser tests ────────────────────────────────────────────────────

    #[test]
    fn iso_to_ms_parses_bitmex_timestamp_format() {
        // Live payload timestamp format: "2026-06-14T15:20:10.445Z"
        let ms = iso_to_ms("2026-06-14T15:20:10.445Z").expect("must parse");
        // chrono-computed value: 1781450410445 ms
        assert_eq!(ms, 1_781_450_410_445);
        // Sanity: must be > 1.7 trillion (well into 2026)
        assert!(ms > 1_700_000_000_000);
    }

    #[test]
    fn parse_rest_recent_trades_basic() {
        let raw = serde_json::json!([{
            "timestamp": "2026-06-14T15:20:10.445Z",
            "symbol": "XBTUSD",
            "side": "Buy",
            "size": 1300,
            "price": 63933.8,
            "trdMatchID": "00000000-006d-1000-0000-0032e5114348",
            "grossValue": 2033356,
            "homeNotional": 0.02033356,
            "foreignNotional": 1300,
            "trdType": "Regular"
        }]);
        let trades = parse_rest_recent_trades(&raw).expect("should parse trades");
        assert_eq!(trades.len(), 1);
        let t = &trades[0];
        assert_eq!(t.id, "00000000-006d-1000-0000-0032e5114348");
        assert!((t.price - 63933.8).abs() < 1e-6);
        assert!((t.quantity - 1300.0).abs() < 1e-6);
        assert_eq!(t.side, TradeSide::Buy);
        assert_eq!(t.timestamp, 1_781_450_410_445);
    }

    #[test]
    fn parse_rest_recent_trades_sell_side() {
        let raw = serde_json::json!([{
            "timestamp": "2026-06-14T15:20:10.000Z",
            "symbol": "XBTUSD",
            "side": "Sell",
            "size": 500,
            "price": 63900.0,
            "trdMatchID": "aabbcc",
        }]);
        let trades = parse_rest_recent_trades(&raw).expect("should parse");
        assert_eq!(trades[0].side, TradeSide::Sell);
    }

    #[test]
    fn parse_rest_klines_open_time_is_bucket_open() {
        // Live bucketed payload: timestamp = END of period.
        // binSize=1m → 60_000 ms; open_time = close_ts − 60_000.
        let raw = serde_json::json!([{
            "timestamp": "2026-06-14T15:20:00.000Z",
            "symbol": "XBTUSD",
            "open": 63996.3,
            "high": 63996.2,
            "low": 63933.4,
            "close": 63940.1,
            "trades": 176,
            "volume": 1664700,
            "vwap": 63944.3428,
            "lastSize": 100,
            "turnover": 2603365749_u64,
            "homeNotional": 26.03,
            "foreignNotional": 1664700
        }]);
        // reverse=true: single item, nothing to flip
        let klines = parse_rest_klines(&raw, 60_000).expect("should parse");
        assert_eq!(klines.len(), 1);
        let k = &klines[0];
        // close timestamp ms for "2026-06-14T15:20:00.000Z"
        let close_ts = iso_to_ms("2026-06-14T15:20:00.000Z").unwrap();
        assert_eq!(k.close_time, Some(close_ts));
        assert_eq!(k.open_time, close_ts - 60_000);
        assert!((k.open  - 63996.3).abs() < 1e-6);
        assert!((k.high  - 63996.2).abs() < 1e-6);
        assert!((k.low   - 63933.4).abs() < 1e-6);
        assert!((k.close - 63940.1).abs() < 1e-6);
        // volume = homeNotional (BTC)
        assert!((k.volume - 26.03).abs() < 1e-6);
        assert_eq!(k.trades, Some(176));
    }

    #[test]
    fn parse_rest_klines_reverses_to_oldest_first() {
        // API returns newest-first (reverse=true). After parsing: oldest-first.
        let raw = serde_json::json!([
            // newer bucket (comes first in API response)
            {"timestamp": "2026-06-14T15:20:00.000Z", "open": 2.0, "high": 2.0, "low": 2.0, "close": 2.0, "homeNotional": 1.0},
            // older bucket
            {"timestamp": "2026-06-14T15:19:00.000Z", "open": 1.0, "high": 1.0, "low": 1.0, "close": 1.0, "homeNotional": 1.0},
        ]);
        let klines = parse_rest_klines(&raw, 60_000).expect("should parse");
        // After reverse: older bucket first
        assert!((klines[0].open - 1.0).abs() < 1e-9);
        assert!((klines[1].open - 2.0).abs() < 1e-9);
    }

    #[test]
    fn parse_rest_funding_rate_history_basic() {
        let raw = serde_json::json!([{
            "timestamp": "2026-06-14T12:00:00.000Z",
            "symbol": "XBTUSD",
            "fundingInterval": "2000-01-01T08:00:00.000Z",
            "fundingRate": -0.000133,
            "fundingRateDaily": -0.000399
        }]);
        let rates = parse_rest_funding_rate_history(&raw).expect("should parse");
        assert_eq!(rates.len(), 1);
        assert!((rates[0].rate - (-0.000133)).abs() < 1e-10);
        assert!(rates[0].timestamp > 0);
    }

    #[test]
    fn parse_rest_liquidation_history_empty_array_is_ok() {
        // BitMEX returns [] when no recent liquidations — must not error.
        let raw = serde_json::json!([]);
        let liq = parse_rest_liquidation_history(&raw, "XBTUSD").expect("empty ok");
        assert!(liq.is_empty());
    }

    #[test]
    fn parse_rest_liquidation_history_side_inversion() {
        // BitMEX "Buy" liq order = short was liquidated → our side = Sell.
        let raw = serde_json::json!([{
            "orderID": "abc",
            "symbol": "XBTUSD",
            "side": "Buy",
            "price": 63000.0,
            "leavesQty": 100
        }]);
        let liq = parse_rest_liquidation_history(&raw, "XBTUSD").expect("should parse");
        assert_eq!(liq.len(), 1);
        assert_eq!(liq[0].side, TradeSide::Sell);
        assert!((liq[0].price - 63000.0).abs() < 1e-6);
    }
}