From 0ad1a94740008d8bba778be5140688ad4d8ffaee Mon Sep 17 00:00:00 2001 From: juliopavila Date: Sun, 18 Aug 2024 23:01:59 -0300 Subject: [PATCH] feat: improve hardhat task --- packages/contracts/hardhat.config.ts | 6 +- packages/contracts/package.json | 8 +- packages/contracts/tasks/deploy-mastercopy.ts | 78 +++++++++++++++++++ ...rcopy-extract.ts => extract-mastercopy.ts} | 2 +- packages/contracts/tasks/mastercopy-deploy.ts | 34 -------- packages/contracts/tasks/mastercopy-verify.ts | 18 ----- packages/contracts/tasks/verify-mastercopy.ts | 56 +++++++++++++ 7 files changed, 143 insertions(+), 59 deletions(-) create mode 100644 packages/contracts/tasks/deploy-mastercopy.ts rename packages/contracts/tasks/{mastercopy-extract.ts => extract-mastercopy.ts} (98%) delete mode 100644 packages/contracts/tasks/mastercopy-deploy.ts delete mode 100644 packages/contracts/tasks/mastercopy-verify.ts create mode 100644 packages/contracts/tasks/verify-mastercopy.ts diff --git a/packages/contracts/hardhat.config.ts b/packages/contracts/hardhat.config.ts index f6955e6..e9a789c 100644 --- a/packages/contracts/hardhat.config.ts +++ b/packages/contracts/hardhat.config.ts @@ -10,9 +10,9 @@ dotenv.config(); const { INFURA_KEY, MNEMONIC, ETHERSCAN_API_KEY, PK } = process.env; import "./tasks/setup"; -import "./tasks/mastercopy-deploy"; -import "./tasks/mastercopy-extract"; -import "./tasks/mastercopy-verify"; +import "./tasks/deploy-mastercopy"; +import "./tasks/extract-mastercopy"; +import "./tasks/verify-mastercopy"; const DEFAULT_MNEMONIC = "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat"; diff --git a/packages/contracts/package.json b/packages/contracts/package.json index 2d51cd3..ca3cde2 100644 --- a/packages/contracts/package.json +++ b/packages/contracts/package.json @@ -5,9 +5,11 @@ "scripts": { "build": "hardhat compile", "test": "hardhat test", - "extract-mastercopy": "yarn run build && yarn hardhat mastercopy:extract", - "deploy-mastercopy": "yarn hardhat mastercopy:deploy --network", - "verify-mastercopy": "yarn hardhat mastercopy:verify --network", + "extract-mastercopy": "yarn run build && yarn hardhat extract:mastercopy", + "deploy-mastercopy": "yarn hardhat deploy:contract --network hardhat", + "deploy-last-mastercopy": "yarn hardhat deploy:contract --network hardhat --current", + "verify-mastercopy": "yarn hardhat verify:mastercopy --network", + "verify-last-mastercopy": "yarn hardhat verify:mastercopy --network --current", "coverage": "hardhat coverage", "lint": "yarn lint:sol && yarn lint:ts", "lint:sol": "solhint 'contracts/**/*.sol'", diff --git a/packages/contracts/tasks/deploy-mastercopy.ts b/packages/contracts/tasks/deploy-mastercopy.ts new file mode 100644 index 0000000..70626d1 --- /dev/null +++ b/packages/contracts/tasks/deploy-mastercopy.ts @@ -0,0 +1,78 @@ +import { Signer } from "ethers"; +import { task, types } from "hardhat/config"; +import { EthereumProvider } from "hardhat/types"; + +import { + EIP1193Provider, + deployMastercopy, + deployAllMastercopies, + readMastercopy, +} from "@gnosis-guild/zodiac-core"; + +task( + "deploy:mastercopy", + "For every version entry on the artifacts file, deploys a mastercopy into the current network" +) + .addOptionalParam( + "contractName", + "The name of the contractName to deploy", + types.string + ) + .addFlag( + "current", + "Deploy the latest version from disk instead of using mastercopies.json" //TODO: Improve the docs + ) + .setAction(async ({ current, contractName }, hre) => { + console.log("contractName", contractName); + const [signer] = await hre.ethers.getSigners(); + const provider = createEIP1193(hre.network.provider, signer); + if (current) { + // Logic to deploy the latest version from disk + await deployLatestMastercopyFromDisk(provider, contractName); + } else { + // using mastercopies.json + await deployAllMastercopies({ + provider, + }); + } + }); + +function createEIP1193( + provider: EthereumProvider, + signer: Signer +): EIP1193Provider { + return { + request: async ({ method, params }) => { + if (method == "eth_sendTransaction") { + const { hash } = await signer.sendTransaction((params as any[])[0]); + return hash; + } + + return provider.request({ method, params }); + }, + }; +} + +async function deployLatestMastercopyFromDisk( + provider: EIP1193Provider, + contract: string +) { + const latestArtifact = readMastercopy({ + contractName: contract, + }); + + const { address, noop } = await deployMastercopy({ + ...latestArtifact, + provider, + }); + const { contractName, contractVersion } = latestArtifact; + if (noop) { + console.log( + `🔄 ${contractName}@${contractVersion}: Already deployed at ${address}` + ); + } else { + console.log( + `🚀 ${contractName}@${contractVersion}: Successfully deployed at ${address}` + ); + } +} diff --git a/packages/contracts/tasks/mastercopy-extract.ts b/packages/contracts/tasks/extract-mastercopy.ts similarity index 98% rename from packages/contracts/tasks/mastercopy-extract.ts rename to packages/contracts/tasks/extract-mastercopy.ts index bcd30fc..7e3e2d9 100644 --- a/packages/contracts/tasks/mastercopy-extract.ts +++ b/packages/contracts/tasks/extract-mastercopy.ts @@ -7,7 +7,7 @@ import packageJson from "../package.json"; const AddressOne = "0x0000000000000000000000000000000000000001"; task( - "mastercopy:extract", + "extract:mastercopy", "Extracts and persists current mastercopy build artifacts" ).setAction(async (_, hre) => { writeMastercopyFromBuild({ diff --git a/packages/contracts/tasks/mastercopy-deploy.ts b/packages/contracts/tasks/mastercopy-deploy.ts deleted file mode 100644 index 6080e97..0000000 --- a/packages/contracts/tasks/mastercopy-deploy.ts +++ /dev/null @@ -1,34 +0,0 @@ -import { Signer } from "ethers"; -import { task } from "hardhat/config"; -import { EthereumProvider } from "hardhat/types"; - -import { - EIP1193Provider, - deployAllMastercopies, -} from "@gnosis-guild/zodiac-core"; - -task( - "mastercopy:deploy", - "For every version entry on the artifacts file, deploys a mastercopy into the current network" -).setAction(async (_, hre) => { - const [signer] = await hre.ethers.getSigners(); - await deployAllMastercopies({ - provider: createEIP1193(hre.network.provider, signer), - }); -}); - -function createEIP1193( - provider: EthereumProvider, - signer: Signer -): EIP1193Provider { - return { - request: async ({ method, params }) => { - if (method == "eth_sendTransaction") { - const { hash } = await signer.sendTransaction((params as any[])[0]); - return hash; - } - - return provider.request({ method, params }); - }, - }; -} diff --git a/packages/contracts/tasks/mastercopy-verify.ts b/packages/contracts/tasks/mastercopy-verify.ts deleted file mode 100644 index e926900..0000000 --- a/packages/contracts/tasks/mastercopy-verify.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { task } from "hardhat/config"; -import { verifyAllMastercopies } from "@gnosis-guild/zodiac-core"; - -const { ETHERSCAN_API_KEY } = process.env; - -task( - "mastercopy:verify", - "Verifies all mastercopies from the artifacts file, in the block explorer corresponding to the current network" -).setAction(async (_, hre) => { - if (!ETHERSCAN_API_KEY) { - throw new Error("Missing ENV ETHERSCAN_API_KEY"); - } - - await verifyAllMastercopies({ - apiUrlOrChainId: String((await hre.ethers.provider.getNetwork()).chainId), - apiKey: ETHERSCAN_API_KEY, - }); -}); diff --git a/packages/contracts/tasks/verify-mastercopy.ts b/packages/contracts/tasks/verify-mastercopy.ts new file mode 100644 index 0000000..6bff379 --- /dev/null +++ b/packages/contracts/tasks/verify-mastercopy.ts @@ -0,0 +1,56 @@ +import { task, types } from "hardhat/config"; +import { + readMastercopy, + verifyAllMastercopies, +} from "@gnosis-guild/zodiac-core"; +import path from "path"; +import fs from "fs"; + +const { ETHERSCAN_API_KEY } = process.env; + +task( + "verify:mastercopy", + "Verifies all mastercopies from the artifacts file, in the block explorer corresponding to the current network" +) + .addOptionalParam( + "contractName", + "The name of the contractName to verify", + types.string + ) + .addFlag( + "current", + "Verify the latest version from disk instead of using mastercopies.json" //TODO: Improve the docs + ) + .setAction(async ({ current, contractName }, hre) => { + if (!ETHERSCAN_API_KEY) { + throw new Error("Missing ENV ETHERSCAN_API_KEY"); + } + + const chainId = String((await hre.ethers.provider.getNetwork()).chainId); + + if (current && contractName) { + const latestArtifact = readMastercopy({ + contractName, + }); + const tempFilePath = path.join( + hre.config.paths.cache, + "latest-mastercopy.json" + ); + fs.writeFileSync(tempFilePath, JSON.stringify([latestArtifact], null, 2)); + + const { address } = latestArtifact; + console.log(`Verifying ${contractName} at address ${address}...`); + + await verifyAllMastercopies({ + apiUrlOrChainId: chainId, + apiKey: ETHERSCAN_API_KEY, + mastercopyArtifactsFile: tempFilePath, + }); + fs.unlinkSync(tempFilePath); + } else { + await verifyAllMastercopies({ + apiUrlOrChainId: chainId, + apiKey: ETHERSCAN_API_KEY, + }); + } + });