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

use alloy_primitives::{Address, I256, U256};
use evm_fork_cache::cache::EvmCache;

use crate::{
    AssetId, Denomination, EvmCacheChainlinkReader, Feed, FeedConfig, FeedId, FeedMetadata,
    FeedSource, OracleAdapterBuildReport, OracleAdapterFeedSkip, OracleAdapterSkipReason,
    OracleError, OracleRegistry, OracleTracker, RoundData, StalenessPolicy,
    cache_reader::{AaveFixedPriceSource, AaveRatioCapSource},
};

/// Declarative Aave V3 asset source registration.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AaveAsset {
    asset: Address,
    id: Option<FeedId>,
    label: Option<String>,
    base: Option<AssetId>,
    quote: Option<Denomination>,
    staleness: StalenessPolicy,
}

impl AaveAsset {
    /// Create an Aave asset registration for `AaveOracle.getSourceOfAsset(asset)`.
    pub fn new(asset: Address) -> Self {
        Self {
            asset,
            id: None,
            label: None,
            base: None,
            quote: None,
            staleness: StalenessPolicy::default(),
        }
    }

    /// Return the Aave asset address.
    pub fn asset(&self) -> Address {
        self.asset
    }

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

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

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

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

    /// Set the quote denomination.
    pub fn quote(mut self, quote: Denomination) -> Self {
        self.quote = 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
    }

    fn feed_for_source(&self, source: Address) -> Feed {
        let mut feed = Feed::proxy(source);
        if let Some(id) = self.id.clone() {
            feed = feed.feed_id(id);
        }
        if let Some(label) = &self.label {
            feed = feed.label(label.clone());
        }
        if let Some(base) = &self.base {
            feed = feed.base(base.clone());
        }
        if let Some(quote) = &self.quote {
            feed = feed.quote(quote.clone());
        }
        feed.staleness(self.staleness)
    }

    fn config_for_source(&self, source: Address) -> Result<FeedConfig, OracleError> {
        self.feed_for_source(source).try_into()
    }
}

/// Cache-backed Aave V3 oracle source discovery adapter.
#[derive(Clone, Debug, Default)]
pub struct AaveV3OracleAdapter {
    oracle: Address,
    assets: Vec<AaveAsset>,
    now_timestamp: Option<u64>,
}

impl AaveV3OracleAdapter {
    /// Create an adapter for an Aave V3 `AaveOracle`.
    pub fn new(oracle: Address) -> Self {
        Self {
            oracle,
            assets: Vec::new(),
            now_timestamp: None,
        }
    }

    /// Return the Aave oracle address.
    pub fn oracle(&self) -> Address {
        self.oracle
    }

    /// Return configured assets.
    pub fn assets(&self) -> &[AaveAsset] {
        &self.assets
    }

    /// Add one asset whose source should be discovered from the Aave oracle.
    pub fn asset(mut self, asset: AaveAsset) -> Self {
        self.assets.push(asset);
        self
    }

    /// Add multiple Aave assets.
    pub fn assets_iter(mut self, assets: impl IntoIterator<Item = AaveAsset>) -> Self {
        self.assets.extend(assets);
        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
    }

    /// Discover and register every compatible Aave source.
    pub async fn build(self, cache: &mut EvmCache) -> Result<OracleTracker, OracleError> {
        let report = self.build_report(cache).await?;
        if let Some(skipped) = report.skipped.first() {
            return Err(OracleError::FeedSkipped(Box::new(
                crate::OracleFeedSkip::from_adapter_skip(skipped),
            )));
        }
        Ok(report.tracker)
    }

    /// Discover compatible sources and report unsupported variants instead of panicking.
    pub async fn build_report(
        self,
        cache: &mut EvmCache,
    ) -> Result<OracleAdapterBuildReport, OracleError> {
        let now_timestamp = match self.now_timestamp {
            Some(now_timestamp) => now_timestamp,
            None => SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .map_err(crate::error::clock_error)?
                .as_secs(),
        };
        let reader = EvmCacheChainlinkReader::new(cache);
        let mut registry = OracleRegistry::new_at_timestamp(now_timestamp);
        let mut skipped = Vec::new();

        let oracle = self.oracle;
        for asset in self.assets {
            if let Some(skip) =
                Self::register_asset(oracle, &reader, &mut registry, asset, now_timestamp).await?
            {
                skipped.push(skip);
            }
        }

        Ok(OracleAdapterBuildReport {
            tracker: OracleTracker::new(registry),
            skipped,
        })
    }

    async fn register_asset(
        oracle: Address,
        reader: &EvmCacheChainlinkReader<'_>,
        registry: &mut OracleRegistry,
        asset: AaveAsset,
        now_timestamp: u64,
    ) -> Result<Option<OracleAdapterFeedSkip>, OracleError> {
        let source = match reader.read_aave_source(oracle, asset.asset()) {
            Ok(source) if source != Address::ZERO => source,
            Ok(_) => {
                return Ok(Some(aave_skip(
                    &asset,
                    asset.asset(),
                    "Aave oracle returned the zero source address",
                )));
            }
            Err(error) => {
                return Ok(Some(aave_skip(
                    &asset,
                    asset.asset(),
                    format!("getSourceOfAsset failed: {error}"),
                )));
            }
        };

        let feed = asset.feed_for_source(source);
        match reader.register_feed(registry, feed.clone()).await {
            Ok(_) => Ok(None),
            // Chainlink-probe read failures and value-range decode failures
            // both mean "not a plain Chainlink proxy": fall through to the
            // Aave-specific source probes.
            Err(OracleError::Provider(chainlink_error)) => {
                Self::try_register_aave_derived_source(
                    reader,
                    registry,
                    asset,
                    source,
                    chainlink_error,
                    now_timestamp,
                )
                .await
            }
            Err(error @ OracleError::Decode(_)) => {
                Self::try_register_aave_derived_source(
                    reader,
                    registry,
                    asset,
                    source,
                    error.to_string(),
                    now_timestamp,
                )
                .await
            }
            Err(error) => Err(error),
        }
    }

    async fn try_register_aave_derived_source(
        reader: &EvmCacheChainlinkReader<'_>,
        registry: &mut OracleRegistry,
        asset: AaveAsset,
        source: Address,
        chainlink_error: String,
        now_timestamp: u64,
    ) -> Result<Option<OracleAdapterFeedSkip>, OracleError> {
        let mut errors = vec![format!("Chainlink-compatible ({chainlink_error})")];
        match Self::register_price_cap_stable_source(reader, registry, &asset, source).await {
            Ok(()) => return Ok(None),
            Err(error) => errors.push(format!("Aave PriceCapAdapterStable ({error})")),
        }
        match Self::register_ratio_cap_source(reader, registry, &asset, source).await {
            Ok(()) => return Ok(None),
            Err(error) => errors.push(format!("Aave PriceCapAdapterBase/CAPO ({error})")),
        }
        match Self::register_synchronicity_peg_to_base_source(reader, registry, &asset, source)
            .await
        {
            Ok(()) => return Ok(None),
            Err(error) => errors.push(format!("Aave CLSynchronicity PegToBase ({error})")),
        }
        match Self::register_fixed_price_source(reader, registry, &asset, source, now_timestamp)
            .await
        {
            Ok(()) => return Ok(None),
            Err(error) => errors.push(format!("Aave fixed-price ({error})")),
        }

        Ok(Some(aave_skip(
            &asset,
            source,
            format!(
                "unsupported Aave source; probes failed: {}",
                errors.join("; ")
            ),
        )))
    }

    async fn register_price_cap_stable_source(
        reader: &EvmCacheChainlinkReader<'_>,
        registry: &mut OracleRegistry,
        asset: &AaveAsset,
        source: Address,
    ) -> Result<(), OracleError> {
        let capped = match reader.read_aave_price_cap_stable(source) {
            Ok(capped) => capped,
            Err(error) => return Err(error),
        };

        let registration_source =
            FeedSource::aave_price_cap_stable(source, capped.underlying_proxy, capped.price_cap);
        let (underlying_round, current_aggregator) =
            read_underlying_chainlink(reader, capped.underlying_proxy)?;

        let mut seeded_round = underlying_round;
        seeded_round.answer = registration_source.normalize_answer(capped.latest_answer);
        let metadata = FeedMetadata {
            decimals: capped.decimals,
            description: capped.description,
            version: U256::ZERO,
        };

        registry
            .register_discovered_feed(
                reader,
                asset.config_for_source(source)?,
                registration_source,
                metadata,
                seeded_round,
                Some(current_aggregator),
            )
            .await?;

        Ok(())
    }

    async fn register_ratio_cap_source(
        reader: &EvmCacheChainlinkReader<'_>,
        registry: &mut OracleRegistry,
        asset: &AaveAsset,
        source: Address,
    ) -> Result<(), OracleError> {
        let ratio = reader.read_aave_ratio_cap(source)?;
        let (base_round, current_aggregator) =
            read_underlying_chainlink(reader, ratio.base_to_usd_proxy)?;
        let max_ratio = ratio_cap_max_ratio(&ratio, base_round.answer);
        let registration_source = FeedSource::aave_ratio_cap(
            source,
            ratio.base_to_usd_proxy,
            ratio.ratio_provider,
            ratio.current_ratio,
            max_ratio,
            ratio.ratio_decimals,
        );

        let mut seeded_round = base_round;
        seeded_round.answer = ratio.latest_answer;
        let metadata = FeedMetadata {
            decimals: ratio.decimals,
            description: ratio.description,
            version: U256::ZERO,
        };

        registry
            .register_discovered_feed(
                reader,
                asset.config_for_source(source)?,
                registration_source,
                metadata,
                seeded_round,
                Some(current_aggregator),
            )
            .await?;

        Ok(())
    }

    async fn register_synchronicity_peg_to_base_source(
        reader: &EvmCacheChainlinkReader<'_>,
        registry: &mut OracleRegistry,
        asset: &AaveAsset,
        source: Address,
    ) -> Result<(), OracleError> {
        let synchronicity = reader.read_aave_synchronicity_peg_to_base(source)?;
        let (asset_round, asset_aggregator) =
            read_underlying_chainlink(reader, synchronicity.asset_to_peg_proxy)?;
        let (peg_round, peg_aggregator) =
            read_underlying_chainlink(reader, synchronicity.peg_to_base_proxy)?;
        let asset_decimals = reader.read_decimals(synchronicity.asset_to_peg_proxy)?;
        let peg_decimals = reader.read_decimals(synchronicity.peg_to_base_proxy)?;
        let registration_source = FeedSource::aave_synchronicity_peg_to_base(
            source,
            synchronicity.asset_to_peg_proxy,
            asset_aggregator,
            asset_round.answer,
            asset_decimals,
            synchronicity.peg_to_base_proxy,
            peg_aggregator,
            peg_round.answer,
            peg_decimals,
            synchronicity.decimals,
        );

        let mut seeded_round = asset_round;
        seeded_round.answer = synchronicity.latest_answer;
        seeded_round.updated_at = seeded_round.updated_at.max(peg_round.updated_at);
        let metadata = FeedMetadata {
            decimals: synchronicity.decimals,
            description: synchronicity.description,
            version: U256::ZERO,
        };

        registry
            .register_discovered_feed(
                reader,
                asset.config_for_source(source)?,
                registration_source,
                metadata,
                seeded_round,
                Some(asset_aggregator),
            )
            .await?;

        Ok(())
    }

    async fn register_fixed_price_source(
        reader: &EvmCacheChainlinkReader<'_>,
        registry: &mut OracleRegistry,
        asset: &AaveAsset,
        source: Address,
        now_timestamp: u64,
    ) -> Result<(), OracleError> {
        let fixed = reader.read_aave_fixed_price(source)?;
        let seeded_round = fixed_price_round(&fixed, now_timestamp);
        let registration_source = FeedSource::aave_fixed_price(source, fixed.latest_answer);
        let metadata = FeedMetadata {
            decimals: fixed.decimals,
            description: fixed.description,
            version: U256::ZERO,
        };
        registry
            .register_discovered_feed(
                reader,
                asset.config_for_source(source)?,
                registration_source,
                metadata,
                seeded_round,
                None,
            )
            .await?;

        Ok(())
    }
}

fn read_underlying_chainlink(
    reader: &EvmCacheChainlinkReader<'_>,
    proxy: Address,
) -> Result<(RoundData, Address), OracleError> {
    reader.read_decimals(proxy)?;
    reader.read_description(proxy)?;
    reader.read_version(proxy)?;
    let round = reader.read_latest_round_data(proxy)?;
    let aggregator = reader.read_aggregator(proxy)?.ok_or_else(|| {
        OracleError::Unsupported("underlying aggregator() returned none".to_string())
    })?;
    Ok((round, aggregator))
}

fn ratio_cap_max_ratio(ratio: &AaveRatioCapSource, base_answer: I256) -> I256 {
    if !ratio.is_capped || base_answer == I256::ZERO {
        return ratio.current_ratio;
    }
    ratio
        .latest_answer
        .saturating_mul(decimal_scale(ratio.ratio_decimals))
        / base_answer
}

fn fixed_price_round(fixed: &AaveFixedPriceSource, now_timestamp: u64) -> RoundData {
    RoundData {
        round_id: U256::ZERO,
        answer: fixed.latest_answer,
        started_at: now_timestamp,
        updated_at: now_timestamp,
        answered_in_round: U256::ZERO,
    }
}

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 aave_skip(
    asset: &AaveAsset,
    proxy: Address,
    reason: impl Into<String>,
) -> OracleAdapterFeedSkip {
    OracleAdapterFeedSkip {
        feed: asset.feed_for_source(proxy),
        proxy,
        reason: OracleAdapterSkipReason::UnsupportedAaveSource {
            error: reason.into(),
        },
    }
}