Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent to load scripts before compilation and typechain #60

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ Deploy the contracts to Hardhat Network:
$ yarn deploy --greeting "Bonjour, le monde!"
```

Deploy the contracts to Rinkeby Network and Verify deployed contract on etherscan

```sh
$ yarn deploy --greeting "Bonjour, le monde!" --network rinkeby --verify
```

## Syntax Highlighting

If you use VSCode, you can enjoy syntax highlighting for your Solidity code via the
Expand Down
22 changes: 21 additions & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import path from "path";
import fs from "fs";
import "@nomiclabs/hardhat-waffle";
import "@nomiclabs/hardhat-etherscan";
import "@typechain/hardhat";
import "hardhat-gas-reporter";
import "solidity-coverage";

import "./tasks/accounts";
import "./tasks/clean";
import "./tasks/deployers";

import { resolve } from "path";

Expand All @@ -25,6 +27,21 @@ const chainIds = {
ropsten: 3,
};

const SKIP_LOAD = process.env.SKIP_LOAD === "true";
const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY || "";

// Prevent to load scripts before compilation and typechain
if (!SKIP_LOAD) {
["deployers"].forEach(folder => {
const tasksPath = path.join(__dirname, "tasks", folder);
fs.readdirSync(tasksPath)
.filter(pth => pth.includes(".ts"))
.forEach(task => {
require(`${tasksPath}/${task}`);
});
});
}

// Ensure that we have all the environment variables we need.
const mnemonic: string | undefined = process.env.MNEMONIC;
if (!mnemonic) {
Expand Down Expand Up @@ -93,6 +110,9 @@ const config: HardhatUserConfig = {
},
},
},
etherscan: {
apiKey: ETHERSCAN_API_KEY,
},
typechain: {
outDir: "typechain",
target: "ethers-v5",
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@ethersproject/bytes": "^5.4.0",
"@ethersproject/providers": "^5.4.3",
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-etherscan": "^2.1.5",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@typechain/ethers-v5": "^7.0.1",
"@typechain/hardhat": "^2.3.0",
Expand Down Expand Up @@ -68,7 +69,7 @@
"scripts": {
"clean": "hardhat clean",
"commit": "git-cz",
"compile": "hardhat compile",
"compile": "SKIP_LOAD=true hardhat compile",
"coverage": "cross-env CODE_COVERAGE=true hardhat coverage --solcoverjs ./.solcover.js --temp artifacts --testfiles \"./test/**/*.ts\"",
"deploy": "hardhat deploy:Greeter",
"lint": "yarn run lint:sol && yarn run lint:ts && yarn run prettier:check",
Expand Down
22 changes: 17 additions & 5 deletions tasks/deployers/greeter.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
import { task } from "hardhat/config";
import { TaskArguments } from "hardhat/types";

import { Greeter, Greeter__factory } from "../../typechain";

task("deploy:Greeter")
.addFlag("verify", "Verify contracts at Etherscan")
.addParam("greeting", "Say hello, be nice")
.setAction(async function (taskArguments: TaskArguments, { ethers }) {
const greeterFactory: Greeter__factory = await ethers.getContractFactory("Greeter");
const greeter: Greeter = <Greeter>await greeterFactory.deploy(taskArguments.greeting);
.setAction(async ({ verify, greeting }, hre) => {
const greeterFactory: Greeter__factory = await hre.ethers.getContractFactory("Greeter");
const greeter: Greeter = <Greeter>await greeterFactory.deploy(greeting);
await greeter.deployed();
console.log("Greeter deployed to: ", greeter.address);

if (verify) {
// We need to wait a little bit to verify the contract after deployment
void delay(30000);
await hre.run("verify:verify", {
address: greeter.address,
constructorArguments: [greeting],
});
}
});

function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Loading