alloy_dyn_abi/ext/
event.rs

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
use crate::{DecodedEvent, DynSolEvent, DynSolType, Error, Result, Specifier};
use alloc::vec::Vec;
use alloy_json_abi::Event;
use alloy_primitives::{LogData, B256};

#[allow(unknown_lints, unnameable_types)]
mod sealed {
    pub trait Sealed {}
    impl Sealed for alloy_json_abi::Event {}
}
use sealed::Sealed;

impl Specifier<DynSolEvent> for Event {
    fn resolve(&self) -> Result<DynSolEvent> {
        let mut indexed = Vec::with_capacity(self.inputs.len());
        let mut body = Vec::with_capacity(self.inputs.len());
        for param in &self.inputs {
            let ty = param.resolve()?;
            if param.indexed {
                indexed.push(ty);
            } else {
                body.push(ty);
            }
        }
        let topic_0 = if self.anonymous { None } else { Some(self.selector()) };

        let num_topics = indexed.len() + topic_0.is_some() as usize;
        if num_topics > 4 {
            return Err(Error::TopicLengthMismatch { expected: 4, actual: num_topics });
        }

        Ok(DynSolEvent::new_unchecked(topic_0, indexed, DynSolType::Tuple(body)))
    }
}

/// Provides event encoding and decoding for the [`Event`] type.
///
/// This trait is sealed and cannot be implemented for types outside of this
/// crate. It is implemented only for [`Event`].
pub trait EventExt: Sealed {
    /// Decodes the given log info according to this item's input types.
    ///
    /// The `topics` parameter is the list of indexed topics, and the `data`
    /// parameter is the non-indexed data.
    ///
    /// The first topic is skipped, unless the event is anonymous.
    ///
    /// For more details, see the [Solidity reference][ref].
    ///
    /// [ref]: https://docs.soliditylang.org/en/latest/abi-spec.html#encoding-of-indexed-event-parameters
    ///
    /// # Errors
    ///
    /// This function will return an error if the decoded data does not match
    /// the expected input types.
    fn decode_log_parts<I>(&self, topics: I, data: &[u8], validate: bool) -> Result<DecodedEvent>
    where
        I: IntoIterator<Item = B256>;

    /// Decodes the given log object according to this item's input types.
    ///
    /// See [`decode_log`](EventExt::decode_log).
    #[inline]
    fn decode_log(&self, log: &LogData, validate: bool) -> Result<DecodedEvent> {
        self.decode_log_parts(log.topics().iter().copied(), &log.data, validate)
    }
}

impl EventExt for Event {
    fn decode_log_parts<I>(&self, topics: I, data: &[u8], validate: bool) -> Result<DecodedEvent>
    where
        I: IntoIterator<Item = B256>,
    {
        self.resolve()?.decode_log_parts(topics, data, validate)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::DynSolValue;
    use alloy_json_abi::EventParam;
    use alloy_primitives::{address, b256, bytes, hex, keccak256, Signed};

    #[test]
    fn empty() {
        let mut event = Event { name: "MyEvent".into(), inputs: vec![], anonymous: false };

        // skips over hash
        let values = event.decode_log_parts(None, &[], false).unwrap();
        assert!(values.indexed.is_empty());
        assert!(values.body.is_empty());

        // but if we validate, we get an error
        let err = event.decode_log_parts(None, &[], true).unwrap_err();
        assert_eq!(err, Error::TopicLengthMismatch { expected: 1, actual: 0 });

        let values = event.decode_log_parts(Some(keccak256("MyEvent()")), &[], true).unwrap();
        assert!(values.indexed.is_empty());
        assert!(values.body.is_empty());
        event.anonymous = true;
        let values = event.decode_log_parts(None, &[], false).unwrap();
        assert!(values.indexed.is_empty());
        assert!(values.body.is_empty());
        let values = event.decode_log_parts(None, &[], true).unwrap();
        assert!(values.indexed.is_empty());
        assert!(values.body.is_empty());
    }

    // https://github.com/rust-ethereum/ethabi/blob/b1710adc18f5b771d2d2519c87248b1ba9430778/ethabi/src/event.rs#L192
    #[test]
    fn test_decoding_event() {
        let event = Event {
            name: "foo".into(),
            inputs: vec![
                EventParam { ty: "int256".into(), indexed: false, ..Default::default() },
                EventParam { ty: "int256".into(), indexed: true, ..Default::default() },
                EventParam { ty: "address".into(), indexed: false, ..Default::default() },
                EventParam { ty: "address".into(), indexed: true, ..Default::default() },
                EventParam { ty: "string".into(), indexed: true, ..Default::default() },
            ],
            anonymous: false,
        };

        let result = event
            .decode_log_parts(
                [
                    b256!("0000000000000000000000000000000000000000000000000000000000000000"),
                    b256!("0000000000000000000000000000000000000000000000000000000000000002"),
                    b256!("0000000000000000000000001111111111111111111111111111111111111111"),
                    b256!("00000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
                    b256!("00000000000000000bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"),
                    b256!("00000000000000000ccccccccccccccccccccccccccccccccccccccccccccccc"),
                ],
                &hex!(
                    "
                    0000000000000000000000000000000000000000000000000000000000000003
                    0000000000000000000000002222222222222222222222222222222222222222
                "
                ),
                false,
            )
            .unwrap();

        assert_eq!(
            result.body,
            [
                DynSolValue::Int(
                    Signed::from_be_bytes(hex!(
                        "0000000000000000000000000000000000000000000000000000000000000003"
                    )),
                    256
                ),
                DynSolValue::Address(address!("2222222222222222222222222222222222222222")),
            ]
        );
        assert_eq!(
            result.indexed,
            [
                DynSolValue::Int(
                    Signed::from_be_bytes(hex!(
                        "0000000000000000000000000000000000000000000000000000000000000002"
                    )),
                    256
                ),
                DynSolValue::Address(address!("1111111111111111111111111111111111111111")),
                DynSolValue::FixedBytes(
                    b256!("00000000000000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"),
                    32
                ),
            ]
        )
    }

    #[test]
    fn parse_log_whole() {
        let correct_event = Event {
            name: "Test".into(),
            inputs: vec![
                EventParam { ty: "(address,address)".into(), indexed: false, ..Default::default() },
                EventParam { ty: "address".into(), indexed: true, ..Default::default() },
            ],
            anonymous: false,
        };
        // swap indexed params
        let mut wrong_event = correct_event.clone();
        wrong_event.inputs[0].indexed = true;
        wrong_event.inputs[1].indexed = false;

        let log = LogData::new_unchecked(
            vec![
                b256!("cf74b4e62f836eeedcd6f92120ffb5afea90e6fa490d36f8b81075e2a7de0cf7"),
                b256!("0000000000000000000000000000000000000000000000000000000000012321"),
            ],
            bytes!(
                "
			0000000000000000000000000000000000000000000000000000000000012345
			0000000000000000000000000000000000000000000000000000000000054321
			"
            ),
        );

        wrong_event.decode_log(&log, false).unwrap();
        // TODO: How do we verify here?
        // wrong_event.decode_log_object(&log, true).unwrap_err();
        correct_event.decode_log(&log, false).unwrap();
        correct_event.decode_log(&log, true).unwrap();
    }
}