forked from CryptoLover705/SmartCryptoTech-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pools.js
186 lines (171 loc) · 6.25 KB
/
pools.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// Copyright (c) 2020-2022, Taegus Cromis, The Conceal Developers
//
// Please see the included LICENSE file for more information.
import path, { dirname } from "path";
import { fileURLToPath } from 'url';
import fs from "graceful-fs";
import axios from "axios";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const request = axios.create({
timeout: 2000, // 2 seconds
headers:{
'User-Agent': 'Conceal Services'
}
});
function shuffle(a) {
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
return a;
}
export function getPoolList(resultCallback) {
try {
fs.readFile(path.join(__dirname, 'data', 'pools.json'), 'utf8', function (err, data) {
if (err) {
resultCallback({success: false, error: err});
} else {
resultCallback({success: true, data: shuffle(JSON.parse(data).ccx)});
}
});
} catch(err) {
resultCallback({success: false, error: err});
}
};
export function getPoolData(resultCallback) {
fs.readFile(path.join(__dirname, 'data', 'pools.json'), 'utf8', function (err, data) {
if (err) {
resultCallback({success: false, error: err});
} else {
var poolData = [];
var counter = 0;
shuffle(JSON.parse(data).ccx).forEach(function (element, index, array) {
var host = element[0];
var urls = element[1];
var name = element[2];
var version = element[3];
function checkForQueryFinished() {
counter++;
if (counter === array.length) {
resultCallback({success: true, data: poolData});
}
}
request.get(urls[0]).then(response => {
let data = response.data;
switch (version) {
case "1":
try {
if (!data.pool) {
data.pool = {};
}
poolData.push({
'info': {
'host': host,
'name': name
},
'network': {
'height': data.network ? (data.network.height || 0) : 0,
},
'pool': {
'lastBlockFound': parseInt(data.pool.lastBlockFound || 0),
'hashrate': data.pool.soloHashrate ? (data.pool.soloHashrate + (data.pool.hashrate || 0)) : (data.pool.hashrate || 0),
'miners': data.pool.soloMiners ? (data.pool.soloMiners + (data.pool.miners || 0)) : (data.pool.miners || 0)
},
'config': {
'minPaymentThreshold': data.config ? data.config.minPaymentThreshold || 0 : 0,
'poolFee': data.config ? data.config.fee || 0 : 0
}
});
checkForQueryFinished();
} catch (err) {
console.log(`Error trying to parse pool info: ${err}`);
checkForQueryFinished();
}
break;
case "2":
try {
var dataObject = {
'info': {
'host': host,
'name': name
},
'network': {
'height': '',
},
'pool': {
'lastBlockFound': data.pool_statistics ? parseInt(data.pool_statistics.lastBlockFoundTime || 0) * 1000 : 0,
'hashrate': data.pool_statistics ? data.pool_statistics.hashRate || 0 : 0,
'miners': data.pool_statistics ? data.pool_statistics.miners || 0 : 0
},
'config': {
'minPaymentThreshold': '',
'poolFee': ''
}
};
} catch (err) {
console.log(`Error trying to parse pool info: ${err}`);
}
request.get(urls[1]).then(response => {
let network = response.data;
dataObject.network.height = network.height || 0;
request.get(urls[2]).then(response => {
let config = response.data;
try {
dataObject.config.minPaymentThreshold = config.min_wallet_payout || 0;
dataObject.config.poolFee = config.pplns_fee || 0;
poolData.push(dataObject);
checkForQueryFinished();
} catch (err) {
console.log(`Error trying to parse pool info: ${err}`);
checkForQueryFinished();
}
}).catch(err => {
console.log(urls[2] + ' -> Status:', err.message);
checkForQueryFinished();
});
}).catch(err => {
console.log(urls[1] + ' -> Status:', err.message);
checkForQueryFinished();
});
break;
case "3":
try {
poolData.push({
'info': {
'host': host,
'name': name
},
'network': {
'height': data.network_statistics.height || 0,
},
'pool': {
'lastBlockFound': parseInt(data.pool_statistics.collective.lastFoundBlock.ts || 0) * 1000,
'hashrate': data.pool_statistics.collective.hashRate || 0,
'miners': data.pool_statistics.collective.miners || 0
},
'config': {
'minPaymentThreshold': data.config.min_wallet_payout || 0,
'poolFee': data.config.pplns_fee || 0
}
});
checkForQueryFinished();
} catch (err) {
console.log(`Error trying to parse pool info: ${err}`);
checkForQueryFinished();
}
break;
default:
resultCallback({success: false, error: { message: "wrong version" }});
}
}).catch(err => {
console.log(urls[0] + ' -> Status:', err.message);
checkForQueryFinished();
});
});
}
});
};