Converting between event types

408 views Asked by At

Is there any clue on how to correctly compare whether event got from frame_system::Module::events() is equal to a specific one from current pallets decl_event!?

I've tried to match the event from the list with inner PostCreated event:

let events: Vec<EventRecord<<T as system::Trait>::Event, T::Hash>> = SystemModule::<T>::events();
        
events.iter().filter(|EventRecord { event, .. }| {
    matches!(event, RawEvent::PostCreated(_, _))
});

But, got an error:

428 |             matches!(event, RawEvent::PostCreated(_, _))
    |                      -----  ^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected associated type, found enum `RawEvent`
    |                      |
    |                      this expression has type `&<T as frame_system::Trait>::Event`
    |
    = note: expected associated type `<T as frame_system::Trait>::Event`
                          found enum `RawEvent<_>`
    = help: consider constraining the associated type `<T as frame_system::Trait>::Event` to `RawEvent<_>`

Furthermore, if I add .into() to the event, I'm getting another error:

428 |             matches!(event.into(), RawEvent::PostCreated(_, _))
    |                            ^^^^ the trait `std::convert::From<&<T as frame_system::Trait>::Event>` is not implemented for `RawEvent<_>`
    |
    = note: required because of the requirements on the impl of `std::convert::Into<RawEvent<_>>` for `&<T as frame_system::Trait>::Event`

I will appreciate any help. Sure that something is wrong with exactly this lines of code.

1

There are 1 answers

2
Shawn Tabrizi On BEST ANSWER

Your "generic" event type should be defined like this in your configuration trait:

/// The overarching event type.
type Event: From<Event<Self>> + Into<<Self as frame_system::Trait>::Event>;

This means that we support the From trait from the Event enum defined in the pallet, and the Into trait for Event enum in the frame_system pallet.

So the conversion can be the following:

  • Raw Event -> Generic Event
  • Generic Event -> System Event

So:

let generic_event: <T as Trait>::Event = RawEvent::MyEvent.into();
let system_event: <T as frame_system::Trait>::Event = generic_event.into();

You can then compare this system_event to the events you get back from frame_system::Module::<T>::events().