Last updated
Was this helpful?
Last updated
Was this helpful?
Given a smart contract address passed as a parameter, this module returns the logs attached to the contract.
First, generate the Protobuf modules and build the Rust code:
Now, you can run the Substreams. The logs retrieved correspond to the 0xbc4ca0eda7647a8ab7c2061c2e118a18a936f13d
(BoredApeYachtClub smart contract). To avoid iterating over the full blockchain, the following command starts at block 17717995
and finished at block 17718004
. Therefore, only the BoredApeYachtClub smart contract logs that happened within this block range are printed.
The output of the command should be similar to:
The smart contract address is passed as a parameter defined in the Substreams manifest (substreams.yml
):
Declaration of the module in the manifest (substreams.yml
):
The module expects two inputs: the parameter as a string, and a raw Ethereum block. The output is the Events
object defined in the Protobuf.
The corresponding Rust function declaration, which matches the name of the module, map_contract_events
:
In this example, you do not need to parse the parameters, as contract_address
is the only string passed and you can use it directly. However, it is necessary to verify that the parameter is a valid Ethereum; this verification is performed by the verify_parameter
function.
Then, you iterate over the events of the contract:
The .logs()
function iterates over the logs of successful transactions within the block.
For every log of a successful transaction, you verify if its address
matches the smart contract address (i.e. you verify if the log was actually emitted by the smart contract). For the comparison, both log.address()
and contract_address
are converted to Vec<u8>
.
Every filtered log (i.e. every log that belongs to the smart contract) is mapped to a pb::eth::event::v1::Event
struct, which was specified in the Protobuf definition.
Finally, you collect all the events in a vector.