evm-oracle-state 0.2.0

EVM-backed Chainlink-style oracle state tracking over evm-fork-cache
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
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
use std::{collections::BTreeMap, sync::Arc};

use alloy_primitives::{Address, B256, I256, U256, U512};

use crate::{
    AggregatorLayout, AggregatorLayoutConfidence, AggregatorLayoutEvidence, ChainlinkFeedProvider,
    FeedConfig, FeedMetadata, OracleBlockRef, OracleError, OracleFeedStatus, OracleSnapshot,
    OracleSourceDescriptor, OracleTransformDescriptor, OracleValueSource, RoundData,
    classify_type_and_version,
};

/// Stable id for a registered feed.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FeedId(Arc<str>);

impl FeedId {
    /// Create a feed id from its string form. Ids compare and order exactly
    /// like their underlying string; cloning is a cheap `Arc` bump.
    pub fn new(id: impl Into<String>) -> Self {
        Self(Arc::from(id.into()))
    }

    /// Borrow the underlying string, e.g. for logging or map keys.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

// Enables allocation-free `BTreeMap<FeedId, _>` lookups by `&str`. `FeedId`'s
// derived `Ord`/`Eq`/`Hash` all delegate to the inner `str`, so the `Borrow`
// consistency contract holds.
impl std::borrow::Borrow<str> for FeedId {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl std::fmt::Display for FeedId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl AsRef<str> for FeedId {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

/// One Chainlink-compatible dependency used by a derived oracle source.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct OracleDependency {
    /// Dependency proxy address.
    pub proxy: Address,
    /// Dependency current aggregator address.
    pub aggregator: Address,
    /// Last known dependency answer.
    pub answer: I256,
}

impl OracleDependency {
    /// Construct a dependency leg from its proxy, current aggregator, and the
    /// last answer to seed derived-source math with until the next event.
    pub fn new(proxy: Address, aggregator: Address, answer: I256) -> Self {
        Self {
            proxy,
            aggregator,
            answer,
        }
    }
}

/// Role of a Chainlink dependency inside `MorphoChainlinkOracleV2`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum MorphoChainlinkFeedRole {
    /// First numerator feed.
    BaseFeed1,
    /// Second numerator feed.
    BaseFeed2,
    /// First denominator feed.
    QuoteFeed1,
    /// Second denominator feed.
    QuoteFeed2,
}

/// One optional Chainlink feed used by `MorphoChainlinkOracleV2`.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct MorphoChainlinkFeed {
    /// Feed role in the Morpho formula.
    pub role: MorphoChainlinkFeedRole,
    /// Dependency proxy, aggregator, and answer.
    pub dependency: OracleDependency,
}

impl MorphoChainlinkFeed {
    /// Construct a Morpho Chainlink dependency.
    pub fn new(role: MorphoChainlinkFeedRole, dependency: OracleDependency) -> Self {
        Self { role, dependency }
    }
}

/// One dependency leg inside an Euler quote adapter.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum EulerQuoteLeg {
    /// Chainlink-backed Euler `ChainlinkOracle` leg.
    Chainlink {
        /// Euler leg adapter address.
        oracle: Address,
        /// Chainlink feed proxy.
        feed: Address,
        /// Chainlink feed current aggregator.
        aggregator: Address,
        /// Last known Chainlink answer.
        answer: I256,
        /// Chainlink feed decimals.
        feed_decimals: u8,
        /// True when the requested leg is the inverse of the adapter's configured pair.
        inverse: bool,
        /// Output decimals for this leg.
        quote_decimals: u8,
    },
    /// Euler `FixedRateOracle` leg.
    FixedRate {
        /// Euler leg adapter address.
        oracle: Address,
        /// Fixed rate scaled to this leg's quote decimals.
        rate: I256,
        /// True when the requested leg is the inverse of the adapter's configured pair.
        inverse: bool,
        /// Requested base decimals.
        base_decimals: u8,
        /// Requested quote decimals.
        quote_decimals: u8,
    },
    /// Euler `RateProviderOracle` leg.
    RateProvider {
        /// Euler leg adapter address.
        oracle: Address,
        /// Rate provider contract.
        rate_provider: Address,
        /// Last known 18-decimal rate.
        rate: I256,
        /// True when the requested leg is the inverse of the adapter's configured pair.
        inverse: bool,
        /// Output decimals for this leg.
        quote_decimals: u8,
    },
}

impl EulerQuoteLeg {
    /// Return the event aggregator that drives this leg, if any.
    pub fn event_aggregator(&self) -> Option<Address> {
        match self {
            Self::Chainlink { aggregator, .. } => Some(*aggregator),
            Self::FixedRate { .. } | Self::RateProvider { .. } => None,
        }
    }

    /// Return the leg price scaled to the leg quote decimals.
    pub fn price(&self) -> I256 {
        self.price_from_event(None, I256::ZERO)
    }

    /// Return the leg price with a dependency event answer applied when relevant.
    pub fn price_from_event(&self, aggregator: Option<Address>, answer: I256) -> I256 {
        match self {
            Self::Chainlink {
                aggregator: dependency_aggregator,
                answer: stored_answer,
                feed_decimals,
                inverse,
                quote_decimals,
                ..
            } => {
                let answer = if aggregator == Some(*dependency_aggregator) {
                    answer
                } else {
                    *stored_answer
                };
                if *inverse {
                    inverse_positive_price(answer, *feed_decimals, *quote_decimals)
                } else {
                    scale_positive_price(answer, *feed_decimals, *quote_decimals)
                }
            }
            Self::FixedRate {
                rate,
                inverse,
                base_decimals,
                quote_decimals,
                ..
            } => {
                if *inverse {
                    inverse_fixed_rate(*rate, *base_decimals, *quote_decimals)
                } else {
                    *rate
                }
            }
            Self::RateProvider {
                rate,
                inverse,
                quote_decimals,
                ..
            } => {
                if *inverse {
                    inverse_positive_price(*rate, 18, *quote_decimals)
                } else {
                    scale_positive_price(*rate, 18, *quote_decimals)
                }
            }
        }
    }
}

/// Network source and transform for a registered feed.
#[non_exhaustive]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub enum FeedSource {
    /// Plain Chainlink-compatible proxy with identity answer transform.
    #[default]
    Chainlink,
    /// Aave `PriceCapAdapterStable` source backed by an underlying Chainlink proxy.
    AavePriceCapStable {
        /// User-facing Aave source adapter.
        source: Address,
        /// Underlying Chainlink proxy read for rounds and aggregator discovery.
        underlying_proxy: Address,
        /// Maximum answer exposed by the Aave adapter.
        price_cap: I256,
    },
    /// Aave ratio-cap/CAPO source backed by a base-to-USD Chainlink proxy.
    AaveRatioCap {
        /// User-facing Aave source adapter.
        source: Address,
        /// Underlying base-to-USD Chainlink proxy.
        base_to_usd_proxy: Address,
        /// Ratio provider used by the Aave source.
        ratio_provider: Address,
        /// Current source ratio.
        current_ratio: I256,
        /// Maximum ratio exposed by the Aave source.
        max_ratio: I256,
        /// Decimals used by the ratio values.
        ratio_decimals: u8,
    },
    /// Aave Chainlink synchronicity adapter composing asset-to-peg and peg-to-base feeds.
    AaveSynchronicityPegToBase {
        /// User-facing Aave source adapter.
        source: Address,
        /// Asset-to-peg Chainlink proxy.
        asset_to_peg_proxy: Address,
        /// Asset-to-peg current aggregator.
        asset_to_peg_aggregator: Address,
        /// Seeded asset-to-peg answer used when the other dependency updates.
        asset_to_peg_answer: I256,
        /// Asset-to-peg decimals.
        asset_to_peg_decimals: u8,
        /// Peg-to-base Chainlink proxy.
        peg_to_base_proxy: Address,
        /// Peg-to-base current aggregator.
        peg_to_base_aggregator: Address,
        /// Seeded peg-to-base answer used when the other dependency updates.
        peg_to_base_answer: I256,
        /// Peg-to-base decimals.
        peg_to_base_decimals: u8,
        /// Decimals for the user-facing composed answer.
        output_decimals: u8,
    },
    /// Aave fixed-price source with no Chainlink dependency.
    AaveFixedPrice {
        /// User-facing Aave source adapter.
        source: Address,
        /// Fixed user-facing answer.
        price: I256,
    },
    /// Morpho Blue `MorphoChainlinkOracleV2` source composed from Chainlink feeds and optional vault conversions.
    MorphoChainlinkV2 {
        /// User-facing Morpho oracle.
        source: Address,
        /// Latest base-vault conversion factor, or 1 when no base vault is configured.
        base_vault_assets: I256,
        /// Latest quote-vault conversion factor, or 1 when no quote vault is configured.
        quote_vault_assets: I256,
        /// Immutable Morpho scale factor.
        scale_factor: I256,
        /// Non-zero Chainlink feed dependencies.
        feeds: Vec<MorphoChainlinkFeed>,
    },
    /// Euler quote source backed by a single leg.
    EulerQuote {
        /// User-facing Euler oracle adapter or router.
        source: Address,
        /// Base asset for the quote.
        base: Address,
        /// Quote asset for the quote.
        quote: Address,
        /// Base token decimals.
        base_decimals: u8,
        /// Quote token decimals.
        quote_decimals: u8,
        /// Quote leg.
        leg: EulerQuoteLeg,
    },
    /// Euler `CrossAdapter` quote source backed by two quote legs.
    EulerCross {
        /// User-facing Euler cross adapter.
        source: Address,
        /// Base asset for the final quote.
        base: Address,
        /// Intermediate asset.
        cross: Address,
        /// Quote asset for the final quote.
        quote: Address,
        /// Base token decimals.
        base_decimals: u8,
        /// Intermediate token decimals.
        cross_decimals: u8,
        /// Quote token decimals.
        quote_decimals: u8,
        /// Base-to-cross leg.
        base_cross: EulerQuoteLeg,
        /// Cross-to-quote leg.
        cross_quote: EulerQuoteLeg,
    },
    /// Pyth EVM price feed keyed by a shared Pyth contract and per-feed price id.
    Pyth {
        /// Shared Pyth contract that emits updates and serves reads.
        pyth: Address,
        /// Pyth price feed id.
        price_id: B256,
        /// Pyth fixed-point exponent.
        expo: i32,
        /// Most recent Pyth confidence interval.
        conf: u64,
    },
    /// RedStone push price feed backed by an on-chain adapter/event source.
    #[cfg(feature = "redstone")]
    RedstonePush {
        /// User-facing RedStone price feed wrapper.
        price_feed: Address,
        /// RedStone adapter or merged price-feed event source.
        adapter: Address,
        /// RedStone data feed id.
        data_feed_id: B256,
    },
    /// Adapter-owned oracle source.
    Custom(OracleSourceDescriptor),
}

impl FeedSource {
    /// Construct an Aave capped stable source transform.
    pub fn aave_price_cap_stable(
        source: Address,
        underlying_proxy: Address,
        price_cap: I256,
    ) -> Self {
        Self::AavePriceCapStable {
            source,
            underlying_proxy,
            price_cap,
        }
    }

    /// Construct an Aave ratio-cap/CAPO source transform.
    pub fn aave_ratio_cap(
        source: Address,
        base_to_usd_proxy: Address,
        ratio_provider: Address,
        current_ratio: I256,
        max_ratio: I256,
        ratio_decimals: u8,
    ) -> Self {
        Self::AaveRatioCap {
            source,
            base_to_usd_proxy,
            ratio_provider,
            current_ratio,
            max_ratio,
            ratio_decimals,
        }
    }

    /// Construct an Aave peg-to-base synchronicity source transform.
    #[allow(clippy::too_many_arguments)]
    pub fn aave_synchronicity_peg_to_base(
        source: Address,
        asset_to_peg_proxy: Address,
        asset_to_peg_aggregator: Address,
        asset_to_peg_answer: I256,
        asset_to_peg_decimals: u8,
        peg_to_base_proxy: Address,
        peg_to_base_aggregator: Address,
        peg_to_base_answer: I256,
        peg_to_base_decimals: u8,
        output_decimals: u8,
    ) -> Self {
        Self::AaveSynchronicityPegToBase {
            source,
            asset_to_peg_proxy,
            asset_to_peg_aggregator,
            asset_to_peg_answer,
            asset_to_peg_decimals,
            peg_to_base_proxy,
            peg_to_base_aggregator,
            peg_to_base_answer,
            peg_to_base_decimals,
            output_decimals,
        }
    }

    /// Construct an Aave fixed-price source.
    pub fn aave_fixed_price(source: Address, price: I256) -> Self {
        Self::AaveFixedPrice { source, price }
    }

    /// Construct a Morpho Blue Chainlink V2 source transform.
    pub fn morpho_chainlink_v2(
        source: Address,
        base_vault_assets: I256,
        quote_vault_assets: I256,
        scale_factor: I256,
        feeds: Vec<MorphoChainlinkFeed>,
    ) -> Self {
        Self::MorphoChainlinkV2 {
            source,
            base_vault_assets,
            quote_vault_assets,
            scale_factor,
            feeds,
        }
    }

    /// Construct an Euler quote source backed by one quote leg.
    pub fn euler_quote(
        source: Address,
        base: Address,
        quote: Address,
        base_decimals: u8,
        quote_decimals: u8,
        leg: EulerQuoteLeg,
    ) -> Self {
        Self::EulerQuote {
            source,
            base,
            quote,
            base_decimals,
            quote_decimals,
            leg,
        }
    }

    /// Construct an Euler cross-adapter source backed by two quote legs.
    #[allow(clippy::too_many_arguments)]
    pub fn euler_cross(
        source: Address,
        base: Address,
        cross: Address,
        quote: Address,
        base_decimals: u8,
        cross_decimals: u8,
        quote_decimals: u8,
        base_cross: EulerQuoteLeg,
        cross_quote: EulerQuoteLeg,
    ) -> Self {
        Self::EulerCross {
            source,
            base,
            cross,
            quote,
            base_decimals,
            cross_decimals,
            quote_decimals,
            base_cross,
            cross_quote,
        }
    }

    /// Rewrite an Euler source's user-facing quote address.
    ///
    /// Router-resolved discovery reads the source config through the resolved
    /// adapter, but the user-facing contract callers quote through (and the
    /// read overlay serves) is the router itself. Non-Euler sources are
    /// returned unchanged.
    #[cfg(feature = "euler")]
    pub(crate) fn with_euler_user_source(self, user_source: Address) -> Self {
        match self {
            Self::EulerQuote {
                base,
                quote,
                base_decimals,
                quote_decimals,
                leg,
                ..
            } => Self::EulerQuote {
                source: user_source,
                base,
                quote,
                base_decimals,
                quote_decimals,
                leg,
            },
            Self::EulerCross {
                base,
                cross,
                quote,
                base_decimals,
                cross_decimals,
                quote_decimals,
                base_cross,
                cross_quote,
                ..
            } => Self::EulerCross {
                source: user_source,
                base,
                cross,
                quote,
                base_decimals,
                cross_decimals,
                quote_decimals,
                base_cross,
                cross_quote,
            },
            other => other,
        }
    }

    /// Construct a Pyth EVM source.
    pub fn pyth(pyth: Address, price_id: B256, expo: i32, conf: u64) -> Self {
        Self::Pyth {
            pyth,
            price_id,
            expo,
            conf,
        }
    }

    /// Construct a RedStone push source.
    #[cfg(feature = "redstone")]
    pub fn redstone_push(price_feed: Address, adapter: Address, data_feed_id: B256) -> Self {
        Self::RedstonePush {
            price_feed,
            adapter,
            data_feed_id,
        }
    }

    /// Construct an adapter-owned custom source.
    ///
    /// Custom sources participate in **Chainlink-style proxy reconciliation
    /// by default**: [`Self::supports_proxy_reconciliation`] returns `true`
    /// for `Custom`, so reconciliation reads
    /// [`OracleSourceDescriptor::read_proxy`] with `latestRoundData()` after
    /// every accepted event (and during [`crate::OracleTracker::reconcile`]).
    /// The `read_proxy` address therefore **must answer `latestRoundData()`**
    /// or reconciliation will fail with [`crate::OracleError::Provider`].
    /// There is currently no per-descriptor flag to opt a custom source out
    /// of this path — if the source is not Chainlink-shaped, point
    /// `read_proxy` at a contract that exposes a Chainlink-compatible
    /// `latestRoundData()` view for it.
    pub fn custom(descriptor: OracleSourceDescriptor) -> Self {
        Self::Custom(descriptor)
    }

    /// Return true when this source is a plain identity Chainlink source.
    pub fn is_identity(&self) -> bool {
        matches!(self, Self::Chainlink)
    }

    /// Return true when the built-in Chainlink reactive handler should route this source.
    pub fn uses_builtin_chainlink_handler(&self) -> bool {
        matches!(
            self,
            Self::Chainlink
                | Self::AavePriceCapStable { .. }
                | Self::AaveRatioCap { .. }
                | Self::AaveSynchronicityPegToBase { .. }
                | Self::MorphoChainlinkV2 { .. }
                | Self::EulerQuote { .. }
                | Self::EulerCross { .. }
        )
    }

    /// Return true when this source can be reconciled by reading a Chainlink proxy.
    ///
    /// Sources that fail this predicate are either reconciled through their
    /// own protocol view (see [`Self::supports_derived_reconciliation`]) or
    /// not reconciled at all.
    pub fn supports_proxy_reconciliation(&self) -> bool {
        !matches!(
            self,
            Self::AaveFixedPrice { .. }
                | Self::MorphoChainlinkV2 { .. }
                | Self::EulerQuote { .. }
                | Self::EulerCross { .. }
                | Self::Pyth { .. }
        )
    }

    /// Return true when this source is reconciled by reading its own protocol
    /// view instead of a Chainlink proxy.
    ///
    /// Morpho `MorphoChainlinkOracleV2` sources re-read `price()` and Euler
    /// quote/cross sources re-read `getQuote(one base unit, base, quote)`;
    /// both promote event-derived snapshots to `Confirmed`/`Corrected` through
    /// [`crate::OracleTracker::reconcile_derived_pending_with`]. Each family
    /// only participates when its adapter feature (`morpho` / `euler`) is
    /// enabled, so builds without the feature never queue derived requests
    /// they cannot serve.
    pub fn supports_derived_reconciliation(&self) -> bool {
        #[cfg(feature = "morpho")]
        if matches!(self, Self::MorphoChainlinkV2 { .. }) {
            return true;
        }
        #[cfg(feature = "euler")]
        if matches!(self, Self::EulerQuote { .. } | Self::EulerCross { .. }) {
            return true;
        }
        false
    }

    /// Proxy that should be read for authoritative Chainlink round data.
    pub fn read_proxy(&self, registration_proxy: Address) -> Address {
        match self {
            Self::Chainlink => registration_proxy,
            Self::AavePriceCapStable {
                underlying_proxy, ..
            } => *underlying_proxy,
            Self::AaveRatioCap {
                base_to_usd_proxy, ..
            } => *base_to_usd_proxy,
            Self::AaveSynchronicityPegToBase {
                asset_to_peg_proxy, ..
            } => *asset_to_peg_proxy,
            Self::AaveFixedPrice { source, .. } => *source,
            Self::MorphoChainlinkV2 { source, .. }
            | Self::EulerQuote { source, .. }
            | Self::EulerCross { source, .. } => *source,
            Self::Pyth { pyth, .. } => *pyth,
            #[cfg(feature = "redstone")]
            Self::RedstonePush { price_feed, .. } => *price_feed,
            Self::Custom(descriptor) => descriptor.read_proxy,
        }
    }

    /// Aggregators whose Chainlink events should drive this source.
    pub fn event_aggregators(&self, current_aggregator: Option<Address>) -> Vec<Address> {
        match self {
            Self::AaveSynchronicityPegToBase {
                asset_to_peg_aggregator,
                peg_to_base_aggregator,
                ..
            } => vec![*asset_to_peg_aggregator, *peg_to_base_aggregator],
            Self::MorphoChainlinkV2 { feeds, .. } => feeds
                .iter()
                .map(|feed| feed.dependency.aggregator)
                .collect(),
            Self::EulerQuote { leg, .. } => leg.event_aggregator().into_iter().collect(),
            Self::EulerCross {
                base_cross,
                cross_quote,
                ..
            } => base_cross
                .event_aggregator()
                .into_iter()
                .chain(cross_quote.event_aggregator())
                .collect(),
            Self::Pyth { pyth, .. } => vec![*pyth],
            #[cfg(feature = "redstone")]
            Self::RedstonePush {
                price_feed,
                adapter,
                ..
            } => {
                let mut aggregators = vec![*adapter];
                if price_feed != adapter {
                    aggregators.push(*price_feed);
                }
                aggregators
            }
            Self::AaveFixedPrice { .. } => Vec::new(),
            _ => current_aggregator.into_iter().collect(),
        }
    }

    /// Source classification for a value produced from this source's event path.
    pub fn event_value_source(&self) -> OracleValueSource {
        match self {
            Self::AavePriceCapStable { .. }
            | Self::AaveRatioCap { .. }
            | Self::AaveSynchronicityPegToBase { .. }
            | Self::AaveFixedPrice { .. }
            | Self::MorphoChainlinkV2 { .. }
            | Self::EulerQuote { .. }
            | Self::EulerCross { .. } => OracleValueSource::Derived,
            _ => OracleValueSource::Event,
        }
    }

    /// Return true when an event from `aggregator` is acceptable for this source.
    pub fn accepts_event_from(
        &self,
        current_aggregator: Option<Address>,
        aggregator: Address,
    ) -> bool {
        self.event_aggregators(current_aggregator)
            .into_iter()
            .any(|event_aggregator| event_aggregator == aggregator)
    }

    /// Return true when this source should use AnswerUpdated events for `aggregator`.
    pub fn wants_answer_updated_from(
        &self,
        current_aggregator: Option<Address>,
        aggregator: Address,
    ) -> bool {
        match self {
            Self::AaveRatioCap { .. }
            | Self::AaveSynchronicityPegToBase { .. }
            | Self::MorphoChainlinkV2 { .. }
            | Self::EulerQuote { .. }
            | Self::EulerCross { .. } => self.accepts_event_from(current_aggregator, aggregator),
            _ => false,
        }
    }

    /// Return true when Chainlink storage adapters can safely mirror this event.
    pub fn supports_direct_chainlink_storage_effects(&self) -> bool {
        match self {
            Self::AaveRatioCap { .. }
            | Self::AaveSynchronicityPegToBase { .. }
            | Self::MorphoChainlinkV2 { .. }
            | Self::EulerQuote { .. }
            | Self::EulerCross { .. }
            | Self::Pyth { .. } => false,
            #[cfg(feature = "redstone")]
            Self::RedstonePush { .. } => false,
            _ => true,
        }
    }

    /// Apply the source transform to an event or read answer.
    pub fn normalize_answer(&self, answer: I256) -> I256 {
        self.normalize_answer_from_event(None, answer)
    }

    /// Apply the source transform to an answer emitted by a specific dependency aggregator.
    pub fn normalize_answer_from_event(&self, aggregator: Option<Address>, answer: I256) -> I256 {
        match self {
            Self::Chainlink => answer,
            Self::AavePriceCapStable { price_cap, .. } if answer > *price_cap => *price_cap,
            Self::AavePriceCapStable { .. } => answer,
            Self::AaveRatioCap {
                current_ratio,
                max_ratio,
                ratio_decimals,
                ..
            } => {
                if answer <= I256::ZERO || *current_ratio <= I256::ZERO {
                    return I256::ZERO;
                }
                let capped_ratio = if current_ratio < max_ratio {
                    *current_ratio
                } else {
                    *max_ratio
                };
                if capped_ratio <= I256::ZERO {
                    return I256::ZERO;
                }
                div_or_zero(
                    answer.saturating_mul(capped_ratio),
                    decimal_scale(*ratio_decimals),
                )
            }
            Self::AaveSynchronicityPegToBase {
                asset_to_peg_aggregator,
                asset_to_peg_answer,
                asset_to_peg_decimals,
                peg_to_base_aggregator,
                peg_to_base_answer,
                peg_to_base_decimals,
                output_decimals,
                ..
            } => {
                let asset_to_peg = if aggregator == Some(*asset_to_peg_aggregator) {
                    answer
                } else {
                    *asset_to_peg_answer
                };
                let peg_to_base = if aggregator == Some(*peg_to_base_aggregator) {
                    answer
                } else {
                    *peg_to_base_answer
                };
                normalize_synchronicity_answer(
                    asset_to_peg,
                    *asset_to_peg_decimals,
                    peg_to_base,
                    *peg_to_base_decimals,
                    *output_decimals,
                )
            }
            Self::AaveFixedPrice { price, .. } => *price,
            Self::MorphoChainlinkV2 {
                base_vault_assets,
                quote_vault_assets,
                scale_factor,
                feeds,
                ..
            } => normalize_morpho_chainlink_v2_answer(
                *base_vault_assets,
                *quote_vault_assets,
                *scale_factor,
                feeds,
                aggregator,
                answer,
            ),
            Self::EulerQuote { leg, .. } => leg.price_from_event(aggregator, answer),
            Self::EulerCross {
                cross_decimals,
                base_cross,
                cross_quote,
                ..
            } => normalize_euler_cross_answer(
                base_cross.price_from_event(aggregator, answer),
                cross_quote.price_from_event(aggregator, answer),
                *cross_decimals,
            ),
            Self::Pyth { .. } => answer,
            #[cfg(feature = "redstone")]
            Self::RedstonePush { .. } => answer,
            Self::Custom(descriptor) => match &descriptor.transform {
                OracleTransformDescriptor::Identity | OracleTransformDescriptor::Custom { .. } => {
                    answer
                }
                OracleTransformDescriptor::PriceCap { cap } if answer > *cap => *cap,
                OracleTransformDescriptor::PriceCap { .. } => answer,
            },
        }
    }

    /// Apply the source transform to round data.
    pub fn normalize_round(&self, mut round: RoundData) -> RoundData {
        round.answer = match self {
            Self::AaveSynchronicityPegToBase {
                asset_to_peg_aggregator,
                ..
            } => self.normalize_answer_from_event(Some(*asset_to_peg_aggregator), round.answer),
            Self::MorphoChainlinkV2 { .. }
            | Self::EulerQuote { .. }
            | Self::EulerCross { .. }
            | Self::Pyth { .. } => round.answer,
            _ => self.normalize_answer(round.answer),
        };
        round
    }

    pub(crate) fn normalize_dependency_answers(
        &self,
        asset_to_peg_answer: I256,
        peg_to_base_answer: I256,
    ) -> Option<I256> {
        match self {
            Self::AaveSynchronicityPegToBase {
                asset_to_peg_decimals,
                peg_to_base_decimals,
                output_decimals,
                ..
            } => Some(normalize_synchronicity_answer(
                asset_to_peg_answer,
                *asset_to_peg_decimals,
                peg_to_base_answer,
                *peg_to_base_decimals,
                *output_decimals,
            )),
            _ => None,
        }
    }

    pub(crate) fn with_synchronicity_dependency_state(
        &self,
        asset_to_peg_aggregator: Address,
        asset_to_peg_answer: I256,
        peg_to_base_aggregator: Address,
        peg_to_base_answer: I256,
    ) -> Option<Self> {
        match self {
            Self::AaveSynchronicityPegToBase {
                source,
                asset_to_peg_proxy,
                asset_to_peg_decimals,
                peg_to_base_proxy,
                peg_to_base_decimals,
                output_decimals,
                ..
            } => Some(Self::AaveSynchronicityPegToBase {
                source: *source,
                asset_to_peg_proxy: *asset_to_peg_proxy,
                asset_to_peg_aggregator,
                asset_to_peg_answer,
                asset_to_peg_decimals: *asset_to_peg_decimals,
                peg_to_base_proxy: *peg_to_base_proxy,
                peg_to_base_aggregator,
                peg_to_base_answer,
                peg_to_base_decimals: *peg_to_base_decimals,
                output_decimals: *output_decimals,
            }),
            _ => None,
        }
    }

    /// Return Pyth source fields for Pyth-backed registrations.
    pub fn pyth_source(&self) -> Option<(Address, B256, i32, u64)> {
        match self {
            Self::Pyth {
                pyth,
                price_id,
                expo,
                conf,
            } => Some((*pyth, *price_id, *expo, *conf)),
            _ => None,
        }
    }

    /// Return this Pyth source with event-updated metadata.
    pub fn with_pyth_event_metadata(&self, price_id: B256, expo: i32, conf: u64) -> Option<Self> {
        match self {
            Self::Pyth { pyth, .. } => Some(Self::Pyth {
                pyth: *pyth,
                price_id,
                expo,
                conf,
            }),
            _ => None,
        }
    }
}

fn decimal_scale(decimals: u8) -> I256 {
    let mut scale = I256::unchecked_from(1_i8);
    for _ in 0..decimals {
        scale = scale.saturating_mul(I256::unchecked_from(10_i8));
    }
    scale
}

fn div_or_zero(numerator: I256, denominator: I256) -> I256 {
    if denominator == I256::ZERO {
        I256::ZERO
    } else {
        numerator / denominator
    }
}

fn normalize_synchronicity_answer(
    asset_to_peg: I256,
    asset_to_peg_decimals: u8,
    peg_to_base: I256,
    peg_to_base_decimals: u8,
    output_decimals: u8,
) -> I256 {
    if asset_to_peg <= I256::ZERO || peg_to_base <= I256::ZERO {
        return I256::ZERO;
    }
    let numerator = asset_to_peg
        .saturating_mul(peg_to_base)
        .saturating_mul(decimal_scale(output_decimals));
    let denominator = decimal_scale(asset_to_peg_decimals.saturating_add(peg_to_base_decimals));
    div_or_zero(numerator, denominator)
}

fn normalize_morpho_chainlink_v2_answer(
    base_vault_assets: I256,
    quote_vault_assets: I256,
    scale_factor: I256,
    feeds: &[MorphoChainlinkFeed],
    aggregator: Option<Address>,
    answer: I256,
) -> I256 {
    if base_vault_assets < I256::ZERO
        || quote_vault_assets <= I256::ZERO
        || scale_factor <= I256::ZERO
    {
        return I256::ZERO;
    }

    // Mirror `MorphoChainlinkOracleV2.price()`:
    // `SCALE_FACTOR.mulDiv(baseFeed1 * baseFeed2 * baseVaultAssets,
    //                      quoteFeed1 * quoteFeed2 * quoteVaultAssets)`.
    // The inner products are plain checked uint256 math on-chain (they revert
    // on overflow; the event path cannot revert, so overflow degrades to zero,
    // which round classification rejects as an invalid answer). The outer
    // multiply/divide uses a 512-bit intermediate exactly like OpenZeppelin's
    // `Math.mulDiv`, so large scale factors composed with multi-feed products
    // never saturate into a silently wrong price.
    let mut base_product = i256_magnitude(base_vault_assets);
    let mut quote_product = i256_magnitude(quote_vault_assets);
    for feed in feeds {
        let feed_answer = if aggregator == Some(feed.dependency.aggregator) {
            answer
        } else {
            feed.dependency.answer
        };
        if feed_answer < I256::ZERO {
            return I256::ZERO;
        }
        let magnitude = i256_magnitude(feed_answer);
        let product = match feed.role {
            MorphoChainlinkFeedRole::BaseFeed1 | MorphoChainlinkFeedRole::BaseFeed2 => {
                &mut base_product
            }
            MorphoChainlinkFeedRole::QuoteFeed1 | MorphoChainlinkFeedRole::QuoteFeed2 => {
                &mut quote_product
            }
        };
        *product = match product.checked_mul(magnitude) {
            Some(value) => value,
            None => return I256::ZERO,
        };
    }
    if quote_product.is_zero() {
        return I256::ZERO;
    }

    // 2^255 * 2^256 = 2^511 fits a U512, so this cannot overflow.
    let numerator = u512_from_u256(i256_magnitude(scale_factor)) * u512_from_u256(base_product);
    let quotient = numerator / u512_from_u256(quote_product);
    let limbs = quotient.into_limbs();
    if limbs[4..].iter().any(|limb| *limb != 0) {
        return I256::ZERO;
    }
    let low = U256::from_limbs([limbs[0], limbs[1], limbs[2], limbs[3]]);
    I256::try_from(low).unwrap_or(I256::ZERO)
}

/// Magnitude of a non-negative `I256` (callers guard against negatives).
fn i256_magnitude(value: I256) -> U256 {
    value.into_raw()
}

fn u512_from_u256(value: U256) -> U512 {
    let limbs = value.into_limbs();
    U512::from_limbs([limbs[0], limbs[1], limbs[2], limbs[3], 0, 0, 0, 0])
}

fn normalize_euler_cross_answer(
    base_cross_price: I256,
    cross_quote_price: I256,
    cross_decimals: u8,
) -> I256 {
    if base_cross_price <= I256::ZERO || cross_quote_price <= I256::ZERO {
        return I256::ZERO;
    }
    div_or_zero(
        base_cross_price.saturating_mul(cross_quote_price),
        decimal_scale(cross_decimals),
    )
}

fn scale_positive_price(answer: I256, from_decimals: u8, to_decimals: u8) -> I256 {
    if answer <= I256::ZERO {
        return I256::ZERO;
    }
    if from_decimals == to_decimals {
        answer
    } else if from_decimals < to_decimals {
        answer.saturating_mul(decimal_scale(to_decimals - from_decimals))
    } else {
        div_or_zero(answer, decimal_scale(from_decimals - to_decimals))
    }
}

fn inverse_positive_price(answer: I256, feed_decimals: u8, quote_decimals: u8) -> I256 {
    if answer <= I256::ZERO {
        return I256::ZERO;
    }
    div_or_zero(
        decimal_scale(feed_decimals.saturating_add(quote_decimals)),
        answer,
    )
}

fn inverse_fixed_rate(rate: I256, base_decimals: u8, quote_decimals: u8) -> I256 {
    if rate <= I256::ZERO {
        return I256::ZERO;
    }
    div_or_zero(
        decimal_scale(base_decimals.saturating_add(quote_decimals)),
        rate,
    )
}

/// Registered Chainlink-compatible proxy feed.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FeedRegistration {
    /// Stable feed id.
    pub id: FeedId,
    /// User-facing proxy address.
    pub proxy: Address,
    /// Optional human-readable label.
    pub label: Option<String>,
    /// Optional base symbol.
    pub base: Option<String>,
    /// Optional quote symbol.
    pub quote: Option<String>,
    /// Freshness and answer validity policy.
    pub staleness: crate::StalenessPolicy,
    /// Best-known current aggregator.
    pub current_aggregator: Option<Address>,
    /// Best-known current aggregator layout evidence.
    pub aggregator_layout: Option<AggregatorLayoutEvidence>,
    /// Metadata loaded from the proxy.
    pub metadata: FeedMetadata,
    /// Network source and transform used by this registration.
    pub source: FeedSource,
    /// Feed/runtime readiness state.
    pub status: OracleFeedStatus,
}

/// Feed/runtime readiness surfaced by registry, warmup, build, repair, and health reports.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct OracleFeedReadinessReport {
    /// Feed id when the feed reached registration or supplied an id before being skipped.
    pub id: Option<FeedId>,
    /// User-facing proxy/source address.
    pub proxy: Address,
    /// Feed/runtime readiness state.
    pub status: OracleFeedStatus,
    /// Optional machine-readable or human-readable diagnostic.
    pub reason: Option<String>,
}

/// Aggregator routing change found during reconciliation.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AggregatorChange {
    /// Feed id whose aggregator changed.
    pub id: FeedId,
    /// Feed proxy.
    pub proxy: Address,
    /// Previous aggregator.
    pub old: Option<Address>,
    /// New aggregator.
    pub new: Option<Address>,
}

struct PreparedFeed {
    id: FeedId,
    config: FeedConfig,
    source: FeedSource,
    metadata: FeedMetadata,
    round: RoundData,
    current_aggregator: Option<Address>,
}

/// Registry plus latest proxy-read snapshots.
#[derive(Clone, Debug)]
pub struct OracleRegistry {
    registrations: BTreeMap<FeedId, FeedRegistration>,
    ids_by_proxy: BTreeMap<Address, FeedId>,
    snapshots_by_proxy: BTreeMap<Address, OracleSnapshot>,
    layouts_by_code_hash: BTreeMap<B256, AggregatorLayout>,
    now_timestamp: u64,
}

impl OracleRegistry {
    /// Create an empty registry with a fixed wall-clock timestamp for tests.
    pub fn new_at_timestamp(now_timestamp: u64) -> Self {
        Self {
            registrations: BTreeMap::new(),
            ids_by_proxy: BTreeMap::new(),
            snapshots_by_proxy: BTreeMap::new(),
            layouts_by_code_hash: BTreeMap::new(),
            now_timestamp,
        }
    }

    /// Register a Chainlink-compatible proxy and seed state from proxy reads.
    pub async fn register_chainlink_feed<P: ChainlinkFeedProvider>(
        &mut self,
        provider: &P,
        config: FeedConfig,
    ) -> Result<FeedId, OracleError> {
        let id = self.prepare_feed_id(&config)?;
        let source = FeedSource::Chainlink;
        let read_proxy = source.read_proxy(config.proxy);
        let metadata = FeedMetadata {
            decimals: provider.decimals(read_proxy).await?,
            description: provider.description(read_proxy).await?,
            version: provider.version(read_proxy).await?,
        };
        let round = source.normalize_round(provider.latest_round_data(read_proxy).await?);
        let current_aggregator = provider.aggregator(read_proxy).await.unwrap_or(None);
        self.insert_prepared_feed(
            provider,
            PreparedFeed {
                id,
                config,
                source,
                metadata,
                round,
                current_aggregator,
            },
        )
        .await
    }

    #[allow(dead_code)]
    pub(crate) async fn register_discovered_feed<P: ChainlinkFeedProvider>(
        &mut self,
        provider: &P,
        config: FeedConfig,
        source: FeedSource,
        metadata: FeedMetadata,
        round: RoundData,
        current_aggregator: Option<Address>,
    ) -> Result<FeedId, OracleError> {
        let id = self.prepare_feed_id(&config)?;
        self.insert_prepared_feed(
            provider,
            PreparedFeed {
                id,
                config,
                source,
                metadata,
                round,
                current_aggregator,
            },
        )
        .await
    }

    async fn insert_prepared_feed<P: ChainlinkFeedProvider>(
        &mut self,
        provider: &P,
        prepared: PreparedFeed,
    ) -> Result<FeedId, OracleError> {
        let PreparedFeed {
            id,
            config,
            source,
            metadata,
            round,
            current_aggregator,
        } = prepared;
        let aggregator_layout = self
            .detect_aggregator_layout(provider, current_aggregator, None)
            .await;

        let registration = FeedRegistration {
            id: id.clone(),
            proxy: config.proxy,
            label: config.label,
            base: config.base,
            quote: config.quote,
            staleness: config.staleness,
            current_aggregator,
            aggregator_layout,
            metadata: metadata.clone(),
            source,
            status: OracleFeedStatus::Ready,
        };
        let snapshot = OracleSnapshot::proxy_read(
            id.clone(),
            registration.proxy,
            current_aggregator,
            metadata,
            round,
            self.now_timestamp,
            &registration.staleness,
        );

        self.ids_by_proxy.insert(registration.proxy, id.clone());
        self.snapshots_by_proxy.insert(registration.proxy, snapshot);
        self.registrations.insert(id.clone(), registration);
        Ok(id)
    }

    fn prepare_feed_id(&self, config: &FeedConfig) -> Result<FeedId, OracleError> {
        if self.ids_by_proxy.contains_key(&config.proxy) {
            return Err(OracleError::DuplicateProxy(config.proxy));
        }

        let id = config
            .id
            .clone()
            .unwrap_or_else(|| derive_feed_id(config.label.as_deref(), config.proxy));
        if self.registrations.contains_key(&id) {
            return Err(OracleError::DuplicateFeedId(id.to_string()));
        }
        Ok(id)
    }

    /// Return a registration by id.
    pub fn registration(&self, id: FeedId) -> Option<&FeedRegistration> {
        self.registrations.get(&id)
    }

    /// Return feed/runtime readiness for all registered feeds.
    pub fn feed_readiness(&self) -> Vec<OracleFeedReadinessReport> {
        self.registrations
            .values()
            .map(|registration| OracleFeedReadinessReport {
                id: Some(registration.id.clone()),
                proxy: registration.proxy,
                status: registration.status,
                reason: None,
            })
            .collect()
    }

    /// Return the latest snapshot by proxy.
    pub fn latest(&self, proxy: Address) -> Option<&OracleSnapshot> {
        self.snapshots_by_proxy.get(&proxy)
    }

    pub(crate) fn now_timestamp(&self) -> u64 {
        self.now_timestamp
    }

    pub(crate) fn registrations_map(&self) -> &BTreeMap<FeedId, FeedRegistration> {
        &self.registrations
    }

    pub(crate) fn registrations_map_mut(&mut self) -> &mut BTreeMap<FeedId, FeedRegistration> {
        &mut self.registrations
    }

    pub(crate) fn snapshots_by_proxy_mut(&mut self) -> &mut BTreeMap<Address, OracleSnapshot> {
        &mut self.snapshots_by_proxy
    }

    pub(crate) fn id_by_proxy(&self, proxy: Address) -> Option<&FeedId> {
        self.ids_by_proxy.get(&proxy)
    }

    pub(crate) fn record_proxy_id(&mut self, proxy: Address, id: FeedId) {
        self.ids_by_proxy.insert(proxy, id);
    }

    pub(crate) fn replace_snapshot(&mut self, snapshot: OracleSnapshot) {
        self.snapshots_by_proxy.insert(snapshot.proxy, snapshot);
    }

    pub(crate) fn insert_seeded_registration(
        &mut self,
        registration: FeedRegistration,
        round: RoundData,
    ) -> Result<(), OracleError> {
        if self.ids_by_proxy.contains_key(&registration.proxy) {
            return Err(OracleError::DuplicateProxy(registration.proxy));
        }
        if self.registrations.contains_key(&registration.id) {
            return Err(OracleError::DuplicateFeedId(registration.id.to_string()));
        }

        let snapshot = OracleSnapshot::proxy_read(
            registration.id.clone(),
            registration.proxy,
            registration.current_aggregator,
            registration.metadata.clone(),
            registration.source.normalize_round(round),
            self.now_timestamp,
            &registration.staleness,
        );
        self.ids_by_proxy
            .insert(registration.proxy, registration.id.clone());
        self.snapshots_by_proxy.insert(registration.proxy, snapshot);
        self.registrations
            .insert(registration.id.clone(), registration);
        Ok(())
    }

    pub(crate) fn remove_registration_by_id(&mut self, id: FeedId) -> Option<FeedRegistration> {
        let registration = self.registrations.remove(&id)?;
        self.ids_by_proxy.remove(&registration.proxy);
        self.snapshots_by_proxy.remove(&registration.proxy);
        Some(registration)
    }

    pub(crate) fn remove_registration_by_proxy(
        &mut self,
        proxy: Address,
    ) -> Option<FeedRegistration> {
        let id = self.ids_by_proxy.get(&proxy)?.clone();
        self.remove_registration_by_id(id)
    }

    pub(crate) async fn detect_aggregator_layout<P: ChainlinkFeedProvider>(
        &mut self,
        provider: &P,
        aggregator: Option<Address>,
        block: Option<OracleBlockRef>,
    ) -> Option<AggregatorLayoutEvidence> {
        let aggregator = aggregator?;
        let code_hash = provider
            .aggregator_code_hash_at(aggregator, block)
            .await
            .unwrap_or(None);
        let type_and_version = provider
            .aggregator_type_and_version_at(aggregator, block)
            .await
            .unwrap_or(None);

        if let Some(type_and_version) = type_and_version {
            let layout = classify_type_and_version(&type_and_version);
            if let Some(code_hash) = code_hash
                && is_cacheable_layout(layout)
            {
                self.layouts_by_code_hash.insert(code_hash, layout);
            }
            return Some(AggregatorLayoutEvidence {
                aggregator,
                type_and_version: Some(type_and_version),
                code_hash,
                layout,
                confidence: AggregatorLayoutConfidence::TypeAndVersion,
            });
        }

        if let Some(code_hash) = code_hash {
            if let Some(layout) = self.layouts_by_code_hash.get(&code_hash).copied() {
                return Some(AggregatorLayoutEvidence::from_code_hash_cache(
                    aggregator, code_hash, layout,
                ));
            }
            return Some(AggregatorLayoutEvidence::unknown(
                aggregator,
                Some(code_hash),
            ));
        }

        Some(AggregatorLayoutEvidence::unknown(aggregator, None))
    }

    /// Return cloned registrations for routing rebuilds.
    ///
    /// Prefer [`Self::registrations_iter`] when read-only access is enough —
    /// this iterator clones every registration it yields.
    pub fn registrations(&self) -> impl Iterator<Item = FeedRegistration> + '_ {
        self.registrations_iter().cloned()
    }

    /// Iterate over registrations without cloning, in feed-id order.
    pub fn registrations_iter(&self) -> impl Iterator<Item = &FeedRegistration> {
        self.registrations.values()
    }
}

fn is_cacheable_layout(layout: AggregatorLayout) -> bool {
    matches!(
        layout,
        AggregatorLayout::ChainlinkOcr2V1
            | AggregatorLayout::ChainlinkOcr1V2
            | AggregatorLayout::ChainlinkOcr1V3
            | AggregatorLayout::ChainlinkOcr1V4
    )
}

pub(crate) fn snapshot_from_proxy_read(
    registration: &FeedRegistration,
    round: RoundData,
    aggregator: Option<Address>,
    now_timestamp: u64,
) -> OracleSnapshot {
    OracleSnapshot::proxy_read(
        registration.id.clone(),
        registration.proxy,
        aggregator,
        registration.metadata.clone(),
        round,
        now_timestamp,
        &registration.staleness,
    )
}

pub(crate) fn derive_feed_id(label: Option<&str>, proxy: Address) -> FeedId {
    if let Some(label) = label {
        let slug = label
            .chars()
            .map(|ch| {
                if ch.is_ascii_alphanumeric() {
                    ch.to_ascii_lowercase()
                } else {
                    '-'
                }
            })
            .collect::<String>()
            .trim_matches('-')
            .to_string();
        if !slug.is_empty() {
            return FeedId::new(slug);
        }
    }

    FeedId::new(format!("{proxy:?}"))
}

#[allow(dead_code)]
fn _u256_is_part_of_public_metadata(_: U256) {}