mirror of
				https://github.com/Instadapp/chains.git
				synced 2024-07-29 22:37:19 +00:00 
			
		
		
		
	refactor scripts
This commit is contained in:
		
							parent
							
								
									108129d5a8
								
							
						
					
					
						commit
						1461fc98d6
					
				
							
								
								
									
										34
									
								
								scripts/count.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								scripts/count.js
									
									
									
									
									
										Normal 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) | ||||||
|  | }) | ||||||
|  | @ -5,7 +5,7 @@ const BigNumber = require('bignumber.js') | ||||||
| 
 | 
 | ||||||
| require('dotenv').config() | require('dotenv').config() | ||||||
| 
 | 
 | ||||||
| const ROOT_DIRECTORY = path.join(__dirname, '../') | const ROOT_DIRECTORY = path.join(__dirname, '../../') | ||||||
| 
 | 
 | ||||||
| const CHAINS_DIRECTORY = path.join(ROOT_DIRECTORY, './_data/chains') | const CHAINS_DIRECTORY = path.join(ROOT_DIRECTORY, './_data/chains') | ||||||
| 
 | 
 | ||||||
|  | @ -23,6 +23,41 @@ const CHAIN_ID_REQ = { | ||||||
|   params: [] |   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) { | function formatRpcUrl (rpcUrl) { | ||||||
|   return rpcUrl.replace( |   return rpcUrl.replace( | ||||||
|     '${INFURA_API_KEY}', // eslint-disable-line
 |     '${INFURA_API_KEY}', // eslint-disable-line
 | ||||||
|  | @ -123,36 +158,21 @@ async function verifyJson (json) { | ||||||
|   return json |   return json | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| fs.readdir(CHAINS_DIRECTORY, function (err, files) { | module.exports = { | ||||||
|   if (err) { |   ROOT_DIRECTORY, | ||||||
|     console.error('Could not list the directory.', err) |   CHAINS_DIRECTORY, | ||||||
|     process.exit(1) |   NET_VERSION_REQ, | ||||||
|   } |   CHAIN_ID_REQ, | ||||||
| 
 |   padRight, | ||||||
|   files.forEach(function (file, index) { |   paddedLog, | ||||||
|     const filePath = path.join(CHAINS_DIRECTORY, file) |   tableLog, | ||||||
| 
 |   stat, | ||||||
|     fs.stat(filePath, async function (error, stat) { |   formatRpcUrl, | ||||||
|       if (error) { |   writeJson, | ||||||
|         console.error('Error stating file.', error) |   rpcRequest, | ||||||
|         return |   toNumber, | ||||||
|       } |   getNetworkId, | ||||||
| 
 |   getChainId, | ||||||
|       const ext = path.extname(file) |   queryMulti, | ||||||
|       if (stat.isFile() && ext === '.json') { |   verifyJson | ||||||
|         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) |  | ||||||
|       } |  | ||||||
|     }) |  | ||||||
|   }) |  | ||||||
| }) |  | ||||||
							
								
								
									
										46
									
								
								scripts/verify.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								scripts/verify.js
									
									
									
									
									
										Normal 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' : ''}` | ||||||
|  |   ) | ||||||
|  | }) | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Pedro Gomes
						Pedro Gomes