- Create event structs that derive the
starknet::Eventtrait - Define an
Eventenum in the contract, annotated with#[event], where each variant is linked to an event struct - Emit events with the
emitfunction
#[key] attribute to specific fields, which indexes them for efficient querying later.
Events variant names and structs are recommended to be named consistently, even if it create some redundancy when emitting events.
Here’s a practical example of a contract that emits events when incrementing a counter:
For better code organization, especially in larger contracts, you can define event structs outside of the contract module, as shown in the example here.
While this allows you to group related events in separate modules or files, remember that you must still include all event variants in the contract’s
Event enum.