Events and Erros
Explains how to emit events and errors from the runtime.
A pallet can emit events when it wants to send notification about changes or conditions in the runtime to external entities like users, chain explorers, or dApps.
In custom pallets, you can define:
what type of events you want to be emitted
what information is contained within those events
when those events are emitted
Declaring an event
Events are created using the #[pallet::event]
macro. For example:
Then, the RuntimeEvent
type is needed to aggregate them for the runtime.
Exposing events to your runtime
Any events you define in your pallet must be exposed to the runtime in the /runtime/src/lib.rs
file.
To expose events to the runtime:
Open the
/runtime/src/lib.rs
file in a text editor.Implement the
RuntimeEvent
type in the configuration trait for your pallet:Add the
RuntimeEvent
type to theconstruct_runtime!
macro:In this example, the event is a generic type and requires the
<T>
parameter. The<T>
parameter isn't needed if your events don't use generic types.
Depositing an event
Substrate provides a default implementation of how to deposit an event using macros. Depositing an event has the following structure:
This function places the event in the System pallet's runtime storage for that block. At the beginning of a new block, the System pallet automatically removes all events that were stored from the previous block.
Supported types
Listening to events
Errors
Each FRAME pallet can define a custom DispatchError
by using the #[pallet::error]
macro. For example:
Where to go next
Last updated