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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
use heck::{CamelCase, SnakeCase};
use proc_macro2::{Span, TokenStream};
use quote::quote;
use {ethabi, syn};
use super::{from_token, get_template_names, rust_type, to_syntax_string, to_token};
pub struct Event {
name: String,
log_fields: Vec<TokenStream>,
recreate_inputs_quote: TokenStream,
log_init: Vec<TokenStream>,
wildcard_filter_params: Vec<TokenStream>,
filter_declarations: Vec<TokenStream>,
filter_definitions: Vec<TokenStream>,
filter_init: Vec<TokenStream>,
anonymous: bool,
}
impl<'a> From<&'a ethabi::Event> for Event {
fn from(e: &'a ethabi::Event) -> Self {
let names: Vec<_> = e
.inputs
.iter()
.enumerate()
.map(|(index, param)| {
if param.name.is_empty() {
if param.indexed {
syn::Ident::new(&format!("topic{}", index), Span::call_site())
} else {
syn::Ident::new(&format!("param{}", index), Span::call_site())
}
} else {
syn::Ident::new(¶m.name.to_snake_case(), Span::call_site())
}
})
.collect();
let kinds: Vec<_> = e.inputs.iter().map(|param| rust_type(¶m.kind)).collect();
let log_fields =
names.iter().zip(kinds.iter()).map(|(param_name, kind)| quote! { pub #param_name: #kind }).collect();
let log_iter = quote! { log.next().expect(INTERNAL_ERR).value };
let to_log: Vec<_> = e.inputs.iter().map(|param| from_token(¶m.kind, &log_iter)).collect();
let log_init =
names.iter().zip(to_log.iter()).map(|(param_name, convert)| quote! { #param_name: #convert }).collect();
let topic_kinds: Vec<_> =
e.inputs.iter().filter(|param| param.indexed).map(|param| rust_type(¶m.kind)).collect();
let topic_names: Vec<_> = e
.inputs
.iter()
.enumerate()
.filter(|&(_, param)| param.indexed)
.map(|(index, param)| {
if param.name.is_empty() {
syn::Ident::new(&format!("topic{}", index), Span::call_site())
} else {
syn::Ident::new(¶m.name.to_snake_case(), Span::call_site())
}
})
.collect();
let template_names: Vec<_> = get_template_names(&topic_kinds);
let filter_declarations: Vec<_> = topic_kinds
.iter()
.zip(template_names.iter())
.map(|(kind, template_name)| quote! { #template_name: Into<ethabi::Topic<#kind>> })
.collect();
let filter_definitions: Vec<_> = topic_names
.iter()
.zip(template_names.iter())
.map(|(param_name, template_name)| quote! { #param_name: #template_name })
.collect();
let wildcard_filter_params: Vec<_> = filter_definitions.iter().map(|_| quote! { ethabi::Topic::Any }).collect();
let filter_init: Vec<_> = topic_names
.iter()
.zip(e.inputs.iter().filter(|p| p.indexed))
.enumerate()
.take(3)
.map(|(index, (param_name, param))| {
let topic = syn::Ident::new(&format!("topic{}", index), Span::call_site());
let i = quote! { i };
let to_token = to_token(&i, ¶m.kind);
quote! { #topic: #param_name.into().map(|#i| #to_token), }
})
.collect();
let event_inputs = &e
.inputs
.iter()
.map(|x| {
let name = &x.name;
let kind = to_syntax_string(&x.kind);
let indexed = x.indexed;
quote! {
ethabi::EventParam {
name: #name.to_owned(),
kind: #kind,
indexed: #indexed
}
}
})
.collect::<Vec<_>>();
let recreate_inputs_quote = quote! { vec![ #(#event_inputs),* ] };
Event {
name: e.name.clone(),
log_fields,
recreate_inputs_quote,
log_init,
anonymous: e.anonymous,
wildcard_filter_params,
filter_declarations,
filter_definitions,
filter_init,
}
}
}
impl Event {
pub fn generate_log(&self) -> TokenStream {
let name = syn::Ident::new(&self.name.to_camel_case(), Span::call_site());
let log_fields = &self.log_fields;
quote! {
#[derive(Debug, Clone, PartialEq)]
pub struct #name {
#(#log_fields),*
}
}
}
pub fn generate_event(&self) -> TokenStream {
let name_as_string = &self.name.to_camel_case();
let name = syn::Ident::new(&self.name.to_snake_case(), Span::call_site());
let camel_name = syn::Ident::new(&self.name.to_camel_case(), Span::call_site());
let recreate_inputs_quote = &self.recreate_inputs_quote;
let anonymous = &self.anonymous;
let log_init = &self.log_init;
let filter_init = &self.filter_init;
let filter_declarations = &self.filter_declarations;
let filter_definitions = &self.filter_definitions;
let wildcard_filter_params = &self.wildcard_filter_params;
quote! {
pub mod #name {
use ethabi;
use super::INTERNAL_ERR;
pub fn event() -> ethabi::Event {
ethabi::Event {
name: #name_as_string.into(),
inputs: #recreate_inputs_quote,
anonymous: #anonymous,
}
}
pub fn filter<#(#filter_declarations),*>(#(#filter_definitions),*) -> ethabi::TopicFilter {
let raw = ethabi::RawTopicFilter {
#(#filter_init)*
..Default::default()
};
let e = event();
e.filter(raw).expect(INTERNAL_ERR)
}
pub fn wildcard_filter() -> ethabi::TopicFilter {
filter(#(#wildcard_filter_params),*)
}
pub fn parse_log(log: ethabi::RawLog) -> ethabi::Result<super::super::logs::#camel_name> {
let e = event();
let mut log = e.parse_log(log)?.params.into_iter();
let result = super::super::logs::#camel_name {
#(#log_init),*
};
Ok(result)
}
}
}
}
}
#[cfg(test)]
mod tests {
use super::Event;
use ethabi;
use quote::quote;
#[test]
fn test_empty_log() {
let ethabi_event = ethabi::Event { name: "hello".into(), inputs: vec![], anonymous: false };
let e = Event::from(ðabi_event);
let expected = quote! {
#[derive(Debug, Clone, PartialEq)]
pub struct Hello {}
};
assert_eq!(expected.to_string(), e.generate_log().to_string());
}
#[test]
fn test_empty_event() {
let ethabi_event = ethabi::Event { name: "hello".into(), inputs: vec![], anonymous: false };
let e = Event::from(ðabi_event);
let expected = quote! {
pub mod hello {
use ethabi;
use super::INTERNAL_ERR;
pub fn event() -> ethabi::Event {
ethabi::Event {
name: "Hello".into(),
inputs: vec![],
anonymous: false,
}
}
pub fn filter<>() -> ethabi::TopicFilter {
let raw = ethabi::RawTopicFilter {
..Default::default()
};
let e = event();
e.filter(raw).expect(INTERNAL_ERR)
}
pub fn wildcard_filter() -> ethabi::TopicFilter {
filter()
}
pub fn parse_log(log: ethabi::RawLog) -> ethabi::Result<super::super::logs::Hello> {
let e = event();
let mut log = e.parse_log(log)?.params.into_iter();
let result = super::super::logs::Hello {};
Ok(result)
}
}
};
assert_eq!(expected.to_string(), e.generate_event().to_string());
}
#[test]
fn test_event_with_one_input() {
let ethabi_event = ethabi::Event {
name: "one".into(),
inputs: vec![ethabi::EventParam { name: "foo".into(), kind: ethabi::ParamType::Address, indexed: true }],
anonymous: false,
};
let e = Event::from(ðabi_event);
let expected = quote! {
pub mod one {
use ethabi;
use super::INTERNAL_ERR;
pub fn event() -> ethabi::Event {
ethabi::Event {
name: "One".into(),
inputs: vec![ethabi::EventParam {
name: "foo".to_owned(),
kind: ethabi::ParamType::Address,
indexed: true
}],
anonymous: false,
}
}
pub fn filter<T0: Into<ethabi::Topic<ethabi::Address>>>(foo: T0) -> ethabi::TopicFilter {
let raw = ethabi::RawTopicFilter {
topic0: foo.into().map(|i| ethabi::Token::Address(i)),
..Default::default()
};
let e = event();
e.filter(raw).expect(INTERNAL_ERR)
}
pub fn wildcard_filter() -> ethabi::TopicFilter {
filter(ethabi::Topic::Any)
}
pub fn parse_log(log: ethabi::RawLog) -> ethabi::Result<super::super::logs::One> {
let e = event();
let mut log = e.parse_log(log)?.params.into_iter();
let result = super::super::logs::One {
foo: log.next().expect(INTERNAL_ERR).value.to_address().expect(INTERNAL_ERR)
};
Ok(result)
}
}
};
assert_eq!(expected.to_string(), e.generate_event().to_string());
}
#[test]
fn test_log_with_one_field() {
let ethabi_event = ethabi::Event {
name: "one".into(),
inputs: vec![ethabi::EventParam { name: "foo".into(), kind: ethabi::ParamType::Address, indexed: false }],
anonymous: false,
};
let e = Event::from(ðabi_event);
let expected = quote! {
#[derive(Debug, Clone, PartialEq)]
pub struct One {
pub foo: ethabi::Address
}
};
assert_eq!(expected.to_string(), e.generate_log().to_string());
}
#[test]
fn test_log_with_multiple_field() {
let ethabi_event = ethabi::Event {
name: "many".into(),
inputs: vec![
ethabi::EventParam { name: "foo".into(), kind: ethabi::ParamType::Address, indexed: false },
ethabi::EventParam {
name: "bar".into(),
kind: ethabi::ParamType::Array(Box::new(ethabi::ParamType::String)),
indexed: false,
},
ethabi::EventParam { name: "xyz".into(), kind: ethabi::ParamType::Uint(256), indexed: false },
],
anonymous: false,
};
let e = Event::from(ðabi_event);
let expected = quote! {
#[derive(Debug, Clone, PartialEq)]
pub struct Many {
pub foo: ethabi::Address,
pub bar: Vec<String>,
pub xyz: ethabi::Uint
}
};
assert_eq!(expected.to_string(), e.generate_log().to_string());
}
}