Skip to content

Commit

Permalink
feat: refactor hardhat tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
juliopavila committed Aug 19, 2024
1 parent 0ad1a94 commit 136c272
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 421 deletions.
3 changes: 2 additions & 1 deletion packages/contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import type { HttpNetworkUserConfig } from "hardhat/types";
dotenv.config();
const { INFURA_KEY, MNEMONIC, ETHERSCAN_API_KEY, PK } = process.env;

import "./tasks/setup";
import "./tasks/deploy-mastercopies";
import "./tasks/deploy-mastercopy";
import "./tasks/extract-mastercopy";
import "./tasks/verify-mastercopies";
import "./tasks/verify-mastercopy";

const DEFAULT_MNEMONIC =
Expand Down
16 changes: 9 additions & 7 deletions packages/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
"build": "hardhat compile",
"test": "hardhat test",
"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",
"deploy-mastercopies": "yarn hardhat deploy:mastercopies --network",
"deploy-mastercopy": "yarn hardhat deploy:mastercopy --network",
"verify-mastercopies": "yarn hardhat verify:mastercopies --network",
"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 All @@ -26,8 +26,8 @@
"@gnosis-guild/zodiac-core": "1.1.0",
"@nomicfoundation/hardhat-chai-matchers": "^2.0.7",
"@nomicfoundation/hardhat-ethers": "^3.0.6",
"@nomicfoundation/hardhat-ignition-ethers": "^0.15.5",
"@nomicfoundation/hardhat-ignition": "^0.15.5",
"@nomicfoundation/hardhat-ignition-ethers": "^0.15.5",
"@nomicfoundation/hardhat-network-helpers": "^1.0.11",
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
"@nomicfoundation/hardhat-verify": "^2.0.9",
Expand All @@ -40,17 +40,19 @@
"chai": "4.3.4",
"debug": "4.3.2",
"dotenv": "10.0.0",
"eslint": "^9.8.0",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.2.1",
"eslint": "^9.8.0",
"hardhat-gas-reporter": "^2.2.0",
"fs": "^0.0.1-security",
"hardhat": "^2.22.7",
"hardhat-gas-reporter": "^2.2.0",
"path": "^0.12.7",
"prettier": "^3.3.3",
"rimraf": "^6.0.0",
"solhint-plugin-prettier": "0.1.0",
"solhint": "5.0.2",
"solhint-plugin-prettier": "0.1.0",
"solidity-coverage": "^0.8.12",
"ts-node": "^10.9.2",
"typechain": "^8.1.1",
Expand Down
19 changes: 19 additions & 0 deletions packages/contracts/tasks/create-EIP1193.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { EIP1193Provider } from "@gnosis-guild/zodiac-core";
import { Signer } from "ethers";
import { EthereumProvider } from "hardhat/types";

export 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 });
},
};
}
21 changes: 21 additions & 0 deletions packages/contracts/tasks/deploy-mastercopies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

import { task } from "hardhat/config";

import {
deployAllMastercopies,
} from "@gnosis-guild/zodiac-core";
import { createEIP1193 } from "./create-EIP1193";

task(
"deploy:mastercopies",
"For every version entry on the artifacts file, deploys a mastercopy into the current network"
).setAction(async (_, hre) => {
const [signer] = await hre.ethers.getSigners();
const provider = createEIP1193(hre.network.provider, signer);

await deployAllMastercopies({
provider,
});
});


101 changes: 48 additions & 53 deletions packages/contracts/tasks/deploy-mastercopy.ts
Original file line number Diff line number Diff line change
@@ -1,78 +1,73 @@
import { Signer } from "ethers";
/** @format */

import { task, types } from "hardhat/config";
import { EthereumProvider } from "hardhat/types";

import {
EIP1193Provider,
deployMastercopy,
deployAllMastercopies,
readMastercopy,
} from "@gnosis-guild/zodiac-core";
import { createEIP1193 } from "./create-EIP1193";

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",
"contractVersion",
"The specific version of the contract to deploy",
"latest", // Default value
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);
.setAction(async ({ contractVersion }, hre) => {
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 });
},
};
}
// Deploy the contracts based on the provided version
await deployLatestMastercopyFromDisk(provider, contractVersion);
});

async function deployLatestMastercopyFromDisk(
provider: EIP1193Provider,
contract: string
version?: string
) {
const latestArtifact = readMastercopy({
contractName: contract,
});
const CONTRACTS = [
"Exit",
"ExitERC20",
"ExitERC721",
"CirculatingSupply",
"CirculatingSupplyERC20",
"CirculatingSupplyERC721",
];

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}`
);
for (const contract of CONTRACTS) {
try {
// Read the artifact for the specific contract and version
const artifact = readMastercopy({
contractName: contract,
contractVersion: version === "latest" ? undefined : version,
});

const { address, noop } = await deployMastercopy({
...artifact,
provider,
});

if (noop) {
console.log(
`🔄 ${artifact.contractName}@${artifact.contractVersion}: Already deployed at ${address}`
);
} else {
console.log(
`🚀 ${artifact.contractName}@${artifact.contractVersion}: Successfully deployed at ${address}`
);
}
} catch (error) {
console.error(
`⏭️ Skipping deployment of ${contract}@${version}: Version not found.`
);
// Skip the current contract if there's an error and continue with the next one
continue;
}
}
}
Loading

0 comments on commit 136c272

Please sign in to comment.