rustrade-execution 0.12.1

Stream private account data from financial venues, and execute (live or mock) orders.
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
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
use crate::{
    AccountEvent, AccountEventKind, AccountSnapshot, InstrumentAccountSnapshot,
    InstrumentBalanceUpdate, IsolatedInstrumentState, UnindexedAccountEvent,
    UnindexedAccountSnapshot,
    balance::{AssetBalance, AssetBalanceUpdate},
    cash_flow::CashFlow,
    error::{
        AccountReinitFailure, AccountStreamInitError, ApiError, ClientError, KeyError, OrderError,
        UnindexedAccountStreamInitError, UnindexedApiError, UnindexedClientError,
        UnindexedOrderError,
    },
    fill_recovery::{FillRecoveryGap, FillRecoveryScope},
    map::ExecutionInstrumentMap,
    notice::AccountNotice,
    order::{
        Order, OrderEvent, OrderKey, OrderSnapshot, UnindexedOrderKey, UnindexedOrderSnapshot,
        request::OrderResponseCancel,
        state::{InactiveOrderState, OrderState, UnindexedOrderState},
    },
    trade::{AssetFees, Trade, TradeAmendment, TradeAmendmentKind},
};
use derive_more::Constructor;
use rust_decimal::Decimal;
use rustrade_instrument::{
    asset::{AssetIndex, name::AssetNameExchange},
    exchange::{ExchangeId, ExchangeIndex},
    index::error::IndexError,
    instrument::{InstrumentIndex, name::InstrumentNameExchange},
};
use rustrade_integration::{
    collection::snapshot::Snapshot,
    stream::ext::indexed::{IndexedStream, Indexer},
};
use std::sync::Arc;
use tracing::warn;

pub type IndexedAccountStream<St> = IndexedStream<St, AccountEventIndexer>;

#[derive(Debug, Clone, Constructor)]
pub struct AccountEventIndexer {
    pub map: Arc<ExecutionInstrumentMap>,
}

impl Indexer for AccountEventIndexer {
    type Unindexed = UnindexedAccountEvent;
    type Indexed = AccountEvent;

    fn index(&self, item: Self::Unindexed) -> Result<Self::Indexed, IndexError> {
        self.account_event(item)
    }
}

impl AccountEventIndexer {
    pub fn account_event(&self, event: UnindexedAccountEvent) -> Result<AccountEvent, IndexError> {
        let UnindexedAccountEvent { exchange, kind } = event;

        let exchange = self.map.find_exchange_index(exchange)?;

        let kind = match kind {
            AccountEventKind::Snapshot(snapshot) => {
                AccountEventKind::Snapshot(self.snapshot(snapshot)?)
            }
            AccountEventKind::BalanceSnapshot(snapshot) => {
                AccountEventKind::BalanceSnapshot(self.asset_balance(snapshot.0).map(Snapshot)?)
            }
            AccountEventKind::BalanceStreamUpdate(snapshot) => {
                AccountEventKind::BalanceStreamUpdate(
                    self.asset_balance_update(snapshot.0).map(Snapshot)?,
                )
            }
            AccountEventKind::InstrumentBalanceUpdate(update) => {
                AccountEventKind::InstrumentBalanceUpdate(self.instrument_balance_update(update)?)
            }
            AccountEventKind::OrderSnapshot(snapshot) => {
                AccountEventKind::OrderSnapshot(self.order_snapshot(snapshot.0).map(Snapshot)?)
            }
            AccountEventKind::OrderCancelled(response) => {
                AccountEventKind::OrderCancelled(self.order_response_cancel(response)?)
            }
            AccountEventKind::Trade(trade) => AccountEventKind::Trade(self.trade(trade)?),
            // Termination reason carries no exchange/asset/instrument keys — pass through verbatim.
            AccountEventKind::StreamTerminated(reason) => {
                AccountEventKind::StreamTerminated(reason)
            }
            AccountEventKind::FillRecoveryGaveUp(gap) => {
                AccountEventKind::FillRecoveryGaveUp(self.fill_recovery_gap(gap)?)
            }
            AccountEventKind::TradeAmended(amendment) => {
                AccountEventKind::TradeAmended(self.trade_amendment(amendment)?)
            }
            AccountEventKind::ReinitFailed(failure) => {
                AccountEventKind::ReinitFailed(self.reinit_failure(failure))
            }
            AccountEventKind::CashFlow(flow) => AccountEventKind::CashFlow(self.cash_flow(flow)?),
            AccountEventKind::Notice(notice) => {
                AccountEventKind::Notice(self.notice(exchange, notice))
            }
        };

        Ok(AccountEvent { exchange, kind })
    }

    pub fn snapshot(
        &self,
        snapshot: UnindexedAccountSnapshot,
    ) -> Result<AccountSnapshot, IndexError> {
        let UnindexedAccountSnapshot {
            exchange,
            balances,
            instruments,
        } = snapshot;

        let exchange = self.map.find_exchange_index(exchange)?;

        let balances = balances
            .into_iter()
            .map(|balance| self.asset_balance(balance))
            .collect::<Result<Vec<_>, _>>()?;

        let instruments = instruments
            .into_iter()
            .map(|snapshot| {
                let InstrumentAccountSnapshot {
                    instrument,
                    orders,
                    orders_complete,
                    position,
                    isolated,
                } = snapshot;

                let instrument = self.map.find_instrument_index(&instrument)?;

                let orders = orders
                    .into_iter()
                    .map(|order| self.order_snapshot(order))
                    .collect::<Result<Vec<_>, _>>()?;

                // Per-pair isolated balances are generic over `AssetKey`, so (unlike `position`)
                // their base/quote asset names must be mapped to indices. A pair whose asset is
                // unregistered fails the snapshot index, matching top-level-balance behaviour.
                let isolated = isolated
                    .map(|state| self.isolated_instrument_state(state))
                    .transpose()?;

                Ok(InstrumentAccountSnapshot {
                    instrument,
                    orders,
                    orders_complete,
                    position,
                    isolated,
                })
            })
            .collect::<Result<Vec<_>, _>>()?;

        Ok(AccountSnapshot {
            exchange,
            balances,
            instruments,
        })
    }

    pub fn asset_balance(
        &self,
        balance: AssetBalance<AssetNameExchange>,
    ) -> Result<AssetBalance<AssetIndex>, IndexError> {
        let AssetBalance {
            asset,
            balance,
            time_exchange,
        } = balance;
        let asset = self.map.find_asset_index(&asset)?;

        Ok(AssetBalance {
            asset,
            balance,
            time_exchange,
        })
    }

    pub fn asset_balance_update(
        &self,
        update: AssetBalanceUpdate<AssetNameExchange>,
    ) -> Result<AssetBalanceUpdate<AssetIndex>, IndexError> {
        let AssetBalanceUpdate {
            asset,
            update,
            time_exchange,
        } = update;
        let asset = self.map.find_asset_index(&asset)?;

        Ok(AssetBalanceUpdate {
            asset,
            update,
            time_exchange,
        })
    }

    /// Index the per-pair isolated state's `base`/`quote` asset names to indices.
    ///
    /// # Errors
    /// Returns `IndexError` if either the base or quote asset is not registered in the map —
    /// matching the fail-fast behaviour of top-level [`Self::asset_balance`].
    pub fn isolated_instrument_state(
        &self,
        state: IsolatedInstrumentState<AssetNameExchange>,
    ) -> Result<IsolatedInstrumentState<AssetIndex>, IndexError> {
        let IsolatedInstrumentState { base, quote, risk } = state;

        Ok(IsolatedInstrumentState {
            base: self.asset_balance(base)?,
            quote: self.asset_balance(quote)?,
            risk,
        })
    }

    /// Index a [`FillRecoveryGap`]'s instruments.
    ///
    /// # Errors
    /// Returns `IndexError` if any instrument is not registered in the map. The whole gap fails
    /// rather than dropping that instrument, which would understate what is missing. A venue names
    /// only the instruments its stream was opened with, so this fails only for a stream opened
    /// with an instrument the map does not hold.
    pub fn fill_recovery_gap(
        &self,
        gap: FillRecoveryGap<InstrumentNameExchange>,
    ) -> Result<FillRecoveryGap<InstrumentIndex>, IndexError> {
        let FillRecoveryGap {
            scope,
            start,
            end,
            attempts,
            reason,
        } = gap;

        let scope = match scope {
            FillRecoveryScope::Instruments(instruments) => FillRecoveryScope::Instruments(
                instruments
                    .iter()
                    .map(|instrument| self.map.find_instrument_index(instrument))
                    .collect::<Result<_, _>>()?,
            ),
            FillRecoveryScope::AllInstruments => FillRecoveryScope::AllInstruments,
        };

        Ok(FillRecoveryGap {
            scope,
            start,
            end,
            attempts,
            reason,
        })
    }

    /// Index a [`CashFlow`]'s asset and, when it names one, instrument.
    ///
    /// # Errors
    /// Returns `IndexError` if the asset, or a named instrument, is not registered in the map.
    pub fn cash_flow(
        &self,
        flow: CashFlow<AssetNameExchange, InstrumentNameExchange>,
    ) -> Result<CashFlow<AssetIndex, InstrumentIndex>, IndexError> {
        let CashFlow {
            kind,
            asset,
            amount,
            instrument,
            time_exchange,
            id,
        } = flow;

        Ok(CashFlow {
            kind,
            asset: self.map.find_asset_index(&asset)?,
            amount,
            instrument: instrument
                .map(|instrument| self.map.find_instrument_index(&instrument))
                .transpose()?,
            time_exchange,
            id,
        })
    }

    /// Index an [`AccountNotice`]'s instrument, when it names one.
    ///
    /// Never fails. A notice naming an instrument that is not in the map is kept, with its
    /// instrument set to `None` and a `warn`: a notice changes no state, so dropping one about a
    /// margin call or liquidation would cost more than losing its attribution.
    pub fn notice(
        &self,
        exchange: ExchangeIndex,
        notice: AccountNotice<InstrumentNameExchange>,
    ) -> AccountNotice<InstrumentIndex> {
        let AccountNotice {
            kind,
            instrument,
            time_exchange,
            margin_level,
            status,
        } = notice;

        let instrument = instrument.and_then(|instrument| {
            self.map
                .find_instrument_index(&instrument)
                .inspect_err(|error| {
                    warn!(
                        ?exchange,
                        %instrument,
                        %kind,
                        %status,
                        %error,
                        "account notice names an instrument that is not in the map, \
                         delivering it unattributed",
                    );
                })
                .ok()
        });

        AccountNotice {
            kind,
            instrument,
            time_exchange,
            margin_level,
            status,
        }
    }

    /// Index an [`InstrumentBalanceUpdate`]'s instrument and asset keys.
    ///
    /// # Errors
    /// Returns `IndexError` if the instrument or the asset is not registered in the map.
    pub fn instrument_balance_update(
        &self,
        update: InstrumentBalanceUpdate<AssetNameExchange, InstrumentNameExchange>,
    ) -> Result<InstrumentBalanceUpdate, IndexError> {
        let InstrumentBalanceUpdate {
            instrument,
            balance,
        } = update;

        Ok(InstrumentBalanceUpdate {
            instrument: self.map.find_instrument_index(&instrument)?,
            balance: self.asset_balance_update(balance)?,
        })
    }

    pub fn order_snapshot(
        &self,
        order: UnindexedOrderSnapshot,
    ) -> Result<OrderSnapshot, IndexError> {
        let Order {
            key,
            side,
            price,
            quantity,
            kind,
            time_in_force,
            state,
        } = order;

        let key = self.order_key(key)?;
        let state = self.order_state(state);

        Ok(Order {
            key,
            side,
            price,
            quantity,
            kind,
            time_in_force,
            state,
        })
    }

    pub fn order_response_cancel(
        &self,
        response: OrderResponseCancel<ExchangeId, AssetNameExchange, InstrumentNameExchange>,
    ) -> Result<OrderResponseCancel, IndexError> {
        let OrderResponseCancel { key, state } = response;

        Ok(OrderResponseCancel {
            key: self.order_key(key)?,
            state: match state {
                Ok(cancelled) => Ok(cancelled),
                Err(error) => Err(self.order_error(error)),
            },
        })
    }

    pub fn order_key(&self, key: UnindexedOrderKey) -> Result<OrderKey, IndexError> {
        let UnindexedOrderKey {
            exchange,
            instrument,
            strategy,
            cid,
        } = key;

        Ok(OrderKey {
            exchange: self.map.find_exchange_index(exchange)?,
            instrument: self.map.find_instrument_index(&instrument)?,
            strategy,
            cid,
        })
    }

    /// Index an [`UnindexedOrderState`] to an [`OrderState`].
    ///
    /// Used by `ExecutionManager` to index `open_order` responses. Never fails: an order state
    /// carries no key, and the error of a failed open is indexed by [`Self::order_error`].
    pub fn order_state(&self, state: UnindexedOrderState) -> OrderState {
        match state {
            UnindexedOrderState::Active(active) => OrderState::Active(active),
            UnindexedOrderState::Inactive(inactive) => match inactive {
                InactiveOrderState::OpenFailed(failed) => {
                    OrderState::inactive(self.order_error(failed))
                }
                InactiveOrderState::Cancelled(cancelled) => OrderState::inactive(cancelled),
                InactiveOrderState::FullyFilled(filled) => OrderState::fully_filled(filled),
                InactiveOrderState::Expired(expired) => OrderState::expired(expired),
            },
        }
    }

    /// Index an [`UnindexedApiError`] returned for an order request (open or cancel).
    ///
    /// Never fails. An error carries the venue's answer to a request, and the caller must
    /// receive that answer, so a name the [`ExecutionInstrumentMap`] does not hold degrades the
    /// error instead of discarding it (and the order response or client error carrying it). Each
    /// degrade logs a `warn!` naming what was dropped:
    /// - [`ApiError::BalanceInsufficient`] keeps its variant, with the asset `None`.
    /// - [`ApiError::AssetInvalid`] and [`ApiError::InstrumentInvalid`] have no index to carry,
    ///   so they become [`ApiError::OrderRejected`] holding their own message, which keeps the
    ///   name and the venue's text.
    ///
    /// For an error from any other request, use [`Self::client_error`], which degrades to
    /// [`ApiError::RequestRejected`] instead.
    pub fn api_error(&self, error: UnindexedApiError) -> ApiError {
        self.index_api_error(error, ApiError::OrderRejected)
    }

    /// Index an [`UnindexedApiError`], degrading a name the map does not hold as
    /// [`Self::api_error`] describes. `rejected` builds the rejection that an unresolvable
    /// [`ApiError::AssetInvalid`] or [`ApiError::InstrumentInvalid`] becomes, which depends on
    /// whether the request was an order.
    fn index_api_error(
        &self,
        error: UnindexedApiError,
        rejected: fn(String) -> ApiError,
    ) -> ApiError {
        match error {
            UnindexedApiError::RateLimit { retry_after } => ApiError::rate_limited(retry_after),
            UnindexedApiError::CapacityExceeded(msg) => ApiError::CapacityExceeded(msg),
            UnindexedApiError::Unauthenticated(msg) => ApiError::Unauthenticated(msg),
            UnindexedApiError::AssetInvalid(asset, value) => {
                match self.map.find_asset_index(&asset) {
                    Ok(asset) => ApiError::AssetInvalid(asset, value),
                    Err(_) => self.degrade_unresolvable(
                        UnindexedApiError::AssetInvalid(asset, value),
                        rejected,
                    ),
                }
            }
            UnindexedApiError::InstrumentInvalid(instrument, value) => {
                match self.map.find_instrument_index(&instrument) {
                    Ok(instrument) => ApiError::InstrumentInvalid(instrument, value),
                    Err(_) => self.degrade_unresolvable(
                        UnindexedApiError::InstrumentInvalid(instrument, value),
                        rejected,
                    ),
                }
            }
            UnindexedApiError::BalanceInsufficient(asset, value) => {
                let asset = asset.and_then(|asset| {
                    self.map
                        .find_asset_index(&asset)
                        .inspect_err(|_| {
                            warn!(
                                exchange = %self.map.exchange.value,
                                %asset,
                                message = %value,
                                "AccountEventIndexer dropping the asset of a BalanceInsufficient \
                                 error: the instrument map does not hold it"
                            )
                        })
                        .ok()
                });
                ApiError::BalanceInsufficient(asset, value)
            }
            UnindexedApiError::OrderRejected(reason) => ApiError::OrderRejected(reason),
            UnindexedApiError::OrderAlreadyCancelled => ApiError::OrderAlreadyCancelled,
            UnindexedApiError::OrderAlreadyFullyFilled => ApiError::OrderAlreadyFullyFilled,
            UnindexedApiError::OrderAlreadyExpired => ApiError::OrderAlreadyExpired,
            UnindexedApiError::DuplicateClientOrderId(message) => {
                ApiError::DuplicateClientOrderId(message)
            }
            UnindexedApiError::RequestRejected(reason) => ApiError::RequestRejected(reason),
            UnindexedApiError::BorrowRejected(reject) => ApiError::BorrowRejected(reject),
        }
    }

    /// Degrade an error naming an asset or instrument the map does not hold to the rejection
    /// `rejected` builds, carrying the error's own message so neither the name nor the venue's
    /// text is lost.
    fn degrade_unresolvable(
        &self,
        error: UnindexedApiError,
        rejected: fn(String) -> ApiError,
    ) -> ApiError {
        let error = error.to_string();
        warn!(
            exchange = %self.map.exchange.value,
            %error,
            "AccountEventIndexer degrading an API error to a rejection: the instrument map does \
             not hold the asset or instrument it names"
        );
        rejected(error)
    }

    pub fn order_request<Kind>(
        &self,
        order: &OrderEvent<Kind, ExchangeIndex, InstrumentIndex>,
    ) -> Result<OrderEvent<Kind, ExchangeId, &InstrumentNameExchange>, KeyError>
    where
        Kind: Clone,
    {
        let OrderEvent {
            key:
                OrderKey {
                    exchange,
                    instrument,
                    strategy,
                    cid,
                },
            state,
        } = order;

        let exchange = self.map.find_exchange_id(*exchange)?;
        let instrument = self.map.find_instrument_name_exchange(*instrument)?;

        Ok(OrderEvent {
            key: OrderKey {
                exchange,
                instrument,
                strategy: strategy.clone(),
                cid: cid.clone(),
            },
            state: state.clone(),
        })
    }

    /// Index an [`UnindexedOrderError`]. Never fails: see [`Self::api_error`].
    pub fn order_error(&self, error: UnindexedOrderError) -> OrderError {
        match error {
            UnindexedOrderError::Connectivity(error) => OrderError::Connectivity(error),
            UnindexedOrderError::Rejected(error) => OrderError::Rejected(self.api_error(error)),
            UnindexedOrderError::UnsupportedOrderType(msg) => OrderError::UnsupportedOrderType(msg),
            UnindexedOrderError::InvalidPrecision(violation) => {
                OrderError::InvalidPrecision(violation)
            }
        }
    }

    /// Index an [`UnindexedClientError`]. Never fails: like [`Self::api_error`], but an
    /// unresolvable [`ApiError::AssetInvalid`] or [`ApiError::InstrumentInvalid`] becomes
    /// [`ApiError::RequestRejected`], since the request need not have been an order.
    pub fn client_error(&self, error: UnindexedClientError) -> ClientError {
        match error {
            UnindexedClientError::Connectivity(error) => ClientError::Connectivity(error),
            UnindexedClientError::Api(error) => {
                ClientError::Api(self.index_api_error(error, ApiError::RequestRejected))
            }
            UnindexedClientError::TaskFailed(value) => ClientError::TaskFailed(value),
            UnindexedClientError::Internal(value) => ClientError::Internal(value),
            UnindexedClientError::Truncated { fills_read } => ClientError::Truncated { fills_read },
            UnindexedClientError::TruncatedSnapshot { limit } => {
                ClientError::TruncatedSnapshot { limit }
            }
        }
    }

    /// Index an [`UnindexedAccountStreamInitError`]. Never fails, as [`Self::client_error`].
    pub fn account_stream_init_error(
        &self,
        error: UnindexedAccountStreamInitError,
    ) -> AccountStreamInitError {
        match error {
            AccountStreamInitError::Client(error) => {
                AccountStreamInitError::Client(self.client_error(error))
            }
            AccountStreamInitError::Index(error) => AccountStreamInitError::Index(error),
        }
    }

    /// Index an [`AccountReinitFailure`]. Never fails, as [`Self::client_error`].
    pub fn reinit_failure(
        &self,
        failure: AccountReinitFailure<AssetNameExchange, InstrumentNameExchange>,
    ) -> AccountReinitFailure {
        let AccountReinitFailure { attempt, error } = failure;
        AccountReinitFailure {
            attempt,
            error: self.account_stream_init_error(error),
        }
    }

    /// Index a [`TradeAmendment`]. A replacement trade is indexed as [`trade`](Self::trade) indexes
    /// one, so an amendment fails where its trade would.
    pub fn trade_amendment(
        &self,
        amendment: TradeAmendment<AssetNameExchange, InstrumentNameExchange>,
    ) -> Result<TradeAmendment<AssetIndex, InstrumentIndex>, IndexError> {
        let TradeAmendment {
            instrument,
            order_id,
            time_exchange,
            original,
            kind,
        } = amendment;

        let instrument = self.map.find_instrument_index(&instrument)?;
        let kind = match kind {
            TradeAmendmentKind::Busted { quantity } => TradeAmendmentKind::Busted { quantity },
            TradeAmendmentKind::Corrected { replacement } => TradeAmendmentKind::Corrected {
                replacement: self.trade(replacement)?,
            },
            TradeAmendmentKind::CorrectedUnresolved {
                id,
                price,
                quantity,
            } => TradeAmendmentKind::CorrectedUnresolved {
                id,
                price,
                quantity,
            },
        };

        Ok(TradeAmendment {
            instrument,
            order_id,
            time_exchange,
            original,
            kind,
        })
    }

    /// Index a trade, converting fee asset and computing `fees_quote`.
    ///
    /// Computes `fees_quote` based on fee asset relationship to instrument:
    /// - Fee in quote asset: `fees_quote = Some(fees)`
    /// - Fee in base asset: `fees_quote = Some(fees * price)`
    /// - Fee in third-party asset (e.g., BNB): `fees_quote = None`
    ///
    /// # A fee asset that is not in the map
    /// The trade is kept, with its fee zeroed: `fees` is `0` in the instrument's quote asset and
    /// `fees_quote` is `None`. A `warn` names the fee's asset and amount. Dropping the trade would
    /// leave the engine's position different from the venue's, which costs far more than one
    /// unknown fee. The `warn` is per trade, so an account that pays every fee in an unregistered
    /// asset logs one per fill until the asset is registered. Two cases reach this:
    /// - An asset the account pays fees in but no instrument implies, such as BNB on Binance spot
    ///   with "pay fees with BNB" turned on. Register it with
    ///   [`IndexedInstrumentsBuilder::add_asset`](rustrade_instrument::index::builder::IndexedInstrumentsBuilder::add_asset)
    ///   to keep its fee.
    /// - The `"UNKNOWN"` placeholder some integrations use when the venue gave no fee asset: IBKR
    ///   `fetch_trades` for an execution IB sent no commission report for, and Binance when a
    ///   report omits `commissionAsset`.
    ///
    /// # Errors
    /// Returns `IndexError` if the trade's instrument is not in the map.
    pub fn trade(
        &self,
        trade: Trade<AssetNameExchange, InstrumentNameExchange>,
    ) -> Result<Trade<AssetIndex, InstrumentIndex>, IndexError> {
        let Trade {
            id,
            order_id,
            instrument,
            strategy,
            time_exchange,
            side,
            price: trade_price,
            quantity,
            order_filled_quantity,
            fees,
            origin,
            position_side,
            client_order_id,
        } = trade;

        let instrument_index = self.map.find_instrument_index(&instrument)?;
        // Invariant check: `find_instrument_index` resolves only names built from `instruments`,
        // so the index is always present. Failing loudly beats indexing a fee against nothing.
        let underlying = &self
            .map
            .instruments
            .get_index(instrument_index.index())
            .ok_or_else(|| {
                IndexError::InstrumentIndex(format!(
                    "ExecutionInstrumentMap has no instrument at {instrument_index} for {instrument}"
                ))
            })?
            .underlying;

        let fees = match self.map.find_asset_index(&fees.asset) {
            Ok(fee_asset) => {
                let fees_quote = if fee_asset == underlying.quote {
                    // Fee is in quote asset — no conversion needed
                    Some(fees.fees)
                } else if fee_asset == underlying.base {
                    // Fee is in base asset — convert using trade price
                    Some(fees.fees * trade_price)
                } else {
                    // Fee is in third-party asset (e.g., BNB) — needs external price
                    None
                };
                AssetFees::new(fee_asset, fees.fees, fees_quote)
            }
            Err(error) => {
                warn!(
                    exchange = %self.map.exchange.value,
                    %instrument,
                    trade_id = %id,
                    fee_asset = %fees.asset,
                    fee = %fees.fees,
                    %error,
                    "trade's fee asset is not in the map, keeping the trade with its fee zeroed; \
                     register the asset with IndexedInstrumentsBuilder::add_asset to keep its fee",
                );
                AssetFees::new(underlying.quote, Decimal::ZERO, None)
            }
        };

        Ok(Trade {
            id,
            order_id,
            instrument: instrument_index,
            strategy,
            time_exchange,
            side,
            price: trade_price,
            quantity,
            order_filled_quantity,
            fees,
            origin,
            position_side,
            client_order_id,
        })
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used)] // Test code: panics on bad input are acceptable
mod tests {
    use super::*;
    use crate::{
        error::StreamTerminationReason, fill_recovery::FillRecoveryFailure,
        map::generate_execution_instrument_map,
    };
    use chrono::{DateTime, Utc};
    use rustrade_instrument::{index::IndexedInstruments, test_utils};

    fn binance_indexer() -> AccountEventIndexer {
        let instruments = IndexedInstruments::new(vec![test_utils::instrument(
            ExchangeId::BinanceSpot,
            "BTC",
            "USDT",
        )]);
        let map = generate_execution_instrument_map(&instruments, ExchangeId::BinanceSpot).unwrap();
        AccountEventIndexer::new(Arc::new(map))
    }

    /// `StreamTerminated` carries no asset/instrument keys, so it must index by mapping only the
    /// exchange and passing the reason through verbatim.
    #[test]
    fn account_event_passes_stream_terminated_through_verbatim() {
        let indexer = binance_indexer();
        let reason = StreamTerminationReason::ReconnectBudgetExhausted {
            attempts: 3,
            last_error: "socket reset".to_string(),
        };

        let indexed = indexer
            .account_event(UnindexedAccountEvent::new(
                ExchangeId::BinanceSpot,
                AccountEventKind::StreamTerminated(reason.clone()),
            ))
            .unwrap();

        assert_eq!(indexed.exchange, indexer.map.exchange.key);
        assert!(
            matches!(indexed.kind, AccountEventKind::StreamTerminated(r) if r == reason),
            "expected StreamTerminated to pass through unchanged",
        );
    }

    /// A failed re-init never fails to index: its client error is indexed as
    /// [`AccountEventIndexer::client_error`] indexes one, and an index error passes through.
    #[test]
    fn account_event_indexes_a_reinit_failure() {
        let indexer = binance_indexer();
        let btc = indexer
            .map
            .find_asset_index(&AssetNameExchange::new("BTC"))
            .unwrap();
        let unindexed_client = AccountStreamInitError::Client(ClientError::Api(
            ApiError::BalanceInsufficient(Some(AssetNameExchange::new("BTC")), "low".to_owned()),
        ));
        let indexed_client = AccountStreamInitError::Client(ClientError::Api(
            ApiError::BalanceInsufficient(Some(btc), "low".to_owned()),
        ));
        let index_error = IndexError::AssetIndex("ETH".to_owned());

        for (error, expected) in [
            (unindexed_client, indexed_client),
            (
                AccountStreamInitError::Index(index_error.clone()),
                AccountStreamInitError::Index(index_error),
            ),
        ] {
            let indexed = indexer
                .account_event(UnindexedAccountEvent::new(
                    ExchangeId::BinanceSpot,
                    AccountEventKind::ReinitFailed(AccountReinitFailure::new(2, error)),
                ))
                .unwrap();

            assert_eq!(indexed.exchange, indexer.map.exchange.key);
            assert_eq!(
                indexed.kind,
                AccountEventKind::ReinitFailed(AccountReinitFailure::new(2, expected))
            );
        }
    }

    /// A fill recovery give-up indexes each instrument it names, and fails as a whole when one
    /// is not in the map rather than understating what is missing. `AllInstruments` names none.
    #[test]
    fn account_event_indexes_a_fill_recovery_give_up_by_its_instruments() {
        let indexer = binance_indexer();
        let start = DateTime::<Utc>::MIN_UTC;
        let end = DateTime::<Utc>::MAX_UTC;
        let give_up = |scope| {
            UnindexedAccountEvent::new(
                ExchangeId::BinanceSpot,
                AccountEventKind::FillRecoveryGaveUp(FillRecoveryGap::new(
                    scope,
                    start,
                    end,
                    6,
                    FillRecoveryFailure::TimedOut { timeout_secs: 30 },
                )),
            )
        };
        let index_of = |scope| match indexer.account_event(give_up(scope)) {
            Ok(AccountEvent {
                kind: AccountEventKind::FillRecoveryGaveUp(gap),
                ..
            }) => Ok(gap),
            Ok(other) => panic!("expected FillRecoveryGaveUp, got {other:?}"),
            Err(error) => Err(error),
        };
        let Ok(btc) = indexer
            .map
            .find_instrument_index(&InstrumentNameExchange::new("BTC_USDT"))
        else {
            panic!("BTC_USDT is mapped");
        };

        assert_eq!(
            index_of(FillRecoveryScope::Instruments(vec![
                InstrumentNameExchange::new("BTC_USDT")
            ])),
            Ok(FillRecoveryGap::new(
                FillRecoveryScope::Instruments(vec![btc]),
                start,
                end,
                6,
                FillRecoveryFailure::TimedOut { timeout_secs: 30 },
            ))
        );
        assert_eq!(
            index_of(FillRecoveryScope::AllInstruments).map(|gap| gap.scope),
            Ok(FillRecoveryScope::AllInstruments)
        );
        assert!(
            index_of(FillRecoveryScope::Instruments(vec![
                InstrumentNameExchange::new("BTC_USDT"),
                InstrumentNameExchange::new("ETHUSDT"),
            ]))
            .is_err(),
            "an unmapped instrument fails the whole give-up"
        );
    }

    /// A trade amendment indexes its instrument, and a corrected one its replacement as a trade
    /// is indexed. An unmapped instrument fails it.
    #[test]
    fn account_event_indexes_a_trade_amendment() {
        use crate::{
            order::id::{OrderId, StrategyId},
            trade::TradeId,
        };
        use rust_decimal::Decimal;
        use rustrade_instrument::Side;

        let indexer = binance_indexer();
        let time = DateTime::<Utc>::MIN_UTC;
        let amended = |instrument: &str, kind| {
            UnindexedAccountEvent::new(
                ExchangeId::BinanceSpot,
                AccountEventKind::TradeAmended(TradeAmendment::new(
                    InstrumentNameExchange::new(instrument),
                    OrderId::new("ord-1"),
                    time,
                    Some(TradeId::new("t-1")),
                    kind,
                )),
            )
        };
        let index_of = |event| match indexer.account_event(event) {
            Ok(AccountEvent {
                kind: AccountEventKind::TradeAmended(amendment),
                ..
            }) => Ok(amendment),
            Ok(other) => panic!("expected TradeAmended, got {other:?}"),
            Err(error) => Err(error),
        };
        let Ok(btc) = indexer
            .map
            .find_instrument_index(&InstrumentNameExchange::new("BTC_USDT"))
        else {
            panic!("BTC_USDT is mapped");
        };

        assert_eq!(
            index_of(amended(
                "BTC_USDT",
                TradeAmendmentKind::Busted {
                    quantity: Some(Decimal::ONE)
                }
            )),
            Ok(TradeAmendment::new(
                btc,
                OrderId::new("ord-1"),
                time,
                Some(TradeId::new("t-1")),
                TradeAmendmentKind::Busted {
                    quantity: Some(Decimal::ONE)
                },
            ))
        );

        let replacement = Trade::new(
            TradeId::new("t-2"),
            OrderId::new("ord-1"),
            InstrumentNameExchange::new("BTC_USDT"),
            StrategyId::unknown(),
            time,
            Side::Buy,
            Decimal::TEN,
            Decimal::ONE,
            None,
            AssetFees::new(AssetNameExchange::new("USDT"), Decimal::ONE, None),
        );
        let Ok(TradeAmendment {
            kind: TradeAmendmentKind::Corrected { replacement },
            ..
        }) = index_of(amended(
            "BTC_USDT",
            TradeAmendmentKind::Corrected { replacement },
        ))
        else {
            panic!("a correction indexes to a correction");
        };
        assert_eq!(replacement.instrument, btc);
        assert_eq!(
            replacement.fees.fees_quote,
            Some(Decimal::ONE),
            "indexed as a trade is, fees and all"
        );

        assert!(
            index_of(amended(
                "ETHUSDT",
                TradeAmendmentKind::Busted { quantity: None }
            ))
            .is_err(),
            "an unmapped instrument fails the amendment"
        );
    }

    /// A fill is never dropped for its fee: an unmapped fee asset, or the `"UNKNOWN"` placeholder,
    /// keeps the trade with its fee zeroed in quote and `fees_quote: None`, with one `warn`. A fee
    /// asset registered with `add_asset` indexes as a third-party fee.
    #[test]
    fn trade_keeps_a_fill_whose_fee_asset_is_unmapped() {
        use crate::{
            order::id::{OrderId, StrategyId},
            trade::TradeId,
        };
        use rust_decimal_macros::dec;
        use rustrade_instrument::{Side, index::builder::IndexedInstrumentsBuilder};

        let fill = |fee_asset: &str, fee| {
            Trade::new(
                TradeId::new("t-1"),
                OrderId::new("ord-1"),
                InstrumentNameExchange::new("BTC_USDT"),
                StrategyId::unknown(),
                DateTime::<Utc>::MIN_UTC,
                Side::Buy,
                dec!(50000),
                dec!(0.01),
                None,
                AssetFees::new(AssetNameExchange::new(fee_asset), fee, None),
            )
        };
        let asset = |indexer: &AccountEventIndexer, name: &str| {
            indexer
                .map
                .find_asset_index(&AssetNameExchange::new(name))
                .unwrap()
        };

        let indexer = binance_indexer();
        let usdt = asset(&indexer, "USDT");
        let btc = asset(&indexer, "BTC");

        let (quote, warnings) = count_warnings(|| indexer.trade(fill("USDT", dec!(0.5))));
        assert_eq!(
            quote.unwrap().fees,
            AssetFees::new(usdt, dec!(0.5), Some(dec!(0.5)))
        );
        assert_eq!(warnings, 0);

        let (base, warnings) = count_warnings(|| indexer.trade(fill("BTC", dec!(0.00001))));
        assert_eq!(
            base.unwrap().fees,
            AssetFees::new(btc, dec!(0.00001), Some(dec!(0.5)))
        );
        assert_eq!(warnings, 0);

        for unmapped in ["BNB", "UNKNOWN"] {
            let (kept, warnings) = count_warnings(|| indexer.trade(fill(unmapped, dec!(0.001))));
            let kept = kept.unwrap_or_else(|error| panic!("{unmapped}: dropped: {error}"));
            assert_eq!(
                kept.quantity,
                dec!(0.01),
                "{unmapped}: the fill itself is intact"
            );
            assert_eq!(
                kept.fees,
                AssetFees::new(usdt, Decimal::ZERO, None),
                "{unmapped}: fee zeroed in quote, with no quote equivalent"
            );
            assert_eq!(warnings, 1, "{unmapped}: the zeroed fee is reported");
        }

        let instruments = IndexedInstrumentsBuilder::new()
            .add_instrument(test_utils::instrument(
                ExchangeId::BinanceSpot,
                "BTC",
                "USDT",
            ))
            .add_asset(test_utils::exchange_asset(ExchangeId::BinanceSpot, "BNB"))
            .build();
        let with_bnb = AccountEventIndexer::new(Arc::new(
            generate_execution_instrument_map(&instruments, ExchangeId::BinanceSpot).unwrap(),
        ));
        let bnb = asset(&with_bnb, "BNB");
        let (third_party, warnings) = count_warnings(|| with_bnb.trade(fill("BNB", dec!(0.001))));
        assert_eq!(
            third_party.unwrap().fees,
            AssetFees::new(bnb, dec!(0.001), None),
            "a registered fee asset keeps its fee, with no quote equivalent"
        );
        assert_eq!(warnings, 0);

        let mut unmapped_instrument = fill("USDT", dec!(0.5));
        unmapped_instrument.instrument = InstrumentNameExchange::new("ETH_USDT");
        assert!(
            indexer.trade(unmapped_instrument).is_err(),
            "an unmapped instrument still fails"
        );
    }

    /// Run `index`, returning its value and the number of `WARN` events it emitted on this
    /// thread. A degrade is reported only by its log line, so asserting on the value alone could
    /// not tell a logged degrade from a silent one.
    fn count_warnings<T>(index: impl FnOnce() -> T) -> (T, usize) {
        use std::sync::atomic::{AtomicUsize, Ordering};
        use tracing_subscriber::layer::SubscriberExt;

        #[derive(Default)]
        struct CountWarnings(Arc<AtomicUsize>);

        impl<S: tracing::Subscriber> tracing_subscriber::Layer<S> for CountWarnings {
            fn on_event(
                &self,
                event: &tracing::Event<'_>,
                _: tracing_subscriber::layer::Context<'_, S>,
            ) {
                if *event.metadata().level() == tracing::Level::WARN {
                    self.0.fetch_add(1, Ordering::Relaxed);
                }
            }
        }

        let layer = CountWarnings::default();
        let count = Arc::clone(&layer.0);
        // Thread-local, so other tests running in parallel are not counted.
        let value =
            tracing::subscriber::with_default(tracing_subscriber::registry().with(layer), index);
        (value, count.load(Ordering::Relaxed))
    }

    fn btc_usdt() -> InstrumentNameExchange {
        binance_indexer()
            .map
            .exchange_instruments()
            .next()
            .cloned()
            .unwrap_or_else(|| panic!("the map holds one instrument"))
    }

    #[test]
    fn balance_insufficient_keeps_a_held_asset_and_drops_an_unheld_one() {
        let indexer = binance_indexer();
        let btc = indexer
            .map
            .find_asset_index(&AssetNameExchange::new("BTC"))
            .unwrap();

        for (asset, expected, warnings) in [
            (Some("BTC"), Some(btc), 0),
            // An instrument name, as Binance's error mapping once put there.
            (Some("ETH_USDT"), None, 1),
            (None, None, 0),
        ] {
            let (indexed, warned) = count_warnings(|| {
                indexer.api_error(ApiError::BalanceInsufficient(
                    asset.map(AssetNameExchange::new),
                    "low".to_string(),
                ))
            });
            assert_eq!(
                indexed,
                ApiError::BalanceInsufficient(expected, "low".to_string()),
                "{asset:?}"
            );
            assert_eq!(warned, warnings, "{asset:?}: a dropped asset is logged");
        }
    }

    /// An invalid asset or instrument the map does not hold has no index to carry, so it becomes
    /// a rejection keeping its own message: `OrderRejected` for an order, `RequestRejected` for
    /// any other request.
    #[test]
    fn an_unresolvable_invalid_name_degrades_to_a_rejection_keeping_its_text() {
        let indexer = binance_indexer();
        for (error, text) in [
            (
                ApiError::AssetInvalid(AssetNameExchange::new("XYZ"), "bad".to_string()),
                "asset XYZ invalid: bad",
            ),
            (
                ApiError::InstrumentInvalid(
                    InstrumentNameExchange::new("XYZUSDT"),
                    "bad".to_string(),
                ),
                "instrument XYZUSDT invalid: bad",
            ),
        ] {
            let (indexed, warned) = count_warnings(|| indexer.api_error(error.clone()));
            assert_eq!(indexed, ApiError::OrderRejected(text.to_string()));
            assert_eq!(warned, 1, "{text}: a degrade is logged");

            let (indexed, warned) =
                count_warnings(|| indexer.client_error(ClientError::Api(error)));
            assert_eq!(
                indexed,
                ClientError::Api(ApiError::RequestRejected(text.to_string()))
            );
            assert_eq!(warned, 1, "{text}: a degrade is logged");
        }

        // A name the map holds still indexes, and nothing is logged.
        let (indexed, warned) = count_warnings(|| {
            indexer.api_error(ApiError::InstrumentInvalid(btc_usdt(), "bad".to_string()))
        });
        assert!(matches!(indexed, ApiError::InstrumentInvalid(_, _)));
        assert_eq!(warned, 0);
    }

    /// A response whose rejection names something the map does not hold still indexes, so the
    /// order is settled rather than left in flight.
    #[test]
    fn a_response_whose_rejection_names_an_unresolvable_asset_still_indexes() {
        let indexer = binance_indexer();
        let rejection = || {
            OrderError::Rejected(ApiError::BalanceInsufficient(
                Some(AssetNameExchange::new("ETH_USDT")),
                "low".to_string(),
            ))
        };
        let indexed = OrderError::Rejected(ApiError::BalanceInsufficient(None, "low".to_string()));

        assert_eq!(
            indexer.order_state(OrderState::inactive(rejection())),
            OrderState::inactive(indexed.clone())
        );

        let key = OrderKey {
            exchange: ExchangeId::BinanceSpot,
            instrument: btc_usdt(),
            strategy: crate::order::id::StrategyId::new("test"),
            cid: crate::order::id::ClientOrderId::random(),
        };
        let cancel = indexer
            .order_response_cancel(OrderResponseCancel {
                key,
                state: Err(rejection()),
            })
            .unwrap();
        assert_eq!(cancel.state, Err(indexed));
    }

    /// A cash flow indexes its asset and, when it names one, its instrument, and fails when
    /// either is not in the map.
    #[test]
    fn account_event_indexes_a_cash_flow() {
        use crate::cash_flow::{CashFlowId, CashFlowKind};
        use rust_decimal_macros::dec;

        let indexer = binance_indexer();
        let time = DateTime::<Utc>::MIN_UTC;
        let kind = CashFlowKind::Funding {
            rate: Some(dec!(0.0001)),
            position_quantity: Some(dec!(-2)),
        };
        let flow = |asset: &str, instrument: Option<&str>| {
            UnindexedAccountEvent::new(
                ExchangeId::BinanceSpot,
                AccountEventKind::CashFlow(CashFlow::new(
                    kind.clone(),
                    AssetNameExchange::new(asset),
                    dec!(1.5),
                    instrument.map(InstrumentNameExchange::new),
                    time,
                    Some(CashFlowId::new("cf-1")),
                )),
            )
        };
        let index_of = |event| match indexer.account_event(event) {
            Ok(AccountEvent {
                kind: AccountEventKind::CashFlow(flow),
                ..
            }) => Ok(flow),
            Ok(other) => panic!("expected CashFlow, got {other:?}"),
            Err(error) => Err(error),
        };
        let (Ok(usdt), Ok(btc_usdt)) = (
            indexer
                .map
                .find_asset_index(&AssetNameExchange::new("USDT")),
            indexer
                .map
                .find_instrument_index(&InstrumentNameExchange::new("BTC_USDT")),
        ) else {
            panic!("USDT and BTC_USDT are mapped");
        };

        assert_eq!(
            index_of(flow("USDT", Some("BTC_USDT"))),
            Ok(CashFlow::new(
                kind.clone(),
                usdt,
                dec!(1.5),
                Some(btc_usdt),
                time,
                Some(CashFlowId::new("cf-1")),
            ))
        );
        assert_eq!(
            index_of(flow("USDT", None)).map(|flow| flow.instrument),
            Ok(None)
        );
        assert!(
            index_of(flow("ETH", None)).is_err(),
            "an unmapped asset fails"
        );
        assert!(
            index_of(flow("USDT", Some("ETHUSDT"))).is_err(),
            "an unmapped instrument fails"
        );
    }

    /// A notice indexes its instrument when it names a mapped one, and is kept unattributed,
    /// rather than dropped, when it names one that is not mapped.
    #[test]
    fn account_event_indexes_a_notice() {
        use crate::notice::{AccountNotice, NoticeKind};
        use rust_decimal_macros::dec;
        use smol_str::SmolStr;

        let indexer = binance_indexer();
        let time = DateTime::<Utc>::MIN_UTC;
        let notice = |instrument: Option<&str>| {
            UnindexedAccountEvent::new(
                ExchangeId::BinanceSpot,
                AccountEventKind::Notice(AccountNotice::new(
                    NoticeKind::MarginCall,
                    instrument.map(InstrumentNameExchange::new),
                    time,
                    Some(dec!(1.1)),
                    SmolStr::new("MARGIN_CALL"),
                )),
            )
        };
        let index_of = |event| match indexer.account_event(event) {
            Ok(AccountEvent {
                kind: AccountEventKind::Notice(notice),
                ..
            }) => notice,
            other => panic!("expected Notice, got {other:?}"),
        };
        let Ok(btc_usdt) = indexer
            .map
            .find_instrument_index(&InstrumentNameExchange::new("BTC_USDT"))
        else {
            panic!("BTC_USDT is mapped");
        };

        assert_eq!(
            index_of(notice(Some("BTC_USDT"))),
            AccountNotice::new(
                NoticeKind::MarginCall,
                Some(btc_usdt),
                time,
                Some(dec!(1.1)),
                SmolStr::new("MARGIN_CALL"),
            )
        );
        assert_eq!(index_of(notice(None)).instrument, None);
        assert_eq!(
            index_of(notice(Some("ETHUSDT"))).instrument,
            None,
            "an unmapped instrument is dropped from the notice, not the notice itself"
        );
    }
}