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
use std::{
    sync::Arc,
    time::{SystemTime, UNIX_EPOCH},
};

use alloy_network::Ethereum;
use alloy_primitives::{Address, I256, U256, keccak256};
use alloy_sol_types::sol;
use evm_fork_cache::{cache::EvmCache, reactive::ReactiveHandler};

use crate::{
    AdapterFuture, AssetId, Denomination, EulerQuoteLeg, EvmCacheChainlinkReader, FeedId,
    FeedMetadata, FeedRegistration, FeedSource, OracleAdapterFeedSkip, OracleAdapterId,
    OracleAdapterPlugin, OracleAdapterSkipReason, OracleDiscoveredFeed, OracleDiscoveryContext,
    OracleDiscoveryReport, OracleError, OracleFeedStatus, OracleReactiveHandler, OracleStorageSync,
    RoundData, StalenessPolicy,
};

sol! {
    interface EulerRouterInterface {
        function getConfiguredOracle(address base, address quote) external view returns (address);
        function fallbackOracle() external view returns (address);
    }

    interface EulerPriceOracleInterface {
        function name() external view returns (string);
        function getQuote(uint256 inAmount, address base, address quote) external view returns (uint256 outAmount);
    }

    interface EulerChainlinkOracleInterface {
        function base() external view returns (address);
        function quote() external view returns (address);
        function feed() external view returns (address);
        function maxStaleness() external view returns (uint256);
    }

    interface EulerFixedRateOracleInterface {
        function base() external view returns (address);
        function quote() external view returns (address);
        function rate() external view returns (uint256);
    }

    interface EulerRateProviderOracleInterface {
        function base() external view returns (address);
        function quote() external view returns (address);
        function rateProvider() external view returns (address);
    }

    interface EulerCrossAdapterInterface {
        function base() external view returns (address);
        function cross() external view returns (address);
        function quote() external view returns (address);
        function oracleBaseCross() external view returns (address);
        function oracleCrossQuote() external view returns (address);
    }

    interface RateProviderInterface {
        function getRate() external view returns (uint256);
    }

    interface Erc20Metadata {
        function decimals() external view returns (uint8);
    }
}

use Erc20Metadata::decimalsCall;
use EulerChainlinkOracleInterface::{
    baseCall as chainlinkBaseCall, feedCall, maxStalenessCall, quoteCall as chainlinkQuoteCall,
};
use EulerCrossAdapterInterface::{
    baseCall as crossBaseCall, crossCall, oracleBaseCrossCall, oracleCrossQuoteCall,
    quoteCall as crossQuoteCall,
};
use EulerFixedRateOracleInterface::{
    baseCall as fixedBaseCall, quoteCall as fixedQuoteCall, rateCall,
};
use EulerPriceOracleInterface::{getQuoteCall, nameCall};
use EulerRateProviderOracleInterface::{
    baseCall as rateBaseCall, quoteCall as rateQuoteCall, rateProviderCall,
};
use EulerRouterInterface::{fallbackOracleCall, getConfiguredOracleCall};
use RateProviderInterface::getRateCall;

const ADAPTER_ID: &str = "evm-oracle-state.euler";

/// Declarative Euler quote-pair registration.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct EulerQuotePair {
    base: Address,
    quote: Address,
    oracle: Option<Address>,
    state_key: Option<Address>,
    id: Option<FeedId>,
    label: Option<String>,
    base_label: Option<AssetId>,
    quote_label: Option<Denomination>,
    staleness: StalenessPolicy,
}

impl EulerQuotePair {
    /// Register a base/quote pair to be resolved through an adapter-level router.
    pub fn new(base: Address, quote: Address) -> Self {
        Self {
            base,
            quote,
            oracle: None,
            state_key: None,
            id: None,
            label: None,
            base_label: None,
            quote_label: None,
            staleness: StalenessPolicy::default(),
        }
    }

    /// Register a base/quote pair with a direct Euler oracle adapter.
    pub fn with_oracle(base: Address, quote: Address, oracle: Address) -> Self {
        Self {
            oracle: Some(oracle),
            ..Self::new(base, quote)
        }
    }

    /// Set a stable feed id.
    pub fn id(mut self, id: impl Into<String>) -> Self {
        self.id = Some(FeedId::new(id));
        self
    }

    /// Set a stable feed id.
    pub fn feed_id(mut self, id: FeedId) -> Self {
        self.id = Some(id);
        self
    }

    /// Set a human-readable label.
    pub fn label(mut self, label: impl Into<String>) -> Self {
        self.label = Some(label.into());
        self
    }

    /// Set the base asset label.
    pub fn base_label(mut self, base: AssetId) -> Self {
        self.base_label = Some(base);
        self
    }

    /// Set the quote denomination label.
    pub fn quote_label(mut self, quote: Denomination) -> Self {
        self.quote_label = Some(quote);
        self
    }

    /// Set a max-age staleness policy.
    pub fn max_age_secs(mut self, max_age_secs: u64) -> Self {
        self.staleness = StalenessPolicy::max_age(max_age_secs);
        self
    }

    /// Set the full staleness policy.
    pub fn staleness(mut self, staleness: StalenessPolicy) -> Self {
        self.staleness = staleness;
        self
    }

    /// Set an explicit per-feed state key.
    ///
    /// By default each discovered pair derives a synthetic state key with
    /// [`EulerQuotePair::state_key_for`], so multiple pairs resolved through
    /// one router (or one shared adapter) never collide in the registry.
    pub fn state_key(mut self, state_key: Address) -> Self {
        self.state_key = Some(state_key);
        self
    }

    /// Deterministically derive a synthetic per-feed state key.
    ///
    /// `quote_target` is the user-facing contract the quote is read through:
    /// the router for router-resolved pairs, or the direct oracle adapter.
    /// The derived key is the registry/tracker state key (the registration
    /// `proxy`); the real on-chain addresses stay in [`crate::FeedSource`],
    /// and [`crate::OracleReadOverlay`] serves `getQuote` at the real
    /// `quote_target` address.
    pub fn state_key_for(quote_target: Address, base: Address, quote: Address) -> Address {
        let mut bytes = Vec::with_capacity(ADAPTER_ID.len() + 20 + 20 + 20);
        bytes.extend_from_slice(ADAPTER_ID.as_bytes());
        bytes.extend_from_slice(quote_target.as_slice());
        bytes.extend_from_slice(base.as_slice());
        bytes.extend_from_slice(quote.as_slice());
        let hash = keccak256(bytes);
        Address::from_slice(&hash.as_slice()[12..])
    }
}

/// Cache-backed Euler price oracle discovery adapter.
#[derive(Clone, Debug, Default)]
pub struct EulerOracleAdapter {
    router: Option<Address>,
    pairs: Vec<EulerQuotePair>,
    now_timestamp: Option<u64>,
}

impl EulerOracleAdapter {
    /// Create an adapter with no default router.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create an adapter that resolves quote pairs through `EulerRouter`.
    pub fn router(router: Address) -> Self {
        Self {
            router: Some(router),
            pairs: Vec::new(),
            now_timestamp: None,
        }
    }

    /// Create an adapter for one direct oracle quote pair.
    pub fn oracle(oracle: Address, base: Address, quote: Address) -> Self {
        Self {
            router: None,
            pairs: vec![EulerQuotePair::with_oracle(base, quote, oracle)],
            now_timestamp: None,
        }
    }

    /// Add one quote pair.
    pub fn quote_pair(mut self, pair: EulerQuotePair) -> Self {
        self.pairs.push(pair);
        self
    }

    /// Add multiple quote pairs.
    pub fn quote_pairs(mut self, pairs: impl IntoIterator<Item = EulerQuotePair>) -> Self {
        self.pairs.extend(pairs);
        self
    }

    /// Set a fixed timestamp for deterministic registration status classification.
    pub fn now_timestamp(mut self, now_timestamp: u64) -> Self {
        self.now_timestamp = Some(now_timestamp);
        self
    }

    fn timestamp(&self, fallback: Option<u64>) -> Result<u64, OracleError> {
        if let Some(now_timestamp) = self.now_timestamp.or(fallback) {
            return Ok(now_timestamp);
        }
        Ok(SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_err(crate::error::clock_error)?
            .as_secs())
    }

    fn discover_pairs(
        &self,
        cache: &mut EvmCache,
        now_timestamp: u64,
    ) -> Result<OracleDiscoveryReport, OracleError> {
        let mut report = OracleDiscoveryReport::new();
        for pair in &self.pairs {
            match self.discover_pair(cache, pair, now_timestamp) {
                Ok(feed) => report = report.with_feed(feed),
                Err(error) => {
                    // Report the user-facing quote target (direct oracle or
                    // router) so router-pair skips carry a real address.
                    let skip_target = pair.oracle.or(self.router).unwrap_or_default();
                    report = report.with_skip(OracleAdapterFeedSkip {
                        feed: crate::Feed::proxy(skip_target).build(),
                        proxy: skip_target,
                        reason: OracleAdapterSkipReason::UnsupportedEulerSource {
                            error: error.to_string(),
                        },
                    });
                }
            }
        }
        Ok(report)
    }

    fn discover_pair(
        &self,
        cache: &mut EvmCache,
        pair: &EulerQuotePair,
        now_timestamp: u64,
    ) -> Result<OracleDiscoveredFeed, OracleError> {
        let oracle = match pair.oracle {
            Some(oracle) => oracle,
            None => self.resolve_router_oracle(cache, pair.base, pair.quote)?,
        };
        // The user-facing contract quotes are read through: the direct oracle
        // adapter when configured, otherwise the router. Every pair gets its
        // own synthetic registry state key so multiple pairs resolved through
        // one router (or one shared adapter serving both directions) never
        // collide on the registry's unique-proxy invariant.
        let quote_target = pair.oracle.or(self.router).unwrap_or(oracle);
        let proxy = pair
            .state_key
            .unwrap_or_else(|| EulerQuotePair::state_key_for(quote_target, pair.base, pair.quote));
        let base_decimals = read_asset_decimals(cache, pair.base)?;
        let quote_decimals = read_asset_decimals(cache, pair.quote)?;
        let source = self.read_source(
            cache,
            oracle,
            pair.base,
            pair.quote,
            base_decimals,
            quote_decimals,
            0,
        )?;
        // Rewrite the discovered source's user-facing address to the quote
        // target: for router-resolved pairs the router is the address callers
        // (and the read overlay) quote through, while the legs keep the
        // resolved adapter addresses for event routing.
        let source = source.with_euler_user_source(quote_target);
        // Seed the cold-start value from an authoritative on-chain
        // `getQuote(one base unit, base, quote)` read through the quote
        // target, so the seeded snapshot honestly carries
        // `Confirmed`/`Proxy` semantics. Euler adapters enforce their own
        // `maxStaleness` inside `getQuote`, so a successful read implies the
        // oracle currently considers its dependencies fresh; a revert (stale,
        // unsupported, or misconfigured pair) skips the feed with the revert
        // reason instead of seeding a recomputed value.
        let in_amount = one_base_unit(base_decimals)?;
        let quote_out = cache
            .call_sol(
                quote_target,
                getQuoteCall {
                    inAmount: in_amount,
                    base: pair.base,
                    quote: pair.quote,
                },
            )
            .map_err(|error| {
                OracleError::Provider(format!(
                    "Euler getQuote({in_amount}, {:?}, {:?}) via {quote_target:?} failed: {error}",
                    pair.base, pair.quote
                ))
            })?;
        let answer = i256_from_u256(quote_out, "Euler getQuote outAmount")?;
        let id = pair.id.clone().unwrap_or_else(|| {
            FeedId::new(format!(
                "euler-{quote_target:?}-{:?}-{:?}",
                pair.base, pair.quote
            ))
        });
        let label = pair
            .label
            .clone()
            .or_else(|| Some(format!("Euler {:?}/{:?}", pair.base, pair.quote)));
        let metadata = FeedMetadata {
            decimals: quote_decimals,
            description: label.clone().unwrap_or_else(|| "Euler quote".to_string()),
            version: U256::ZERO,
        };
        let registration = FeedRegistration {
            id,
            proxy,
            label,
            base: pair
                .base_label
                .clone()
                .map(String::from)
                .or_else(|| Some(format!("{:?}", pair.base))),
            quote: pair
                .quote_label
                .clone()
                .map(String::from)
                .or_else(|| Some(format!("{:?}", pair.quote))),
            staleness: pair.staleness,
            current_aggregator: source.event_aggregators(None).first().copied(),
            aggregator_layout: None,
            metadata,
            source,
            status: OracleFeedStatus::Ready,
        };
        let round = RoundData {
            round_id: U256::ZERO,
            answer,
            started_at: now_timestamp,
            updated_at: now_timestamp,
            answered_in_round: U256::ZERO,
        };
        Ok(OracleDiscoveredFeed::new(registration, round))
    }

    fn resolve_router_oracle(
        &self,
        cache: &mut EvmCache,
        base: Address,
        quote: Address,
    ) -> Result<Address, OracleError> {
        let router = self.router.ok_or_else(|| {
            OracleError::Config(crate::error::OracleConfigError::Other(
                "Euler router or direct oracle is required".to_string(),
            ))
        })?;
        let configured = cache
            .call_sol(router, getConfiguredOracleCall { base, quote })
            .map_err(provider_error)?;
        if configured != Address::ZERO {
            return Ok(configured);
        }
        let fallback = cache
            .call_sol(router, fallbackOracleCall {})
            .map_err(provider_error)?;
        if fallback != Address::ZERO {
            return Ok(fallback);
        }
        Err(OracleError::Unsupported(
            "Euler router has no configured or fallback oracle for pair".to_string(),
        ))
    }

    #[allow(clippy::too_many_arguments)]
    fn read_source(
        &self,
        cache: &mut EvmCache,
        oracle: Address,
        requested_base: Address,
        requested_quote: Address,
        requested_base_decimals: u8,
        requested_quote_decimals: u8,
        depth: usize,
    ) -> Result<FeedSource, OracleError> {
        if depth > 4 {
            return Err(OracleError::Unsupported(
                "Euler oracle nesting depth exceeded".to_string(),
            ));
        }
        let name = cache
            .call_sol(oracle, nameCall {})
            .map_err(provider_error)?;
        match name.as_str() {
            "ChainlinkOracle" => {
                let leg = self.read_chainlink_leg(
                    cache,
                    oracle,
                    requested_base,
                    requested_quote,
                    requested_quote_decimals,
                )?;
                Ok(FeedSource::euler_quote(
                    oracle,
                    requested_base,
                    requested_quote,
                    requested_base_decimals,
                    requested_quote_decimals,
                    leg,
                ))
            }
            "FixedRateOracle" => {
                let leg = self.read_fixed_rate_leg(
                    cache,
                    oracle,
                    requested_base,
                    requested_quote,
                    requested_base_decimals,
                    requested_quote_decimals,
                )?;
                Ok(FeedSource::euler_quote(
                    oracle,
                    requested_base,
                    requested_quote,
                    requested_base_decimals,
                    requested_quote_decimals,
                    leg,
                ))
            }
            "RateProviderOracle" => {
                let leg = self.read_rate_provider_leg(
                    cache,
                    oracle,
                    requested_base,
                    requested_quote,
                    requested_quote_decimals,
                )?;
                Ok(FeedSource::euler_quote(
                    oracle,
                    requested_base,
                    requested_quote,
                    requested_base_decimals,
                    requested_quote_decimals,
                    leg,
                ))
            }
            "CrossAdapter" => self.read_cross_source(
                cache,
                oracle,
                requested_base,
                requested_quote,
                requested_base_decimals,
                requested_quote_decimals,
                depth,
            ),
            other => Err(OracleError::Unsupported(format!(
                "unsupported Euler oracle adapter {other}"
            ))),
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn read_cross_source(
        &self,
        cache: &mut EvmCache,
        oracle: Address,
        requested_base: Address,
        requested_quote: Address,
        requested_base_decimals: u8,
        requested_quote_decimals: u8,
        depth: usize,
    ) -> Result<FeedSource, OracleError> {
        let configured_base = cache
            .call_sol(oracle, crossBaseCall {})
            .map_err(provider_error)?;
        let cross = cache
            .call_sol(oracle, crossCall {})
            .map_err(provider_error)?;
        let configured_quote = cache
            .call_sol(oracle, crossQuoteCall {})
            .map_err(provider_error)?;
        let inverse = direction_or_revert(
            requested_base,
            configured_base,
            requested_quote,
            configured_quote,
        )?;
        let oracle_base_cross = cache
            .call_sol(oracle, oracleBaseCrossCall {})
            .map_err(provider_error)?;
        let oracle_cross_quote = cache
            .call_sol(oracle, oracleCrossQuoteCall {})
            .map_err(provider_error)?;
        let cross_decimals = read_asset_decimals(cache, cross)?;
        let (first_oracle, second_oracle) = if inverse {
            (oracle_cross_quote, oracle_base_cross)
        } else {
            (oracle_base_cross, oracle_cross_quote)
        };
        let base_cross = self.read_leg(
            cache,
            first_oracle,
            requested_base,
            cross,
            requested_base_decimals,
            cross_decimals,
            depth + 1,
        )?;
        let cross_quote = self.read_leg(
            cache,
            second_oracle,
            cross,
            requested_quote,
            cross_decimals,
            requested_quote_decimals,
            depth + 1,
        )?;

        Ok(FeedSource::euler_cross(
            oracle,
            requested_base,
            cross,
            requested_quote,
            requested_base_decimals,
            cross_decimals,
            requested_quote_decimals,
            base_cross,
            cross_quote,
        ))
    }

    #[allow(clippy::too_many_arguments)]
    fn read_leg(
        &self,
        cache: &mut EvmCache,
        oracle: Address,
        requested_base: Address,
        requested_quote: Address,
        requested_base_decimals: u8,
        requested_quote_decimals: u8,
        depth: usize,
    ) -> Result<EulerQuoteLeg, OracleError> {
        let source = self.read_source(
            cache,
            oracle,
            requested_base,
            requested_quote,
            requested_base_decimals,
            requested_quote_decimals,
            depth,
        )?;
        match source {
            FeedSource::EulerQuote { leg, .. } => Ok(leg),
            FeedSource::EulerCross { .. } => Err(OracleError::Unsupported(
                "nested Euler CrossAdapter legs are not supported in this MVP".to_string(),
            )),
            _ => Err(OracleError::Unsupported(
                "Euler child source did not produce a quote leg".to_string(),
            )),
        }
    }

    fn read_chainlink_leg(
        &self,
        cache: &mut EvmCache,
        oracle: Address,
        requested_base: Address,
        requested_quote: Address,
        requested_quote_decimals: u8,
    ) -> Result<EulerQuoteLeg, OracleError> {
        let configured_base = cache
            .call_sol(oracle, chainlinkBaseCall {})
            .map_err(provider_error)?;
        let configured_quote = cache
            .call_sol(oracle, chainlinkQuoteCall {})
            .map_err(provider_error)?;
        let inverse = direction_or_revert(
            requested_base,
            configured_base,
            requested_quote,
            configured_quote,
        )?;
        let feed = cache
            .call_sol(oracle, feedCall {})
            .map_err(provider_error)?;
        let _max_staleness = cache
            .call_sol(oracle, maxStalenessCall {})
            .map_err(provider_error)?;
        let reader = EvmCacheChainlinkReader::new(cache);
        let feed_decimals = reader.read_decimals(feed)?;
        let round = reader.read_latest_round_data(feed)?;
        let aggregator = reader.read_aggregator(feed)?.ok_or_else(|| {
            OracleError::Unsupported(
                "Euler ChainlinkOracle feed aggregator() returned none".to_string(),
            )
        })?;
        Ok(EulerQuoteLeg::Chainlink {
            oracle,
            feed,
            aggregator,
            answer: round.answer,
            feed_decimals,
            inverse,
            quote_decimals: requested_quote_decimals,
        })
    }

    fn read_fixed_rate_leg(
        &self,
        cache: &mut EvmCache,
        oracle: Address,
        requested_base: Address,
        requested_quote: Address,
        requested_base_decimals: u8,
        requested_quote_decimals: u8,
    ) -> Result<EulerQuoteLeg, OracleError> {
        let configured_base = cache
            .call_sol(oracle, fixedBaseCall {})
            .map_err(provider_error)?;
        let configured_quote = cache
            .call_sol(oracle, fixedQuoteCall {})
            .map_err(provider_error)?;
        let inverse = direction_or_revert(
            requested_base,
            configured_base,
            requested_quote,
            configured_quote,
        )?;
        let rate = cache
            .call_sol(oracle, rateCall {})
            .map_err(provider_error)?;
        Ok(EulerQuoteLeg::FixedRate {
            oracle,
            rate: i256_from_u256(rate, "rate")?,
            inverse,
            base_decimals: requested_base_decimals,
            quote_decimals: requested_quote_decimals,
        })
    }

    fn read_rate_provider_leg(
        &self,
        cache: &mut EvmCache,
        oracle: Address,
        requested_base: Address,
        requested_quote: Address,
        requested_quote_decimals: u8,
    ) -> Result<EulerQuoteLeg, OracleError> {
        let configured_base = cache
            .call_sol(oracle, rateBaseCall {})
            .map_err(provider_error)?;
        let configured_quote = cache
            .call_sol(oracle, rateQuoteCall {})
            .map_err(provider_error)?;
        let inverse = direction_or_revert(
            requested_base,
            configured_base,
            requested_quote,
            configured_quote,
        )?;
        let rate_provider = cache
            .call_sol(oracle, rateProviderCall {})
            .map_err(provider_error)?;
        let rate = cache
            .call_sol(rate_provider, getRateCall {})
            .map_err(provider_error)?;
        Ok(EulerQuoteLeg::RateProvider {
            oracle,
            rate_provider,
            rate: i256_from_u256(rate, "rate")?,
            inverse,
            quote_decimals: requested_quote_decimals,
        })
    }
}

impl OracleAdapterPlugin for EulerOracleAdapter {
    fn adapter_id(&self) -> OracleAdapterId {
        OracleAdapterId::new(ADAPTER_ID)
    }

    fn discover<'a>(
        &'a self,
        ctx: OracleDiscoveryContext<'a>,
    ) -> AdapterFuture<'a, OracleDiscoveryReport> {
        Box::pin(async move {
            let now_timestamp = self.timestamp(Some(ctx.now_timestamp))?;
            self.discover_pairs(ctx.cache, now_timestamp)
        })
    }

    fn reactive_handler(
        &self,
        registrations: Vec<FeedRegistration>,
        storage_sync: OracleStorageSync,
    ) -> Arc<dyn ReactiveHandler<Ethereum>> {
        Arc::new(OracleReactiveHandler::with_storage_sync(
            registrations,
            storage_sync,
        ))
    }
}

fn direction_or_revert(
    requested_base: Address,
    configured_base: Address,
    requested_quote: Address,
    configured_quote: Address,
) -> Result<bool, OracleError> {
    if requested_base == configured_base && requested_quote == configured_quote {
        Ok(false)
    } else if requested_base == configured_quote && requested_quote == configured_base {
        Ok(true)
    } else {
        Err(OracleError::Unsupported(
            "Euler oracle does not support requested base/quote pair".to_string(),
        ))
    }
}

fn read_asset_decimals(cache: &mut EvmCache, asset: Address) -> Result<u8, OracleError> {
    if is_reserved_asset_address(asset) {
        return Ok(18);
    }
    match cache.call_sol(asset, decimalsCall {}) {
        Ok(decimals) => Ok(decimals),
        Err(_) => Ok(18),
    }
}

fn is_reserved_asset_address(asset: Address) -> bool {
    // Euler denominates non-token quotes as ISO 4217 numeric currency codes
    // cast to addresses (e.g. USD = address(840)). Any address whose value
    // fits the reserved 32-bit range — i.e. whose top 16 bytes are zero — is
    // treated as such a pseudo-asset and priced with 18 decimals.
    asset.as_slice()[..16].iter().all(|byte| *byte == 0)
}

/// One base unit (`10^base_decimals`) for authoritative `getQuote` reads.
///
/// Shared with the derived-reconciliation reader so discovery seeding and
/// post-event protocol reads quote exactly the same in-amount.
pub(crate) fn one_base_unit(base_decimals: u8) -> Result<U256, OracleError> {
    U256::from(10_u64)
        .checked_pow(U256::from(base_decimals))
        .ok_or_else(|| {
            OracleError::Unsupported(format!(
                "Euler base decimals {base_decimals} overflow a uint256 unit amount"
            ))
        })
}

fn i256_from_u256(value: U256, field: &'static str) -> Result<I256, OracleError> {
    I256::try_from(value)
        .map_err(|_| OracleError::Unsupported(format!("{field} does not fit int256")))
}

fn provider_error(error: impl ToString) -> OracleError {
    OracleError::Provider(error.to_string())
}

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

    #[test]
    fn state_keys_are_deterministic_and_distinct_per_pair_and_target() {
        let router = Address::repeat_byte(0x01);
        let other_router = Address::repeat_byte(0x02);
        let weth = Address::repeat_byte(0x0a);
        let usdc = Address::repeat_byte(0x0b);
        let dai = Address::repeat_byte(0x0c);

        let weth_usdc = EulerQuotePair::state_key_for(router, weth, usdc);
        assert_eq!(
            weth_usdc,
            EulerQuotePair::state_key_for(router, weth, usdc),
            "state keys must be deterministic"
        );
        // Distinct pairs on one router, inverse direction on one router, and
        // the same pair on another router must never collide.
        assert_ne!(weth_usdc, EulerQuotePair::state_key_for(router, weth, dai));
        assert_ne!(weth_usdc, EulerQuotePair::state_key_for(router, usdc, weth));
        assert_ne!(
            weth_usdc,
            EulerQuotePair::state_key_for(other_router, weth, usdc)
        );
        // And the key must not shadow the real router address.
        assert_ne!(weth_usdc, router);
    }

    #[test]
    fn reserved_asset_addresses_cover_iso4217_pseudo_assets() {
        // USD = address(840) per Euler's denomination convention.
        let usd = Address::from_slice(&{
            let mut bytes = [0_u8; 20];
            bytes[18] = 0x03;
            bytes[19] = 0x48;
            bytes
        });
        assert!(is_reserved_asset_address(usd));
        assert!(is_reserved_asset_address(Address::ZERO));
        // The full 32-bit reserved range boundary is still reserved.
        let boundary = Address::from_slice(&{
            let mut bytes = [0_u8; 20];
            bytes[16..].copy_from_slice(&0xffff_ffff_u32.to_be_bytes());
            bytes
        });
        assert!(is_reserved_asset_address(boundary));
        // A real token address is not.
        assert!(!is_reserved_asset_address(Address::repeat_byte(0x42)));
    }

    #[test]
    fn one_base_unit_scales_by_decimals() {
        assert_eq!(one_base_unit(0).unwrap(), U256::from(1_u64));
        assert_eq!(
            one_base_unit(18).unwrap(),
            U256::from(1_000_000_000_000_000_000_u128)
        );
        assert!(one_base_unit(78).is_err(), "10^78 overflows uint256");
    }
}