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
use crate::pb::eth::v2::Call;

pub trait Function: Sized {
    const NAME: &'static str;

    fn match_call(log: &Call) -> bool;
    fn decode(log: &Call) -> Result<Self, String>;
    fn encode(&self) -> Vec<u8>;

    /// Attempts to match and decode the call.
    /// If `Self::match_call(log)` is `false`, returns `None`.
    /// If it matches, but decoding fails, logs the decoding error and returns `None`.
    fn match_and_decode(call: impl AsRef<Call>) -> Option<Self> {
        let call = call.as_ref();
        if !Self::match_call(call) {
            return None;
        }

        match Self::decode(&call) {
            Ok(function) => Some(function),
            Err(err) => {
                substreams::log::info!(
                    "Call for function `{}` at index {} matched but failed to decode with error: {}",
                    Self::NAME,
                    call.index,
                    err
                );
                None
            }
        }
    }
}

impl AsRef<Call> for Call {
    fn as_ref(&self) -> &Self {
        self
    }
}