wevm/modules/
v0.rs

1use wevm_proc_macro::module;
2
3#[module(env0)]
4mod test {
5    // Asset
6    fn get_balance(
7        offset_asset_id: *const u8,
8        length_asset_id: usize,
9        offset_address: *const u8,
10        length_address: usize,
11    ) -> (i32, i64) {
12        |caller: Caller<Runtime>| {
13            env::asset::get_balance(
14                offset_asset_id,
15                length_asset_id,
16                offset_address,
17                length_address,
18                0,
19                1,
20                caller,
21            )
22        }
23    }
24
25    fn transfer(
26        offset_asset_id: *const u8,
27        length_asset_id: usize,
28        offset_recipient: *const u8,
29        length_recipient: usize,
30        amount: i64,
31    ) -> i32 {
32        |caller: Caller<Runtime>| {
33            env::asset::transfer(
34                offset_asset_id,
35                length_asset_id,
36                offset_recipient,
37                length_recipient,
38                0,
39                1,
40                amount,
41                caller,
42            )
43        }
44    }
45
46    fn issue(
47        offset_name: *const u8,
48        length_name: usize,
49        offset_description: *const u8,
50        length_description: usize,
51        quantity: i64,
52        decimals: i32,
53        is_reissuable: bool,
54    ) -> (i32, *const u8, usize) {
55        |caller: Caller<Runtime>| {
56            env::asset::issue(
57                offset_name,
58                length_name,
59                offset_description,
60                length_description,
61                quantity,
62                decimals as i64,
63                is_reissuable,
64                caller,
65            )
66        }
67    }
68
69    fn burn(offset_asset_id: *const u8, length_asset_id: usize, amount: i64) -> i32 {
70        |caller: Caller<Runtime>| env::asset::burn(offset_asset_id, length_asset_id, amount, caller)
71    }
72
73    fn reissue(
74        offset_asset_id: *const u8,
75        length_asset_id: usize,
76        amount: i64,
77        is_reissuable: bool,
78    ) -> i32 {
79        |caller: Caller<Runtime>| {
80            env::asset::reissue(
81                offset_asset_id,
82                length_asset_id,
83                amount,
84                is_reissuable,
85                caller,
86            )
87        }
88    }
89
90    // Block
91    fn get_block_timestamp() -> (i32, i64) {
92        |caller: Caller<Runtime>| {
93            env::block::get_block_field(env::Field::String("timestamp".to_string()), caller)
94        }
95    }
96
97    fn get_block_height() -> (i32, i64) {
98        |caller: Caller<Runtime>| {
99            env::block::get_block_field(env::Field::String("height".to_string()), caller)
100        }
101    }
102
103    // Call contract
104    fn call_arg_int(value: i64) {
105        |caller: Caller<Runtime>| env::call_contract::call_arg_int(value, caller)
106    }
107
108    fn call_arg_bool(value: bool) {
109        |caller: Caller<Runtime>| env::call_contract::call_arg_bool(value, caller)
110    }
111
112    fn call_arg_binary(offset_value: *const u8, length_value: usize) -> i32 {
113        |caller: Caller<Runtime>| {
114            env::call_contract::call_arg_binary(offset_value, length_value, caller)
115        }
116    }
117
118    fn call_arg_string(offset_value: *const u8, length_value: usize) -> i32 {
119        |caller: Caller<Runtime>| {
120            env::call_contract::call_arg_string(offset_value, length_value, caller)
121        }
122    }
123
124    fn call_payment(offset_asset_id: *const u8, length_asset_id: usize, amount: i64) -> i32 {
125        |caller: Caller<Runtime>| {
126            env::call_contract::call_payment(offset_asset_id, length_asset_id, amount, caller)
127        }
128    }
129
130    fn call_contract(
131        offset_contract_id: *const u8,
132        length_contract_id: usize,
133        offset_func_name: *const u8,
134        length_func_name: usize,
135    ) -> i32 {
136        |caller: Caller<Runtime>| {
137            env::call_contract::call_contract(
138                offset_contract_id,
139                length_contract_id,
140                offset_func_name,
141                length_func_name,
142                None,
143                None,
144                caller,
145            )
146        }
147    }
148
149    fn call_contract_params(
150        offset_contract_id: *const u8,
151        length_contract_id: usize,
152        offset_func_name: *const u8,
153        length_func_name: usize,
154        offset_params: *const u8,
155        length_params: usize,
156    ) -> i32 {
157        |caller: Caller<Runtime>| {
158            env::call_contract::call_contract(
159                offset_contract_id,
160                length_contract_id,
161                offset_func_name,
162                length_func_name,
163                Some(offset_params),
164                Some(length_params),
165                caller,
166            )
167        }
168    }
169
170    // Converts
171    fn parse_int(offset: *const u8, length: usize) -> (i32, i64) {
172        |caller: Caller<Runtime>| env::converts::parse_int(offset, length, caller)
173    }
174
175    fn parse_bool(offset_string: *const u8, length_string: usize) -> (i32, bool) {
176        |caller: Caller<Runtime>| env::converts::parse_bool(offset_string, length_string, caller)
177    }
178
179    fn to_bytes(value: i64) -> (i32, *const u8, usize) {
180        |caller: Caller<Runtime>| env::converts::to_bytes(value, caller)
181    }
182
183    fn to_int(offset: *const u8, length: usize) -> (i32, i64) {
184        |caller: Caller<Runtime>| env::converts::to_int(offset, length, caller)
185    }
186
187    fn to_string_bool(value: bool) -> (i32, *const u8, usize) {
188        |caller: Caller<Runtime>| env::converts::to_string(value != 0, caller)
189    }
190
191    fn to_string_int(value: i64) -> (i32, *const u8, usize) {
192        |caller: Caller<Runtime>| env::converts::to_string(value, caller)
193    }
194
195    // Crypto
196    fn fast_hash(offset_bytes: *const u8, length_bytes: usize) -> (i32, *const u8, usize) {
197        |caller: Caller<Runtime>| env::crypto::fast_hash(offset_bytes, length_bytes, caller)
198    }
199
200    fn secure_hash(offset_bytes: *const u8, length_bytes: usize) -> (i32, *const u8, usize) {
201        |caller: Caller<Runtime>| env::crypto::secure_hash(offset_bytes, length_bytes, caller)
202    }
203
204    fn blake2b256(offset_bytes: *const u8, length_bytes: usize) -> (i32, *const u8, usize) {
205        |caller: Caller<Runtime>| env::crypto::blake2b256(offset_bytes, length_bytes, caller)
206    }
207
208    fn keccak256(offset_bytes: *const u8, length_bytes: usize) -> (i32, *const u8, usize) {
209        |caller: Caller<Runtime>| env::crypto::keccak256(offset_bytes, length_bytes, caller)
210    }
211
212    fn sha256(offset_bytes: *const u8, length_bytes: usize) -> (i32, *const u8, usize) {
213        |caller: Caller<Runtime>| env::crypto::sha256(offset_bytes, length_bytes, caller)
214    }
215
216    fn sig_verify(
217        offset_message: *const u8,
218        length_message: usize,
219        offset_signature: *const u8,
220        length_signature: usize,
221        offset_public_key: *const u8,
222        length_public_key: usize,
223    ) -> (i32, bool) {
224        |caller: Caller<Runtime>| {
225            env::crypto::sig_verify(
226                offset_message,
227                length_message,
228                offset_signature,
229                length_signature,
230                offset_public_key,
231                length_public_key,
232                caller,
233            )
234        }
235    }
236
237    // Lease
238    fn lease_address(
239        offset_address: *const u8,
240        length_address: usize,
241        amount: i64,
242    ) -> (i32, *const u8, usize) {
243        |caller: Caller<Runtime>| {
244            env::lease::lease(offset_address, length_address, 1, amount, caller)
245        }
246    }
247
248    fn lease_alias(
249        offset_alias: *const u8,
250        length_alias: usize,
251        amount: i64,
252    ) -> (i32, *const u8, usize) {
253        |caller: Caller<Runtime>| env::lease::lease(offset_alias, length_alias, 2, amount, caller)
254    }
255
256    fn cancel_lease(offset_lease_id: *const u8, length_lease_id: usize) -> i32 {
257        |caller: Caller<Runtime>| env::lease::cancel_lease(offset_lease_id, length_lease_id, caller)
258    }
259
260    // Memory
261    fn binary_equals(
262        offset_left: *const u8,
263        length_left: usize,
264        offset_right: *const u8,
265        length_right: usize,
266    ) -> (i32, bool) {
267        |caller: Caller<Runtime>| {
268            env::memory::binary_equals(offset_left, length_left, offset_right, length_right, caller)
269        }
270    }
271
272    fn string_equals(
273        offset_left: *const u8,
274        length_left: usize,
275        offset_right: *const u8,
276        length_right: usize,
277    ) -> (i32, bool) {
278        |caller: Caller<Runtime>| {
279            env::memory::string_equals(offset_left, length_left, offset_right, length_right, caller)
280        }
281    }
282
283    fn join(
284        offset_left: *const u8,
285        length_left: usize,
286        offset_right: *const u8,
287        length_right: usize,
288    ) -> (i32, *const u8, usize) {
289        |caller: Caller<Runtime>| {
290            env::memory::join(offset_left, length_left, offset_right, length_right, caller)
291        }
292    }
293
294    fn contains(
295        offset_bytes: *const u8,
296        length_bytes: usize,
297        offset_subbytes: *const u8,
298        length_subbytes: usize,
299    ) -> (i32, bool) {
300        |caller: Caller<Runtime>| {
301            env::memory::contains(
302                offset_bytes,
303                length_bytes,
304                offset_subbytes,
305                length_subbytes,
306                caller,
307            )
308        }
309    }
310
311    fn drop(offset_bytes: *const u8, length_bytes: usize, n: i64) -> (i32, *const u8, usize) {
312        |_caller: Caller<Runtime>| env::memory::drop(offset_bytes, length_bytes, n)
313    }
314
315    fn drop_right(offset_bytes: *const u8, length_bytes: usize, n: i64) -> (i32, *const u8, usize) {
316        |_caller: Caller<Runtime>| env::memory::drop_right(offset_bytes, length_bytes, n)
317    }
318
319    fn index_of(
320        offset_string: *const u8,
321        length_string: usize,
322        offset_substring: *const u8,
323        length_substring: usize,
324    ) -> (i32, i64) {
325        |caller: Caller<Runtime>| {
326            env::memory::index_of(
327                false,
328                offset_string,
329                length_string,
330                offset_substring,
331                length_substring,
332                caller,
333            )
334        }
335    }
336
337    fn last_index_of(
338        offset_string: *const u8,
339        length_string: usize,
340        offset_substring: *const u8,
341        length_substring: usize,
342    ) -> (i32, i64) {
343        |caller: Caller<Runtime>| {
344            env::memory::index_of(
345                true,
346                offset_string,
347                length_string,
348                offset_substring,
349                length_substring,
350                caller,
351            )
352        }
353    }
354
355    fn take(offset_bytes: *const u8, length_bytes: usize, n: i64) -> (i32, *const u8, usize) {
356        |_caller: Caller<Runtime>| env::memory::take(offset_bytes, length_bytes, n)
357    }
358
359    fn take_right(offset_bytes: *const u8, length_bytes: usize, n: i64) -> (i32, *const u8, usize) {
360        |_caller: Caller<Runtime>| env::memory::take_right(offset_bytes, length_bytes, n)
361    }
362
363    // Storage
364    fn contains_key(
365        offset_address: *const u8,
366        length_address: usize,
367        offset_key: *const u8,
368        length_key: usize,
369    ) -> (i32, bool) {
370        |caller: Caller<Runtime>| {
371            env::storage::contains_key(
372                offset_address,
373                length_address,
374                offset_key,
375                length_key,
376                caller,
377            )
378        }
379    }
380
381    fn get_storage_int(
382        offset_address: *const u8,
383        length_address: usize,
384        offset_key: *const u8,
385        length_key: usize,
386    ) -> (i32, i64) {
387        |caller: Caller<Runtime>| {
388            env::storage::get_storage_int(
389                offset_address,
390                length_address,
391                offset_key,
392                length_key,
393                caller,
394            )
395        }
396    }
397
398    fn get_storage_bool(
399        offset_address: *const u8,
400        length_address: usize,
401        offset_key: *const u8,
402        length_key: usize,
403    ) -> (i32, bool) {
404        |caller: Caller<Runtime>| {
405            env::storage::get_storage_bool(
406                offset_address,
407                length_address,
408                offset_key,
409                length_key,
410                caller,
411            )
412        }
413    }
414
415    fn get_storage_binary(
416        offset_address: *const u8,
417        length_address: usize,
418        offset_key: *const u8,
419        length_key: usize,
420    ) -> (i32, *const u8, usize) {
421        |caller: Caller<Runtime>| {
422            env::storage::get_storage_binary(
423                offset_address,
424                length_address,
425                offset_key,
426                length_key,
427                caller,
428            )
429        }
430    }
431
432    fn get_storage_string(
433        offset_address: *const u8,
434        length_address: usize,
435        offset_key: *const u8,
436        length_key: usize,
437    ) -> (i32, *const u8, usize) {
438        |caller: Caller<Runtime>| {
439            env::storage::get_storage_string(
440                offset_address,
441                length_address,
442                offset_key,
443                length_key,
444                caller,
445            )
446        }
447    }
448
449    fn set_storage_int(offset_key: *const u8, length_key: usize, value: i64) -> i32 {
450        |caller: Caller<Runtime>| {
451            env::storage::set_storage_int(offset_key, length_key, value, caller)
452        }
453    }
454
455    fn set_storage_bool(offset_key: *const u8, length_key: usize, value: bool) -> i32 {
456        |caller: Caller<Runtime>| {
457            env::storage::set_storage_bool(offset_key, length_key, value, caller)
458        }
459    }
460
461    fn set_storage_binary(
462        offset_key: *const u8,
463        length_key: usize,
464        offset_value: *const u8,
465        length_value: usize,
466    ) -> i32 {
467        |caller: Caller<Runtime>| {
468            env::storage::set_storage_binary(
469                offset_key,
470                length_key,
471                offset_value,
472                length_value,
473                caller,
474            )
475        }
476    }
477
478    fn set_storage_string(
479        offset_key: *const u8,
480        length_key: usize,
481        offset_value: *const u8,
482        length_value: usize,
483    ) -> i32 {
484        |caller: Caller<Runtime>| {
485            env::storage::set_storage_string(
486                offset_key,
487                length_key,
488                offset_value,
489                length_value,
490                caller,
491            )
492        }
493    }
494
495    // Tx
496    fn get_tx_sender() -> (i32, *const u8, usize) {
497        |caller: Caller<Runtime>| env::tx::tx(env::Field::String("sender".to_string()), caller)
498    }
499
500    fn get_payments() -> (i32, i32) {
501        |caller: Caller<Runtime>| {
502            let (err, num) = env::tx::get_payments(caller);
503            match i32::try_from(num) {
504                Ok(value) => (err, value),
505                Err(_) => (
506                    Error::Runtime(RuntimeError::ConvertingNumericTypes).as_i32(),
507                    0,
508                ),
509            }
510        }
511    }
512
513    fn get_payment_asset_id(number: i32) -> (i32, *const u8, usize) {
514        |caller: Caller<Runtime>| env::tx::get_payment_asset_id(number as i64, caller)
515    }
516
517    fn get_payment_amount(number: i32) -> (i32, i64) {
518        |caller: Caller<Runtime>| env::tx::get_payment_amount(number as i64, caller)
519    }
520
521    // Utils
522    fn base_58(offset_bytes: *const u8, length_bytes: usize) -> (i32, *const u8, usize) {
523        |caller: Caller<Runtime>| env::utils::base58(offset_bytes, length_bytes, caller)
524    }
525
526    fn to_base_58_string(offset_bytes: *const u8, length_bytes: usize) -> (i32, *const u8, usize) {
527        |caller: Caller<Runtime>| env::utils::to_base58_string(offset_bytes, length_bytes, caller)
528    }
529
530    fn to_le_bytes(offset_bytes: *const u8, length_bytes: usize) -> (i32, *const u8, usize) {
531        |caller: Caller<Runtime>| env::utils::to_le_bytes(offset_bytes, length_bytes, caller)
532    }
533
534    fn caller() -> (i32, *const u8, usize) {
535        |caller: Caller<Runtime>| env::utils::caller(caller)
536    }
537
538    fn require(offset_message: *const u8, length_message: usize) -> i32 {
539        |caller: Caller<Runtime>| env::utils::require(offset_message, length_message, caller)
540    }
541}