* Bump example deps * System events example * Example comments * Link to example in docs * Typo in own surname, tsk, tsk... * Update README.md
34 lines
1.0 KiB
JavaScript
Executable File
34 lines
1.0 KiB
JavaScript
Executable File
// Import the API
|
|
const { ApiPromise } = require('@polkadot/api');
|
|
|
|
async function main () {
|
|
// Create our API with a default connection to the local node
|
|
const api = await ApiPromise.create();
|
|
|
|
// subscribe to system events via storage
|
|
api.query.system.events((events) => {
|
|
console.log(`\nReceived ${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.toArray().forEach((data, index) => {
|
|
console.log(`\t\t\t${types[index].type}: ${data.toString()}`);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
main().catch((error) => {
|
|
console.error(error);
|
|
process.exit(-1);
|
|
});
|