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
use ethabi;
use proc_macro2::TokenStream;
use quote::quote;
use super::{
from_template_param, get_template_names, input_names, rust_type, template_param_type, to_ethabi_param_vec, to_token,
};
pub struct Constructor {
inputs_declarations: Vec<TokenStream>,
inputs_definitions: Vec<TokenStream>,
tokenize: Vec<TokenStream>,
recreate_inputs: TokenStream,
}
impl<'a> From<&'a ethabi::Constructor> for Constructor {
fn from(c: &'a ethabi::Constructor) -> Self {
let input_names = input_names(&c.inputs);
let inputs_declarations =
c.inputs.iter().enumerate().map(|(index, param)| template_param_type(¶m.kind, index)).collect();
let kinds: Vec<_> = c.inputs.iter().map(|param| rust_type(¶m.kind)).collect();
let template_names: Vec<_> = get_template_names(&kinds);
let inputs_definitions = input_names
.iter()
.zip(template_names.iter())
.map(|(param_name, template_name)| quote! { #param_name: #template_name });
let inputs_definitions = Some(quote! { code: ethabi::Bytes }).into_iter().chain(inputs_definitions).collect();
let tokenize: Vec<_> = input_names
.iter()
.zip(c.inputs.iter())
.map(|(param_name, param)| to_token(&from_template_param(¶m.kind, ¶m_name), ¶m.kind))
.collect();
Constructor {
inputs_declarations,
inputs_definitions,
tokenize,
recreate_inputs: to_ethabi_param_vec(&c.inputs),
}
}
}
impl Constructor {
pub fn generate(&self) -> TokenStream {
let declarations = &self.inputs_declarations;
let definitions = &self.inputs_definitions;
let tokenize = &self.tokenize;
let recreate_inputs = &self.recreate_inputs;
quote! {
pub fn constructor<#(#declarations),*>(#(#definitions),*) -> ethabi::Bytes {
let c = ethabi::Constructor {
inputs: #recreate_inputs,
};
let tokens = vec![#(#tokenize),*];
c.encode_input(code, &tokens).expect(INTERNAL_ERR)
}
}
}
}
#[cfg(test)]
mod tests {
use super::Constructor;
use ethabi;
use quote::quote;
#[test]
fn test_no_params() {
let ethabi_constructor = ethabi::Constructor { inputs: vec![] };
let c = Constructor::from(ðabi_constructor);
let expected = quote! {
pub fn constructor<>(code: ethabi::Bytes) -> ethabi::Bytes {
let c = ethabi::Constructor {
inputs: vec![],
};
let tokens = vec![];
c.encode_input(code, &tokens).expect(INTERNAL_ERR)
}
};
assert_eq!(expected.to_string(), c.generate().to_string());
}
#[test]
fn test_one_param() {
let ethabi_constructor = ethabi::Constructor {
inputs: vec![ethabi::Param { name: "foo".into(), kind: ethabi::ParamType::Uint(256) }],
};
let c = Constructor::from(ðabi_constructor);
let expected = quote! {
pub fn constructor<T0: Into<ethabi::Uint> >(code: ethabi::Bytes, foo: T0) -> ethabi::Bytes {
let c = ethabi::Constructor {
inputs: vec![ethabi::Param {
name: "foo".to_owned(),
kind: ethabi::ParamType::Uint(256usize)
}],
};
let tokens = vec![ethabi::Token::Uint(foo.into())];
c.encode_input(code, &tokens).expect(INTERNAL_ERR)
}
};
assert_eq!(expected.to_string(), c.generate().to_string());
}
}