Events example (#426)

* Bump example deps

* System events example

* Example comments

* Link to example in docs

* Typo in own surname, tsk, tsk...

* Update README.md
This commit is contained in:
Jaco Greeff
2018-11-29 18:04:47 +01:00
committed by GitHub
parent 383ee611cb
commit 0e6d138b1f
9 changed files with 66 additions and 9 deletions
+1
View File
@@ -22,3 +22,4 @@
- [Listen to balance change](examples/promise/03_listen_to_balance_change/README.md)
- [Read chain state](examples/promise/05_read_storage/README.md)
- [Make a transfer](examples/promise/07_transfer_dots/README.md)
- [Display system events](examples/promise/08_system_events/README.md)
@@ -11,8 +11,8 @@
},
"dependencies": {
"@polkadot/api": "^0.31.18",
"@polkadot/rpc-provider": "^0.31.18"
"@polkadot/api": "^0.32.12",
"@polkadot/rpc-provider": "^0.32.12"
},
"devDependencies": {
"rimraf": "^2.6.2"
@@ -10,7 +10,7 @@
"start": "node index.js"
},
"dependencies": {
"@polkadot/api": "^0.31.18"
"@polkadot/api": "^0.32.12"
},
"devDependencies": {
"rimraf": "^2.6.2"
@@ -10,7 +10,7 @@
"start": "node index.js"
},
"dependencies": {
"@polkadot/api": "^0.31.18"
"@polkadot/api": "^0.32.12"
},
"devDependencies": {
"rimraf": "^2.6.2"
@@ -1,7 +1,7 @@
{
"name": "04_read_storage",
"version": "0.2.0",
"description": "Example showing how to transfer DOTs",
"description": "Example showing how to query storage",
"main": "index.js",
"author": "chevdor",
"license": "MIT",
@@ -10,9 +10,9 @@
"start": "node index.js"
},
"dependencies": {
"@polkadot/api": "^0.31.18"
"@polkadot/api": "^0.32.12"
},
"devDependencies": {
"rimraf": "2.6.2"
"rimraf": "^2.6.2"
}
}
@@ -10,9 +10,9 @@
"start": "node index.js"
},
"dependencies": {
"@polkadot/api": "^0.31.82"
"@polkadot/api": "^0.32.12"
},
"devDependencies": {
"rimraf": "2.6.2"
"rimraf": "^2.6.2"
}
}
@@ -0,0 +1,5 @@
# Traverse events
Query the system events and extract information from them. This example runs until exited via Ctrl-C
[include](index.js)
+33
View File
@@ -0,0 +1,33 @@
// 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);
});
@@ -0,0 +1,18 @@
{
"name": "08_system_events",
"version": "0.1.0",
"description": "Example showing how to query and parse events",
"main": "index.js",
"author": "Jaco Greeff <jacogr@gmail.com>",
"license": "MIT",
"scripts": {
"clean": "rimraf node_modules",
"start": "node index.js"
},
"dependencies": {
"@polkadot/api": "^0.32.12"
},
"devDependencies": {
"rimraf": "^2.6.2"
}
}