Skip to content

Commit

Permalink
updated: migrating to ts boilerplate with lock contract and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Salmandabbakuti committed Nov 3, 2023
1 parent 4fc3469 commit 2c480e5
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 25 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Be aware. dont commit this file or any of theses keys to your repo.
PRIVATE_KEY=
POLYGON_MUMBAI_RPC_URL=
POLYGON_MAINNET_RPC_URL=
ETHERSCAN_API_KEY=
47 changes: 47 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# adding tests
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Node.js CI

on:
push:
branches: [main, develop, init, "feat/*"]
pull_request:
branches: [main, develop, init, "feat/*"]

jobs:
e2e-test:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
node-version: [18.x, 20.x, 21.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
runs-on: ${{ matrix.os }}

steps:
- uses: actions/checkout@v4
- name: Use Node.js ${{ matrix.node-version }} on ${{ matrix.os }}
uses: actions/setup-node@v3
# uses: borales/[email protected]
with:
node-version: ${{ matrix.node-version }}
- name: "Install packages with npm"
run: npm install
- name: "Copy .env.example to .env"
run: cp .env.example .env
- name: "Start hardhat node"
run: npx hardhat node & sleep 3
- name: "List accounts with balances"
run: npx hardhat accounts
- name: "Check for solidity linter errors"
run: npx hardhat check
- name: "Compile contracts"
run: npm run compile
- name: "Check solidity coverage"
run: npm run coverage
- name: "Run tests"
run: npm run test
- name: "Deploy contracts"
run: npm run deploy
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,25 @@
# minimal-hardhat-boilerplate
# Hardhat Boilerplate

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 v18+ and npm v8+
> Rename `env.example` to `.env` and add your env specific keys.
Try running some of the following tasks:

```shell
npm install

# Set/Read/Remove hardhat config variables
# npx hardhat vars set API_KEY
# npx hardhat vars get API_KEY
# npx hardhat vars DELETE API_KEY
# npx hardhat vars list

# set PRIVATE_KEY
npx hardhat vars set PRIVATE_KEY


# starts local node
npx hardhat node

# list accounts with balances
npx hardhat accounts

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

# compile contracts
npx hardhat compile

Expand All @@ -31,8 +29,8 @@ npx hardhat deploy --network local
# deploy contract in scripts/deploy.ts on specified network
npx hardhat run scripts/deploy.ts --network local

# verify contract
npx hardhat verify --network <deployed network> <deployed contract address> "<constructor1>" "<constructor2>"
#check linter issues using solhint plugin
npx hardhat check

# check coverage using solidity-coverage plugin: supports hardhat network only
npx hardhat coverage --network hardhat
Expand All @@ -43,6 +41,9 @@ npx hardhat test
# remove all compiled and deployed artifacts
npx hardhat clean

# verify contract
npx hardhat verify --network <deployed network> <deployed contract address> "<constructor1>" "<constructor2>"

# show help
npx hardhat help
```
84 changes: 80 additions & 4 deletions hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,95 @@
import { HardhatUserConfig, vars } from "hardhat/config";
import { HardhatUserConfig, task } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
import "dotenv/config";

const accounts = vars.has("PRIVATE_KEY") ? [vars.get("PRIVATE_KEY")] : [];
const accounts = process.env.PRIVATE_KEY ? [process.env.PRIVATE_KEY] : [];

// https://hardhat.org/guides/create-task.html
task(
"accounts",
"Prints the list of accounts with balances",
async (_, hre) => {
const accounts = await hre.ethers.getSigners();
const provider = hre.ethers.provider;

for (const account of accounts) {
const balance = await provider.getBalance(account.address);
console.log(
`${account.address} - ${hre.ethers.formatEther(balance)} ETH`
);
}
}
);

task("deploy", "Deploys Contract", async (_, hre) => {
const currentTimestampInSeconds = Math.round(Date.now() / 1000);
console.log("currentTimestampInSeconds:", currentTimestampInSeconds);
const unlockTime = currentTimestampInSeconds + 300;
const lockedAmount = hre.ethers.parseEther("0.001");

const lockInstance = await hre.ethers.deployContract("Lock", [unlockTime], {
value: lockedAmount
});

await lockInstance.waitForDeployment();
console.log("contract deployed at:", lockInstance.target);
});

task("balance", "Prints an account's balance")
.addParam("account", "The account's address")
.setAction(async ({ account }, hre) => {
const provider = hre.ethers.provider;
const balance = await provider.getBalance(account);
console.log(hre.ethers.formatEther(balance), "ETH");
});

const config: HardhatUserConfig = {
solidity: "0.8.19",
defaultNetwork: "local",
networks: {
hardhat: {
chainId: 1337
},
local: {
url: "http://127.0.0.1:8545"
},
mumbai: {
url: "https://rpc-mumbai.maticvigil.com",
url:
process.env.POLYGON_MUMBAI_RPC_URL ||
"https://rpc-mumbai.maticvigil.com",
accounts
},
polygon: {
url:
process.env.POLYGON_MAINNET_RPC_URL ||
"https://rpc-mainnet.maticvigil.com",
accounts
}
},
etherscan: {
// API key for Polygonscan
apiKey: process.env.ETHERSCAN_API_KEY
},
gasReporter: {
enabled: true,
currency: "USD"
},
solidity: {
version: "0.8.19",
settings: {
optimizer: {
enabled: true,
runs: 200
}
}
},
paths: {
sources: "./contracts",
tests: "./test",
cache: "./cache",
artifacts: "./artifacts"
},
mocha: {
timeout: 20000
}
};

Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hardhat-boilerplate-minimal",
"version": "1.0.1",
"description": "Minimal hardhat boilerplate for Dapp development",
"name": "hardhat-boilerplate",
"version": "1.2.2",
"description": "Hardhat boilerplate for Dapp development",
"private": true,
"scripts": {
"test": "npx hardhat test",
Expand All @@ -13,6 +13,7 @@
"license": "ISC",
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^3.0.0",
"dotenv": "^16.3.1",
"hardhat": "^2.19.0"
}
}
2 changes: 1 addition & 1 deletion scripts/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ethers } from "hardhat";

async function main() {
const currentTimestampInSeconds = Math.round(Date.now() / 1000);
const unlockTime = currentTimestampInSeconds + 60;
const unlockTime = currentTimestampInSeconds + 300;
const lockedAmount = ethers.parseEther("0.001");

const lockInstance = await ethers.deployContract("Lock", [unlockTime], {
Expand Down
10 changes: 6 additions & 4 deletions test/Lock.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
time,
loadFixture,
loadFixture
} from "@nomicfoundation/hardhat-toolbox/network-helpers";
import { anyValue } from "@nomicfoundation/hardhat-chai-matchers/withArgs";
import { expect } from "chai";
import { ethers } from "hardhat";

describe("Lock", function () {
describe("Lock Contract Tests", function () {
// We define a fixture to reuse the same setup in every test.
// We use loadFixture to run this setup once, snapshot that state,
// and reset Hardhat Network to that snapshot in every test.
Expand All @@ -20,8 +20,10 @@ describe("Lock", function () {
// Contracts are deployed using the first signer/account by default
const [owner, otherAccount] = await ethers.getSigners();

const Lock = await ethers.getContractFactory("Lock");
const lock = await Lock.deploy(unlockTime, { value: lockedAmount });
const lock = await ethers.deployContract("Lock", [unlockTime], {
value: lockedAmount
});
await lock.waitForDeployment();

return { lock, unlockTime, lockedAmount, owner, otherAccount };
}
Expand Down

0 comments on commit 2c480e5

Please sign in to comment.