1

This seems very basic, but I'm unable to figure out at which block number an event was emitted.

Specifically, I'm listening for Grandpa.NewAuthorities system event and just want to know at which block it was emitted. I've looked through the entire event, but can't find anything that would enable this. What am I missing here?

2 Answers 2

1

You are missing some crucial information, such as which API you are using.

For instance, on the JS API, each Codec result retrieved from storage has a createdAtHash getter. This means you can do -

api.query.system.events((v) => {
  console.log('Vec<EventRecord> created at', v.createdAtHash.toHex());
}); 

Since the underlying subscribeStorage RPCs do return the blockHash, other APIs/middleware-layers would expose it in a different way.

0

What are events? Events are state in storage that is reset at the beginning of each block. So in order to get those events the code must know which block is associated I would think. You can see why there would not be a block number or hash stored in the events, as anyone that got an event must already know that information.

https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.polkadot.io#/chainstate - have a look in system/events().

If you did a raw storage query for 26aa394eea5630e07c48ae0c9558cef780d41e5e16056765bc8461851072c9d7 then you would get the events back as of the block that you were querying the storage for.

Hope this sheds some light that events are not magic and work the same way as all the other storage.

1
  • I am aware of how events are stored. The problem, however, is that the listener gives me an event, but doesn't tell me in which block it was included. In general, the block number doesn't need to be part of the event, but it should be in the message received by the listener. To get around this, I query the events of every block, checking if its included. Bad design, but I dont see how to do it differently. Jul 5, 2022 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.