substreams_ethereum_core/
function.rs

1use crate::pb::eth::v2::Call;
2
3pub trait Function: Sized {
4    const NAME: &'static str;
5
6    fn match_call(log: &Call) -> bool;
7    fn decode(log: &Call) -> Result<Self, String>;
8    fn encode(&self) -> Vec<u8>;
9
10    /// Attempts to match and decode the call.
11    /// If `Self::match_call(log)` is `false`, returns `None`.
12    /// If it matches, but decoding fails, logs the decoding error and returns `None`.
13    fn match_and_decode(call: impl AsRef<Call>) -> Option<Self> {
14        let call = call.as_ref();
15        if !Self::match_call(call) {
16            return None;
17        }
18
19        match Self::decode(&call) {
20            Ok(function) => Some(function),
21            Err(err) => {
22                substreams::log::info!(
23                    "Call for function `{}` at index {} matched but failed to decode with error: {}",
24                    Self::NAME,
25                    call.index,
26                    err
27                );
28                None
29            }
30        }
31    }
32}
33
34impl AsRef<Call> for Call {
35    fn as_ref(&self) -> &Self {
36        self
37    }
38}