-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcheckBalanceAtHeight.ts
78 lines (64 loc) · 2.23 KB
/
checkBalanceAtHeight.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { ApiPromise, WsProvider } from '@polkadot/api';
import * as fs from 'fs';
import * as path from 'path';
import { input, select } from '@inquirer/prompts';
let provider;
// Main function
const main = async () => {
// Input
const network = await select({
message: 'Choose the Moonbeam Network to query.',
choices: [
{ name: 'Moonriver', value: 'moonriver' },
{ name: 'Moonbeam', value: 'moonbeam' },
],
});
if (network === 'moonriver') {
provider = new WsProvider('wss://moonriver.unitedbloc.com');
} else if (network === 'moonbeam') {
provider = new WsProvider('wss://moonbeam.unitedbloc.com');
}
const block = await input({
message: 'What is the block height (number)?',
});
// Read JSON Data
const addressesDataBuffer = fs.readFileSync(path.resolve(`./addresses.json`));
const addressesDataJSONString = addressesDataBuffer.toString();
const addressesArray = JSON.parse(addressesDataJSONString);
// Create API Provider
const api = await ApiPromise.create({
provider: provider,
noInitWarn: true,
});
// Create API Provider at a Given Block Hash
const blockHash = await api.rpc.chain.getBlockHash(block);
const apiAt = await api.at(blockHash);
// Get TimeStamp
const time = (await apiAt.query.timestamp.now()).toHuman() as any;
let results = [];
for (let address of addressesArray['moonbeam']) {
const balance = (await apiAt.query.system.account(address)).toHuman() as any;
// Get reserved and free balanace at a given block
let realBalance = (
(BigInt(balance.data.free.replaceAll(',', '')) +
BigInt(balance.data.reserved.replaceAll(',', ''))) /
BigInt(10 ** 16)
).toString();
realBalance = realBalance.slice(0, -2) + '.' + realBalance.slice(-2);
results.push({
network: network,
date: new Date(Number(time.replaceAll(',', ''))),
address: address,
balance: realBalance,
});
}
console.log(results);
// Save data to JSON file
const dataJSON = JSON.stringify(results, null, 2);
fs.writeFileSync(`${network}_address_balance_block${block}.json`, dataJSON, 'utf-8');
await api.disconnect();
};
// Execute the main function
main().catch((error) => {
console.error('An error occurred:', error);
});