-
Notifications
You must be signed in to change notification settings - Fork 0
/
polkadot.js
25 lines (22 loc) · 964 Bytes
/
polkadot.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
fs = require('fs');
const { ApiPromise, WsProvider } = require('@polkadot/api');
const { writeCSV } = require('./utils.js');
const network = "polkadot";
const wsProvider = "wss://polkadot.polkastats.io/rpc";
const exportDir = "/var/www/substrate-data-csv"
async function main () {
let currentKnownSessionIndex = 0;
const provider = new WsProvider(wsProvider);
const api = await ApiPromise.create({ provider });
await api.rpc.chain.subscribeNewHeads(async (header) => {
const blockNumber = header.number.toNumber();
const sessionInfo = await api.derive.session.info();
const currentSessionIndex = sessionInfo.currentIndex.toNumber();
const currentEraIndex = sessionInfo.activeEra.toNumber();
if (currentSessionIndex > currentKnownSessionIndex) {
currentKnownSessionIndex = currentSessionIndex;
writeCSV(api, network, exportDir, currentEraIndex, currentSessionIndex, blockNumber);
}
});
}
main().catch(console.error);