-
-
Save l00k/3c3ecb56b851c7ba20cd1f4e485a30a9 to your computer and use it in GitHub Desktop.
Submit node sync progress to telegram bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const http = require('http'); | |
function request (method, url, body) | |
{ | |
const bodyRaw = JSON.stringify(body); | |
const urlOptions = new URL(url); | |
const options = { | |
protocol: urlOptions.protocol, | |
host: urlOptions.host, | |
hostname: urlOptions.hostname, | |
port: urlOptions.port, | |
path: urlOptions.pathname, | |
method, | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': Buffer.byteLength(bodyRaw) | |
} | |
} | |
return new Promise((resolve, reject) => { | |
let rawData = ''; | |
const req = http.request(options, (resp) => { | |
resp.setEncoding('utf8'); | |
resp.on('data', (chunk) => { | |
rawData += chunk; | |
}); | |
resp.on('end', () => { | |
if (resp.headers['content-type'].includes('json')) { | |
resolve({ response: resp, data: JSON.parse(rawData) }); | |
} | |
else { | |
resolve({ response: resp, data: rawData }); | |
} | |
}); | |
}); | |
req.on('error', (e) => { | |
reject(e); | |
}); | |
req.write(bodyRaw); | |
req.end(); | |
}) | |
} | |
const nodeRpcRequest = async (url, rpcMethod, params = []) => { | |
const body = { | |
jsonrpc: '2.0', | |
id: 1, | |
method: rpcMethod, | |
params, | |
}; | |
const { response, data } = await request('POST', url, body); | |
return data.result; | |
} | |
(async() => { | |
const nodeState = { | |
nodeKey: process.env.NODE_KEY, | |
relayChain: { | |
peers: 0, | |
highestBlock: 0, | |
finalizedBlock: 0, | |
}, | |
paraChain: { | |
peers: 0, | |
highestBlock: 0, | |
finalizedBlock: 0, | |
} | |
}; | |
{ | |
const response = await nodeRpcRequest( | |
process.env.NODE_RELAYCHAIN_URL, | |
'system_health' | |
); | |
nodeState.relayChain.peers = response.peers; | |
} | |
{ | |
const response = await nodeRpcRequest( | |
process.env.NODE_RELAYCHAIN_URL, | |
'system_syncState' | |
); | |
nodeState.relayChain.finalizedBlock = response.currentBlock; | |
nodeState.relayChain.highestBlock = response.highestBlock; | |
} | |
{ | |
const response = await nodeRpcRequest( | |
process.env.NODE_PARACHAIN_URL, | |
'system_health' | |
); | |
nodeState.paraChain.peers = response.peers; | |
} | |
{ | |
const response = await nodeRpcRequest( | |
process.env.NODE_PARACHAIN_URL, | |
'system_syncState' | |
); | |
nodeState.paraChain.finalizedBlock = response.currentBlock; | |
nodeState.paraChain.highestBlock = response.highestBlock; | |
} | |
const { response, data } = await request( | |
'POST', | |
process.env.API_URL, | |
nodeState | |
); | |
console.log(response.statusCode, response.statusMessage); | |
if (response.statusCode !== 200) { | |
console.dir(data, { depth: 10 }); | |
} | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run this script in < 15minutes frames (5 minutes should be ok)