How to tell someone using your blockchain something happened.
Users of the application need feedback about what happened when they clicked something or interacted on your chain through a public endpoint you've given them.
Transactions can fail for any number of reasons, so your users need to know and that is handled through and Event. Confirmations are similar: tell your user it went okay with an Event.
Right, so my pallet should be able to emit Events?
Yep, pretty much without exception. Here's how:
In the Pallet
type Event
Add Event type to the `Config:
// Note: this is the pallet-required `Configuration Trait`pubtraitConfig: frame_system::Config{// copy the line below exactly as shown into the `Config` section// of your `Pallet`. You'll eventually be able to interpret that// if you don't lnow. Just keep truckin'.typeEvent:From<Event>+Into<<Selfas frame_system::Config>::Event>;}
decl_module!
And now add the decl_module! ("declare_module") macro to give access to the deposit_event() method.
Reminder: the decl_module! macro is the same we used for creating Dispatchable Calls for a Pallet.
decl_event!
decl_event! ("declare event") macro is the way Rust implements an event
We are showing generic types in these examples. The syntax for generic events requires the where.
In the Runtime
We've defined a trait in the Pallet for Config and so now it has to be implemented at runtime.
In your runtime lib.rs file, add
The code above is simply specifying the type for Event . Note the <T> is not shown and that is because we are defining the concrete type to be implemented by the Pallet we're configuring.
Add the events to the runtime build macro construct_runtime! by adding:
decl_module! {
pub struct Module<T: Config> for enum Call where origin: T::Origin {
// This line is new
fn deposit_event() = default;
// More dispatchable calls that you compose woud go here
}
}
decl_event!(
pub enum Event<T> where AccountId = <T as frame_system::Config>::AccountId {
EmitInput(AccountId, u32),
}
);
./runtime/src/lib.rs
impl simple_event::Config for Runtime {
type Event = Event;
}