-
Notifications
You must be signed in to change notification settings - Fork 11
/
check-coin-explorers-availability.js
57 lines (45 loc) · 1.56 KB
/
check-coin-explorers-availability.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
const fetch = require('node-fetch');
const coins = require('./src/lib/coins');
let coinsToRemove = [];
(async () => {
const getInfo = async explorerUrl => {
try {
const response = await fetch(`${explorerUrl}/status?q=getInfo`);
const isJson = response.headers.get('Content-Type').includes('application/json');
const body = isJson ? await response.json() : await response.text();
if (!response.ok) {
throw new Error(body);
}
return body;
} catch (e) {
return null;
}
};
const checkApiEndpoints = async coin => {
const getInfoRes = await Promise.all(coins[coin].api.map((value) => {
return getInfo(value);
}));
let longestBlockHeight = 0;
let apiEndPointIndex = null;
console.log('checkExplorerEndpoints', getInfoRes);
for (let i = 0; i < coins[coin].api.length; i++) {
if (getInfoRes[i] &&
'info' in getInfoRes[i] &&
'version' in getInfoRes[i].info) {
if (getInfoRes[i].info.blocks > longestBlockHeight) {
longestBlockHeight = getInfoRes[i].info.blocks;
apiEndPointIndex = i;
}
}
}
console.log('apiEndPointIndex', apiEndPointIndex);
return apiEndPointIndex !== null ? coins[coin].api[apiEndPointIndex] : null;
};
for (let coin in coins) {
const explorerUrl = await checkApiEndpoints(coin);
if (!explorerUrl) coinsToRemove.push(coin)
console.log(`${coin} explorer url`, explorerUrl);
}
console.log('all done!');
console.log('coins to remove', coinsToRemove);
})();