-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrandom.js
77 lines (62 loc) · 2.28 KB
/
random.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
65
66
67
68
69
70
71
72
73
74
75
76
77
const fs = require("fs");
const { ethers } = require("ethers");
const axios = require("axios");
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY;
if (!ETHERSCAN_API_KEY) {
console.error("Please set ETHERSCAN_API_KEY environment variable");
process.exit(1);
}
const FILE_PREFIX = "random";
const YES_FILENAME = `yes-${FILE_PREFIX}.txt`;
const NO_FILENAME = `no-${FILE_PREFIX}.txt`;
const ERR_FILENAME = `err-${FILE_PREFIX}.txt`;
let noCount = readCounterFromFile(NO_FILENAME);
let errCount = readCounterFromFile(ERR_FILENAME);
function createRandomWallet() {
return ethers.Wallet.createRandom();
}
async function fetchBalancesForAddresses(privateKeys, addresses) {
const url = `https://api.etherscan.io/api?module=account&action=balancemulti&address=${addresses.join(',')}&tag=latest&apikey=${ETHERSCAN_API_KEY}`;
try {
const { data } = await axios.get(url);
if (data.status === "1") {
data.result.forEach((result, index) => {
const { account, balance } = result;
if (balance === "0") {
noCount++;
updateCounterFile(NO_FILENAME, noCount);
} else {
writeToFile(privateKeys[index], account, balance, YES_FILENAME);
}
});
} else if (data.status === "0") {
errCount++;
updateCounterFile(ERR_FILENAME, errCount);
}
} catch (error) {
console.error("Failed to fetch balances:", error.message);
}
}
function writeToFile(privateKey, address, balance, filename) {
const record = `${privateKey},${address},${balance}\n`;
fs.appendFile(filename, record, (err) => {
if (err) console.error("Failed to save data:", err.message);
else console.log(`Saved: ${address}`);
});
}
function readCounterFromFile(filename) {
if (fs.existsSync(filename)) {
return parseInt(fs.readFileSync(filename, 'utf8'), 10);
}
return 0;
}
function updateCounterFile(filename, count) {
fs.writeFileSync(filename, count.toString());
}
function checkBalancesForMultipleAddresses() {
const wallets = Array.from({ length: 20 }, createRandomWallet);
const privateKeys = wallets.map(wallet => wallet.privateKey);
const addresses = wallets.map(wallet => wallet.address);
fetchBalancesForAddresses(privateKeys, addresses);
}
setInterval(checkBalancesForMultipleAddresses, 1000);