rustrade-data 0.11.0

High performance & normalised WebSocket intergration for leading cryptocurrency exchanges - batteries included.
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
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
use crate::{
    exchange::Connector, instrument::InstrumentData, subscription::candle::CandleInterval,
};
use derive_more::Display;
use fnv::FnvHashMap;
use rustrade_instrument::{
    Keyed,
    asset::name::AssetNameInternal,
    exchange::ExchangeId,
    instrument::market_data::{MarketDataInstrument, kind::MarketDataInstrumentKind},
};
use rustrade_integration::{
    Validator, error::SocketError, protocol::websocket::WsMessage, subscription::SubscriptionId,
};
use serde::{Deserialize, Serialize};
use smol_str::{ToSmolStr, format_smolstr};
use std::{borrow::Borrow, fmt::Debug, hash::Hash};

/// OrderBook [`SubscriptionKind`]s and the associated Barter output data models.
pub mod book;

/// Candle [`SubscriptionKind`] and the associated Barter output data model.
pub mod candle;

/// Option Greeks data model for options analytics.
pub mod greeks;

/// Liquidation [`SubscriptionKind`] and the associated Barter output data model.
pub mod liquidation;

/// Quote [`SubscriptionKind`] and the associated Barter output data model.
pub mod quote;

/// Public trade [`SubscriptionKind`] and the associated Barter output data model.
pub mod trade;

/// Defines kind of a [`Subscription`], and the output [`Self::Event`] that it yields.
pub trait SubscriptionKind
where
    Self: Debug + Clone,
{
    type Event: Debug;
    fn as_str(&self) -> &'static str;
}

/// Barter [`Subscription`] used to subscribe to a [`SubscriptionKind`] for a particular exchange
/// [`MarketDataInstrument`].
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
pub struct Subscription<Exchange = ExchangeId, Inst = MarketDataInstrument, Kind = SubKind> {
    pub exchange: Exchange,
    #[serde(flatten)]
    pub instrument: Inst,
    #[serde(alias = "type")]
    pub kind: Kind,
}

pub fn display_subscriptions_without_exchange<Exchange, Instrument, Kind>(
    subscriptions: &[Subscription<Exchange, Instrument, Kind>],
) -> String
where
    Instrument: std::fmt::Display,
    Kind: std::fmt::Display,
{
    subscriptions
        .iter()
        .map(
            |Subscription {
                 exchange: _,
                 instrument,
                 kind,
             }| { format_smolstr!("({instrument}, {kind})") },
        )
        .collect::<Vec<_>>()
        .join(",")
}

impl<Exchange, Instrument, Kind> std::fmt::Display for Subscription<Exchange, Instrument, Kind>
where
    Exchange: std::fmt::Display,
    Instrument: std::fmt::Display,
    Kind: std::fmt::Display,
{
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "({}|{}|{})", self.exchange, self.kind, self.instrument)
    }
}

#[derive(
    Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Display, Deserialize, Serialize,
)]
pub enum SubKind {
    PublicTrades,
    OrderBooksL1,
    OrderBooksL2,
    OrderBooksL3,
    Liquidations,
    /// Candle (kline) subscription at a specific [`CandleInterval`] resolution.
    ///
    /// Unlike the other [`SubKind`]s, a candle subscription is **not** fully specified
    /// without a resolution — there is no venue-agnostic "default" candle stream — so the
    /// [`interval`](CandleInterval) is carried in the variant. The `derive_more::Display`
    /// tag is the fixed `"candles"` (independent of the interval), matching the typed
    /// [`Candles`](candle::Candles) kind tag.
    #[display("candles")]
    Candles {
        interval: CandleInterval,
    },
    /// Real-time top-of-book quotes (best bid/ask). Generic subscription kind
    /// that may be supported by multiple exchanges providing quote data.
    Quotes,
}

impl<Exchange, S, Kind> From<(Exchange, S, S, MarketDataInstrumentKind, Kind)>
    for Subscription<Exchange, MarketDataInstrument, Kind>
where
    S: Into<AssetNameInternal>,
{
    fn from(
        (exchange, base, quote, instrument_kind, kind): (
            Exchange,
            S,
            S,
            MarketDataInstrumentKind,
            Kind,
        ),
    ) -> Self {
        Self::new(exchange, (base, quote, instrument_kind), kind)
    }
}

impl<InstrumentKey, Exchange, S, Kind>
    From<(
        InstrumentKey,
        Exchange,
        S,
        S,
        MarketDataInstrumentKind,
        Kind,
    )> for Subscription<Exchange, Keyed<InstrumentKey, MarketDataInstrument>, Kind>
where
    S: Into<AssetNameInternal>,
{
    fn from(
        (instrument_id, exchange, base, quote, instrument_kind, kind): (
            InstrumentKey,
            Exchange,
            S,
            S,
            MarketDataInstrumentKind,
            Kind,
        ),
    ) -> Self {
        let instrument = Keyed::new(instrument_id, (base, quote, instrument_kind).into());

        Self::new(exchange, instrument, kind)
    }
}

impl<Exchange, I, Instrument, Kind> From<(Exchange, I, Kind)>
    for Subscription<Exchange, Instrument, Kind>
where
    I: Into<Instrument>,
{
    fn from((exchange, instrument, kind): (Exchange, I, Kind)) -> Self {
        Self::new(exchange, instrument, kind)
    }
}

impl<Instrument, Exchange, Kind> Subscription<Exchange, Instrument, Kind> {
    /// Constructs a new [`Subscription`] using the provided configuration.
    pub fn new<I>(exchange: Exchange, instrument: I, kind: Kind) -> Self
    where
        I: Into<Instrument>,
    {
        Self {
            exchange,
            instrument: instrument.into(),
            kind,
        }
    }
}

impl<Exchange, Instrument, Kind> Validator for Subscription<Exchange, Instrument, Kind>
where
    Exchange: Connector,
    Instrument: InstrumentData,
{
    type Error = SocketError;

    fn validate(self) -> Result<Self, Self::Error>
    where
        Self: Sized,
    {
        // Validate the Exchange supports the Subscription InstrumentKind
        if exchange_supports_instrument_kind(Exchange::ID, self.instrument.kind()) {
            Ok(self)
        } else {
            Err(SocketError::Unsupported {
                entity: Exchange::ID.to_string(),
                item: self.instrument.kind().to_string(),
            })
        }
    }
}

/// Determines whether the [`Connector`] associated with this [`ExchangeId`] supports the
/// ingestion of market data for the provided [`MarketDataInstrumentKind`].
// The arms are grouped by `MarketDataInstrumentKind`, each group granting the exchanges whose
// connector serves that kind and denying the rest. Collapsing to `matches!` would fold the groups
// into one boolean expression, leaving nowhere to record which exchanges were verified.
#[allow(clippy::match_like_matches_macro)]
pub fn exchange_supports_instrument_kind(
    exchange: ExchangeId,
    instrument_kind: &MarketDataInstrumentKind,
) -> bool {
    use rustrade_instrument::{
        exchange::ExchangeId::*, instrument::market_data::kind::MarketDataInstrumentKind::*,
    };

    match (exchange, instrument_kind) {
        // Spot: granted per exchange whose connector serves a spot market, denied to every other,
        // as the other kinds are. A new connector serving spot must be added here, or its spot
        // subscriptions fail validation, which says so.
        (
            AlpacaCrypto | AlpacaIex | AlpacaSip | BinanceSpot | Bitfinex | BybitSpot | Coinbase
            | GateioSpot | HyperliquidSpot | Kraken | LseFx | LseCrypto | LseEquities
            | MassiveStocks | MassiveCrypto | MassiveForex | Okx,
            Spot,
        ) => true,
        (_, Spot) => false,

        // Future
        (GateioFuturesUsd | GateioFuturesBtc | Okx, Future { .. }) => true,
        (_, Future { .. }) => false,

        // Perpetual
        (
            BinanceFuturesUsd | Bitmex | Okx | BybitPerpetualsUsd | GateioPerpetualsUsd
            | GateioPerpetualsBtc | HyperliquidPerp,
            Perpetual,
        ) => true,
        (_, Perpetual) => false,

        // Option
        (GateioOptions | Okx | LseOptions | MassiveOptions, Option { .. }) => true,
        (_, Option { .. }) => false,

        // Cfd
        (LseCfd | LseFutures, Cfd) => true,
        (_, Cfd) => false,
    }
}

impl<Instrument> Validator for Subscription<ExchangeId, Instrument, SubKind>
where
    Instrument: InstrumentData,
{
    type Error = SocketError;

    fn validate(self) -> Result<Self, Self::Error>
    where
        Self: Sized,
    {
        // Validate the Exchange supports the Subscription InstrumentKind
        if exchange_supports_instrument_kind_sub_kind(
            &self.exchange,
            self.instrument.kind(),
            self.kind,
        ) {
            Ok(self)
        } else {
            Err(SocketError::Unsupported {
                entity: self.exchange.to_string(),
                item: format!("({}, {})", self.instrument.kind(), self.kind),
            })
        }
    }
}

/// Determines whether the [`Connector`] associated with this [`ExchangeId`] supports the
/// ingestion of market data for the provided [`MarketDataInstrumentKind`] and [`SubKind`] combination.
///
/// [`SubKind::Candles`] carries its resolution, so support is checked **per interval**:
/// [`CandleInterval`] is the venue-agnostic union of every resolution any connector
/// serves, and no venue serves all of it (Binance publishes no `5s`/`15s`/`30s` kline).
pub fn exchange_supports_instrument_kind_sub_kind(
    exchange_id: &ExchangeId,
    instrument_kind: &MarketDataInstrumentKind,
    sub_kind: SubKind,
) -> bool {
    use ExchangeId::*;
    use MarketDataInstrumentKind::*;
    use SubKind::*;

    match (exchange_id, instrument_kind, sub_kind) {
        (BinanceSpot, Spot, PublicTrades | OrderBooksL1 | OrderBooksL2) => true,
        (BinanceSpot, Spot, Candles { interval }) => {
            crate::exchange::binance::supports_candle_interval(interval)
        }
        (
            BinanceFuturesUsd,
            Perpetual,
            PublicTrades | OrderBooksL1 | OrderBooksL2 | Liquidations,
        ) => true,
        (BinanceFuturesUsd, Perpetual, Candles { interval }) => {
            crate::exchange::binance::supports_candle_interval(interval)
        }
        (Bitfinex, Spot, PublicTrades) => true,
        (Bitmex, Perpetual, PublicTrades) => true,
        (BybitSpot, Spot, PublicTrades | OrderBooksL1 | OrderBooksL2) => true,
        (BybitPerpetualsUsd, Perpetual, PublicTrades | OrderBooksL1 | OrderBooksL2) => true,
        (Coinbase, Spot, PublicTrades) => true,
        (GateioSpot, Spot, PublicTrades) => true,
        (GateioFuturesUsd, Future { .. }, PublicTrades) => true,
        (GateioFuturesBtc, Future { .. }, PublicTrades) => true,
        (GateioPerpetualsUsd, Perpetual, PublicTrades) => true,
        (GateioPerpetualsBtc, Perpetual, PublicTrades) => true,
        (GateioOptions, Option { .. }, PublicTrades) => true,
        (Kraken, Spot, PublicTrades | OrderBooksL1) => true,
        (Okx, Spot | Future { .. } | Perpetual | Option { .. }, PublicTrades) => true,
        (HyperliquidPerp, Perpetual, PublicTrades | OrderBooksL2) => true,
        (HyperliquidSpot, Spot, PublicTrades | OrderBooksL2) => true,

        // Alpaca's top of book is its quote: `OrderBooksL1` reads the `quotes` channel, which the
        // typed `Quotes` kind also serves. `Quotes` itself has no arm because `DynamicStreams`, the
        // one caller of this matrix, carries no quote channel, and the engine's market data no quote
        // event. IEX quotes are IEX's own top of book rather than the NBBO; SIP quotes are the NBBO.
        //
        // ⚠️ `AlpacaSip` is declared on the strength of the code path, which it shares with IEX, and
        // has never been run against the real feed: it needs a paid subscription. See
        // `exchange::alpaca`.
        (AlpacaCrypto | AlpacaIex | AlpacaSip, Spot, PublicTrades | OrderBooksL1) => true,

        // London Strategic Edge serves both kinds from ONE frame: its WebSocket publishes a single
        // tick carrying `price`, `bid`, `ask` and `volume` together, so a dataset that serves one
        // of these kinds necessarily serves the other. The two arms differ only in instrument kind,
        // which is fixed per dataset.
        //
        // ⚠️ A `PublicTrade` from this feed MAY NOT BE A PRINT. The tick's `price` equals its `bid`
        // on every sample taken -- 3,966 of 3,966 ticks spanning every dataset family -- so a trade
        // decoded from it is a bid-side quote wearing a trade's shape, and its arrival is not
        // evidence that a transaction occurred, at that price or at all.
        //
        // ⚠️ And its `amount` is genuine on two of these venues and FABRICATED on the other two,
        // with no in-band signal separating them. `LseCrypto` and `LseEquities` carry a real
        // per-tick size, which reconciles against the provider's own one-minute candles to the last
        // decimal. `LseFx` and `LseCfd` carry a hard-coded `1.0` on every tick of every symbol
        // sampled -- a placeholder that, being a plausible positive number, aggregates into a
        // legitimate-looking total at any resolution, leaving volume-weighted prices and size
        // filters on those two meaningless rather than merely imprecise.
        //
        // Support is declared regardless, and uniformly: the frame is identical across datasets, so
        // withholding the kind would hide the feed rather than the hazard. Saying so is the useful
        // answer. The full reasoning lives on the trade decoder in the `lse` module.
        (LseFx | LseCrypto | LseEquities, Spot, PublicTrades | OrderBooksL1) => true,
        (LseCfd | LseFutures, Cfd, PublicTrades | OrderBooksL1) => true,
        // Option contracts tick on the same frame, subscribed per underlying, but with `bid` and
        // `ask` null on every one -- there is no quote to serve, so `PublicTrades` is the only
        // kind. Unlike the venues above, these trades ARE prints: they reconcile one-for-one
        // against the provider's REST option-print tape.
        (LseOptions, Option { .. }, PublicTrades) => true,
        // No `Candles` arm exists for any London Strategic Edge venue, deliberately: the provider's
        // WebSocket carries no candle channel at all -- the tick above is its only data frame, so
        // there is nothing to subscribe to. Its candles are served exclusively over the REST vault,
        // by a path that never reaches this matrix. The absence is a decision, not an omission.

        // Massive: a cluster per `ExchangeId`. The top of book is the cluster's quote channel, read
        // as `OrderBooksL1`; `Quotes` itself has no arm, for the reason given for Alpaca above.
        // Forex publishes no trades. Candles are Massive's per-second and per-minute aggregates,
        // the only intervals it aggregates at; the connectors refuse any other, and this matrix
        // refuses it first. `Massive` alone is the REST client's id and streams nothing.
        //
        // ⚠️ `MassiveOptions` is declared on the strength of the code path, which it shares with
        // stocks, and has never been run against the real feed: it needs an options subscription.
        (MassiveStocks | MassiveCrypto, Spot, PublicTrades | OrderBooksL1) => true,
        (MassiveForex, Spot, OrderBooksL1) => true,
        (MassiveOptions, Option { .. }, PublicTrades | OrderBooksL1) => true,
        (
            MassiveStocks | MassiveCrypto | MassiveForex,
            Spot,
            Candles {
                interval: CandleInterval::Sec1 | CandleInterval::Min1,
            },
        ) => true,
        (
            MassiveOptions,
            Option { .. },
            Candles {
                interval: CandleInterval::Sec1 | CandleInterval::Min1,
            },
        ) => true,

        (_, _, _) => false,
    }
}

/// Metadata generated from a collection of Barter [`Subscription`]s, including the exchange
/// specific subscription payloads that are sent to the exchange.
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct SubscriptionMeta<InstrumentKey> {
    /// `HashMap` containing the mapping between a [`SubscriptionId`] and
    /// it's associated Barter [`MarketDataInstrument`].
    pub instrument_map: Map<InstrumentKey>,
    /// Collection of [`WsMessage`]s containing exchange specific subscription payloads to be sent.
    pub ws_subscriptions: Vec<WsMessage>,
}

/// New type`HashMap` that maps a [`SubscriptionId`] to some associated type `T`.
///
/// Used by [`ExchangeTransformer`](crate::transformer::ExchangeTransformer)s to identify the
/// Barter [`MarketDataInstrument`] associated with incoming exchange messages.
#[derive(Clone, Eq, PartialEq, Debug, Deserialize, Serialize)]
pub struct Map<T>(pub FnvHashMap<SubscriptionId, T>);

impl<T> FromIterator<(SubscriptionId, T)> for Map<T> {
    fn from_iter<Iter>(iter: Iter) -> Self
    where
        Iter: IntoIterator<Item = (SubscriptionId, T)>,
    {
        Self(iter.into_iter().collect::<FnvHashMap<SubscriptionId, T>>())
    }
}

impl<T> Map<T> {
    /// Find the `InstrumentKey` associated with the provided [`SubscriptionId`].
    pub fn find<SubId>(&self, id: &SubId) -> Result<&T, SocketError>
    where
        SubscriptionId: Borrow<SubId>,
        SubId: AsRef<str> + Hash + Eq + ?Sized,
    {
        self.0
            .get(id)
            .ok_or_else(|| SocketError::Unidentifiable(SubscriptionId(id.as_ref().to_smolstr())))
    }

    /// Find the mutable reference to `T` associated with the provided [`SubscriptionId`].
    pub fn find_mut<SubId>(&mut self, id: &SubId) -> Result<&mut T, SocketError>
    where
        SubscriptionId: Borrow<SubId>,
        SubId: AsRef<str> + Hash + Eq + ?Sized,
    {
        self.0
            .get_mut(id)
            .ok_or_else(|| SocketError::Unidentifiable(SubscriptionId(id.as_ref().to_smolstr())))
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)] // Test code: panics on bad input are acceptable
mod tests {
    use super::*;

    mod supports {
        use super::*;
        use chrono::{DateTime, Utc};
        use rust_decimal::Decimal;
        use rustrade_instrument::{
            exchange::ExchangeId,
            instrument::{
                kind::option::{OptionExercise, OptionKind},
                market_data::kind::MarketDataOptionContract,
            },
        };

        /// Spot is granted to exactly the exchanges whose connector serves a spot market, and
        /// denied to every other, connector or not, rather than claimed by default.
        #[test]
        fn spot_is_granted_only_where_a_connector_serves_it() {
            use ExchangeId::*;
            for exchange in [
                AlpacaCrypto,
                AlpacaIex,
                AlpacaSip,
                BinanceSpot,
                Bitfinex,
                BybitSpot,
                Coinbase,
                GateioSpot,
                HyperliquidSpot,
                Kraken,
                LseFx,
                LseCrypto,
                LseEquities,
                MassiveStocks,
                MassiveCrypto,
                MassiveForex,
                Okx,
            ] {
                assert!(
                    exchange_supports_instrument_kind(exchange, &MarketDataInstrumentKind::Spot),
                    "{exchange:?}"
                );
            }
            for exchange in [
                BinanceFuturesUsd,
                Bitmex,
                BybitPerpetualsUsd,
                GateioFuturesBtc,
                GateioFuturesUsd,
                GateioOptions,
                GateioPerpetualsBtc,
                GateioPerpetualsUsd,
                HyperliquidPerp,
                LseCfd,
                LseFutures,
                LseOptions,
                MassiveOptions,
                // No connector at all: a sample, as `ExchangeId` cannot be enumerated. Add a new
                // connector-backed exchange to one of the two lists.
                BinanceMargin,
                Deribit,
                Other,
            ] {
                assert!(
                    !exchange_supports_instrument_kind(exchange, &MarketDataInstrumentKind::Spot),
                    "{exchange:?}"
                );
            }
        }

        /// The London Strategic Edge datasets, paired with the single
        /// [`MarketDataInstrumentKind`] each one serves.
        const LSE: [(ExchangeId, MarketDataInstrumentKind); 5] = [
            (ExchangeId::LseFx, MarketDataInstrumentKind::Spot),
            (ExchangeId::LseCrypto, MarketDataInstrumentKind::Spot),
            (ExchangeId::LseEquities, MarketDataInstrumentKind::Spot),
            (ExchangeId::LseFutures, MarketDataInstrumentKind::Cfd),
            (ExchangeId::LseCfd, MarketDataInstrumentKind::Cfd),
        ];

        #[test]
        fn test_lse_supports_exactly_its_own_kind() {
            // Each dataset serves one kind and must deny the others.
            for (exchange, supported) in LSE {
                assert!(
                    exchange_supports_instrument_kind(exchange, &supported),
                    "{exchange:?} should support {supported:?}"
                );

                for denied in [
                    MarketDataInstrumentKind::Spot,
                    MarketDataInstrumentKind::Cfd,
                    MarketDataInstrumentKind::Perpetual,
                ] {
                    if denied == supported {
                        continue;
                    }
                    assert!(
                        !exchange_supports_instrument_kind(exchange, &denied),
                        "{exchange:?} should not support {denied:?}"
                    );
                }
            }
        }

        fn option_kind() -> MarketDataInstrumentKind {
            MarketDataInstrumentKind::Option(MarketDataOptionContract {
                kind: OptionKind::Call,
                exercise: OptionExercise::American,
                expiry: DateTime::<Utc>::MAX_UTC,
                strike: Decimal::ONE,
            })
        }

        #[test]
        fn test_lse_options_supports_only_the_option_kind() {
            assert!(exchange_supports_instrument_kind(
                ExchangeId::LseOptions,
                &option_kind()
            ));
            for denied in [
                MarketDataInstrumentKind::Spot,
                MarketDataInstrumentKind::Cfd,
                MarketDataInstrumentKind::Perpetual,
            ] {
                assert!(
                    !exchange_supports_instrument_kind(ExchangeId::LseOptions, &denied),
                    "LseOptions should not support {denied:?}"
                );
            }
        }

        #[test]
        fn test_lse_options_serves_public_trades_only() {
            assert!(exchange_supports_instrument_kind_sub_kind(
                &ExchangeId::LseOptions,
                &option_kind(),
                SubKind::PublicTrades
            ));

            // Every option tick publishes `bid` and `ask` as null, so there is no quote to serve.
            for sub_kind in [
                SubKind::OrderBooksL1,
                SubKind::OrderBooksL2,
                SubKind::OrderBooksL3,
            ] {
                assert!(
                    !exchange_supports_instrument_kind_sub_kind(
                        &ExchangeId::LseOptions,
                        &option_kind(),
                        sub_kind
                    ),
                    "LseOptions should not serve {sub_kind}"
                );
            }

            assert!(
                !exchange_supports_instrument_kind_sub_kind(
                    &ExchangeId::LseOptions,
                    &MarketDataInstrumentKind::Spot,
                    SubKind::PublicTrades
                ),
                "LseOptions serves option contracts only"
            );
        }

        #[test]
        fn test_cfd_denied_for_non_cfd_exchanges() {
            for exchange in [
                ExchangeId::BinanceSpot,
                ExchangeId::Coinbase,
                ExchangeId::Okx,
                ExchangeId::GateioOptions,
                ExchangeId::Mock,
            ] {
                assert!(
                    !exchange_supports_instrument_kind(exchange, &MarketDataInstrumentKind::Cfd),
                    "{exchange:?} should not support Cfd"
                );
            }
        }

        #[test]
        fn test_lse_supports_both_tick_derived_sub_kinds_for_its_own_kind_only() {
            // One tick frame carries price, bid, ask and size together, so every dataset serves
            // both kinds decoded from it -- and serves neither under an instrument kind it does
            // not model.
            for (exchange, supported) in LSE {
                for sub_kind in [SubKind::PublicTrades, SubKind::OrderBooksL1] {
                    assert!(
                        exchange_supports_instrument_kind_sub_kind(&exchange, &supported, sub_kind),
                        "{exchange:?} should support ({supported:?}, {sub_kind})"
                    );

                    for denied in [
                        MarketDataInstrumentKind::Spot,
                        MarketDataInstrumentKind::Cfd,
                        MarketDataInstrumentKind::Perpetual,
                    ] {
                        if denied == supported {
                            continue;
                        }
                        assert!(
                            !exchange_supports_instrument_kind_sub_kind(
                                &exchange, &denied, sub_kind
                            ),
                            "{exchange:?} should not support ({denied:?}, {sub_kind})"
                        );
                    }
                }
            }
        }

        #[test]
        fn test_lse_serves_no_candle_stream_at_any_interval() {
            // Pins the absent `Candles` arm as a decision: the provider's WebSocket has no candle
            // channel, and its candles reach users over the REST vault, which never consults this
            // matrix. A future edit that "completes" the LSE rows fails here.
            for (exchange, kind) in LSE {
                for interval in CandleInterval::ALL {
                    assert!(
                        !exchange_supports_instrument_kind_sub_kind(
                            &exchange,
                            &kind,
                            SubKind::Candles { interval },
                        ),
                        "{exchange:?} serves no candle stream, including {interval:?}"
                    );
                }
            }
        }

        #[test]
        fn test_lse_denies_sub_kinds_its_tick_cannot_serve() {
            // The tick is top-of-book, so there is no depth to serve; the feed publishes no
            // liquidations at all. `Quotes` is denied here as it is for every other venue -- no
            // exchange declares it in this matrix.
            for (exchange, kind) in LSE {
                for sub_kind in [
                    SubKind::OrderBooksL2,
                    SubKind::OrderBooksL3,
                    SubKind::Liquidations,
                    SubKind::Quotes,
                ] {
                    assert!(
                        !exchange_supports_instrument_kind_sub_kind(&exchange, &kind, sub_kind),
                        "{exchange:?} should not support ({kind:?}, {sub_kind})"
                    );
                }
            }
        }

        #[test]
        fn hyperliquid_serves_trades_and_depth_on_each_connector_for_its_own_kind_only() {
            for (exchange, kind) in [
                (
                    ExchangeId::HyperliquidPerp,
                    MarketDataInstrumentKind::Perpetual,
                ),
                (ExchangeId::HyperliquidSpot, MarketDataInstrumentKind::Spot),
            ] {
                for sub_kind in [SubKind::PublicTrades, SubKind::OrderBooksL2] {
                    assert!(
                        exchange_supports_instrument_kind_sub_kind(&exchange, &kind, sub_kind),
                        "{exchange:?} should support ({kind:?}, {sub_kind})"
                    );
                }
                for sub_kind in [
                    SubKind::OrderBooksL1,
                    SubKind::OrderBooksL3,
                    SubKind::Liquidations,
                    SubKind::Quotes,
                ] {
                    assert!(
                        !exchange_supports_instrument_kind_sub_kind(&exchange, &kind, sub_kind),
                        "{exchange:?} should not support ({kind:?}, {sub_kind})"
                    );
                }
            }
            assert!(!exchange_supports_instrument_kind_sub_kind(
                &ExchangeId::HyperliquidSpot,
                &MarketDataInstrumentKind::Perpetual,
                SubKind::PublicTrades
            ));
            assert!(!exchange_supports_instrument_kind_sub_kind(
                &ExchangeId::HyperliquidPerp,
                &MarketDataInstrumentKind::Spot,
                SubKind::PublicTrades
            ));
        }
    }

    mod supports_massive {
        use super::*;
        use rustrade_instrument::instrument::{
            kind::option::{OptionExercise, OptionKind},
            market_data::kind::MarketDataOptionContract,
        };

        fn option_kind() -> MarketDataInstrumentKind {
            MarketDataInstrumentKind::Option(MarketDataOptionContract {
                kind: OptionKind::Call,
                exercise: OptionExercise::American,
                expiry: chrono::DateTime::<chrono::Utc>::MAX_UTC,
                strike: rust_decimal::Decimal::ONE,
            })
        }

        /// Each cluster, paired with the one instrument kind it streams.
        fn clusters() -> [(ExchangeId, MarketDataInstrumentKind); 4] {
            [
                (ExchangeId::MassiveStocks, MarketDataInstrumentKind::Spot),
                (ExchangeId::MassiveCrypto, MarketDataInstrumentKind::Spot),
                (ExchangeId::MassiveForex, MarketDataInstrumentKind::Spot),
                (ExchangeId::MassiveOptions, option_kind()),
            ]
        }

        fn other_kinds() -> [MarketDataInstrumentKind; 4] {
            [
                MarketDataInstrumentKind::Spot,
                MarketDataInstrumentKind::Perpetual,
                MarketDataInstrumentKind::Cfd,
                option_kind(),
            ]
        }

        #[test]
        fn each_cluster_supports_exactly_its_own_instrument_kind() {
            for (exchange, supported) in clusters() {
                assert!(
                    exchange_supports_instrument_kind(exchange, &supported),
                    "{exchange:?} should support {supported:?}"
                );
                for denied in other_kinds().into_iter().filter(|kind| *kind != supported) {
                    assert!(
                        !exchange_supports_instrument_kind(exchange, &denied),
                        "{exchange:?} should not support {denied:?}"
                    );
                    assert!(
                        !exchange_supports_instrument_kind_sub_kind(
                            &exchange,
                            &denied,
                            SubKind::OrderBooksL1
                        ),
                        "{exchange:?} should not serve {denied:?}"
                    );
                }
            }
        }

        #[test]
        fn every_cluster_but_forex_serves_trades_and_every_cluster_serves_top_of_book() {
            for (exchange, kind) in clusters() {
                assert_eq!(
                    exchange_supports_instrument_kind_sub_kind(
                        &exchange,
                        &kind,
                        SubKind::PublicTrades
                    ),
                    exchange != ExchangeId::MassiveForex,
                    "{exchange:?} trades"
                );
                assert!(
                    exchange_supports_instrument_kind_sub_kind(
                        &exchange,
                        &kind,
                        SubKind::OrderBooksL1
                    ),
                    "{exchange:?} top of book"
                );
            }
        }

        #[test]
        fn candles_are_served_at_one_second_and_one_minute_only() {
            for (exchange, kind) in clusters() {
                for interval in CandleInterval::ALL {
                    assert_eq!(
                        exchange_supports_instrument_kind_sub_kind(
                            &exchange,
                            &kind,
                            SubKind::Candles { interval }
                        ),
                        matches!(interval, CandleInterval::Sec1 | CandleInterval::Min1),
                        "{exchange:?} candles at {interval:?}"
                    );
                }
            }
        }

        #[test]
        fn quotes_depth_and_liquidations_are_refused() {
            for (exchange, kind) in clusters() {
                for sub_kind in [
                    SubKind::Quotes,
                    SubKind::OrderBooksL2,
                    SubKind::OrderBooksL3,
                    SubKind::Liquidations,
                ] {
                    assert!(
                        !exchange_supports_instrument_kind_sub_kind(&exchange, &kind, sub_kind),
                        "{exchange:?} should not serve {sub_kind}"
                    );
                }
            }
        }

        #[test]
        fn the_rest_client_id_streams_nothing() {
            for kind in other_kinds() {
                for sub_kind in [SubKind::PublicTrades, SubKind::OrderBooksL1] {
                    assert!(!exchange_supports_instrument_kind_sub_kind(
                        &ExchangeId::Massive,
                        &kind,
                        sub_kind
                    ));
                }
            }
        }
    }

    mod subscription {
        use super::*;
        use crate::{
            exchange::{coinbase::Coinbase, okx::Okx},
            subscription::trade::PublicTrades,
        };
        use rustrade_instrument::instrument::market_data::MarketDataInstrument;

        mod de {
            use super::*;
            use crate::{
                exchange::{
                    binance::{futures::BinanceFuturesUsd, spot::BinanceSpot},
                    gateio::perpetual::GateioPerpetualsUsd,
                    okx::Okx,
                },
                subscription::{book::OrderBooksL2, trade::PublicTrades},
            };
            use rustrade_instrument::instrument::market_data::MarketDataInstrument;

            #[test]
            fn test_subscription_okx_spot_public_trades() {
                let input = r#"
                {
                    "exchange": "okx",
                    "base": "btc",
                    "quote": "usdt",
                    "instrument_kind": "spot",
                    "kind": "public_trades"
                }
                "#;

                serde_json::from_str::<Subscription<Okx, MarketDataInstrument, PublicTrades>>(
                    input,
                )
                .unwrap();
            }

            #[test]
            fn test_subscription_binance_spot_public_trades() {
                let input = r#"
                {
                    "exchange": "binance_spot",
                    "base": "btc",
                    "quote": "usdt",
                    "instrument_kind": "spot",
                    "kind": "public_trades"
                }
                "#;

                serde_json::from_str::<Subscription<BinanceSpot, MarketDataInstrument, PublicTrades>>(input)
                    .unwrap();
            }

            #[test]
            fn test_subscription_binance_futures_usd_order_books_l2() {
                let input = r#"
                {
                    "exchange": "binance_futures_usd",
                    "base": "btc",
                    "quote": "usdt",
                    "instrument_kind": "perpetual",
                    "kind": "order_books_l2"
                }
                "#;

                serde_json::from_str::<
                    Subscription<BinanceFuturesUsd, MarketDataInstrument, OrderBooksL2>,
                >(input)
                .unwrap();
            }

            #[test]
            fn subscription_gateio_futures_usd_public_trades() {
                let input = r#"
                {
                    "exchange": "gateio_perpetuals_usd",
                    "base": "btc",
                    "quote": "usdt",
                    "instrument_kind": "perpetual",
                    "kind": "public_trades"
                }
                "#;

                serde_json::from_str::<
                    Subscription<GateioPerpetualsUsd, MarketDataInstrument, PublicTrades>,
                >(input)
                .unwrap();
            }
        }

        #[test]
        fn test_validate_bitfinex_public_trades() {
            struct TestCase {
                input: Subscription<Coinbase, MarketDataInstrument, PublicTrades>,
                expected:
                    Result<Subscription<Coinbase, MarketDataInstrument, PublicTrades>, SocketError>,
            }

            let tests = vec![
                TestCase {
                    // TC0: Valid Coinbase Spot PublicTrades subscription
                    input: Subscription::from((
                        Coinbase,
                        "base",
                        "quote",
                        MarketDataInstrumentKind::Spot,
                        PublicTrades,
                    )),
                    expected: Ok(Subscription::from((
                        Coinbase,
                        "base",
                        "quote",
                        MarketDataInstrumentKind::Spot,
                        PublicTrades,
                    ))),
                },
                TestCase {
                    // TC1: Invalid Coinbase FuturePerpetual PublicTrades subscription
                    input: Subscription::from((
                        Coinbase,
                        "base",
                        "quote",
                        MarketDataInstrumentKind::Perpetual,
                        PublicTrades,
                    )),
                    expected: Err(SocketError::Unsupported {
                        entity: "".to_string(),
                        item: "".to_string(),
                    }),
                },
            ];

            for (index, test) in tests.into_iter().enumerate() {
                let actual = test.input.validate();
                match (actual, test.expected) {
                    (Ok(actual), Ok(expected)) => {
                        assert_eq!(actual, expected, "TC{} failed", index)
                    }
                    (Err(_), Err(_)) => {
                        // Test passed
                    }
                    (actual, expected) => {
                        // Test failed
                        panic!(
                            "TC{index} failed because actual != expected. \nActual: {actual:?}\nExpected: {expected:?}\n"
                        );
                    }
                }
            }
        }

        #[test]
        fn test_validate_okx_public_trades() {
            struct TestCase {
                input: Subscription<Okx, MarketDataInstrument, PublicTrades>,
                expected:
                    Result<Subscription<Okx, MarketDataInstrument, PublicTrades>, SocketError>,
            }

            let tests = vec![
                TestCase {
                    // TC0: Valid Okx Spot PublicTrades subscription
                    input: Subscription::from((
                        Okx,
                        "base",
                        "quote",
                        MarketDataInstrumentKind::Spot,
                        PublicTrades,
                    )),
                    expected: Ok(Subscription::from((
                        Okx,
                        "base",
                        "quote",
                        MarketDataInstrumentKind::Spot,
                        PublicTrades,
                    ))),
                },
                TestCase {
                    // TC1: Valid Okx FuturePerpetual PublicTrades subscription
                    input: Subscription::from((
                        Okx,
                        "base",
                        "quote",
                        MarketDataInstrumentKind::Perpetual,
                        PublicTrades,
                    )),
                    expected: Ok(Subscription::from((
                        Okx,
                        "base",
                        "quote",
                        MarketDataInstrumentKind::Perpetual,
                        PublicTrades,
                    ))),
                },
            ];

            for (index, test) in tests.into_iter().enumerate() {
                let actual = test.input.validate();
                match (actual, test.expected) {
                    (Ok(actual), Ok(expected)) => {
                        assert_eq!(actual, expected, "TC{} failed", index)
                    }
                    (Err(_), Err(_)) => {
                        // Test passed
                    }
                    (actual, expected) => {
                        // Test failed
                        panic!(
                            "TC{index} failed because actual != expected. \nActual: {actual:?}\nExpected: {expected:?}\n"
                        );
                    }
                }
            }
        }
    }

    mod sub_kind {
        use super::*;

        #[test]
        fn candles_variant_serde_round_trips() {
            let kind = SubKind::Candles {
                interval: CandleInterval::Min1,
            };

            let json = serde_json::to_string(&kind).unwrap();
            let back = serde_json::from_str::<SubKind>(&json).unwrap();

            assert_eq!(kind, back, "SubKind::Candles must serde round-trip");
        }

        #[test]
        fn candles_variant_serde_shape_is_externally_tagged() {
            // `SubKind` is an externally-tagged enum (no `#[serde(tag = ...)]`), so the struct
            // variant serialises as `{"Candles":{"interval":"1m"}}` — NOT an internally-tagged
            // `{"type":"candles",...}` form. Pin the wire shape so a future tag/rename change is
            // caught here rather than silently shipped to config-driven consumers.
            let json = serde_json::to_string(&SubKind::Candles {
                interval: CandleInterval::Min1,
            })
            .unwrap();
            assert_eq!(json, r#"{"Candles":{"interval":"1m"}}"#);
        }

        #[test]
        fn candles_display_tag_is_interval_independent() {
            // The `derive_more::Display` tag is the fixed kind name, never the interval.
            assert_eq!(
                SubKind::Candles {
                    interval: CandleInterval::Sec1
                }
                .to_string(),
                "candles"
            );
            assert_eq!(
                SubKind::Candles {
                    interval: CandleInterval::Month1
                }
                .to_string(),
                "candles"
            );
        }

        #[test]
        fn candles_supported_per_interval_on_both_binance_venues() {
            // `CandleInterval` is a venue-agnostic union, so candle support is decided
            // per interval, not per `SubKind`: both Binance venues serve every interval
            // Binance publishes a kline for, and neither serves `5s`/`15s`/`30s`.
            for interval in CandleInterval::ALL {
                let expected = crate::exchange::binance::supports_candle_interval(interval);
                assert_eq!(
                    exchange_supports_instrument_kind_sub_kind(
                        &ExchangeId::BinanceSpot,
                        &MarketDataInstrumentKind::Spot,
                        SubKind::Candles { interval },
                    ),
                    expected,
                    "BinanceSpot/Spot Candles {interval:?}"
                );
                assert_eq!(
                    exchange_supports_instrument_kind_sub_kind(
                        &ExchangeId::BinanceFuturesUsd,
                        &MarketDataInstrumentKind::Perpetual,
                        SubKind::Candles { interval },
                    ),
                    expected,
                    "BinanceFuturesUsd/Perpetual Candles {interval:?}"
                );
            }
        }

        #[test]
        fn candles_rejected_for_unsupported_venue_kind_pairing() {
            // A supported venue with the wrong instrument kind is still rejected.
            assert!(
                !exchange_supports_instrument_kind_sub_kind(
                    &ExchangeId::BinanceSpot,
                    &MarketDataInstrumentKind::Perpetual,
                    SubKind::Candles {
                        interval: CandleInterval::Min1,
                    },
                ),
                "BinanceSpot does not serve Perpetual candles"
            );
        }
    }

    mod instrument_map {
        use super::*;
        use rustrade_instrument::instrument::market_data::MarketDataInstrument;

        #[test]
        fn test_find_instrument() {
            // Initialise SubscriptionId-InstrumentKey HashMap
            let ids = Map(FnvHashMap::from_iter([(
                SubscriptionId::from("present"),
                MarketDataInstrument::from(("base", "quote", MarketDataInstrumentKind::Spot)),
            )]));

            struct TestCase {
                input: SubscriptionId,
                expected: Result<MarketDataInstrument, SocketError>,
            }

            let cases = vec![
                TestCase {
                    // TC0: SubscriptionId (channel) is present in the HashMap
                    input: SubscriptionId::from("present"),
                    expected: Ok(MarketDataInstrument::from((
                        "base",
                        "quote",
                        MarketDataInstrumentKind::Spot,
                    ))),
                },
                TestCase {
                    // TC1: SubscriptionId (channel) is not present in the HashMap
                    input: SubscriptionId::from("not present"),
                    expected: Err(SocketError::Unidentifiable(SubscriptionId::from(
                        "not present",
                    ))),
                },
            ];

            for (index, test) in cases.into_iter().enumerate() {
                let actual = ids.find(&test.input);
                match (actual, test.expected) {
                    (Ok(actual), Ok(expected)) => {
                        assert_eq!(*actual, expected, "TC{} failed", index)
                    }
                    (Err(_), Err(_)) => {
                        // Test passed
                    }
                    (actual, expected) => {
                        // Test failed
                        panic!(
                            "TC{index} failed because actual != expected. \nActual: {actual:?}\nExpected: {expected:?}\n"
                        );
                    }
                }
            }
        }
    }
}