testnumbat_wasm_node/api/
endpoint_arg_api_node.rs

1use crate::{error_hook, AndesApiImpl};
2use alloc::vec::Vec;
3use testnumbat_wasm::{
4    api::{EndpointArgumentApi, Handle},
5    err_msg,
6    types::BoxedBytes,
7};
8
9extern "C" {
10    fn getNumArguments() -> i32;
11    fn getArgumentLength(id: i32) -> i32;
12    fn getArgument(id: i32, dstOffset: *mut u8) -> i32;
13
14    // big int API
15    fn bigIntNew(value: i64) -> i32;
16    fn bigIntGetUnsignedArgument(arg_id: i32, dest: i32);
17    fn bigIntGetSignedArgument(arg_id: i32, dest: i32);
18
19    // small int API
20    fn smallIntGetUnsignedArgument(id: i32) -> i64;
21    fn smallIntGetSignedArgument(id: i32) -> i64;
22
23    // managed buffer API
24    fn mBufferNew() -> i32;
25    fn mBufferGetArgument(argId: i32, mBufferHandle: i32) -> i32;
26}
27
28/// Interface to only be used by code generated by the macros.
29/// The smart contract code doesn't have access to these methods directly.
30impl EndpointArgumentApi for AndesApiImpl {
31    #[inline]
32    fn get_num_arguments(&self) -> i32 {
33        unsafe { getNumArguments() }
34    }
35
36    #[inline]
37    fn get_argument_len(&self, arg_index: i32) -> usize {
38        unsafe { getArgumentLength(arg_index) as usize }
39    }
40
41    fn copy_argument_to_slice(&self, arg_index: i32, slice: &mut [u8]) {
42        unsafe {
43            let byte_len = getArgument(arg_index, slice.as_mut_ptr()) as usize;
44            if byte_len != slice.len() {
45                error_hook::signal_error(err_msg::ARG_BAD_LENGTH);
46            }
47        }
48    }
49
50    fn get_argument_vec_u8(&self, arg_index: i32) -> Vec<u8> {
51        let len = self.get_argument_len(arg_index);
52        let mut res = Vec::with_capacity(len);
53        if len > 0 {
54            unsafe {
55                res.set_len(len);
56                getArgument(arg_index, res.as_mut_ptr());
57            }
58        }
59        res
60    }
61
62    fn get_argument_boxed_bytes(&self, arg_index: i32) -> BoxedBytes {
63        let len = self.get_argument_len(arg_index);
64        unsafe {
65            let mut res = BoxedBytes::allocate(len);
66            if len > 0 {
67                getArgument(arg_index, res.as_mut_ptr());
68            }
69            res
70        }
71    }
72
73    #[inline]
74    fn get_argument_big_uint_raw(&self, arg_id: i32) -> i32 {
75        unsafe {
76            let handle = bigIntNew(0);
77            bigIntGetUnsignedArgument(arg_id, handle);
78            handle
79        }
80    }
81
82    #[inline]
83    fn get_argument_big_int_raw(&self, arg_id: i32) -> i32 {
84        unsafe {
85            let handle = bigIntNew(0);
86            bigIntGetSignedArgument(arg_id, handle);
87            handle
88        }
89    }
90
91    #[inline]
92    fn get_argument_managed_buffer_raw(&self, arg_id: i32) -> Handle {
93        unsafe {
94            let handle = mBufferNew();
95            mBufferGetArgument(arg_id, handle);
96            handle
97        }
98    }
99
100    #[inline]
101    fn get_argument_u64(&self, arg_id: i32) -> u64 {
102        unsafe { smallIntGetUnsignedArgument(arg_id) as u64 }
103    }
104
105    #[inline]
106    fn get_argument_i64(&self, arg_id: i32) -> i64 {
107        unsafe { smallIntGetSignedArgument(arg_id) }
108    }
109}