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
#![allow(dead_code)]
#![allow(stable_features)]
#![recursion_limit="1024"]
extern crate proc_macro;
#[macro_use]
extern crate syn;
#[macro_use]
extern crate quote;
mod gen;
mod gen_arg;
mod gen_event;
mod gen_finish;
mod gen_payable;
use gen::*;
fn wasm32_mode() -> bool {
let debug_mode = cfg!(debug_assertions);
!debug_mode
}
#[proc_macro_attribute]
pub fn contract(
_args: proc_macro::TokenStream,
input: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
let proc_input = &parse_macro_input!(input as syn::ItemTrait);
let contract = Contract::new(proc_input);
let contract_struct = contract.struct_name.clone();
let trait_name = contract.trait_name.clone();
let method_impls = contract.extract_method_impls();
let call_methods = contract.generate_call_methods();
let event_defs = contract.generate_event_defs();
let event_impls = contract.generate_event_impls();
let endpoints = contract.generate_endpoints();
let function_selector_body = contract.generate_function_selector_body();
let bi_where = quote! {
where
BigInt: BigIntApi + 'static,
BigUint: BigUintApi<BigInt> + 'static,
for<'b> BigInt: AddAssign<&'b BigInt>,
for<'b> BigInt: SubAssign<&'b BigInt>,
for<'b> BigInt: MulAssign<&'b BigInt>,
};
let api_where = quote! {
#bi_where
T: ContractHookApi<BigInt> + ContractIOApi<BigInt, BigUint> + Clone + 'static
};
let main_definition = quote! {
use elrond_wasm;
use elrond_wasm::Address;
use elrond_wasm::StorageKey;
use elrond_wasm::ErrorMessage;
use elrond_wasm::ContractHookApi;
use elrond_wasm::ContractIOApi;
use elrond_wasm::BigIntApi;
use elrond_wasm::BigUintApi;
use elrond_wasm_node::ArwenBigInt;
use elrond_wasm_node::ArwenBigUint;
use elrond_wasm_node::*;
use core::ops::{AddAssign, SubAssign, MulAssign};
pub trait #trait_name<BigInt, BigUint>: ContractHookApi<BigInt> + Sized
#bi_where
{
#(#method_impls)*
#(#event_defs)*
}
pub struct #contract_struct<T, BigInt, BigUint>
#api_where
{
api: T,
_phantom1: BigInt,
_phantom2: BigUint,
}
impl <T, BigInt, BigUint> #contract_struct<T, BigInt, BigUint>
where
BigInt: BigIntApi + 'static,
BigUint: BigUintApi<BigInt> + 'static,
for<'b> BigInt: AddAssign<&'b BigInt>,
for<'b> BigInt: SubAssign<&'b BigInt>,
for<'b> BigInt: MulAssign<&'b BigInt>,
T: ContractHookApi<BigInt> + ContractIOApi<BigInt, BigUint> + Clone + 'static
{
pub fn new(api: T) -> Self {
#contract_struct {
api: api,
_phantom1: BigInt::phantom(),
_phantom2: BigUint::phantom(),
}
}
}
impl <T, BigInt, BigUint> ContractHookApi<BigInt> for #contract_struct<T, BigInt, BigUint>
#api_where
{
#[inline]
fn get_owner(&self) -> Address {
self.api.get_owner()
}
#[inline]
fn get_caller(&self) -> Address {
self.api.get_caller()
}
#[inline]
fn storage_store(&self, key: &StorageKey, value: &Vec<u8>) {
self.api.storage_store(key, value);
}
#[inline]
fn storage_load(&self, key: &StorageKey) -> Vec<u8> {
self.api.storage_load(key)
}
#[inline]
fn storage_store_bytes32(&self, key: &StorageKey, value: &[u8; 32]) {
self.api.storage_store_bytes32(key, value);
}
#[inline]
fn storage_load_bytes32(&self, key: &StorageKey) -> [u8; 32] {
self.api.storage_load_bytes32(key)
}
#[inline]
fn storage_store_big_int(&self, key: &StorageKey, value: &BigInt) {
self.api.storage_store_big_int(key, value);
}
#[inline]
fn storage_load_big_int(&self, key: &StorageKey) -> BigInt {
self.api.storage_load_big_int(key)
}
#[inline]
fn get_call_value_big_int(&self) -> BigInt {
self.api.get_call_value_big_int()
}
#[inline]
fn send_tx(&self, to: &Address, amount: &BigInt, message: &str) {
self.api.send_tx(to, amount, message);
}
#[inline]
fn get_gas_left(&self) -> i64 {
self.api.get_gas_left()
}
#[inline]
fn sha256(&self, data: &Vec<u8>) -> [u8; 32] {
self.api.sha256(data)
}
#[inline]
fn keccak256(&self, data: &Vec<u8>) -> [u8; 32] {
self.api.keccak256(data)
}
}
impl <T, BigInt, BigUint> #trait_name<BigInt, BigUint> for #contract_struct<T, BigInt, BigUint>
#api_where
{
#(#event_impls)*
}
impl <T, BigInt, BigUint> #contract_struct<T, BigInt, BigUint>
#api_where
{
#(#call_methods)*
}
};
if wasm32_mode() {
proc_macro::TokenStream::from(quote! {
#main_definition
fn new_arwen_instance() -> #contract_struct<ArwenApiImpl, ArwenBigInt, ArwenBigUint> {
#contract_struct {
api: ArwenApiImpl{},
_phantom1: ArwenBigInt::phantom(),
_phantom2: ArwenBigUint::phantom(),
}
}
#(#endpoints)*
})
} else {
proc_macro::TokenStream::from(quote! {
#main_definition
use elrond_wasm::CallableContract;
impl <T, BigInt, BigUint> CallableContract for #contract_struct<T, BigInt, BigUint>
#api_where
{
fn call(&self, fn_name: &'static str) {
#function_selector_body
}
fn clone_contract(&self) -> Box<dyn CallableContract> {
Box::new(#contract_struct {
api: self.api.clone(),
_phantom1: BigInt::phantom(),
_phantom2: BigUint::phantom(),
})
}
}
})
}
}