Skip to content

Commit

Permalink
Remove dotenv in favor of configuration variables (#178)
Browse files Browse the repository at this point in the history
* convert env variables to configuration variables

* modify docs and re-add env variable for test scenario

* update lock file

* add infura api key in ci

* update solidity-coverage package

* update readme with GitHub secrets

* remove quotes from mnemonic command

* Remove dotenv

* Run typechain in postcompile instead of postinstall

* Update ci.yml

* docs: polish README

---------

Co-authored-by: Christopher Dedominici <[email protected]>
Co-authored-by: Paul Razvan Berg <[email protected]>
  • Loading branch information
3 people committed Nov 26, 2023
1 parent 4f9c2eb commit 1e00b94
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 207 deletions.
10 changes: 0 additions & 10 deletions .env.example

This file was deleted.

13 changes: 12 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
name: "CI"

env:
DOTENV_CONFIG_PATH: "./.env.example"
HARDHAT_VAR_MNEMONIC: "test test test test test test test test test test test junk"
HARDHAT_VAR_INFURA_API_KEY: "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
# Uncomment the following lines to set your configuration variables using
# GitHub secrets (https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions)
# HARDHAT_VAR_MNEMONIC: ${{ secrets.Mnemonic }}
# HARDHAT_VAR_INFURA_API_KEY: ${{ secrets.InfuraApiKey }}
# HARDHAT_VAR_ARBISCAN_API_KEY: ${{ secrets.ArbiscanApiKey }}
# HARDHAT_VAR_BSCSCAN_API_KEY: ${{ secrets.BscscanApiKey }}
# HARDHAT_VAR_ETHERSCAN_API_KEY: ${{ secrets.EtherscanApiKey }}
# HARDHAT_VAR_OPTIMISM_API_KEY: ${{ secrets.OptimismApiKey }}
# HARDHAT_VAR_POLYGONSCAN_API_KEY: ${{ secrets.PolygonscanApiKey }}
# HARDHAT_VAR_SNOWTRACE_API_KEY: ${{ secrets.SnowtraceApiKey }}

on:
workflow_dispatch:
Expand Down
3 changes: 0 additions & 3 deletions .solcover.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
module.exports = {
istanbulReporter: ["html", "lcov"],
providerOptions: {
mnemonic: process.env.MNEMONIC,
},
skipFiles: ["test"],
};
23 changes: 18 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,35 @@ request made to the `main` branch.

Note though that to make this work, you must use your `INFURA_API_KEY` and your `MNEMONIC` as GitHub secrets.

For more information on how to set up GitHub secrets, check out the
[docs](https://docs.github.com/en/actions/security-guides/using-secrets-in-github-actions).

You can edit the CI script in [.github/workflows/ci.yml](./.github/workflows/ci.yml).

## Usage

### Pre Requisites

Before being able to run any command, you need to create a `.env` file and set a BIP-39 compatible mnemonic as an
environment variable. You can follow the example in `.env.example`. If you don't already have a mnemonic, you can use
this [website](https://iancoleman.io/bip39/) to generate one.

Then, proceed with installing dependencies:
First, you need to install the dependencies:

```sh
$ pnpm install
```

Then, you need to set up all the required
[Hardhat Configuration Variables](https://hardhat.org/hardhat-runner/docs/guides/configuration-variables). You might
also want to install some that are optional.

To assist with the setup process, run `pnpm dlx hardhat vars setup`. To set a particular value, such as a BIP-39
mnemonic variable, execute this:

```sh
$ pnpm dlx hardhat vars set MNEMONIC
? Enter value: ‣ here is where your twelve words mnemonic should be put my friend
```

If you do not already have a mnemonic, you can generate one using this [website](https://iancoleman.io/bip39/).

### Compile

Compile the smart contracts with Hardhat:
Expand Down
34 changes: 12 additions & 22 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,17 @@
import "@nomicfoundation/hardhat-toolbox";
import { config as dotenvConfig } from "dotenv";
import "hardhat-deploy";
import type { HardhatUserConfig } from "hardhat/config";
import { vars } from "hardhat/config";
import type { NetworkUserConfig } from "hardhat/types";
import { resolve } from "path";

import "./tasks/accounts";
import "./tasks/greet";
import "./tasks/taskDeploy";

const dotenvConfigPath: string = process.env.DOTENV_CONFIG_PATH || "./.env";
dotenvConfig({ path: resolve(__dirname, dotenvConfigPath) });
// Run 'npx hardhat vars setup' to see the list of variables that need to be set

// Ensure that we have all the environment variables we need.
const mnemonic: string | undefined = process.env.MNEMONIC;
if (!mnemonic) {
throw new Error("Please set your MNEMONIC in a .env file");
}

const infuraApiKey: string | undefined = process.env.INFURA_API_KEY;
if (!infuraApiKey) {
throw new Error("Please set your INFURA_API_KEY in a .env file");
}
const mnemonic: string = vars.get("MNEMONIC");
const infuraApiKey: string = vars.get("INFURA_API_KEY");

const chainIds = {
"arbitrum-mainnet": 42161,
Expand Down Expand Up @@ -66,14 +56,14 @@ const config: HardhatUserConfig = {
},
etherscan: {
apiKey: {
arbitrumOne: process.env.ARBISCAN_API_KEY || "",
avalanche: process.env.SNOWTRACE_API_KEY || "",
bsc: process.env.BSCSCAN_API_KEY || "",
mainnet: process.env.ETHERSCAN_API_KEY || "",
optimisticEthereum: process.env.OPTIMISM_API_KEY || "",
polygon: process.env.POLYGONSCAN_API_KEY || "",
polygonMumbai: process.env.POLYGONSCAN_API_KEY || "",
sepolia: process.env.ETHERSCAN_API_KEY || "",
arbitrumOne: vars.get("ARBISCAN_API_KEY", ""),
avalanche: vars.get("SNOWTRACE_API_KEY", ""),
bsc: vars.get("BSCSCAN_API_KEY", ""),
mainnet: vars.get("ETHERSCAN_API_KEY", ""),
optimisticEthereum: vars.get("OPTIMISM_API_KEY", ""),
polygon: vars.get("POLYGONSCAN_API_KEY", ""),
polygonMumbai: vars.get("POLYGONSCAN_API_KEY", ""),
sepolia: vars.get("ETHERSCAN_API_KEY", ""),
},
},
gasReporter: {
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@
"@typescript-eslint/parser": "^5.44.0",
"chai": "^4.3.7",
"cross-env": "^7.0.3",
"dotenv": "^16.0.3",
"eslint": "^8.28.0",
"eslint-config-prettier": "^8.5.0",
"ethers": "^6.4.0",
"fs-extra": "^10.1.0",
"hardhat": "^2.12.2",
"hardhat": "^2.19.0",
"hardhat-deploy": "^0.11.29",
"hardhat-gas-reporter": "^1.0.9",
"lodash": "^4.17.21",
Expand All @@ -38,7 +37,7 @@
"rimraf": "^4.1.2",
"solhint": "^3.4.0",
"solhint-plugin-prettier": "^0.0.5",
"solidity-coverage": "^0.8.2",
"solidity-coverage": "^0.8.5",
"ts-generator": "^0.1.1",
"ts-node": "^10.9.1",
"typechain": "^8.2.0",
Expand Down Expand Up @@ -69,7 +68,7 @@
"lint": "pnpm lint:sol && pnpm lint:ts && pnpm prettier:check",
"lint:sol": "solhint --max-warnings 0 \"contracts/**/*.sol\"",
"lint:ts": "eslint --ignore-path ./.eslintignore --ext .js,.ts .",
"postinstall": "DOTENV_CONFIG_PATH=./.env.example pnpm typechain",
"postcompile": "pnpm typechain",
"prettier:check": "prettier --check \"**/*.{js,json,md,sol,ts,yml}\"",
"prettier:write": "prettier --write \"**/*.{js,json,md,sol,ts,yml}\"",
"task:deployGreeter": "hardhat task:deployGreeter",
Expand Down
Loading

0 comments on commit 1e00b94

Please sign in to comment.