testnumbat_wasm_node/api/
log_api_node.rs

1use crate::AndesApiImpl;
2use testnumbat_wasm::{
3    api::{Handle, LogApi},
4    types::ArgBuffer,
5};
6
7extern "C" {
8    fn writeLog(pointer: *const u8, length: i32, topicPtr: *const u8, numTopics: i32);
9
10    fn writeEventLog(
11        numTopics: i32,
12        topicLengthsOffset: *const u8,
13        topicOffset: *const u8,
14        dataOffset: *const u8,
15        dataLength: i32,
16    );
17
18    #[cfg(not(feature = "unmanaged-ei"))]
19    fn managedWriteLog(topicsHandle: i32, dataHandle: i32);
20}
21
22const LEGACY_TOPIC_LENGTH: usize = 32;
23
24/// Interface to only be used by code generated by the macros.
25/// The smart contract code doesn't have access to these methods directly.
26impl LogApi for AndesApiImpl {
27    fn write_event_log(&self, topics_buffer: &ArgBuffer, data: &[u8]) {
28        unsafe {
29            writeEventLog(
30                topics_buffer.num_args() as i32,
31                topics_buffer.arg_lengths_bytes_ptr(),
32                topics_buffer.arg_data_ptr(),
33                data.as_ptr(),
34                data.len() as i32,
35            );
36        }
37    }
38
39    fn write_legacy_log(&self, topics: &[[u8; 32]], data: &[u8]) {
40        let mut topics_raw = [0u8; LEGACY_TOPIC_LENGTH * 10]; // hopefully we never have more than 10 topics
41        for i in 0..topics.len() {
42            topics_raw[LEGACY_TOPIC_LENGTH * i..LEGACY_TOPIC_LENGTH * (i + 1)]
43                .copy_from_slice(&topics[i]);
44        }
45        unsafe {
46            writeLog(
47                data.as_ptr(),
48                data.len() as i32,
49                topics_raw.as_ptr(),
50                topics.len() as i32,
51            );
52        }
53    }
54
55    #[cfg(feature = "unmanaged-ei")]
56    fn managed_write_log(&self, topics_handle: Handle, data_handle: Handle) {
57        use testnumbat_wasm::types::{
58            managed_vec_of_buffers_to_arg_buffer, ManagedBuffer, ManagedType, ManagedVec,
59        };
60        let topics = ManagedVec::from_raw_handle(self.clone(), topics_handle);
61        let topics_arg_buffer = managed_vec_of_buffers_to_arg_buffer(&topics);
62        let data = ManagedBuffer::from_raw_handle(self.clone(), data_handle);
63        self.write_event_log(&topics_arg_buffer, data.to_boxed_bytes().as_slice());
64    }
65
66    #[cfg(not(feature = "unmanaged-ei"))]
67    fn managed_write_log(&self, topics_handle: Handle, data_handle: Handle) {
68        unsafe {
69            managedWriteLog(topics_handle, data_handle);
70        }
71    }
72}