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
use crate::{
Result, SolType, Word,
abi::{
AbiDecoderConfig,
token::{Token, TokenSeq, WordToken},
},
};
use alloc::vec::Vec;
use alloy_primitives::{B256, FixedBytes, Log, LogData};
mod topic;
pub use topic::EventTopic;
mod topic_list;
pub use topic_list::TopicList;
/// Solidity event.
///
/// # Implementer's Guide
///
/// It should not be necessary to implement this trait manually. Instead, use
/// the [`sol!`](crate::sol!) procedural macro to parse Solidity syntax into
/// types that implement this trait.
pub trait SolEvent: Sized {
/// The underlying tuple type which represents this event's non-indexed
/// parameters. These parameters are ABI encoded and included in the log
/// body.
///
/// If this event has no non-indexed parameters, this will be the unit type
/// `()`.
type DataTuple<'a>: SolType<Token<'a> = Self::DataToken<'a>>;
/// The [`TokenSeq`] type corresponding to the tuple.
type DataToken<'a>: TokenSeq<'a>;
/// The underlying tuple type which represents this event's topics.
///
/// These are ABI encoded and included in the log struct returned by the
/// RPC node.
///
/// See the [`TopicList`] trait for more details.
type TopicList: TopicList;
/// The event's ABI signature.
///
/// For anonymous events, this is unused, but is still present.
const SIGNATURE: &'static str;
/// The event's ABI signature hash, or selector: `keccak256(SIGNATURE)`
///
/// For non-anonymous events, this will be the first topic (`topic0`).
/// For anonymous events, this is unused, but is still present.
#[doc(alias = "SELECTOR")]
const SIGNATURE_HASH: FixedBytes<32>;
/// Whether the event is anonymous.
const ANONYMOUS: bool;
/// Convert decoded rust data to the event type.
///
/// Does not check that `topics[0]` is the correct hash.
/// Use [`new_checked`](Self::new_checked) instead.
fn new(
topics: <Self::TopicList as SolType>::RustType,
data: <Self::DataTuple<'_> as SolType>::RustType,
) -> Self;
/// Convert decoded rust data to the event type.
///
/// Checks that `topics[0]` is the correct hash.
#[inline]
fn new_checked(
topics: <Self::TopicList as SolType>::RustType,
data: <Self::DataTuple<'_> as SolType>::RustType,
) -> Result<Self> {
Self::check_signature(&topics).map(|()| Self::new(topics, data))
}
/// Check that the event's signature matches the given topics.
#[inline]
fn check_signature(topics: &<Self::TopicList as SolType>::RustType) -> Result<()> {
// Overridden for non-anonymous events in `sol!`.
let _ = topics;
Ok(())
}
/// Tokenize the event's non-indexed parameters.
fn tokenize_body(&self) -> Self::DataToken<'_>;
// TODO: avoid clones here
/// The event's topics.
fn topics(&self) -> <Self::TopicList as SolType>::RustType;
/// The size of the ABI-encoded dynamic data in bytes.
#[inline]
fn abi_encoded_size(&self) -> usize {
if let Some(size) = <Self::DataTuple<'_> as SolType>::ENCODED_SIZE {
return size;
}
self.tokenize_body().total_words() * Word::len_bytes()
}
/// ABI-encode the dynamic data of this event into the given buffer.
#[inline]
fn encode_data_to(&self, out: &mut Vec<u8>) {
out.reserve(self.abi_encoded_size());
out.extend(crate::abi::encode_sequence(&self.tokenize_body()));
}
/// ABI-encode the dynamic data of this event.
#[inline]
fn encode_data(&self) -> Vec<u8> {
let mut out = Vec::new();
self.encode_data_to(&mut out);
out
}
/// Encode the topics of this event into the given buffer.
///
/// # Errors
///
/// This method should return an error only if the buffer is too small.
fn encode_topics_raw(&self, out: &mut [WordToken]) -> Result<()>;
/// Encode the topics of this event.
///
/// The returned vector will have length `Self::TopicList::COUNT`.
#[inline]
fn encode_topics(&self) -> Vec<WordToken> {
let mut out = vec![WordToken(B256::ZERO); Self::TopicList::COUNT];
self.encode_topics_raw(&mut out).unwrap();
out
}
/// Encode the topics of this event into a fixed-size array.
///
/// This method will not compile if `LEN` is not equal to `Self::TopicList::COUNT`.
#[inline]
fn encode_topics_array<const LEN: usize>(&self) -> [WordToken; LEN] {
const { assert!(LEN == Self::TopicList::COUNT, "topic list length mismatch") };
let mut out = [WordToken(B256::ZERO); LEN];
self.encode_topics_raw(&mut out).unwrap();
out
}
/// Encode this event to a [`LogData`].
fn encode_log_data(&self) -> LogData {
LogData::new_unchecked(
self.encode_topics().into_iter().map(Into::into).collect(),
self.encode_data().into(),
)
}
/// Transform ca [`Log`] containing this event into a [`Log`] containing
/// [`LogData`].
fn encode_log(log: &Log<Self>) -> Log<LogData> {
Log { address: log.address, data: log.data.encode_log_data() }
}
/// Decode the topics of this event from the given data.
///
/// This requires the input to contain exactly [`TopicList::COUNT`] topics.
#[inline]
fn decode_topics<I, D>(topics: I) -> Result<<Self::TopicList as SolType>::RustType>
where
I: IntoIterator<Item = D>,
D: Into<WordToken>,
{
<Self::TopicList as TopicList>::detokenize(topics)
}
/// ABI-decodes the dynamic data of this event from the given buffer.
#[inline]
fn abi_decode_data<'a>(data: &'a [u8]) -> Result<<Self::DataTuple<'a> as SolType>::RustType> {
<Self::DataTuple<'a> as SolType>::abi_decode_sequence(data)
}
/// ABI-decodes the dynamic data with a custom decoder configuration.
#[inline]
fn abi_decode_data_with_config<'a>(
data: &'a [u8],
config: AbiDecoderConfig,
) -> Result<<Self::DataTuple<'a> as SolType>::RustType> {
<Self::DataTuple<'a> as SolType>::abi_decode_sequence_with_config(data, config)
}
/// ABI-decodes the dynamic data of this event from the given buffer, with validation.
///
/// This is the same as [`abi_decode_data`](Self::abi_decode_data), but performs
/// validation checks on the decoded data tuple.
#[inline]
// TODO: Deprecate in favor of a validating decoder configuration.
// #[deprecated(note = "use a validating decoder configuration")]
fn abi_decode_data_validate<'a>(
data: &'a [u8],
) -> Result<<Self::DataTuple<'a> as SolType>::RustType> {
Self::abi_decode_data_with_config(data, AbiDecoderConfig::new().validate(true))
}
/// Decode the event from the given log info.
fn decode_raw_log<I, D>(topics: I, data: &[u8]) -> Result<Self>
where
I: IntoIterator<Item = D>,
D: Into<WordToken>,
{
let topics = Self::decode_topics(topics)?;
// Check signature before decoding the data.
Self::check_signature(&topics)?;
let body = Self::abi_decode_data(data)?;
Ok(Self::new(topics, body))
}
/// Decodes the event from the given log info with a custom decoder configuration.
fn decode_raw_log_with_config<I, D>(
topics: I,
data: &[u8],
config: AbiDecoderConfig,
) -> Result<Self>
where
I: IntoIterator<Item = D>,
D: Into<WordToken>,
{
let topics = Self::decode_topics(topics)?;
Self::check_signature(&topics)?;
let body = Self::abi_decode_data_with_config(data, config)?;
Ok(Self::new(topics, body))
}
/// Decode the event from the given log info, with validation.
///
/// This is the same as [`decode_raw_log`](Self::decode_raw_log), but performs
/// validation checks on the decoded topics and data.
// TODO: Deprecate in favor of a validating decoder configuration.
// #[deprecated(note = "use a validating decoder configuration")]
fn decode_raw_log_validate<I, D>(topics: I, data: &[u8]) -> Result<Self>
where
I: IntoIterator<Item = D>,
D: Into<WordToken>,
{
Self::decode_raw_log_with_config(topics, data, AbiDecoderConfig::new().validate(true))
}
/// Decode the event from the given log object.
fn decode_log_data(log: &LogData) -> Result<Self> {
Self::decode_raw_log(log.topics(), &log.data)
}
/// Decodes the event from the given log object with a custom decoder configuration.
fn decode_log_data_with_config(log: &LogData, config: AbiDecoderConfig) -> Result<Self> {
Self::decode_raw_log_with_config(log.topics(), &log.data, config)
}
/// Decode the event from the given log object with validation.
// TODO: Deprecate in favor of a validating decoder configuration.
// #[deprecated(note = "use a validating decoder configuration")]
fn decode_log_data_validate(log: &LogData) -> Result<Self> {
Self::decode_log_data_with_config(log, AbiDecoderConfig::new().validate(true))
}
/// Decode the event from the given log object.
fn decode_log(log: &Log) -> Result<Log<Self>> {
Self::decode_log_data(&log.data).map(|data| Log { address: log.address, data })
}
/// Decodes the event from the given log object with a custom decoder configuration.
fn decode_log_with_config(log: &Log, config: AbiDecoderConfig) -> Result<Log<Self>> {
Self::decode_log_data_with_config(&log.data, config)
.map(|data| Log { address: log.address, data })
}
/// Decode the event from the given log object with validation.
// TODO: Deprecate in favor of a validating decoder configuration.
// #[deprecated(note = "use a validating decoder configuration")]
fn decode_log_validate(log: &Log) -> Result<Log<Self>> {
Self::decode_log_with_config(log, AbiDecoderConfig::new().validate(true))
}
}