Skip to content

Commit

Permalink
adding hardhat toolbox and few improvements (#3)
Browse files Browse the repository at this point in the history
* updated: changes related toolbox and other improvements

* Update ci.yml

* few fixes

* Update README.md

* updated: pipeline to use latest npm
  • Loading branch information
Salmandabbakuti authored Nov 24, 2022
1 parent 62b25bd commit 1ee9568
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 2,485 deletions.
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ PRIV_KEY=
ETHEREUM_MAINNET_RPC_URL=
GOERLI_RPC_URL=
POLYGON_MUMBAI_RPC_URL=
POLYGON_MAINNET_RPC_URL=
POLYGON_MAINNET_RPC_URL=
ETHERSCAN_API_KEY=
22 changes: 13 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ jobs:
e2e-test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node-version: [14.x, 16.x, 18.x]
Expand All @@ -26,16 +27,19 @@ jobs:
# uses: borales/[email protected]
with:
node-version: ${{ matrix.node-version }}
cache: yarn
- name: "Install packages"
run: yarn
- name: "Rename .env.example to .env"
run: mv .env.example .env
- name: "Install packages with latest npm"
run: npm i -g npm@latest && npm install
- name: "Copy .env.example to .env"
run: cp .env.example .env
- name: "Start hardhat node"
run: yarn hardhat node & sleep 3
run: npx hardhat node & sleep 3
- name: "Check for solidity linter errors"
run: npx hardhat check
- name: "Compile contracts"
run: yarn compile
run: npm run compile
- name: "Check solidity coverage"
run: npm run coverage
- name: "Run tests"
run: yarn test
run: npm run test
- name: "Deploy contracts"
run: yarn deploy
run: npm run deploy
9 changes: 5 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@ bin-release/
.settings/
node_modules/
package-lock.json
yarn.lock

# Executables
*.swf
*.air
*.ipa
*.apk
*.env

# Project files, i.e. `.project`, `.actionScriptProperties` and `.flexProperties`
# should NOT be excluded as they contain compiler settings and other important
# information for Eclipse / Flash Builder.

node_modules
.env

#Hardhat files
cache
artifacts
artifacts/
coverage/
coverage.json
40 changes: 30 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,48 @@

This project demonstrates a basic Hardhat use case. It comes with a sample contract, a test for that contract, a sample script that deploys that contract, and an example of a task implementation, which simply lists the available accounts with balances.

> Recommended to use Node.js v14+ and npm v7+.
> Rename `env.example` to `.env` and add your env specific keys.
Try running some of the following tasks:

```shell
yarn install
npm install

# starts local node
npx hardhat node

# list accounts with balances
npx hardhat accounts

# show balance eth of specified account
npx hardhat balance --account '0x47a9...'

yarn hardhat node # starts local node
# compile contracts
npx hardhat compile

yarn hardhat accounts # list accounts with balances
# deploy contract defined in tasks on specified network
npx hardhat deploy --network local

yarn hardhat balance --account '0x47a9...' # show balance eth of specified account
# deploy contract in scripts/deploy.js on specified network
npx hardhat run scripts/deploy.js --network local

yarn hardhat compile # compiles contracts
#check linter issues using solhint plugin
npx hardhat check

yarn hardhat deploy --network local # deploys contract defined in tasks on specified network
# check coverage using solidity-coverage plugin: supports hardhat network only
npx hardhat coverage --network hardhat

yarn hardhat run --network local scripts/deploy.js # deploys contract in scripts/deploy.js
# unit tests including gas usage
npx hardhat test

yarn hardhat test # runs tests
# remove all compiled and deployed artifacts
npx hardhat clean

yarn hardhat clean # removes all compiled and deployed artifacts
# verify contract
npx hardhat verify --network <deployed network> <deployed contract address> "<constructor1>" "<constructor2>"

yarn hardhat help # shows help
# show help
npx hardhat help
```
14 changes: 12 additions & 2 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
require("@nomiclabs/hardhat-ethers");
require('@nomicfoundation/hardhat-toolbox');
require('@nomicfoundation/hardhat-chai-matchers');
require("@nomiclabs/hardhat-solhint");
require('dotenv').config();

// defining accounts to reuse.
Expand Down Expand Up @@ -51,7 +53,7 @@ module.exports = {
url: process.env.GOERLI_RPC_URL,
accounts // private keys
},
polygonTest: {
polygonMumbai: {
url: process.env.POLYGON_MUMBAI_RPC_URL,
accounts
},
Expand All @@ -60,6 +62,14 @@ module.exports = {
accounts
}
},
etherscan: {
// API key for Polygonscan
apiKey: process.env.ETHERSCAN_API_KEY
},
gasReporter: {
enabled: true,
currency: "USD",
},
solidity: {
version: "0.8.16",
settings: {
Expand Down
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
"description": "hardhat and ethersjs boilerplate for Dapp development",
"main": "index.js",
"scripts": {
"test": "yarn hardhat test",
"compile": "yarn hardhat compile",
"deploy": "yarn hardhat deploy"
"test": "npx hardhat test",
"compile": "npx hardhat compile",
"deploy": "npx hardhat deploy",
"coverage": "npx hardhat coverage --network hardhat"
},
"keywords": [],
"author": "Salman Dabbakuti",
"license": "ISC",
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.1.1",
"chai": "^4.3.6",
"@nomicfoundation/hardhat-chai-matchers": "^1.0.4",
"@nomicfoundation/hardhat-toolbox": "^2.0.0",
"@nomiclabs/hardhat-solhint": "^2.0.0",
"dotenv": "^16.0.3",
"ethers": "^5.7.0",
"hardhat": "^2.10.2"
}
}
}
5 changes: 5 additions & 0 deletions scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/*
* This script can only be run through Hardhat, and not through node directly.
* Since ethers or any other hardhat plugins are globally available to Hardhat Runtime Environment. So we are not importing them explicitly.
* So when running this script through node, we will get an error saying that ethers or any other plugins not defined error.
*/

async function main() {
const contractFactory = await ethers.getContractFactory("Greeter");
Expand Down
29 changes: 22 additions & 7 deletions test/sample-test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
/*
* This script can only be run through Hardhat, and not through node directly.
* Since ethers or any other hardhat plugins are globally available to Hardhat Runtime Environment. So we are not importing them explicitly.
* So when running this script through node, we will get an error saying that ethers or any other plugins not defined error.
*/

const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("Contract Tests", function () {
it("Should return the new greeting once it's changed", async function () {
let accounts;
let greeterContract;

// `before` will run only once, useful for deploying the contract and use it on every test
// It receives a callback, which can be async.
before(async () => {
// Get the ContractFactory and Signers here.
const contractFactory = await ethers.getContractFactory("Greeter");
const contract = await contractFactory.deploy("Hello, world!");
await contract.deployed();
accounts = await ethers.getSigners();
// Deploy the contract specifying the constructor arguments
greeterContract = await contractFactory.deploy("Hello, Hardhat!");
await greeterContract.deployed();
});

expect(await contract.getGreeting()).to.equal("Hello, world!");
const setGreetingTx = await contract.setGreeting("Hola, mundo!");
it("Should return the new greeting once it's changed", async function () {
expect(await greeterContract.getGreeting()).to.equal("Hello, Hardhat!");
const setGreetingTx = await greeterContract.setGreeting("Hola, mundo!");
// wait for the transaction to be mined
await setGreetingTx.wait();
expect(await contract.getGreeting()).to.equal("Hola, mundo!");
expect(await greeterContract.getGreeting()).to.equal("Hola, mundo!");
});
});
Loading

0 comments on commit 1ee9568

Please sign in to comment.