-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
130 lines (109 loc) · 4.48 KB
/
index.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
require('dotenv').config();
const { Worker } = require('worker_threads');
const path = require('path');
const fs = require('fs').promises;
const config = require('./config');
const { numWorkers, addressToCheck, blockchains, maxRetries, retryDelay, networkErrorRetryDelay, concurrencyLimit, minBalance } = config;
const apiKeys = {};
let totalKeysPerFile = 0;
let totalChecked = 0, totalNonZero = 0;
let totalErrors = 0;
async function loadApiKeysFromEnvFile(envFileName) {
const envContent = await fs.readFile(path.join(__dirname, envFileName), 'utf8');
return envContent.split('\n')
.filter(line => line.trim() && !line.startsWith('#') && line.includes('ALCHEMY_API_KEY_'))
.map(line => line.split('=')[1].trim());
}
async function initializeApiKeys() {
await Promise.all(Object.keys(blockchains).map(async blockchain => {
const keys = await loadApiKeysFromEnvFile(`./API_Keys/${blockchain}.env`);
apiKeys[blockchain] = keys;
totalKeysPerFile = Math.max(totalKeysPerFile, keys.length);
}));
}
console.log(`Address to check: ${addressToCheck}`);
class WorkerPool {
constructor(numWorkers) {
this.workers = Array.from({ length: numWorkers }, (_, i) => this.createWorker(i));
this.freeWorkers = [...this.workers];
this.tasks = [];
}
createWorker(index) {
const worker = new Worker(path.join(__dirname, 'worker.js'));
worker.on('message', message => this.onMessage(worker, message));
worker.on('error', err => this.onError(worker, err));
worker.on('exit', code => this.onExit(worker, code, index));
return worker;
}
onMessage(worker, message) {
if (message.type === 'walletCheckResult') {
totalChecked += Number(message.totalChecked);
totalNonZero += Number(message.totalNonZero);
} else if (message.type === 'error') {
totalErrors += 1;
}
this.freeWorkers.push(worker);
this.runNextTask();
}
onError(worker, err) {
console.error(`Worker error: ${err}`);
this.freeWorkers.push(worker);
this.runNextTask();
}
onExit(worker, code, index) {
console.log(`Worker exited with code ${code}`);
this.workers[index] = this.createWorker(index);
this.runNextTask();
}
runNextTask() {
if (this.freeWorkers.length && this.tasks.length) {
const worker = this.freeWorkers.pop();
worker.postMessage(this.tasks.shift());
}
}
addTask(task) {
this.tasks.push(task);
this.runNextTask();
}
}
async function main() {
await initializeApiKeys();
const keysPerTypePerWorker = Math.floor(totalKeysPerFile / numWorkers);
const pool = new WorkerPool(numWorkers);
for (let i = 0; i < numWorkers; i++) {
const workerApiKeys = {};
Object.keys(apiKeys).forEach(blockchain => {
const start = i * keysPerTypePerWorker;
workerApiKeys[blockchain] = apiKeys[blockchain].slice(start, start + keysPerTypePerWorker);
});
pool.addTask({ blockchains, apiKeys: workerApiKeys, address: addressToCheck, maxRetries, retryDelay, networkErrorRetryDelay, concurrencyLimit, minBalance });
}
(async () => {
const { default: logUpdate } = await import('log-update');
const { default: chalk } = await import('chalk');
const { default: boxen } = await import('boxen');
console.clear();
let lastCheckedCount = 0;
function displayStats() {
const checkedThisSecond = totalChecked - lastCheckedCount;
const formattedLines = [
chalk.blue(`Score: ${chalk.bold(checkedThisSecond.toLocaleString('en-US'))}`),
chalk.green(`Result: ${chalk.bold(totalNonZero.toLocaleString('en-US'))}`),
chalk.magenta(`Blockchains: ${chalk.bold(Object.keys(blockchains).length)}`),
chalk.yellow(`Workers: ${chalk.bold(numWorkers)}`),
chalk.cyan(`Keys per worker: ${chalk.bold(keysPerTypePerWorker * Object.keys(blockchains).length)}`),
chalk.red(`Errors: ${chalk.bold(totalErrors)}`)
];
logUpdate(boxen(formattedLines.join('\n'), {
padding: 1,
margin: 1,
borderStyle: 'round',
borderColor: 'blue',
align: 'left'
}));
lastCheckedCount = totalChecked;
}
setInterval(displayStats, 1000);
})();
}
main();