3

I want to get events list by using polkadot.js/api.

I can see api.query.system.events() in docs.

Docs: https://polkadot.js.org/docs/substrate/storage#eventcount-u32

However, this api lists events for current block only.

Is there any api to get list of events for specific block height? or range of blocks?

1 Answer 1

6

For events (or any queries) at a specific point in the chain -

// Get an API decorated at a specific point. This will -
//   - connect
//   - retrieve the metadata
//   - decorate the API based on what is available
//   - make any state queries at that point
const at = await api.at(blockHash);

// any queries now made will be at that point
const events = await at.query.system.events();

Previously (it is now deprecated, but still available, i.e. not removed), you could also do await api.query.syste.events.at(blockHash). Internally this form would also use the api.at(blockHash) detailed above. The api.at form is preferred since it re-decorates the API at that runtime point, while the older form would assume the state endpoints are the same.

1
  • Hi @Jaco. Is there any way to filter events list by section or method? For example, I want to get events list which section is "staking" only
    – Heindrick
    May 20, 2022 at 7:21

Your Answer

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

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