Skip to content

Commit

Permalink
Merge pull request #8 from gretzke/feat/custom-broadcast
Browse files Browse the repository at this point in the history
Add support for a custom broadcast directory
  • Loading branch information
ZeroEkkusu authored Oct 11, 2024
2 parents c2be7a4 + 8910482 commit d1fb566
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 13 deletions.
16 changes: 8 additions & 8 deletions extractor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ Note: Only TransparentUpgradeableProxy by OpenZeppelin is supported at the momen
*/

// Note: Do not force in production.
async function extractAndSaveJson(scriptName, chainId, rpcUrl, force) {
async function extractAndSaveJson(scriptName, chainId, rpcUrl, force, broadcastDir, outDir) {
// ========== PREPARE FILES ==========

// Latest broadcast
const filePath = path.join(__dirname, `../../broadcast/${scriptName}/${chainId}/run-latest.json`);
const filePath = path.join(__dirname, `../../${broadcastDir}/${scriptName}/${chainId}/run-latest.json`);
const jsonData = JSON.parse(fs.readFileSync(filePath, "utf-8"));

// Previously extracted data
Expand Down Expand Up @@ -115,7 +115,7 @@ async function extractAndSaveJson(scriptName, chainId, rpcUrl, force) {
proxyType: matchedItem.proxyType,
deploymentTxn: matchedItem.deploymentTxn,
input: {
constructor: matchConstructorInputs(getABI(contractName), currentTransaction.arguments),
constructor: matchConstructorInputs(getABI(contractName, outDir), currentTransaction.arguments),
},
};

Expand All @@ -136,7 +136,7 @@ async function extractAndSaveJson(scriptName, chainId, rpcUrl, force) {
version: await getVersion(currentTransaction.contractAddress, rpcUrl),
deploymentTxn: currentTransaction.hash,
input: {
constructor: matchConstructorInputs(getABI(contractName), currentTransaction.arguments),
constructor: matchConstructorInputs(getABI(contractName, outDir), currentTransaction.arguments),
},
};

Expand Down Expand Up @@ -173,7 +173,7 @@ async function extractAndSaveJson(scriptName, chainId, rpcUrl, force) {
proxyType: nextTransaction.contractName,
deploymentTxn: nextTransaction.hash,
input: {
constructor: matchConstructorInputs(getABI(contractName), currentTransaction.arguments),
constructor: matchConstructorInputs(getABI(contractName, outDir), currentTransaction.arguments),
initializeData: nextTransaction.arguments[2],
},
};
Expand All @@ -199,7 +199,7 @@ async function extractAndSaveJson(scriptName, chainId, rpcUrl, force) {
version: await getVersion(currentTransaction.contractAddress, rpcUrl),
deploymentTxn: currentTransaction.hash,
input: {
constructor: matchConstructorInputs(getABI(contractName), currentTransaction.arguments),
constructor: matchConstructorInputs(getABI(contractName, outDir), currentTransaction.arguments),
},
};

Expand Down Expand Up @@ -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;
Expand Down
33 changes: 28 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ const { generateAndSaveMarkdown } = require("./generateMarkdown.js");
* foundry (https://getfoundry.sh) required
*/
async function main() {
let [chainId, scriptName, skipJsonFlag, rpcUrl, force] = 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);
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`);
Expand All @@ -41,7 +41,8 @@ function validateAndExtractInputs() {
let skipJsonFlag = false;
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":
Expand Down Expand Up @@ -74,18 +75,40 @@ function validateAndExtractInputs() {
console.error("Error: --rpc-url flag requires an rpc url");
process.exit(1);
}
case "-b":
case "--broadcast-dir":
// Check if there's another argument after --broadcast-dir and the argument is not another command
if (i + 1 < args.length && args[i + 1].charAt(0) !== "-") {
broadcastDir = args[i + 1];
i++; // Skip the next argument
break;
} else {
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];
return [chainId, scriptName, skipJsonFlag, rpcUrl, forceFlag, broadcastDir, outDir];
}

const printHelp = () => {
console.log(
"\nUsage: node lib/forge-chronicles <scriptName> [-c chain-id] [-r rpc-url] [-s skip-json]\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\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 <scriptName> [-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",
);
};

Expand Down

0 comments on commit d1fb566

Please sign in to comment.