Эх сурвалжийг харах

refactor: the deploy script into a hardhat task

chore: add TS_NODE_TRANSPILE_ONLY=1 to typechain script so that typechain task works
refactor: add types to "mnemonic" and "infuraApiKey" vars
Paul Razvan Berg 4 жил өмнө
parent
commit
7f34ae04b1

+ 4 - 3
hardhat.config.ts

@@ -5,6 +5,7 @@ import "solidity-coverage";
 
 import "./tasks/accounts";
 import "./tasks/clean";
+import "./tasks/deployers";
 
 import { resolve } from "path";
 
@@ -25,12 +26,12 @@ const chainIds = {
 };
 
 // Ensure that we have all the environment variables we need.
-const mnemonic = process.env.MNEMONIC;
+const mnemonic: string | undefined = process.env.MNEMONIC;
 if (!mnemonic) {
   throw new Error("Please set your MNEMONIC in a .env file");
 }
 
-const infuraApiKey = process.env.INFURA_API_KEY;
+const infuraApiKey: string | undefined = process.env.INFURA_API_KEY;
 if (!infuraApiKey) {
   throw new Error("Please set your INFURA_API_KEY in a .env file");
 }
@@ -82,7 +83,7 @@ const config: HardhatUserConfig = {
         // https://github.com/paulrberg/solidity-template/issues/31
         bytecodeHash: "none",
       },
-      // You should disable the optimizer when debugging
+      // Disable the optimizer when debugging
       // https://hardhat.org/hardhat-network/#solidity-optimizer-support
       optimizer: {
         enabled: true,

+ 2 - 3
package.json

@@ -69,8 +69,7 @@
     "commit": "git-cz",
     "compile": "hardhat compile",
     "coverage": "hardhat coverage --solcoverjs ./.solcover.js --temp artifacts --testfiles \"./test/**/*.ts\"",
-    "deploy": "hardhat run scripts/deploy.ts",
-    "deploy:network": "hardhat run scripts/deploy.ts --network",
+    "deploy": "hardhat deploy:Greeter --greeting",
     "lint": "yarn run lint:sol && yarn run lint:ts && yarn run prettier:check",
     "lint:sol": "solhint --config ./.solhint.json --max-warnings 0 \"contracts/**/*.sol\"",
     "lint:ts": "eslint --config ./.eslintrc.yaml --ignore-path ./.eslintignore --ext .js,.ts .",
@@ -78,6 +77,6 @@
     "prettier": "prettier --config ./.prettierrc --write \"**/*.{js,json,md,sol,ts}\"",
     "prettier:check": "prettier --check --config ./.prettierrc \"**/*.{js,json,md,sol,ts}\"",
     "test": "hardhat test",
-    "typechain": "hardhat typechain"
+    "typechain": "TS_NODE_TRANSPILE_ONLY hardhat typechain"
   }
 }

+ 0 - 22
scripts/deploy.ts

@@ -1,22 +0,0 @@
-// We require the Hardhat Runtime Environment explicitly here. This is optional but useful for running the
-// script in a standalone fashion through `node <script>`. When running the script with `hardhat run <script>`,
-// you'll find the Hardhat Runtime Environment's members available in the global scope.
-import { ethers } from "hardhat";
-
-import { Greeter, Greeter__factory } from "../typechain";
-
-async function main(): Promise<void> {
-  const Greeter: Greeter__factory = await ethers.getContractFactory("Greeter");
-  const greeter: Greeter = await Greeter.deploy("Hello, Hardhat!");
-  await greeter.deployed();
-
-  console.log("Greeter deployed to: ", greeter.address);
-}
-
-// We recommend this pattern to be able to use async/await everywhere and properly handle errors.
-main()
-  .then(() => process.exit(0))
-  .catch((error: Error) => {
-    console.error(error);
-    process.exit(1);
-  });

+ 13 - 0
tasks/deployers/greeter.ts

@@ -0,0 +1,13 @@
+import { task } from "hardhat/config";
+import { TaskArguments } from "hardhat/types";
+
+import { Greeter, Greeter__factory } from "../../typechain";
+
+task("deploy:Greeter")
+  .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);
+    await greeter.deployed();
+    console.log("Greeter deployed to: ", greeter.address);
+  });

+ 1 - 0
tasks/deployers/index.ts

@@ -0,0 +1 @@
+import "./greeter";