Events
Overview
Event logs are a fundamental component of the Ethereum blockchain that allow smart contracts to emit structured data during transaction execution. They serve as an efficient way to store and retrieve information about contract interactions.
SP1 Contract Call enables zero-knowledge verification of Ethereum event logs by fetching transaction receipts and their associated logs, then proving their validity within the zkVM. This allows applications to trustlessly verify that specific events occurred on the blockchain without requiring direct access to the Ethereum network.
How to query event logs
Events declaration
To be able to query events, you first need to declare them using Alloy sol!
macro:
sol! {
interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
}
}
You'll need to interact with the module generated by the sol!
macro in both the host and the client, so it's a good practice to have the code above in a shared lib accessible in both environments.
Host-Side Event Processing
Events logs are prefetched in the host with EvmSketch::get_logs()
. You just need to build a Filter
and call get_log()
, like in the example below:
let filter = Filter::new()
.address(WETH)
.at_block_hash(sketch.anchor.header().hash_slow())
.event(IERC20::Transfer::SIGNATURE);
let event_logs = sketch.get_logs(&filter).await.unwrap();
Besides returning the requested event logs, get_log()
records the corresponding receipts in order to later include them in the EvmSketchInput
when EvmSketch::finalize()
is called.
You can find more info about how to query event logs in the Alloy documentation.
zkVM Execution
In the client, the event logs can be easily queried, like in the example below:
let executor = ClientExecutor::new(&state_sketch).unwrap();
let filter = Filter::new().address(WETH).event(IERC20::Transfer::SIGNATURE);
let logs = executor.get_logs::<IERC20::Transfer>(filter).unwrap();