use crate::hostio::{self, wrap_hostio};
use alloc::{vec, vec::Vec};
use alloy_primitives::B256;
use alloy_sol_types::{token::WordToken, SolEvent, TopicList};
fn emit_log(bytes: &[u8], topics: usize) {
unsafe { hostio::emit_log(bytes.as_ptr(), bytes.len(), topics) }
}
pub fn raw_log(topics: &[B256], data: &[u8]) -> Result<(), &'static str> {
if topics.len() > 4 {
return Err("too many topics");
}
let mut bytes: Vec<u8> = vec![];
bytes.extend(topics.iter().flat_map(|x| x.0.iter()));
bytes.extend(data);
emit_log(&bytes, topics.len());
Ok(())
}
pub fn log<T: SolEvent>(event: T) {
let mut topics = [WordToken::default(); 4];
event.encode_topics_raw(&mut topics).unwrap();
let count = T::TopicList::COUNT;
let mut bytes = Vec::with_capacity(32 * count);
for topic in &topics[..count] {
bytes.extend_from_slice(topic.as_slice());
}
event.encode_data_to(&mut bytes);
emit_log(&bytes, count);
}
pub fn memory_grow(pages: u16) {
unsafe { hostio::memory_grow(pages) }
}
wrap_hostio!(
gas_left evm_gas_left u64
);
wrap_hostio!(
ink_left evm_ink_left u64
);