From cd4ace7d5baeae84b9be2fcc78eb8213819996b1 Mon Sep 17 00:00:00 2001 From: mettete <58823555+mettete@users.noreply.github.com> Date: Tue, 13 Feb 2024 13:46:37 +0300 Subject: [PATCH] Update genAbi.js --- scripts/genAbi.js | 87 +++++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/scripts/genAbi.js b/scripts/genAbi.js index ec8479a2f0..32838eb2d9 100644 --- a/scripts/genAbi.js +++ b/scripts/genAbi.js @@ -1,78 +1,83 @@ -const { runTypeChain, glob } = require('typechain') -const { execSync } = require('child_process') -const { unlinkSync } = require('fs') - -const getPackagePath = packageName => { - const path = require.resolve(`${packageName}/package.json`) - return path.substr(0, path.indexOf('package.json')) -} +const { runTypeChain, glob } = require('typechain'); +const { execSync } = require('child_process'); +const { unlinkSync } = require('fs'); + +/** + * Get the path of a package by its name. + * @param {string} packageName The name of the package. + * @returns {string} The path to the package. + */ +const getPackagePath = (packageName) => { + const path = require.resolve(`${packageName}/package.json`); + return path.substr(0, path.indexOf('package.json')); +}; async function main() { - const cwd = process.cwd() - - const nitroPath = getPackagePath('@arbitrum/nitro-contracts') - const tokenBridgePath = getPackagePath('@arbitrum/token-bridge-contracts') + const cwd = process.cwd(); - console.log('Compiling paths.') + // Retrieve paths for nitro and token bridge contracts + const nitroPath = getPackagePath('@arbitrum/nitro-contracts'); + const tokenBridgePath = getPackagePath('@arbitrum/token-bridge-contracts'); - const npmExec = process.env['npm_execpath'] - if (!npmExec || npmExec === '') - throw new Error( - 'No support for npm_execpath env variable in package manager' - ) + console.log('Compiling contracts.'); - // TODO: use `HARDHAT_ARTIFACT_PATH` to write files to arbitrum sdk instead of the packages themselves. - // this is currently broken since hardhat throws a weird error: - // `Error HH702: Invalid artifact path [...] its correct case-sensitive path is...` - // https://yarnpkg.com/advanced/rulebook#packages-should-never-write-inside-their-own-folder-outside-of-postinstall - // instead of writing in postinstall in each of those packages, we should target a local folder in sdk's postinstall + // Validate npm_execpath environment variable + const npmExec = process.env['npm_execpath']; + if (!npmExec || npmExec === '') { + throw new Error('npm_execpath environment variable is not set or empty. Please ensure your package manager is configured correctly.'); + } - console.log('building nitro') + // Compile contracts for nitro and token bridge + console.log('Building nitro contracts...'); execSync(`${npmExec} run build`, { cwd: nitroPath, - }) + }); - console.log('building token bridge') + console.log('Building token bridge contracts...'); execSync(`${npmExec} run build`, { cwd: tokenBridgePath, - }) + }); - console.log('Done compiling') + console.log('Compilation complete.'); + // Define paths to contract JSON files, excluding build-info const nitroFiles = glob(cwd, [ `${tokenBridgePath}/build/contracts/!(build-info)/**/+([a-zA-Z0-9_]).json`, `${nitroPath}/build/contracts/!(build-info)/**/+([a-zA-Z0-9_]).json`, - ]) + ]); - // TODO: generate files into different subfolders (ie `/nitro/*`) to avoid overwrite of contracts with the same name + // Generate TypeChain typings for nitro contracts await runTypeChain({ cwd, filesToProcess: nitroFiles, allFiles: nitroFiles, outDir: './src/lib/abi/', target: 'ethers-v5', - }) + }); + // Define path for classic contract files const classicFiles = glob(cwd, [ - // we have a hardcoded abi for the old outbox - `./src/lib/dataEntities/Outbox.json`, - ]) + './src/lib/dataEntities/Outbox.json', // Hardcoded ABI for the old outbox + ]); + // Generate TypeChain typings for classic contracts await runTypeChain({ cwd, filesToProcess: classicFiles, allFiles: classicFiles, outDir: './src/lib/abi/classic', target: 'ethers-v5', - }) + }); - // we delete the index file since it doesn't play well with tree shaking - unlinkSync(`${cwd}/src/lib/abi/index.ts`) - unlinkSync(`${cwd}/src/lib/abi/classic/index.ts`) + // Delete index files to improve compatibility with tree shaking + unlinkSync(`${cwd}/src/lib/abi/index.ts`); + unlinkSync(`${cwd}/src/lib/abi/classic/index.ts`); - console.log('Typechain generated') + console.log('TypeChain typings generated.'); } main() - .then(() => console.log('Done.')) - .catch(console.error) + .then(() => console.log('Process completed successfully.')) + .catch((error) => { + console.error('An error occurred:', error); + });