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
use crate::{
    num_bigint::{BigInt, BigUint},
    tx_mock::TxPanic,
    DebugApi,
};
use alloc::vec::Vec;
use multiversx_sc::api::{EndpointArgumentApi, EndpointArgumentApiImpl, ManagedBufferApi};
use num_traits::cast::ToPrimitive;

impl EndpointArgumentApi for DebugApi {
    type EndpointArgumentApiImpl = DebugApi;

    fn argument_api_impl() -> Self::EndpointArgumentApiImpl {
        DebugApi::new_from_static()
    }
}

impl DebugApi {
    fn get_argument_vec_u8(&self, arg_index: i32) -> Vec<u8> {
        let arg_idx_usize = arg_index as usize;
        assert!(
            arg_idx_usize < self.input_ref().args.len(),
            "Tx arg index out of range"
        );
        self.input_ref().args[arg_idx_usize].clone()
    }
}

/// Interface to only be used by code generated by the macros.
/// The smart contract code doesn't have access to these methods directly.
impl EndpointArgumentApiImpl for DebugApi {
    fn get_num_arguments(&self) -> i32 {
        self.input_ref().args.len() as i32
    }

    fn get_argument_len(&self, arg_index: i32) -> usize {
        let arg = self.get_argument_vec_u8(arg_index);
        arg.len()
    }

    fn load_argument_managed_buffer(&self, arg_index: i32, dest: Self::ManagedBufferHandle) {
        let arg_bytes = self.get_argument_vec_u8(arg_index);
        self.mb_overwrite(dest, arg_bytes.as_slice());
    }

    fn get_argument_i64(&self, arg_index: i32) -> i64 {
        // specific implementation provided, in order to simulate the VM error (status 10 instead of 4)
        let bytes = self.get_argument_vec_u8(arg_index);
        let bi = BigInt::from_signed_bytes_be(&bytes);
        if let Some(v) = bi.to_i64() {
            v
        } else {
            std::panic::panic_any(TxPanic {
                status: 10,
                message: "argument out of range".to_string(),
            })
        }
    }

    fn get_argument_u64(&self, arg_index: i32) -> u64 {
        // specific implementation provided, in order to simulate the VM error (status 10 instead of 4)
        let bytes = self.get_argument_vec_u8(arg_index);
        let bu = BigUint::from_bytes_be(&bytes);
        if let Some(v) = bu.to_u64() {
            v
        } else {
            std::panic::panic_any(TxPanic {
                status: 10,
                message: "argument out of range".to_string(),
            })
        }
    }

    fn load_callback_closure_buffer(&self, dest: Self::ManagedBufferHandle) {
        let closure_data = self.input_ref().promise_callback_closure_data.as_slice();
        self.mb_overwrite(dest, closure_data);
    }
}