refactor scripts

This commit is contained in:
Pedro Gomes 2019-07-02 12:00:13 +02:00
parent 108129d5a8
commit 1461fc98d6
6 changed files with 134 additions and 34 deletions

34
scripts/count.js Normal file
View File

@ -0,0 +1,34 @@
const fs = require('fs')
const path = require('path')
const { CHAINS_DIRECTORY, stat, tableLog, toNumber } = require('./shared')
fs.readdir(CHAINS_DIRECTORY, async function (err, files) {
if (err) {
console.error('Could not list the directory.', err)
process.exit(1)
}
let matches = []
await Promise.all(
files.map(async function (file, index) {
const filePath = path.join(CHAINS_DIRECTORY, file)
const fileStat = await stat(filePath)
const ext = path.extname(file)
if (fileStat.isFile() && ext === '.json') {
let json = require(filePath)
if (toNumber(json.chainId) !== toNumber(json.networkId)) {
matches.push(json)
}
}
return fileStat
})
)
console.log(
`There are ${files.length} known EVM chains from which ${
matches.length
} have unmatched ids, here are the list of those chains: `
)
tableLog(matches)
})

View File

@ -5,7 +5,7 @@ const BigNumber = require('bignumber.js')
require('dotenv').config()
const ROOT_DIRECTORY = path.join(__dirname, '../')
const ROOT_DIRECTORY = path.join(__dirname, '../../')
const CHAINS_DIRECTORY = path.join(ROOT_DIRECTORY, './_data/chains')
@ -23,6 +23,41 @@ const CHAIN_ID_REQ = {
params: []
}
function padRight (n, width, z) {
z = z || '0'
n = n + ''
return n.length >= width ? n : n + new Array(width - n.length + 1).join(z)
}
function paddedLog (...args) {
let output = ''
args.forEach(arg => {
output += padRight(`${arg}`, 32, ' ')
})
console.log(output)
}
function tableLog (array) {
console.log('\n')
paddedLog('Name', 'Chain', 'ChainId', 'NetworkId')
console.log('\n')
array.map(json =>
paddedLog(json.name, json.chain, json.chainId, json.networkId)
)
console.log('\n')
}
function stat (filePath) {
return new Promise((resolve, reject) => {
fs.stat(filePath, function (error, stat) {
if (error) {
return reject(error)
}
resolve(stat)
})
})
}
function formatRpcUrl (rpcUrl) {
return rpcUrl.replace(
'${INFURA_API_KEY}', // eslint-disable-line
@ -123,36 +158,21 @@ async function verifyJson (json) {
return json
}
fs.readdir(CHAINS_DIRECTORY, function (err, files) {
if (err) {
console.error('Could not list the directory.', err)
process.exit(1)
}
files.forEach(function (file, index) {
const filePath = path.join(CHAINS_DIRECTORY, file)
fs.stat(filePath, async function (error, stat) {
if (error) {
console.error('Error stating file.', error)
return
}
const ext = path.extname(file)
if (stat.isFile() && ext === '.json') {
let json = require(filePath)
const fileName = file.replace(ext, '')
if (toNumber(fileName)) {
json.chainId = toNumber(fileName)
}
json = await verifyJson(json)
console.log(
`${json.chain.toUpperCase()} chainId=${json.chainId} networId=${
json.networkId
}`
)
await writeJson(filePath, json)
}
})
})
})
module.exports = {
ROOT_DIRECTORY,
CHAINS_DIRECTORY,
NET_VERSION_REQ,
CHAIN_ID_REQ,
padRight,
paddedLog,
tableLog,
stat,
formatRpcUrl,
writeJson,
rpcRequest,
toNumber,
getNetworkId,
getChainId,
queryMulti,
verifyJson
}

46
scripts/verify.js Normal file
View File

@ -0,0 +1,46 @@
const fs = require('fs')
const path = require('path')
const {
CHAINS_DIRECTORY,
tableLog,
stat,
writeJson,
toNumber,
verifyJson
} = require('./shared')
fs.readdir(CHAINS_DIRECTORY, async function (err, files) {
if (err) {
console.error('Could not list the directory.', err)
process.exit(1)
}
console.log('Verifying:', CHAINS_DIRECTORY)
let result = []
await Promise.all(
files.map(async function (file, index) {
const filePath = path.join(CHAINS_DIRECTORY, file)
const fileStat = await stat(filePath)
const ext = path.extname(file)
if (fileStat.isFile() && ext === '.json') {
let json = require(filePath)
const fileName = file.replace(ext, '')
if (toNumber(fileName)) {
json.chainId = toNumber(fileName)
}
json = await verifyJson(json)
await writeJson(filePath, json)
result.push(json)
}
return fileStat
})
)
tableLog(result)
console.log(
`Successfully verified ${files.length} file${files.length > 1 ? 's' : ''}`
)
})