diff --git a/extractor.js b/extractor.js index 3c5c52d..232fe2b 100644 --- a/extractor.js +++ b/extractor.js @@ -14,7 +14,7 @@ Note: Only TransparentUpgradeableProxy by OpenZeppelin is supported at the momen */ // Note: Do not force in production. -async function extractAndSaveJson(scriptName, chainId, rpcUrl, force, broadcastDir) { +async function extractAndSaveJson(scriptName, chainId, rpcUrl, force, broadcastDir, outDir) { // ========== PREPARE FILES ========== // Latest broadcast @@ -115,7 +115,7 @@ async function extractAndSaveJson(scriptName, chainId, rpcUrl, force, broadcastD proxyType: matchedItem.proxyType, deploymentTxn: matchedItem.deploymentTxn, input: { - constructor: matchConstructorInputs(getABI(contractName), currentTransaction.arguments), + constructor: matchConstructorInputs(getABI(contractName, outDir), currentTransaction.arguments), }, }; @@ -136,7 +136,7 @@ async function extractAndSaveJson(scriptName, chainId, rpcUrl, force, broadcastD version: await getVersion(currentTransaction.contractAddress, rpcUrl), deploymentTxn: currentTransaction.hash, input: { - constructor: matchConstructorInputs(getABI(contractName), currentTransaction.arguments), + constructor: matchConstructorInputs(getABI(contractName, outDir), currentTransaction.arguments), }, }; @@ -173,7 +173,7 @@ async function extractAndSaveJson(scriptName, chainId, rpcUrl, force, broadcastD proxyType: nextTransaction.contractName, deploymentTxn: nextTransaction.hash, input: { - constructor: matchConstructorInputs(getABI(contractName), currentTransaction.arguments), + constructor: matchConstructorInputs(getABI(contractName, outDir), currentTransaction.arguments), initializeData: nextTransaction.arguments[2], }, }; @@ -199,7 +199,7 @@ async function extractAndSaveJson(scriptName, chainId, rpcUrl, force, broadcastD version: await getVersion(currentTransaction.contractAddress, rpcUrl), deploymentTxn: currentTransaction.hash, input: { - constructor: matchConstructorInputs(getABI(contractName), currentTransaction.arguments), + constructor: matchConstructorInputs(getABI(contractName, outDir), currentTransaction.arguments), }, }; @@ -309,8 +309,8 @@ function matchConstructorInputs(abi, inputData) { // IN: contract name // OUT: contract ABI -function getABI(contractName) { - const filePath = path.join(__dirname, `../../out/${contractName}.sol/${contractName}.json`); +function getABI(contractName, outDir) { + const filePath = path.join(__dirname, `../../${outDir}/${contractName}.sol/${contractName}.json`); const fileData = fs.readFileSync(filePath, "utf8"); const abi = JSON.parse(fileData).abi; return abi; diff --git a/index.js b/index.js index b8c32de..71a2ebf 100644 --- a/index.js +++ b/index.js @@ -10,12 +10,12 @@ const { generateAndSaveMarkdown } = require("./generateMarkdown.js"); * foundry (https://getfoundry.sh) required */ async function main() { - let [chainId, scriptName, skipJsonFlag, rpcUrl, force, broadcastDir] = validateAndExtractInputs(); + let [chainId, scriptName, skipJsonFlag, rpcUrl, force, broadcastDir, outDir] = validateAndExtractInputs(); if (rpcUrl === undefined) { console.warn("\u{26A0} No rpc url provided, skipping version fetching"); } let json; - if (!skipJsonFlag) json = await extractAndSaveJson(scriptName, chainId, rpcUrl, force, broadcastDir); + if (!skipJsonFlag) json = await extractAndSaveJson(scriptName, chainId, rpcUrl, force, broadcastDir, outDir); else { console.log("Skipping json extraction, using existing json file"); const recordFilePath = path.join(__dirname, `../../deployments/json/${chainId}.json`); @@ -42,6 +42,7 @@ function validateAndExtractInputs() { let chainId = 31337; let rpcUrl = process.env.RPC_URL; let broadcastDir = "broadcast"; + let outDir = "out"; for (let i = 0; i < args.length; i++) { switch (args[i]) { case "--force": @@ -85,18 +86,29 @@ function validateAndExtractInputs() { console.error("Error: --broadcast-dir flag requires a directory path"); process.exit(1); } + case "-o": + case "--out-dir": + // Check if there's another argument after --output-dir and the argument is not another command + if (i + 1 < args.length && args[i + 1].charAt(0) !== "-") { + outDir = args[i + 1]; + i++; // Skip the next argument + break; + } else { + console.error("Error: --out-dir flag requires a directory path"); + process.exit(1); + } default: printHelp(); process.exit(1); } } - return [chainId, scriptName, skipJsonFlag, rpcUrl, forceFlag, broadcastDir]; + return [chainId, scriptName, skipJsonFlag, rpcUrl, forceFlag, broadcastDir, outDir]; } const printHelp = () => { console.log( - "\nUsage: node lib/forge-chronicles [-c chain-id] [-r rpc-url] [-s skip-json] [-b broadcast-dir]\n\nCommands:\n -c, --chain-id\tChain id of the network where the script was executed (default: 31337)\n -r, --rpc-url\t\tRPC url used to fetch the version of the contract or verify an upgrade (default: $RPC_URL). If no rpc url is provided, version fetching is skipped.\n -s, --skip-json\tSkips the json generation and creates the markdown file using an existing json file\n -b, --broadcast-dir\tDirectory where the broadcast files are stored (default: broadcast)\n\nOptions:\n -h, --help\t\tPrint help\n -v, --version\t\tPrint version\n\nDocumentation can be found at https://github.com/0xPolygon/forge-chronicles", + "\nUsage: node lib/forge-chronicles [-c chain-id] [-r rpc-url] [-s skip-json] [-b broadcast-dir] [-o out-dir]\n\nCommands:\n -c, --chain-id\tChain id of the network where the script was executed (default: 31337)\n -r, --rpc-url\t\tRPC url used to fetch the version of the contract or verify an upgrade (default: $RPC_URL). If no rpc url is provided, version fetching is skipped.\n -s, --skip-json\tSkips the json generation and creates the markdown file using an existing json file\n -b, --broadcast-dir\tDirectory where the broadcast files are stored (default: broadcast)\n -o, --out-dir\t\tDirectory where the foundry output files are stored (default: out)\n\nOptions:\n -h, --help\t\tPrint help\n -v, --version\t\tPrint version\n\nDocumentation can be found at https://github.com/0xPolygon/forge-chronicles", ); };