-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
55 lines (48 loc) · 1.42 KB
/
main.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
const Web3 = require("web3");
const fs = require("fs");
var sleep = require("sleep");
const infuraAPIKey = ''; // Optional
const providerURL = "https://mainnet.infura.io" + infuraAPIKey;
var web3 = new Web3(new Web3.providers.HttpProvider(providerURL));
let balance;
let balances = [];
// INITIALIZE LOG FILES
fs.openSync("errors.txt", "w");
fs.openSync("balances.csv", "w");
console.log("Log files initialized");
// FILE WE'LL READ ADDRESSES FROM
const readFrom = "addresses.csv";
var lineReader = require("readline").createInterface({
input: fs.createReadStream(readFrom, { encoding: "utf8" })
});
// https://stackoverflow.com/a/32599033
let counter = 0;
lineReader.on("line", async line => {
counter++;
if (counter % 5000 == 0) {
console.log("Iterations : ", counter);
console.log("Pausing job...", new Date());
lineReader.pause();
sleep.sleep(5);
console.log("Resuming job...", new Date());
lineReader.resume();
}
// Force lower case so the checksum is not checked
await getBalance(line.toLowerCase());
});
lineReader.on("close", () => {
console.log("Balances written to balances.csv");
});
const getBalance = async address => {
sleep.msleep(2);
try {
balance = await web3.eth.getBalance(address);
fs.appendFileSync("balances.csv", `${address},${balance}\n`);
} catch (err) {
try {
fs.appendFileSync("errors.txt", err + "\n");
} catch (err) {
console.log(err);
}
}
};