Events
Events allow for data to be logged publicly to the blockchain. Log entries provide the contract's address, a series of up to four topics, and some arbitrary length binary data. The Stylus Rust SDK provides a few ways to publish event logs described below.
Learn More
Log
Using the evm::log function in the Stylus SDK is the preferred way to log events. It ensures that an event will be logged in a Solidity ABI-compatible format. The log function takes any type that implements Alloy SolEvent trait. It's not recommended to attempt to implement this trait on your own. Instead, make use of the provided sol! macro to declare your Events and their schema using Solidity-style syntax to declare the parameter types. Alloy will create ABI-compatible Rust types which you can instantiate and pass to the evm::log function.
Log Usage
This code has yet to be audited. Please use at your own risk.
// sol! macro event declaration
// Up to 3 parameters can be indexed.
// Indexed parameters helps you filter the logs efficiently
sol! {
event Log(address indexed sender, string message);
event AnotherLog();
}
#[storage]
#[entrypoint]
pub struct Events {}
#[public]
impl Events {
fn user_main(_input: Vec<u8>) -> ArbResult {
// emits a 'Log' event, defined above in the sol! macro
evm::log(Log {
sender: Address::from([0x11; 20]),
message: "Hello world!".to_string(),
});
// no data, but 'AnotherLog' event will still emit to the chain
evm::log(AnotherLog {});
Ok(vec![])
}
}