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
use alloy_primitives::{Address, B256, FixedBytes, I256, U256, b256};
use alloy_rpc_types_eth::Log as RpcLog;
use alloy_sol_types::SolEvent;

use crate::ChainlinkEventDecodeError;

/// `AnswerUpdated(int256,uint256,uint256)` topic0.
pub const ANSWER_UPDATED_TOPIC: B256 =
    b256!("0559884fd3a460db3073b7fc896cc77986f16e378210ded43186175bf646fc5f");

/// `NewRound(uint256,address,uint256)` topic0.
pub const NEW_ROUND_TOPIC: B256 =
    b256!("0109fc6f55cf40689f02fbaad7af7fe7bbac8a3d2186600afc7d3e10cac60271");

/// OCR2 `NewTransmission(uint32,int192,address,uint32,int192[],bytes,int192,bytes32,uint40)` topic0.
pub const OCR2_NEW_TRANSMISSION_TOPIC: B256 =
    b256!("c797025feeeaf2cd924c99e9205acb8ec04d5cad21c41ce637a38fb6dee6016a");

/// OCR1 `NewTransmission(uint32,int192,address,int192[],bytes,bytes32)` topic0.
pub const OCR1_NEW_TRANSMISSION_TOPIC: B256 =
    b256!("f6a97944f31ea060dfde0566e4167c1a1082551e64b60ecb14d599a9d023d451");

/// Pyth `PriceFeedUpdate(bytes32,uint64,int64,uint64)` topic0.
#[cfg(feature = "pyth")]
pub const PYTH_PRICE_FEED_UPDATE_TOPIC: B256 =
    b256!("d06a6b7f4918494b3719217d1802786c1f5112a6c1d88fe2cfec00b4584f6aec");

/// RedStone `ValueUpdate(uint256,bytes32,uint256)` topic0.
#[cfg(feature = "redstone")]
pub const REDSTONE_VALUE_UPDATE_TOPIC: B256 =
    b256!("f36866d965ee70c8632ff558f5cf8d41ee9ca1d0d0bc7700786e57be60747390");

mod ocr1_abi {
    alloy_sol_types::sol! {
        event NewTransmission(
            uint32 indexed aggregatorRoundId,
            int192 answer,
            address transmitter,
            int192[] observations,
            bytes observers,
            bytes32 rawReportContext
        );
    }
}

mod ocr2_abi {
    alloy_sol_types::sol! {
        event NewTransmission(
            uint32 indexed aggregatorRoundId,
            int192 answer,
            address transmitter,
            uint32 observationsTimestamp,
            int192[] observations,
            bytes observers,
            int192 juelsPerFeeCoin,
            bytes32 configDigest,
            uint40 epochAndRound
        );
    }
}

/// Decoded Chainlink OCR1 `NewTransmission` event.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Ocr1NewTransmission {
    /// Emitting aggregator address.
    pub aggregator: Address,
    /// Aggregator-local round id.
    pub aggregator_round_id: U256,
    /// OCR1 median answer.
    pub answer: I256,
    /// Address that transmitted the report.
    pub transmitter: Address,
    /// OCR1 raw report context.
    pub raw_report_context: B256,
    /// OCR1 config digest extracted from `rawReportContext`.
    pub config_digest: FixedBytes<16>,
    /// OCR1 epoch and round packed as uint40.
    pub epoch_and_round: u64,
    /// Log block number, when present.
    pub block_number: Option<u64>,
    /// Log index, when present.
    pub log_index: Option<u64>,
    /// Whether the provider marked the log as removed.
    pub removed: bool,
}

/// Decoded Chainlink `AnswerUpdated` event.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AnswerUpdated {
    /// Emitting aggregator address.
    pub aggregator: Address,
    /// Signed answer from the event.
    pub current: I256,
    /// Aggregator-local round id.
    pub round_id: U256,
    /// Event `updatedAt` timestamp.
    pub updated_at: u64,
    /// Log block number, when present.
    pub block_number: Option<u64>,
    /// Log index, when present.
    pub log_index: Option<u64>,
    /// Whether the provider marked the log as removed.
    pub removed: bool,
}

/// Decoded Chainlink OCR2 `NewTransmission` event.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Ocr2NewTransmission {
    /// Emitting aggregator address.
    pub aggregator: Address,
    /// Aggregator-local round id.
    pub aggregator_round_id: U256,
    /// OCR2 median answer.
    pub answer: I256,
    /// Address that transmitted the report.
    pub transmitter: Address,
    /// OCR2 observations timestamp, used as `startedAt`.
    pub observations_timestamp: u64,
    /// OCR2 config digest for the transmission.
    pub config_digest: B256,
    /// OCR2 epoch and round packed as uint40.
    pub epoch_and_round: u64,
    /// Log block number, when present.
    pub block_number: Option<u64>,
    /// Log index, when present.
    pub log_index: Option<u64>,
    /// Whether the provider marked the log as removed.
    pub removed: bool,
}

/// Decoded Chainlink `NewRound` event.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NewRound {
    /// Emitting aggregator address.
    pub aggregator: Address,
    /// Aggregator-local round id.
    pub round_id: U256,
    /// Address that started the round.
    pub started_by: Address,
    /// Event `startedAt` timestamp.
    pub started_at: u64,
    /// Log block number, when present.
    pub block_number: Option<u64>,
    /// Log index, when present.
    pub log_index: Option<u64>,
    /// Whether the provider marked the log as removed.
    pub removed: bool,
}

/// Decoded Pyth `PriceFeedUpdate` event.
#[cfg(feature = "pyth")]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PythPriceFeedUpdate {
    /// Emitting Pyth contract address.
    pub pyth: Address,
    /// Pyth price feed id.
    pub price_id: B256,
    /// Pyth publish timestamp.
    pub publish_time: u64,
    /// Fixed-point price value.
    pub price: i64,
    /// Pyth confidence interval.
    pub conf: u64,
    /// Log block number, when present.
    pub block_number: Option<u64>,
    /// Log index, when present.
    pub log_index: Option<u64>,
    /// Whether the provider marked the log as removed.
    pub removed: bool,
}

/// Decoded RedStone `ValueUpdate` event.
#[cfg(feature = "redstone")]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct RedstoneValueUpdate {
    /// Emitting RedStone adapter address.
    pub adapter: Address,
    /// RedStone data feed id.
    pub data_feed_id: B256,
    /// Unsigned RedStone value.
    pub value: U256,
    /// Event `updatedAt` timestamp.
    pub updated_at: u64,
    /// Log block number, when present.
    pub block_number: Option<u64>,
    /// Log index, when present.
    pub log_index: Option<u64>,
    /// Whether the provider marked the log as removed.
    pub removed: bool,
}

/// Decode a Chainlink `AnswerUpdated` log.
pub fn decode_answer_updated(log: &RpcLog) -> Result<AnswerUpdated, ChainlinkEventDecodeError> {
    require_topic(log, ANSWER_UPDATED_TOPIC)?;
    require_topic_count(log, 3)?;
    let data = data_word(log, "updated_at")?;
    Ok(AnswerUpdated {
        aggregator: log.address(),
        current: topic_i256(log.topics()[1]),
        round_id: topic_u256(log.topics()[2]),
        updated_at: u256_to_u64(data, "updated_at")?,
        block_number: log.block_number,
        log_index: log.log_index,
        removed: log.removed,
    })
}

/// Decode a RedStone `ValueUpdate` log.
#[cfg(feature = "redstone")]
pub fn decode_redstone_value_update(
    log: &RpcLog,
) -> Result<RedstoneValueUpdate, ChainlinkEventDecodeError> {
    require_topic(log, REDSTONE_VALUE_UPDATE_TOPIC)?;
    require_topic_count(log, 1)?;
    let data = log.inner.data.data.as_ref();
    if data.len() != 96 {
        return Err(ChainlinkEventDecodeError::WrongDataLength {
            expected: 96,
            actual: data.len(),
        });
    }

    let value = U256::from_be_slice(&data[0..32]);
    let data_feed_id = B256::from_slice(&data[32..64]);
    let updated_at = u256_to_u64(U256::from_be_slice(&data[64..96]), "updated_at")?;

    Ok(RedstoneValueUpdate {
        adapter: log.address(),
        data_feed_id,
        value,
        updated_at,
        block_number: log.block_number,
        log_index: log.log_index,
        removed: log.removed,
    })
}

/// Decode a Chainlink OCR1 `NewTransmission` log.
pub fn decode_ocr1_new_transmission(
    log: &RpcLog,
) -> Result<Ocr1NewTransmission, ChainlinkEventDecodeError> {
    require_topic(log, OCR1_NEW_TRANSMISSION_TOPIC)?;
    require_topic_count(log, 2)?;
    let decoded = ocr1_abi::NewTransmission::decode_log_validate(&log.inner).map_err(|error| {
        ChainlinkEventDecodeError::AbiDecode {
            event: "NewTransmission",
            message: error.to_string(),
        }
    })?;
    let data = decoded.data;
    Ok(Ocr1NewTransmission {
        aggregator: log.address(),
        aggregator_round_id: U256::from(data.aggregatorRoundId),
        answer: int192_to_i256(data.answer),
        transmitter: data.transmitter,
        raw_report_context: data.rawReportContext,
        config_digest: ocr1_config_digest(data.rawReportContext),
        epoch_and_round: u40_from_context(data.rawReportContext),
        block_number: log.block_number,
        log_index: log.log_index,
        removed: log.removed,
    })
}

/// Decode a Chainlink OCR2 `NewTransmission` log.
pub fn decode_ocr2_new_transmission(
    log: &RpcLog,
) -> Result<Ocr2NewTransmission, ChainlinkEventDecodeError> {
    require_topic(log, OCR2_NEW_TRANSMISSION_TOPIC)?;
    require_topic_count(log, 2)?;
    let decoded = ocr2_abi::NewTransmission::decode_log_validate(&log.inner).map_err(|error| {
        ChainlinkEventDecodeError::AbiDecode {
            event: "NewTransmission",
            message: error.to_string(),
        }
    })?;
    let data = decoded.data;
    Ok(Ocr2NewTransmission {
        aggregator: log.address(),
        aggregator_round_id: U256::from(data.aggregatorRoundId),
        answer: int192_to_i256(data.answer),
        transmitter: data.transmitter,
        observations_timestamp: u64::from(data.observationsTimestamp),
        config_digest: data.configDigest,
        epoch_and_round: data.epochAndRound.as_limbs()[0],
        block_number: log.block_number,
        log_index: log.log_index,
        removed: log.removed,
    })
}

/// Decode a Chainlink `NewRound` log.
pub fn decode_new_round(log: &RpcLog) -> Result<NewRound, ChainlinkEventDecodeError> {
    require_topic(log, NEW_ROUND_TOPIC)?;
    require_topic_count(log, 3)?;
    let data = data_word(log, "started_at")?;
    Ok(NewRound {
        aggregator: log.address(),
        round_id: topic_u256(log.topics()[1]),
        started_by: topic_address(log.topics()[2]),
        started_at: u256_to_u64(data, "started_at")?,
        block_number: log.block_number,
        log_index: log.log_index,
        removed: log.removed,
    })
}

/// Decode a Pyth `PriceFeedUpdate` log.
#[cfg(feature = "pyth")]
pub fn decode_pyth_price_feed_update(
    log: &RpcLog,
) -> Result<PythPriceFeedUpdate, ChainlinkEventDecodeError> {
    require_topic(log, PYTH_PRICE_FEED_UPDATE_TOPIC)?;
    require_topic_count(log, 2)?;
    let data = log.inner.data.data.as_ref();
    if data.len() != 96 {
        return Err(ChainlinkEventDecodeError::WrongDataLength {
            expected: 96,
            actual: data.len(),
        });
    }

    let publish_time = u256_to_u64(U256::from_be_slice(&data[0..32]), "publish_time")?;
    let price = int64_word(&data[32..64])?;
    let conf = u256_to_u64(U256::from_be_slice(&data[64..96]), "conf")?;

    Ok(PythPriceFeedUpdate {
        pyth: log.address(),
        price_id: log.topics()[1],
        publish_time,
        price,
        conf,
        block_number: log.block_number,
        log_index: log.log_index,
        removed: log.removed,
    })
}

fn require_topic(log: &RpcLog, expected: B256) -> Result<(), ChainlinkEventDecodeError> {
    let actual = log.topics().first().copied();
    if actual == Some(expected) {
        Ok(())
    } else {
        Err(ChainlinkEventDecodeError::WrongTopic { expected, actual })
    }
}

fn require_topic_count(log: &RpcLog, expected: usize) -> Result<(), ChainlinkEventDecodeError> {
    let actual = log.topics().len();
    if actual == expected {
        Ok(())
    } else {
        Err(ChainlinkEventDecodeError::WrongTopicCount { expected, actual })
    }
}

fn data_word(log: &RpcLog, field: &'static str) -> Result<U256, ChainlinkEventDecodeError> {
    let data = log.inner.data.data.as_ref();
    if data.len() != 32 {
        return Err(ChainlinkEventDecodeError::WrongDataLength {
            expected: 32,
            actual: data.len(),
        });
    }

    let _ = field;
    Ok(U256::from_be_slice(data))
}

fn topic_u256(topic: B256) -> U256 {
    U256::from_be_slice(topic.as_slice())
}

fn topic_i256(topic: B256) -> I256 {
    I256::from_raw(topic_u256(topic))
}

fn topic_address(topic: B256) -> Address {
    Address::from_slice(&topic.as_slice()[12..])
}

fn int192_to_i256(value: alloy_primitives::Signed<192, 3>) -> I256 {
    let raw = value.into_raw();
    let mut limbs = [0_u64; 4];
    limbs[..3].copy_from_slice(raw.as_limbs());
    if raw.as_limbs()[2] & (1_u64 << 63) != 0 {
        limbs[3] = u64::MAX;
    }
    I256::from_raw(U256::from_limbs(limbs))
}

fn ocr1_config_digest(raw_report_context: B256) -> FixedBytes<16> {
    let mut digest = [0_u8; 16];
    digest.copy_from_slice(&raw_report_context.as_slice()[11..27]);
    FixedBytes::from(digest)
}

fn u40_from_context(raw_report_context: B256) -> u64 {
    let bytes = raw_report_context.as_slice();
    u64::from_be_bytes([
        0, 0, 0, bytes[27], bytes[28], bytes[29], bytes[30], bytes[31],
    ])
}

#[cfg(feature = "pyth")]
fn int64_word(word: &[u8]) -> Result<i64, ChainlinkEventDecodeError> {
    debug_assert_eq!(word.len(), 32);
    let negative = word[24] & 0x80 != 0;
    let expected_prefix = if negative { 0xff } else { 0x00 };
    if word[..24].iter().any(|byte| *byte != expected_prefix) {
        return Err(ChainlinkEventDecodeError::AbiDecode {
            event: "PriceFeedUpdate",
            message: "price does not fit int64".to_string(),
        });
    }
    let mut bytes = [0_u8; 8];
    bytes.copy_from_slice(&word[24..32]);
    Ok(i64::from_be_bytes(bytes))
}

fn u256_to_u64(value: U256, field: &'static str) -> Result<u64, ChainlinkEventDecodeError> {
    u64::try_from(value).map_err(|_| ChainlinkEventDecodeError::Uint64Overflow { field, value })
}