Last active
March 5, 2020 03:06
-
-
Save waylandc/7564358064f0a2c0dcc3f7d2285f480f to your computer and use it in GitHub Desktop.
query past event info from previous substrate blocks
This file contains hidden or 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
// using polkadot.js api this code will retrieve events from blocks 0-50 | |
// you can go as far back on full archive nodes and about 150-200 blocks | |
// back on non-archive nodes | |
// code contributed from Stefie@riot https://github.com/polkadot-js/api/issues/578 | |
for(i=50;i>=0;i--){ | |
var hash = await api.rpc.chain.getBlockHash(i); | |
var events = await api.query.system.events.at(hash); | |
console.log(`\n #${i} Received ${events.length} events:`); | |
// loop through the Vec<EventRecord> | |
events.forEach((record) => { | |
// extract the phase, event and the event types | |
const { event, phase } = record; | |
const types = event.typeDef; | |
// show what we are busy with | |
console.log(`\t${event.section}:${event.method}:: (phase=${phase.toString()})`); | |
console.log(`\t\t${event.meta.documentation.toString()}`); | |
// loop through each of the parameters, displaying the type and data | |
event.data.forEach((data, index) => { | |
console.log(`\t\t\t${types[index].type}: ${data.toString()}`); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment