Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements to genAbi.js for Better Readability and Error Handling #412

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 46 additions & 41 deletions scripts/genAbi.js
Original file line number Diff line number Diff line change
@@ -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);
});