Przeglądaj źródła

refactor: simplify hardhat.config.ts

refactor: use hardhat network in solidity-coverage
Paul Razvan Berg 4 lat temu
rodzic
commit
0c8e0814e1
4 zmienionych plików z 41 dodań i 42 usunięć
  1. 2 2
      .solcover.js
  2. 36 38
      hardhat.config.ts
  3. 2 1
      package.json
  4. 1 1
      tsconfig.json

+ 2 - 2
.solcover.js

@@ -1,6 +1,6 @@
 const shell = require("shelljs");
 
-/* The environment variables are loaded in buidler.config.ts */
+// The environment variables are loaded in buidler.config.ts
 const mnemonic = process.env.MNEMONIC;
 if (!mnemonic) {
   throw new Error("Please set your MNEMONIC in a .env file");
@@ -15,7 +15,7 @@ module.exports = {
     await run("typechain");
   },
   onIstanbulComplete: async function (_config) {
-    /* We need to do this because solcover generates bespoke artifacts. */
+    // We need to do this because solcover generates bespoke artifacts.
     shell.rm("-rf", "./artifacts");
     shell.rm("-rf", "./typechain");
   },

+ 36 - 38
hardhat.config.ts

@@ -2,8 +2,8 @@ import { config as dotenvConfig } from "dotenv";
 import { resolve } from "path";
 dotenvConfig({ path: resolve(__dirname, "./.env") });
 
-import { HardhatNetworkAccountsUserConfig } from "hardhat/types";
 import { HardhatUserConfig } from "hardhat/config";
+import { NetworkUserConfig } from "hardhat/types";
 import "./tasks/accounts";
 import "./tasks/clean";
 
@@ -11,61 +11,59 @@ import "@nomiclabs/hardhat-waffle";
 import "hardhat-typechain";
 import "solidity-coverage";
 
-/**
- * @dev You must have a `.env` file. Follow the example in `.env.example`.
- * @param {string} network The name of the testnet
- */
-function createNetworkConfig(
-  network?: string,
-): { accounts: HardhatNetworkAccountsUserConfig; url: string | undefined } {
-  if (!process.env.MNEMONIC) {
-    throw new Error("Please set your MNEMONIC in a .env file");
-  }
+const chainIds = {
+  ganache: 1337,
+  goerli: 5,
+  hardhat: 31337,
+  kovan: 42,
+  mainnet: 1,
+  rinkeby: 4,
+  ropsten: 3,
+};
 
-  if (!process.env.INFURA_API_KEY) {
-    throw new Error("Please set your INFURA_API_KEY");
-  }
+// Ensure that we have all the environment variables we need.
+let mnemonic: string;
+if (!process.env.MNEMONIC) {
+  throw new Error("Please set your MNEMONIC in a .env file");
+} else {
+  mnemonic = process.env.MNEMONIC;
+}
 
+let infuraApiKey: string;
+if (!process.env.INFURA_API_KEY) {
+  throw new Error("Please set your INFURA_API_KEY in a .env file");
+} else {
+  infuraApiKey = process.env.INFURA_API_KEY;
+}
+
+function createTestnetConfig(network: keyof typeof chainIds): NetworkUserConfig {
+  const url: string = "https://" + network + ".infura.io/v3/" + infuraApiKey;
   return {
     accounts: {
       count: 10,
       initialIndex: 0,
-      mnemonic: process.env.MNEMONIC,
+      mnemonic,
       path: "m/44'/60'/0'/0",
     },
-    url: network ? `https://${network}.infura.io/v3/${process.env.INFURA_API_KEY}` : undefined,
+    chainId: chainIds[network],
+    url,
   };
 }
 
 const config: HardhatUserConfig = {
   defaultNetwork: "hardhat",
   mocha: {
-    /* Without this property set, the "setTimeout" from the Greeter.js file wouldn't work. */
+    // Without this property set, the "setTimeout" from the Greeter.js file wouldn't work.
     delay: true,
   },
   networks: {
     hardhat: {
-      chainId: 31337,
-    },
-    coverage: {
-      url: "http://127.0.0.1:8555",
-    },
-    goerli: {
-      ...createNetworkConfig("goerli"),
-      chainId: 5,
-    },
-    kovan: {
-      ...createNetworkConfig("kovan"),
-      chainId: 42,
-    },
-    rinkeby: {
-      ...createNetworkConfig("rinkeby"),
-      chainId: 4,
-    },
-    ropsten: {
-      ...createNetworkConfig("ropsten"),
-      chainId: 3,
+      chainId: chainIds.hardhat,
     },
+    goerli: createTestnetConfig("goerli"),
+    kovan: createTestnetConfig("kovan"),
+    rinkeby: createTestnetConfig("rinkeby"),
+    ropsten: createTestnetConfig("ropsten"),
   },
   paths: {
     artifacts: "./artifacts",
@@ -77,8 +75,8 @@ const config: HardhatUserConfig = {
   },
   solidity: {
     version: "0.7.4",
-    /* https://hardhat.org/hardhat-network/#solidity-optimizer-support */
     settings: {
+      // https://hardhat.org/hardhat-network/#solidity-optimizer-support
       optimizer: {
         enabled: true,
         runs: 200,

+ 2 - 1
package.json

@@ -51,6 +51,7 @@
   "keywords": [
     "blockchain",
     "ethereum",
+    "hardhat",
     "smart-contracts",
     "solidity"
   ],
@@ -62,7 +63,7 @@
     "clean": "hardhat clean",
     "commit": "git-cz",
     "compile": "hardhat compile",
-    "coverage": "hardhat coverage --solcoverjs ./.solcover.js --network coverage --temp artifacts --testfiles \"./test/**/*.ts\"",
+    "coverage": "hardhat coverage --solcoverjs ./.solcover.js --temp artifacts --testfiles \"./test/**/*.ts\"",
     "lint": "yarn run lint:sol && yarn run lint:ts && yarn run prettier:list-different",
     "lint:sol": "solhint --config ./.solhint.json --max-warnings 0 \"contracts/**/*.sol\"",
     "lint:ts": "eslint --config ./.eslintrc.yaml --ignore-path ./.eslintignore --ext .js,.ts .",

+ 1 - 1
tsconfig.json

@@ -14,13 +14,13 @@
   },
   "exclude": ["node_modules"],
   "include": [
-    "types/**/*",
     "artifacts/**/*",
     "artifacts/**/*.json",
     "scripts/**/*",
     "tasks/**/*",
     "test/**/*",
     "typechain/**/*",
+    "types/**/*",
     "hardhat.config.ts"
   ]
 }