Skip to content

Instantly share code, notes, and snippets.

@waylandc
Last active March 5, 2020 03:06
Show Gist options
  • Save waylandc/7564358064f0a2c0dcc3f7d2285f480f to your computer and use it in GitHub Desktop.
Save waylandc/7564358064f0a2c0dcc3f7d2285f480f to your computer and use it in GitHub Desktop.
query past event info from previous substrate blocks
// 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