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
use quote::ToTokens;
pub mod mock {
use crate::ir::{ModuleImplIR, ModuleStructIR, TypeIR};
use quote::quote;
pub fn module_impl() -> ModuleImplIR {
let module = quote! {
impl Erc20 {
/// Initializes the contract with the given parameters.
pub fn init(&mut self, total_supply: Option<U256>) {
if let Some(total_supply) = total_supply {
self.total_supply.set(total_supply);
self.balances.set(self.env().caller(), total_supply);
}
}
/// Upgrades the contract with the given parameters.
pub fn upgrade(&mut self, total_supply: Option<U256>) {
if let Some(total_supply) = total_supply {
self.total_supply.set(total_supply);
self.balances.set(self.env().caller(), total_supply);
}
}
/// Returns the total supply of the token.
pub fn total_supply(&self) -> U256 {
self.total_supply.get_or_default()
}
/// Pay to mint.
#[odra(payable)]
pub fn pay_to_mint(&mut self) {
let attached_value = self.env().attached_value();
self.total_supply
.set(self.total_supply() + U256::from(attached_value.as_u64()));
}
/// Approve.
#[odra(non_reentrant)]
pub fn approve(&mut self, to: &Address, amount: &U256, msg: Maybe<String>) {
self.env.emit_event(Approval {
owner: self.env.caller(),
spender: to,
value: amount
});
}
/// Airdrops the given amount to the given addresses.
pub fn airdrop(to: &[Address], amount: &U256) {
}
fn private_function() {
}
}
};
let attr = quote!();
ModuleImplIR::try_from((&attr, &module)).unwrap()
}
pub fn module_factory_on() -> ModuleImplIR {
let module = quote! {
impl Erc20 {
/// Returns the total supply of the token.
pub fn total_supply(&self) -> U256 {
self.total_supply.get_or_default()
}
/// Pay to mint.
#[odra(payable)]
pub fn pay_to_mint(&mut self) {
let attached_value = self.env().attached_value();
self.total_supply
.set(self.total_supply() + U256::from(attached_value.as_u64()));
}
}
};
let attr = quote!(factory = on);
ModuleImplIR::try_from((&attr, &module)).unwrap()
}
pub fn module_factory_impl() -> ModuleImplIR {
let module = quote! {
impl Erc20Factory {
pub fn init(&mut self, value: u32) {
self.value.set(value);
}
/// Returns the total supply of the token.
pub fn total_supply(&self) -> U256 {
self.total_supply.get_or_default()
}
/// Pay to mint.
#[odra(payable)]
pub fn pay_to_mint(&mut self) {
let attached_value = self.env().attached_value();
self.total_supply
.set(self.total_supply() + U256::from(attached_value.as_u64()));
}
/// Approve.
#[odra(non_reentrant)]
pub fn approve(&mut self, to: &Address, amount: &U256, msg: Maybe<String>) {
self.env.emit_event(Approval {
owner: self.env.caller(),
spender: to,
value: amount
});
}
}
};
let attr = quote!();
ModuleImplIR::try_from((&attr, &module)).unwrap()
}
pub fn module_trait_impl() -> ModuleImplIR {
let module = quote! {
impl IErc20 for Erc20 {
fn total_supply(&self) -> U256 {
self.total_supply.get_or_default()
}
#[odra(payable)]
fn pay_to_mint(&mut self) {
let attached_value = self.env().attached_value();
self.total_supply
.set(self.total_supply() + U256::from(attached_value.as_u64()));
}
}
};
let attr = quote!();
ModuleImplIR::try_from((&attr, &module)).unwrap()
}
pub fn module_delegation() -> ModuleImplIR {
let module = quote! {
impl Erc20 {
/// Returns the total supply of the token.
pub fn total_supply(&self) -> U256 {
self.total_supply.get_or_default()
}
delegate! {
to self.ownable {
/// Returns the owner of the contract.
fn get_owner(&self) -> Address;
/// Sets the owner of the contract.
fn set_owner(&mut self, new_owner: Address);
}
to self.metadata {
/// Returns the name of the token.
fn name(&self) -> String;
fn symbol(&self) -> String;
}
}
}
};
let attr = quote!();
ModuleImplIR::try_from((&attr, &module)).unwrap()
}
pub fn module_invalid_delegation() -> ModuleImplIR {
let module = quote! {
impl Erc20 {
/// Returns the total supply of the token.
pub fn total_supply(&self) -> U256 {
self.total_supply.get_or_default()
}
delegate! {
to self.ownable {
/// Returns the owner of the contract.
pub fn get_owner(&self) -> Address;
/// Sets the owner of the contract.
fn set_owner(&mut self, new_owner: Address);
}
}
}
};
let attr = quote!();
ModuleImplIR::try_from((&attr, &module)).unwrap()
}
pub fn module_definition() -> ModuleStructIR {
let module = quote!(
pub struct CounterPack {
counter0: SubModule<Counter>,
counter1: SubModule<Counter>,
counter2: SubModule<Counter>,
counters: Var<u32>,
counters_map: Mapping<u8, Counter>
}
);
let attr = quote!(
name = "MyCounterPack",
version = "0.1.0",
events = [OnTransfer, OnApprove],
errors = Erc20Errors
);
ModuleStructIR::try_from((&attr, &module)).unwrap()
}
pub fn factory_module_definition() -> ModuleStructIR {
let module = quote!(
pub struct CounterPack {
counter0: SubModule<Counter>,
counters: Var<u32>,
counters_map: Mapping<u8, Counter>
}
);
let attr = quote!(factory = on);
ModuleStructIR::try_from((&attr, &module)).unwrap()
}
pub fn invalid_module_definition() -> ModuleStructIR {
let module = quote!(
pub struct Module {
v: Var<u32>,
v1: Var<u32>,
v2: Var<u32>,
v3: Var<u32>,
v4: Var<u32>,
v5: Var<u32>,
v6: Var<u32>,
v7: Var<u32>,
v8: Var<u32>,
v9: Var<u32>,
v10: Var<u32>,
v11: Var<u32>,
v12: Var<u32>,
v13: Var<u32>,
v14: Var<u32>,
v15: Var<u32>
}
);
let attr = quote!();
ModuleStructIR::try_from((&attr, &module)).unwrap()
}
pub fn empty_module_definition() -> ModuleStructIR {
let module = quote!(
pub struct CounterPack;
);
let attr = quote!();
ModuleStructIR::try_from((&attr, &module)).unwrap()
}
pub fn custom_struct() -> TypeIR {
let ty = quote!(
struct MyType {
a: String,
b: u32
}
);
TypeIR::try_from(&ty).unwrap()
}
pub fn custom_enum() -> TypeIR {
let ty = quote!(
enum MyType {
/// Description of A
A = 10,
/// Description of B
B
}
);
TypeIR::try_from(&ty).unwrap()
}
pub fn custom_complex_enum() -> TypeIR {
let ty = quote!(
enum MyType {
/// Description of A
A { a: String, b: u32 },
/// Description of B
B(u32, String),
/// Description of C
C(),
/// Description of D
D {}
}
);
TypeIR::try_from(&ty).unwrap()
}
pub fn custom_union() -> TypeIR {
let ty = quote!(
union MyUnion {
f1: u32,
f2: f32,
}
);
TypeIR::try_from(&ty).unwrap()
}
pub fn ext_contract() -> ModuleImplIR {
let ext = quote!(
pub trait Token {
fn balance_of(&self, owner: Address) -> U256;
}
);
let attr = quote!();
ModuleImplIR::try_from((&attr, &ext)).unwrap()
}
}
#[track_caller]
pub fn assert_eq<A: ToTokens, B: ToTokens>(a: A, b: B) {
fn parse<T: ToTokens>(e: T) -> String {
let e = e.to_token_stream().to_string();
let e = syn::parse_file(&e).unwrap();
prettyplease::unparse(&e)
}
pretty_assertions::assert_eq!(parse(a), parse(b));
}