Last active
September 27, 2022 12:18
-
-
Save charleslukes/018b29ae2aff9e9137b59602082a77b3 to your computer and use it in GitHub Desktop.
GET PREVIOUS EVENTS ON POLKADOT CHAIN
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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