From 012c383451b31bd80177e6ad5994f8ff574470c7 Mon Sep 17 00:00:00 2001 From: Pedro Gomes Date: Mon, 17 Jun 2019 18:09:18 +0200 Subject: [PATCH] fix script\ --- _data/chains/100.json | 2 +- _data/chains/30.json | 2 +- _data/chains/31.json | 2 +- _data/chains/64.json | 2 +- _data/chains/76.json | 2 +- _data/chains/77.json | 2 +- _data/chains/99.json | 2 +- script/index.js | 76 +++++++++++++++++++++++-------------------- 8 files changed, 48 insertions(+), 42 deletions(-) diff --git a/_data/chains/100.json b/_data/chains/100.json index 09448efa..07f656fb 100644 --- a/_data/chains/100.json +++ b/_data/chains/100.json @@ -14,5 +14,5 @@ "infoURL": "https://forum.poa.network/c/xdai-chain", "shortName": "xdai", "chainId": 100, - "networkId": 1 + "networkId": 100 } \ No newline at end of file diff --git a/_data/chains/30.json b/_data/chains/30.json index b192ceae..ad357bd0 100644 --- a/_data/chains/30.json +++ b/_data/chains/30.json @@ -15,5 +15,5 @@ "infoURL": "https://rsk.co", "shortName": "rsk", "chainId": 30, - "networkId": 775 + "networkId": 30 } \ No newline at end of file diff --git a/_data/chains/31.json b/_data/chains/31.json index 71450107..79b40305 100644 --- a/_data/chains/31.json +++ b/_data/chains/31.json @@ -17,5 +17,5 @@ "infoURL": "https://rsk.co", "shortName": "trsk", "chainId": 31, - "networkId": 8052 + "networkId": 31 } \ No newline at end of file diff --git a/_data/chains/64.json b/_data/chains/64.json index 3a3ab6de..b8965b13 100644 --- a/_data/chains/64.json +++ b/_data/chains/64.json @@ -14,5 +14,5 @@ "infoURL": "https://ellaism.org", "shortName": "ella", "chainId": 64, - "networkId": 1 + "networkId": 64 } \ No newline at end of file diff --git a/_data/chains/76.json b/_data/chains/76.json index 8aa58632..77ccac9e 100644 --- a/_data/chains/76.json +++ b/_data/chains/76.json @@ -14,5 +14,5 @@ "infoURL": "https://mix-blockchain.org", "shortName": "mix", "chainId": 76, - "networkId": 1 + "networkId": 76 } \ No newline at end of file diff --git a/_data/chains/77.json b/_data/chains/77.json index b2c65771..61d4c328 100644 --- a/_data/chains/77.json +++ b/_data/chains/77.json @@ -16,5 +16,5 @@ "infoURL": "https://poa.network", "shortName": "poa", "chainId": 77, - "networkId": 1 + "networkId": 77 } \ No newline at end of file diff --git a/_data/chains/99.json b/_data/chains/99.json index 6627ec97..45be7b29 100644 --- a/_data/chains/99.json +++ b/_data/chains/99.json @@ -14,5 +14,5 @@ "infoURL": "https://poa.network", "shortName": "skl", "chainId": 99, - "networkId": 2 + "networkId": 99 } \ No newline at end of file diff --git a/script/index.js b/script/index.js index 55cfcf23..7f9bec6f 100644 --- a/script/index.js +++ b/script/index.js @@ -31,7 +31,7 @@ function formatRpcUrl (rpcUrl) { } async function writeJson (filePath, json) { - console.log('Overwriting', filePath) + // console.log('Overwriting', filePath) return new Promise((resolve, reject) => { const data = JSON.stringify(json, null, 2) fs.writeFile(filePath, data, (err, res) => { @@ -91,6 +91,38 @@ async function getChainId (rpcUrl) { } } +async function queryMulti (urls, apiCall) { + let result = null + let results = await Promise.all( + urls.map(async url => { + try { + return await apiCall(url) + } catch (error) { + return null + } + }) + ) + if (results && results.length) { + results = results.filter(x => !!x) + result = results[0] || null + } + return result +} + +async function verifyJson (json) { + if (json.rpc && json.rpc.length) { + const chainId = await queryMulti(json.rpc, getChainId) + if (chainId) { + json.chainId = chainId + } + const networkId = await queryMulti(json.rpc, getNetworkId) + if (networkId) { + json.networkId = networkId + } + } + return json +} + fs.readdir(CHAINS_DIRECTORY, function (err, files) { if (err) { console.error('Could not list the directory.', err) @@ -108,43 +140,17 @@ fs.readdir(CHAINS_DIRECTORY, function (err, files) { const ext = path.extname(file) if (stat.isFile() && ext === '.json') { - const json = require(filePath) - console.log('Verifying file', file, 'through file name') + let json = require(filePath) const fileName = file.replace(ext, '') - if (toNumber(fileName) !== toNumber(json.chainId)) { - console.log(`File ${file} chainId doesn't match file name`) + if (toNumber(fileName)) { json.chainId = toNumber(fileName) } - - if (json.rpc && json.rpc.length) { - console.log('Verifying file', file, 'through rpc url') - try { - const chainIdArr = await Promise.all( - json.rpc.map(rpcUrl => getChainId(rpcUrl)) - ).filter(x => !!x) - const chainId = - chainIdArr && chainIdArr.length ? chainIdArr[0] : null - if (chainId && chainId !== toNumber(json.chainId)) { - console.log(`File ${file} chainId doesn't match rpc response`) - json.chainId = chainId - } - } catch (error) { - // do nothing - } - try { - const networkIdArr = await Promise.all( - json.rpc.map(rpcUrl => getNetworkId(rpcUrl)) - ).filter(x => !!x) - const networkId = - networkIdArr && networkIdArr.length ? networkIdArr[0] : null - if (networkId && networkId !== toNumber(json.networkId)) { - console.log(`File ${file} networkId doesn't match rpc response`) - json.networkId = networkId - } - } catch (error) { - // do nothing - } - } + json = await verifyJson(json) + console.log( + `${json.chain.toUpperCase()} chainId=${json.chainId} networId=${ + json.networkId + }` + ) await writeJson(filePath, json) } })