Skip to content

Commit

Permalink
updating dependencies, using ethers for tasks, few cleanups (#1)
Browse files Browse the repository at this point in the history
* removed ropsten, rinkeby as they will be depreciated, adding goerli
* updated: dependencies, tests
  • Loading branch information
Salmandabbakuti authored Aug 20, 2022
1 parent 8b6e2d6 commit 4bd0387
Show file tree
Hide file tree
Showing 8 changed files with 2,518 additions and 73 deletions.
33 changes: 21 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
# Basic Sample Hardhat Project
# 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.
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.

Try running some of the following tasks:

```shell
npx hardhat node # starts local node
npx hardhat accounts # list accounts
npx hardhat balance --account '0x47a9...' # show balance eth of specified account
npx hardhat compile # compiles contracts
npx hardhat deploy # deploys contract defined in tasks
node scripts/deployContract.js
npx hardhat run --network local scripts/deployContract.js # deplys contract on specified network
npx hardhat clean
npx hardhat test
npx hardhat help
yarn install

yarn hardhat node # starts local node

yarn hardhat accounts # list accounts with balances

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

yarn hardhat compile # compiles contracts

yarn hardhat deploy --network local # deploys contract defined in tasks on specified network

yarn hardhat run --network local scripts/deploy.js # deploys contract in scripts/deploy.js

yarn hardhat test # runs tests

yarn hardhat clean # removes all compiled and deployed artifacts

yarn hardhat help # shows help
```
2 changes: 1 addition & 1 deletion contracts/greeter.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
pragma solidity 0.8.7;

contract greeter {
string private greeting;
Expand Down
40 changes: 18 additions & 22 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
require("@nomiclabs/hardhat-ethers");
require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-web3");

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("hello", "Prints Hello World", () => console.log("Hello World!"));

task("accounts", "Prints the list of accounts", async () => {
task("accounts", "Prints the list of accounts with balances", async () => {
const accounts = await ethers.getSigners();
const provider = await ethers.provider;

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

task("deploy", "Deploys Contract", async () => {
const Greeter = await ethers.getContractFactory("greeter");
const greeter = await Greeter.deploy("Hello, Hardhat!");
await greeter.deployed();
console.log("Greeter contract deployed to:", greeter.address);
const contractFactory = await ethers.getContractFactory("greeter");
const contract = await contractFactory.deploy("Hello, Hardhat!");
await contract.deployed();
console.log("contract deployed at:", contract.address);
});

task("balance", "Prints an account's balance")
.addParam("account", "The account's address")
.setAction(async (taskArgs) => {
const account = web3.utils.toChecksumAddress(taskArgs.account);
const balance = await web3.eth.getBalance(account);

console.log(web3.utils.fromWei(balance, "ether"), "ETH");
.setAction(async ({ account }) => {
const provider = await ethers.provider;
const balance = await provider.getBalance(account);
console.log(hre.ethers.utils.formatEther(balance), "ETH");
});


module.exports = {
defaultNetwork: "local",
networks: {
Expand All @@ -39,25 +39,21 @@ module.exports = {
local: {
url: "http://127.0.0.1:8545",
},
rinkeby: {
url: "", // rpc providers: infura, alchemy
accounts: [] // private keys
},
ropsten: {
goerli: {
url: "", // rpc providers: infura, alchemy
accounts: [] // private keys
},
polygonTest: {
url: "https://rpc-mumbai.maticvigil.com/", // rpc providers: polygon, infura, alchemy
url: "", // rpc providers: polygon, infura, alchemy
accounts: []
},
polygonMain: {
url: "https://rpc-mainnet.maticvigil.com", // rpc providers: infura,polygon, alchemy
url: "", // rpc providers: infura,polygon, alchemy
accounts: []
}
},
solidity: {
version: "0.8.3",
version: "0.8.7",
settings: {
optimizer: {
enabled: true,
Expand All @@ -74,4 +70,4 @@ module.exports = {
mocha: {
timeout: 20000
}
}
};
21 changes: 10 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
{
"name": "hardhat-boilerplate",
"version": "1.0.0",
"description": "hardhat and ethersjs boilerplate for ethereum development",
"description": "hardhat and ethersjs boilerplate for Dapp development",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "yarn hardhat test",
"compile": "yarn hardhat compile",
"deploy": "yarn hardhat deploy"
},
"keywords": [],
"author": "",
"author": "Salman Dabbakuti",
"license": "ISC",
"devDependencies": {
"@nomiclabs/hardhat-ethers": "^2.0.2",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@nomiclabs/hardhat-web3": "^2.0.0",
"chai": "^4.3.4",
"ethers": "^5.4.7",
"hardhat": "^2.6.4",
"web3": "^1.5.3"
"@nomiclabs/hardhat-ethers": "^2.1.1",
"chai": "^4.3.6",
"ethers": "^5.7.0",
"hardhat": "^2.10.2"
}
}
}
21 changes: 21 additions & 0 deletions scripts/deploy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@

async function main() {
const contractFactory = await ethers.getContractFactory("greeter");
const contract = await contractFactory.deploy("Hello, Hardhat!");
await contract.deployed();
return contract;
}

main()
.then(async (contract) => {
console.log("Contract deployed at:", contract.address);
// Write to contract
const tx = await contract.setGreeting("Hello Ethereum Devs!");
await tx.wait();
// Read from contract
const greeting = await contract.getGreeting();
console.log('Greeting from contract:', greeting);
})
.catch((error) => {
console.error(error);
});
20 changes: 0 additions & 20 deletions scripts/deployContract.js

This file was deleted.

14 changes: 7 additions & 7 deletions test/sample-test.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("Greeter", function () {
describe("Contract Tests", function () {
it("Should return the new greeting once it's changed", async function () {
const Greeter = await ethers.getContractFactory("greeter");
const greeter = await Greeter.deploy("Hello, world!");
await greeter.deployed();
const contractFactory = await ethers.getContractFactory("greeter");
const contract = await contractFactory.deploy("Hello, world!");
await contract.deployed();

expect(await greeter.getGreeting()).to.equal("Hello, world!");
const setGreetingTx = await greeter.setGreeting("Hola, mundo!");
expect(await contract.getGreeting()).to.equal("Hello, world!");
const setGreetingTx = await contract.setGreeting("Hola, mundo!");
// wait for the transaction to be mined
await setGreetingTx.wait();
expect(await greeter.getGreeting()).to.equal("Hola, mundo!");
expect(await contract.getGreeting()).to.equal("Hola, mundo!");
});
});
Loading

0 comments on commit 4bd0387

Please sign in to comment.