numbat_wasm_debug/api/
endpoint_arg_api_mock.rs1use crate::{TxContext, TxPanic};
2use alloc::vec::Vec;
3use numbat_wasm::{
4 api::{EndpointArgumentApi, Handle},
5 types::BoxedBytes,
6};
7use num_bigint::{BigInt, BigUint, Sign};
8use num_traits::cast::ToPrimitive;
9
10impl EndpointArgumentApi for TxContext {
13 fn get_num_arguments(&self) -> i32 {
14 self.tx_input_box.args.len() as i32
15 }
16
17 fn get_argument_len(&self, arg_index: i32) -> usize {
18 let arg = self.get_argument_vec_u8(arg_index);
19 arg.len()
20 }
21
22 fn copy_argument_to_slice(&self, _arg_index: i32, _slice: &mut [u8]) {
23 panic!("copy_argument_to_slice not yet implemented")
24 }
25
26 fn get_argument_vec_u8(&self, arg_index: i32) -> Vec<u8> {
27 let arg_idx_usize = arg_index as usize;
28 assert!(
29 arg_idx_usize < self.tx_input_box.args.len(),
30 "Tx arg index out of range"
31 );
32 self.tx_input_box.args[arg_idx_usize].clone()
33 }
34
35 fn get_argument_boxed_bytes(&self, arg_index: i32) -> BoxedBytes {
36 self.get_argument_vec_u8(arg_index).into()
37 }
38
39 fn get_argument_big_uint_raw(&self, arg_index: i32) -> Handle {
40 let arg_bytes = self.get_argument_boxed_bytes(arg_index);
41 let mut tx_output = self.tx_output_cell.borrow_mut();
42 let result = BigInt::from_bytes_be(Sign::Plus, arg_bytes.as_slice());
43 tx_output
44 .managed_types
45 .big_int_map
46 .insert_new_handle(result)
47 }
48
49 fn get_argument_big_int_raw(&self, arg_index: i32) -> i32 {
50 let arg_bytes = self.get_argument_boxed_bytes(arg_index);
51 let mut tx_output = self.tx_output_cell.borrow_mut();
52 let result = BigInt::from_signed_bytes_be(arg_bytes.as_slice());
53 tx_output
54 .managed_types
55 .big_int_map
56 .insert_new_handle(result)
57 }
58
59 fn get_argument_managed_buffer_raw(&self, arg_index: i32) -> Handle {
60 let arg_bytes = self.get_argument_boxed_bytes(arg_index);
61 let mut tx_output = self.tx_output_cell.borrow_mut();
62 tx_output
63 .managed_types
64 .managed_buffer_map
65 .insert_new_handle(arg_bytes.as_slice().into())
66 }
67
68 fn get_argument_i64(&self, arg_index: i32) -> i64 {
69 let bytes = self.get_argument_vec_u8(arg_index);
70 let bi = BigInt::from_signed_bytes_be(&bytes);
71 if let Some(v) = bi.to_i64() {
72 v
73 } else {
74 std::panic::panic_any(TxPanic {
75 status: 10,
76 message: b"argument out of range".to_vec(),
77 })
78 }
79 }
80
81 fn get_argument_u64(&self, arg_index: i32) -> u64 {
82 let bytes = self.get_argument_vec_u8(arg_index);
83 let bu = BigUint::from_bytes_be(&bytes);
84 if let Some(v) = bu.to_u64() {
85 v
86 } else {
87 std::panic::panic_any(TxPanic {
88 status: 10,
89 message: b"argument out of range".to_vec(),
90 })
91 }
92 }
93}