Skip to content

Commit

Permalink
feat: improve hardhat task
Browse files Browse the repository at this point in the history
  • Loading branch information
juliopavila committed Aug 19, 2024
1 parent 90fb019 commit 0ad1a94
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 59 deletions.
6 changes: 3 additions & 3 deletions packages/contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
8 changes: 5 additions & 3 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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'",
Expand Down
78 changes: 78 additions & 0 deletions packages/contracts/tasks/deploy-mastercopy.ts
Original file line number Diff line number Diff line change
@@ -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}`
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
34 changes: 0 additions & 34 deletions packages/contracts/tasks/mastercopy-deploy.ts

This file was deleted.

18 changes: 0 additions & 18 deletions packages/contracts/tasks/mastercopy-verify.ts

This file was deleted.

56 changes: 56 additions & 0 deletions packages/contracts/tasks/verify-mastercopy.ts
Original file line number Diff line number Diff line change
@@ -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,
});
}
});

0 comments on commit 0ad1a94

Please sign in to comment.