multiversx_chain_vm/vm_hooks/vh_handler/
vh_endpoint_finish.rs

1use num_bigint::{BigInt, BigUint};
2
3use crate::{
4    types::RawHandle,
5    vm_hooks::{VMHooksHandlerSource, VMHooksManagedTypes},
6};
7
8/// Interface to only be used by code generated by the macros.
9/// The smart contract code doesn't have access to these methods directly.
10pub trait VMHooksEndpointFinish: VMHooksHandlerSource + VMHooksManagedTypes {
11    fn finish_slice_u8(&self, slice: &[u8]) {
12        let mut v = vec![0u8; slice.len()];
13        v.copy_from_slice(slice);
14        let mut tx_result = self.result_lock();
15        tx_result.result_values.push(v)
16    }
17
18    fn finish_big_int_raw(&self, handle: RawHandle) {
19        let bi_bytes = self.bi_get_signed_bytes(handle);
20        let mut tx_result = self.result_lock();
21        tx_result.result_values.push(bi_bytes);
22    }
23
24    fn finish_big_uint_raw(&self, handle: RawHandle) {
25        let bu_bytes = self.bi_get_unsigned_bytes(handle);
26        let mut tx_result = self.result_lock();
27        tx_result.result_values.push(bu_bytes);
28    }
29
30    fn finish_managed_buffer_raw(&self, handle: RawHandle) {
31        self.finish_slice_u8(self.m_types_lock().mb_get(handle));
32    }
33
34    fn finish_i64(&self, value: i64) {
35        if value == 0 {
36            self.finish_slice_u8(&[]);
37        } else {
38            self.finish_slice_u8(BigInt::from(value).to_signed_bytes_be().as_slice());
39        }
40    }
41
42    fn finish_u64(&self, value: u64) {
43        if value == 0 {
44            self.finish_slice_u8(&[]);
45        } else {
46            self.finish_slice_u8(BigUint::from(value).to_bytes_be().as_slice());
47        }
48    }
49}