stylus_core/
lib.rs

1// Copyright 2024-2025, Offchain Labs, Inc.
2// For licensing, see https://github.com/OffchainLabs/stylus-sdk-rs/blob/main/licenses/COPYRIGHT.md
3
4//! Defines host environment methods Stylus SDK contracts have access to.
5pub mod calls;
6pub mod deploy;
7pub mod host;
8pub mod storage;
9
10use alloy_sol_types::{abi::token::WordToken, SolEvent, TopicList};
11pub use host::*;
12pub use storage::TopLevelStorage;
13
14/// Emits a typed, Alloy log.
15pub fn log<T: SolEvent>(vm: &dyn Host, event: T) {
16    // According to the alloy docs, encode_topics_raw fails only if the array is too small
17    let mut topics = [WordToken::default(); 4];
18    event.encode_topics_raw(&mut topics).unwrap();
19
20    let count = T::TopicList::COUNT;
21    let mut bytes = Vec::with_capacity(32 * count);
22    for topic in &topics[..count] {
23        bytes.extend_from_slice(topic.as_slice());
24    }
25    event.encode_data_to(&mut bytes);
26    vm.emit_log(&bytes, count);
27}