-
Notifications
You must be signed in to change notification settings - Fork 5
/
checkBlockFinality.ts
45 lines (37 loc) · 1.41 KB
/
checkBlockFinality.ts
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
Simple code snippet to fetch the hash of the latest finalized block
and retrieve that blocks number. Another strategy might be getting the block
of the transaction and check if blockTransaction <= blockFinalized
*/
import { ApiPromise, WsProvider } from '@polkadot/api';
import yargs from 'yargs';
const args = yargs.options({
network: { type: 'string', demandOption: true, alias: 'n' },
}).argv;
// Create Provider
let wsProvider;
if (args['network'] === 'moonbeam') {
wsProvider = new WsProvider('wss://wss.api.moonbeam.network');
} else if (args['network'] === 'moonriver') {
wsProvider = new WsProvider('wss://wss.api.moonriver.moonbeam.network');
} else if (args['network'] === 'moonbase') {
wsProvider = new WsProvider('wss://wss.api.moonbase.moonbeam.network');
} else {
console.error('Network not supported');
process.exit();
}
const main = async () => {
// Wait for Provider
const api = await ApiPromise.create({
provider: wsProvider,
});
await api.isReady;
// Get latest block that is finalized
const finalizedHeadHash = await api.rpc.chain.getFinalizedHead();
// Get finalized block to retrieve number
const finalizedBlock = (await api.rpc.chain.getBlock(finalizedHeadHash)).toJSON();
// Block number is stored in finalizedBlock.block.header.number
console.log(`Block number ${finalizedBlock.block['header'].number} is the last Finalized`);
await api.disconnect();
};
main();