-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnewBlocks.js
30 lines (23 loc) · 1.03 KB
/
newBlocks.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// Import the API
const { ApiPromise, WsProvider } = require('@polkadot/api');
async function main () {
// Here we don't pass the (optional) provider, connecting directly to the default
// node/port, i.e. `ws://127.0.0.1:9944`. Await for the isReady promise to ensure
// the API has connected to the node and completed the initialisation process
const provider = new WsProvider('ws://127.0.0.1:8546');
//const provider = new WsProvider('ws://127.0.0.1:9944');
const api = await ApiPromise.create({provider});
// We only display a couple, then unsubscribe
let count = 0;
// Subscribe to the new headers on-chain. The callback is fired when new headers
// are found, the call itself returns a promise with a subscription that can be
// used to unsubscribe from the newHead subscription
const unsubscribe = await api.rpc.chain.subscribeNewHeads((header) => {
console.log(`Chain is at block: #${header.number}`);
if (++count === 256) {
unsubscribe();
process.exit(0);
}
});
}
main().catch(console.error);