forked from jorgecuesta/chains_tester
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
137 lines (112 loc) · 4.19 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
131
132
133
134
135
136
137
const axios = require('axios')
const web3 = require('web3')
const _ = require('lodash')
const fs = require('fs')
const path = require('path')
const commander = require('commander')
const pkg = require('./package.json')
const {CHAIN_SYNCING_PAYLOAD, CHAIN_TYPE_PAYLOAD, CHAIN_MAP} = require('./constants')
commander.program.version(pkg.version)
commander.program.showHelpAfterError()
const myParseInt = (value) => {
// parseInt takes a string and a radix
const parsedValue = parseInt(value, 10)
if (isNaN(parsedValue)) {
throw new commander.InvalidArgumentError('Not a number.')
}
return parsedValue
}
const parsePath = (pathStr) => {
const chainsFilePath = path.resolve(pathStr)
if (!fs.existsSync(chainsFilePath)) {
throw new commander.InvalidArgumentError(`Provided file does not exists: ${chainsFilePath}`)
}
return chainsFilePath
}
commander.program
.option('-i, --interval <minutes>', 'interval in minutes', myParseInt, 5)
.option('-c, --chains <path>', 'chains file path', parsePath, './chains.json')
commander.program.parse(process.argv)
const options = commander.program.opts()
const chainsFilePath = options.chains
const check = async (chainData) => {
const chain = CHAIN_MAP[chainData.id]
const heightPayload = CHAIN_TYPE_PAYLOAD[chain.type]
const syncingPayload = CHAIN_SYNCING_PAYLOAD[chain.type]
if (!CHAIN_TYPE_PAYLOAD[chain.type]) {
console.log(`Chain ${chain.name} not supported yet. Skipping...`)
return
}
let syncStatus = 'Unavailable'
if (syncingPayload) {
let urlExt = chain.type === 'avax' ? '/ext/info' : ''
const syncingResponse = await axios.post(chainData.url + urlExt, syncingPayload, {
auth: chainData.basic_auth,
headers: {
'Content-Type': 'application/json',
},
})
if (syncingResponse.status !== 200) console.log(`${chain.name} returns non 200 code for syncing status`)
else {
if (chain.type === 'eth' || chain.type === 'hmy') {
syncStatus = syncingResponse.data.result === false ? 'Sync' : 'Syncing'
} else if (chain.type === 'avax') {
syncStatus = syncingResponse.data.result.isBootstrapped === true ? 'Sync' : 'Syncing'
} else if (chain.type === 'sol') {
syncStatus = syncingResponse.data.result === 'ok' ? 'Sync' : 'Syncing'
}
}
}
let height = 'Unavailable'
if (chain.type === 'avax' && syncStatus === 'Sync') {
const heightResponse = await axios.post(chainData.url + '/ext/P', heightPayload, {
auth: chainData.basic_auth,
headers: {
'Content-Type': 'application/json',
},
})
if (heightResponse.status !== 200) console.log(`${chain.name} returns non 200 code.`)
height = heightResponse.data.result.height
} else if (chain.type !== 'avax') {
const heightResponse = await axios.post(chainData.url, heightPayload, {
auth: chainData.basic_auth,
headers: {
'Content-Type': 'application/json',
},
})
if (heightResponse.status !== 200) console.log(`${chain.name} returns non 200 code.`)
if (chain.type === 'eth' || chain.type === 'hmy') {
height = web3.utils.toNumber(heightResponse.data.result)
} else if(chain.type === 'sol') {
height = web3.utils.toNumber(heightResponse.data.result)
} else {
height = JSON.stringify(heightResponse.data)
}
}
console.log(`${chain.name}: Height = ${height} Sync Status = ${syncStatus}`)
}
const run = async () => {
console.log(`reading chains from: ${chainsFilePath}`)
let rawData = fs.readFileSync(chainsFilePath)
let chains = JSON.parse(rawData)
const orderedChains = _.orderBy(chains, [ 'id' ], [ 'asc' ])
for (let i = 0; i < orderedChains.length; i++) {
const chainData = chains[i]
try {
await check(chainData)
} catch (e) {
console.log(`${CHAIN_MAP[chainData.id].name}: Sync Status Check Error = ${e.message}`)
}
}
console.log('chains check done')
}
(() => {
console.log('starting chains check...')
run().then(() => {
console.log(`Scheduling next check in ${options.interval} minutes`)
setInterval(() => {
console.log('starting chains check...')
run().catch(e => console.error(e))
}, 1000 * 60 * options.interval)
}).catch(e => console.error(e))
})()