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#![no_std]
4
5//! Defines host environment methods Stylus SDK contracts have access to.
6
7extern crate alloc;
8
9pub mod calls;
10pub mod deploy;
11pub mod host;
12pub mod storage;
13
14use alloy_sol_types::{abi::token::WordToken, SolEvent, TopicList};
15
16use alloc::vec::Vec;
17
18pub use host::*;
19pub use storage::TopLevelStorage;
20
21/// Emits a typed, Alloy log.
22pub fn log<T: SolEvent>(vm: &dyn Host, event: T) {
23    // According to the alloy docs, encode_topics_raw fails only if the array is too small
24    let mut topics = [WordToken::default(); 4];
25    event.encode_topics_raw(&mut topics).unwrap();
26
27    let count = T::TopicList::COUNT;
28    let mut bytes = Vec::with_capacity(32 * count);
29    for topic in &topics[..count] {
30        bytes.extend_from_slice(topic.as_slice());
31    }
32    event.encode_data_to(&mut bytes);
33    vm.emit_log(&bytes, count);
34}