-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch-withdrawals.js
64 lines (52 loc) · 1.49 KB
/
fetch-withdrawals.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
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
import Web3 from "web3";
import dotenv from "dotenv";
import { readFile, writeFile } from "fs/promises";
// Lib
import { parse, stringify } from "./lib/json.js";
dotenv.config();
const readCache = async () => {
try {
return parse(await readFile("withdrawals.json"), [
"sum",
"amount",
"index",
]);
} catch (err) {
return {
lastBlock: -1,
withdrawals: {},
};
}
};
const range = (start, end) =>
Array.from({ length: end - start + 1 }, (_, i) => start + i);
const web3 = new Web3(process.env.PROVIDER_URL);
const cache = await readCache();
const lastBlock = await web3.eth.getBlockNumber();
const handleBlock = async (number) => {
const { withdrawals } = await web3.eth.getBlock(number);
if (!withdrawals) {
return;
}
for (const withdrawal of withdrawals) {
if (!cache.withdrawals[withdrawal.address]) {
cache.withdrawals[withdrawal.address] = {
sum: 0n,
details: [],
};
}
cache.withdrawals[withdrawal.address].sum += BigInt(withdrawal.amount);
cache.withdrawals[withdrawal.address].details.push({
block: number,
index: BigInt(withdrawal.index),
amount: BigInt(withdrawal.amount),
});
}
};
for (let number = cache.lastBlock + 1; number < lastBlock; number += 100) {
const to = number + 99;
console.log(`Processing blocks ${number} to ${to}`);
await Promise.all(range(number, to).map(handleBlock));
cache.lastBlock = to;
await writeFile("withdrawals.json", stringify(cache));
}