Skip to content

Commit

Permalink
Redefine environment variables for hardhat config (#380)
Browse files Browse the repository at this point in the history
To simplify running the hardhat node for integration tests we defined
hardhat network in the hardhat config file and a command to run the node
with `pnpm run node:forking`.

We also separated environment variables and used `dotenv-safer` to
handle them.

The `.env` file will be automatically created based on the
`.env.example` if it is not available. To run the integration tests we
need to define `MAINNET_RPC_URL` in the `.env` file.
  • Loading branch information
dimpar authored Apr 26, 2024
2 parents e3cba2d + 10f0813 commit b9f9936
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 25 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/solidity.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ jobs:

- name: Deploy contracts
env:
ACCOUNTS_PRIVATE_KEYS: ${{ secrets.TESTNET_ETH_CONTRACT_OWNER_PRIVATE_KEY }}
CHAIN_API_URL: ${{ secrets.SEPOLIA_CHAIN_API_URL }}
SEPOLIA_PRIVATE_KEY: ${{ secrets.TESTNET_ETH_CONTRACT_OWNER_PRIVATE_KEY }}
SEPOLIA_RPC_URL: ${{ secrets.SEPOLIA_CHAIN_API_URL }}
ETHERSCAN_API_KEY: ${{ secrets.ETHERSCAN_API_KEY }}
run: |
pnpm run deploy --network ${{ github.event.inputs.environment }}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ tmp/

# Environment configuration files
.envrc
.env
.env.*
!.env.example
!.env.ci.example
11 changes: 11 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions solidity/.env

This file was deleted.

1 change: 1 addition & 0 deletions solidity/.env.ci.example
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

9 changes: 7 additions & 2 deletions solidity/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
CHAIN_API_URL=
ACCOUNTS_PRIVATE_KEYS=
MAINNET_RPC_URL=
MAINNET_PRIVATE_KEY=

SEPOLIA_RPC_URL=
SEPOLIA_PRIVATE_KEY=

ETHERSCAN_API_KEY=

COINMARKETCAP_API_KEY=
6 changes: 4 additions & 2 deletions solidity/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@ $ pnpm test

To run the integration tests follow these steps:

- Run the Hardhat Node locally forking Mainnet at block `19680873`:
- Define `MAINNET_RPC_URL` environment variable pointing to Ethereum Mainnet RPC URL.

- Run the Hardhat Node locally forking Mainnet:

```
$ npx hardhat node --no-deploy --fork https://eth-mainnet.g.alchemy.com/v2/<key> --fork-block-number 19680873
$ pnpm run node:forking
```

- Once the local node is running you can execute the integration tests:
Expand Down
46 changes: 35 additions & 11 deletions solidity/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ import "hardhat-contract-sizer"
import "hardhat-deploy"
import "solidity-docgen"
import "@keep-network/hardhat-helpers"
import dotenv from "dotenv-safer"

dotenv.config({
allowEmptyValues: true,
example: process.env.CI ? ".env.ci.example" : ".env.example",
})

const MAINNET_RPC_URL = process.env.MAINNET_RPC_URL ?? ""

const MAINNET_PRIVATE_KEY = process.env.MAINNET_PRIVATE_KEY
? [process.env.MAINNET_PRIVATE_KEY]
: []

const SEPOLIA_RPC_URL = process.env.SEPOLIA_RPC_URL ?? ""

const SEPOLIA_PRIVATE_KEY = process.env.SEPOLIA_PRIVATE_KEY
? [process.env.SEPOLIA_PRIVATE_KEY]
: []

const ETHERSCAN_API_KEY = process.env.ETHERSCAN_API_KEY ?? ""

const COINMARKETCAP_API_KEY = process.env.COINMARKETCAP_API_KEY ?? ""

const config: HardhatUserConfig = {
solidity: {
Expand All @@ -26,23 +48,25 @@ const config: HardhatUserConfig = {
},

networks: {
hardhat: {
forking:
process.env.FORKING === "true"
? { url: MAINNET_RPC_URL, blockNumber: 19680873 }
: undefined,
},
integration: {
url: "http://localhost:8545",
},
sepolia: {
url: process.env.CHAIN_API_URL || "",
url: SEPOLIA_RPC_URL,
chainId: 11155111,
accounts: process.env.ACCOUNTS_PRIVATE_KEYS
? process.env.ACCOUNTS_PRIVATE_KEYS.split(",")
: undefined,
accounts: SEPOLIA_PRIVATE_KEY,
tags: ["etherscan"],
},
mainnet: {
url: process.env.CHAIN_API_URL || "",
url: MAINNET_RPC_URL,
chainId: 1,
accounts: process.env.ACCOUNTS_PRIVATE_KEYS
? process.env.ACCOUNTS_PRIVATE_KEYS.split(",")
: undefined,
accounts: MAINNET_PRIVATE_KEY,
tags: ["etherscan"],
},
},
Expand All @@ -57,8 +81,8 @@ const config: HardhatUserConfig = {

etherscan: {
apiKey: {
sepolia: process.env.ETHERSCAN_API_KEY,
mainnet: process.env.ETHERSCAN_API_KEY,
sepolia: ETHERSCAN_API_KEY,
mainnet: ETHERSCAN_API_KEY,
},
},

Expand Down Expand Up @@ -98,7 +122,7 @@ const config: HardhatUserConfig = {

gasReporter: {
enabled: true,
coinmarketcap: process.env.COINMARKETCAP_API_KEY,
coinmarketcap: COINMARKETCAP_API_KEY,
},

typechain: {
Expand Down
11 changes: 7 additions & 4 deletions solidity/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
],
"scripts": {
"clean": "hardhat clean && rm -rf cache/ export/ gen/ export.json",
"build": "hardhat compile",
"deploy": "hardhat deploy --export export.json",
"docs": "hardhat docgen",
"prepare:env": "cp -n .env.example .env || true",
"build": "npm run prepare:env && hardhat compile",
"deploy": "npm run prepare:env && hardhat deploy --export export.json",
"docs": "npm run prepare:env && hardhat docgen",
"format": "npm run lint:js && npm run lint:sol && npm run lint:config",
"format:fix": "npm run lint:js:fix && npm run lint:sol:fix && npm run lint:config:fix",
"lint:js": "eslint .",
Expand All @@ -26,7 +27,8 @@
"lint:sol:fix": "solhint 'contracts/**/*.sol' --fix && prettier --write 'contracts/**/*.sol'",
"lint:config": "prettier --check '**/*.@(json)'",
"lint:config:fix": "prettier --write '**/*.@(json)'",
"test": "hardhat test ./test/*.test.ts",
"node:forking": "FORKING=true hardhat node --no-deploy",
"test": "npm run prepare:env && hardhat test ./test/*.test.ts",
"test:integration": "hardhat test --deploy-fixture ./test/integration/*.test.ts --network integration"
},
"devDependencies": {
Expand All @@ -45,6 +47,7 @@
"@types/mocha": "^10.0.6",
"@types/node": "^20.9.4",
"chai": "^4.3.10",
"dotenv-safer": "^1.0.0",
"eslint": "^8.54.0",
"ethers": "^6.8.1",
"hardhat": "^2.19.1",
Expand Down
4 changes: 4 additions & 0 deletions solidity/types/dotenv-safer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "dotenv-safer" {
// eslint-disable-next-line import/prefer-default-export
export function config(options = {})
}

0 comments on commit b9f9936

Please sign in to comment.