Skip to content

Instantly share code, notes, and snippets.

@charleslukes
Last active September 27, 2022 12:18
Show Gist options
  • Save charleslukes/018b29ae2aff9e9137b59602082a77b3 to your computer and use it in GitHub Desktop.
Save charleslukes/018b29ae2aff9e9137b59602082a77b3 to your computer and use it in GitHub Desktop.
GET PREVIOUS EVENTS ON POLKADOT CHAIN
const oldEvents = async () => {
// get current block
const currentBlock = await api.rpc.chain.getHeader();
const currentBlockData = currentBlock.toJSON();
// By default a Polkadot/Substrate node will only keep state for the last 256 blocks, unless it is explicitly run in archive mode
for (let index = 0; index < 255; index++) {
const blockNum = currentBlockData.number - (index + 1);
const blockHash = await api.rpc.chain.getBlockHash(blockNum);
const apiAt = await api.at(blockHash.toJSON());
const events = await apiAt.query.system.events();
events.forEach((record) => {
// Extract the phase, event and the event types
const { event } = record;
const { method } = event.toHuman();
// logs the event name
console.log({method})
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment