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
use alloy_dyn_abi::DynSolValue;
use alloy_dyn_abi::EventExt;
use alloy_json_abi::JsonAbi;
use alloy_primitives::B256;
use ethers::core::abi::ethabi::ethereum_types::H256;
use ethers::core::types::Log;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use thiserror::Error;

pub struct Parser<'a> {
    abi: &'a JsonAbi,
}

/// A decoded event which is self-describing through String keys.
#[derive(Debug, Serialize, Deserialize)]
pub struct KeyedEvent {
    /// The name of the event.
    name: String,

    /// The data of the emitted event, both indexed and body.
    data: serde_json::Value,
}

#[derive(Error, Debug)]
pub enum ParsingError {
    /// The name of the decoded event is not found in the ABI. This might
    /// indicate an ABI mismatch.
    #[error("event not found for given abi")]
    UnknownEvent { selector: H256 },
    /// The name of the event IS found in the ABI, yet decoding still failed.
    /// This might indicate an out-of-date ABI.
    #[error("could not decode, abi might mismatch data")]
    DecodingError(#[from] alloy_dyn_abi::Error),
}

impl<'a> Parser<'a> {
    pub fn new(abi: &'a JsonAbi) -> Self {
        Self { abi }
    }

    pub fn parse(&self, log: &Log) -> Result<KeyedEvent, ParsingError> {
        let selector = log.topics.first().unwrap();
        let definition = self
            .abi
            .events()
            .find(|e| e.selector().0 == selector.0)
            .ok_or(ParsingError::UnknownEvent {
                selector: *selector,
            })?;

        let topics = log.topics.iter().map(|t| B256::from_slice(&t.0));
        let decoded = definition
            .decode_log_parts(topics, &log.data, true)
            .map_err(ParsingError::DecodingError)?;
        let indexed = definition.inputs.iter().filter(|e| e.indexed);
        let body = definition.inputs.iter().filter(|e| !e.indexed);

        let indexed = indexed.zip(decoded.indexed);
        let body = body.zip(decoded.body);

        let values: Map<String, Value> = indexed
            .chain(body)
            .map(|(k, v)| {
                // For now we only support simple types, not structs and such.
                (k.name.clone(), dyn_sol_to_json(v))
            })
            .collect();

        Ok(KeyedEvent {
            name: definition.name.clone(),
            data: Value::Object(values),
        })
    }
}

fn dyn_sol_to_json(val: DynSolValue) -> Value {
    use base64::prelude::*;

    match val {
        DynSolValue::Bool(b) => Value::Bool(b),
        DynSolValue::Int(i, _) => Value::String(i.to_dec_string()),
        DynSolValue::Uint(i, _) => Value::String(i.to_string()),
        DynSolValue::FixedBytes(v, _) => Value::String(BASE64_STANDARD.encode(v.0)),
        DynSolValue::Address(a) => Value::String(a.to_string()),
        DynSolValue::Function(p) => Value::String(p.to_string()),
        DynSolValue::Bytes(b) => Value::String(BASE64_STANDARD.encode(b)),
        DynSolValue::String(s) => Value::String(s),
        DynSolValue::Array(a) => Value::Array(a.into_iter().map(dyn_sol_to_json).collect()),
        DynSolValue::FixedArray(a) => Value::Array(a.into_iter().map(dyn_sol_to_json).collect()),
        DynSolValue::Tuple(a) => Value::Array(a.into_iter().map(dyn_sol_to_json).collect()),
        DynSolValue::CustomStruct {
            name: _,
            prop_names,
            tuple,
        } => {
            let map = prop_names
                .into_iter()
                .zip(tuple.into_iter().map(dyn_sol_to_json))
                .collect();
            Value::Object(map)
        }
    }
}

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

    fn logs() -> Vec<Log> {
        #[derive(Deserialize)]
        struct ApiResponse {
            pub result: Vec<Log>,
        }
        let file = include_str!("../testdata/logs.json");
        let response: ApiResponse = serde_json::from_str(&file).unwrap();
        response.result
    }

    fn erc20_abi() -> JsonAbi {
        let json = include_str!("../testdata/erc20.json");
        serde_json::from_str(&json).unwrap()
    }

    #[test]
    fn erc20_parsing_works() {
        let abi = erc20_abi();
        let parser = Parser::new(&abi);
        for log in logs() {
            parser.parse(&log).unwrap();
        }
    }

    mod ibc {
        use super::*;

        fn logs() -> Vec<Log> {
            let file = include_str!("../testdata/ibc/logs.json");
            let response: Vec<Log> = serde_json::from_str(&file).unwrap();
            response
        }

        fn abi() -> JsonAbi {
            let json = include_str!("../testdata/ibc/abi.json");
            serde_json::from_str(&json).unwrap()
        }

        #[test]
        fn ibc_parsing_works() {
            let abi = abi();
            let parser = Parser::new(&abi);
            for log in logs() {
                parser.parse(&log).unwrap();
            }
        }
    }
}