From 8b6ad8828b66de43f54db7e623dee950517fe21f Mon Sep 17 00:00:00 2001 From: SMAshhar <63975782+SMAshhar@users.noreply.github.com> Date: Mon, 24 Apr 2023 21:18:19 -0700 Subject: [PATCH 01/16] Update README.md --- step00_hardhat_helloworld/README.md | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/step00_hardhat_helloworld/README.md b/step00_hardhat_helloworld/README.md index ff2fa72..f6c48d0 100644 --- a/step00_hardhat_helloworld/README.md +++ b/step00_hardhat_helloworld/README.md @@ -1,12 +1,6 @@ -# Basic Sample Hardhat Project +# Hardhat Hello World Project -[Before starting development please go through the Ethereum Developer Documentation in detail](https://ethereum.org/en/developers/docs/) - -Follow this Text Book: - -[Solidity Programming Essentials: A guide to building smart contracts and tokens using the widely used Solidity language, 2nd Edition](https://www.amazon.com/Solidity-Programming-Essentials-building-contracts/dp/1803231181/ref=sr_1_2_sspa) - -[We will follow this getting started page](https://hardhat.org/getting-started/) +### Step 01: Initialize Create a Project Directory @@ -43,3 +37,11 @@ npx hardhat node node scripts/sample-script.js npx hardhat help ``` + +[Before starting development please go through the Ethereum Developer Documentation in detail](https://ethereum.org/en/developers/docs/) + +Follow this Text Book: + +[Solidity Programming Essentials: A guide to building smart contracts and tokens using the widely used Solidity language, 2nd Edition](https://www.amazon.com/Solidity-Programming-Essentials-building-contracts/dp/1803231181/ref=sr_1_2_sspa) + +[We will follow this getting started page](https://hardhat.org/getting-started/) From 8a0b04bd00b2c586a0437c91c50f53c694251eb8 Mon Sep 17 00:00:00 2001 From: SMAshhar <63975782+SMAshhar@users.noreply.github.com> Date: Mon, 24 Apr 2023 21:19:32 -0700 Subject: [PATCH 02/16] Update README.md --- step00_hardhat_helloworld/README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/step00_hardhat_helloworld/README.md b/step00_hardhat_helloworld/README.md index f6c48d0..8a89d95 100644 --- a/step00_hardhat_helloworld/README.md +++ b/step00_hardhat_helloworld/README.md @@ -2,7 +2,11 @@ ### Step 01: Initialize -Create a Project Directory +Create a Project Directory: Name it "Hardhat-Initiation" (or whatever is good with you) + +initialize project by running: + + npm init Make it an npm library: From a056ca095e9cb7e04e306fdf5a5b77a26aff6cb0 Mon Sep 17 00:00:00 2001 From: SMAshhar <63975782+SMAshhar@users.noreply.github.com> Date: Mon, 24 Apr 2023 21:37:25 -0700 Subject: [PATCH 03/16] Update README.md --- step00_hardhat_helloworld/README.md | 41 ++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/step00_hardhat_helloworld/README.md b/step00_hardhat_helloworld/README.md index 8a89d95..efcf47c 100644 --- a/step00_hardhat_helloworld/README.md +++ b/step00_hardhat_helloworld/README.md @@ -6,19 +6,46 @@ Create a Project Directory: Name it "Hardhat-Initiation" (or whatever is good wi initialize project by running: - npm init + npm init -Make it an npm library: +### Step 02: Install Hardhat -npm init + npm install --save-dev hardhat + +### Step 03: Initialize Hardhat -npm install --save-dev hardhat + npx hardhat -npx hardhat +Select one of the three options to create a basic project. You can go for Javascript or typescript, but lets go for an empty file this time. + +This is initialize an empty hardhat file in your directory. + +### Step 04: Installing Dependencies + +Run the following in your terminal for basic dependencies. + + npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers + +#### OR + +Run the following for the full toolbox. + + npm install @nomicfoundation/hardhat-toolbox + +### Step 05: Make Folders and File Structure + +Make 3 folders in your main directory: +1. contract +2. script +3. test + +Now make three files: + +1. Make a file in "contract" directory. Name it "MyTest.sol". +2. Make a file in "script" directory. Name it "deploy.js". +3. Make a file in "test" directory. Name it "test.js". -Select to create a basic project from the menu -npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers npx hardhat compile From 0ab710266d99ee7820352dfbf24cd0bc26ccd0ba Mon Sep 17 00:00:00 2001 From: SMAshhar <63975782+SMAshhar@users.noreply.github.com> Date: Mon, 24 Apr 2023 21:42:39 -0700 Subject: [PATCH 04/16] Update README.md --- step00_hardhat_helloworld/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/step00_hardhat_helloworld/README.md b/step00_hardhat_helloworld/README.md index efcf47c..489d938 100644 --- a/step00_hardhat_helloworld/README.md +++ b/step00_hardhat_helloworld/README.md @@ -45,7 +45,8 @@ Now make three files: 2. Make a file in "script" directory. Name it "deploy.js". 3. Make a file in "test" directory. Name it "test.js". - +*GOOD TO HAVE: If working on VSCode, the extension Solidity by Nomic Foundation is very helpfull for predefined code structures for solidity. +(https://marketplace.visualstudio.com/items?itemName=NomicFoundation.hardhat-solidity) npx hardhat compile From 2635216a57e56c3874cf225a68139c9291815e98 Mon Sep 17 00:00:00 2001 From: SMAshhar <63975782+SMAshhar@users.noreply.github.com> Date: Tue, 25 Apr 2023 19:39:08 -0700 Subject: [PATCH 05/16] Update README.md --- step00_hardhat_helloworld/README.md | 42 +++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/step00_hardhat_helloworld/README.md b/step00_hardhat_helloworld/README.md index 489d938..3d2ca4c 100644 --- a/step00_hardhat_helloworld/README.md +++ b/step00_hardhat_helloworld/README.md @@ -48,6 +48,48 @@ Now make three files: *GOOD TO HAVE: If working on VSCode, the extension Solidity by Nomic Foundation is very helpfull for predefined code structures for solidity. (https://marketplace.visualstudio.com/items?itemName=NomicFoundation.hardhat-solidity) +### Step 06: Let us start with writing our first contract + +Add the following simple code to the file MyTest.sol file we just made in the contract folder. + +```shell +//SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.9; + +//IMPORT THE HARDHAT CONSOLE +import "../node_modules/hardhat/console.sol"; + +//WRITING A SIMPLE CONTRACT +contract MyTest { + uint256 public unlockedTime; + address payable public owner; + + event Widthrawal(uint256 amount, uint256 when); + + constructor(uint256 _unlockedTime) payable { + require( + block.timestamp < _unlockedTime, + "Unlocked time should be in future." + ); + + unlockedTime = _unlockedTime; + owner = payable(msg.sender); + } +//A SIMPLE WITHDRAWING FUNCTION + function withdraw() public { + require( + block.timestamp >= unlockedTime, + "Wait till the time period ends" + ); + require(msg.sender == owner, "You are not an owner"); + + emit Widthrawal(address(this).balance, block.timestamp); + + owner.transfer(address(this).balance); + } +} +``` + npx hardhat compile npx hardhat test From 3ba6e80fd683bff8afef8ebf31e9bdaa0337a2b2 Mon Sep 17 00:00:00 2001 From: SMAshhar <63975782+SMAshhar@users.noreply.github.com> Date: Tue, 25 Apr 2023 22:07:09 -0700 Subject: [PATCH 06/16] Update README.md --- step00_hardhat_helloworld/README.md | 135 ++++++++++++++++++++++++---- 1 file changed, 120 insertions(+), 15 deletions(-) diff --git a/step00_hardhat_helloworld/README.md b/step00_hardhat_helloworld/README.md index 3d2ca4c..55f8786 100644 --- a/step00_hardhat_helloworld/README.md +++ b/step00_hardhat_helloworld/README.md @@ -18,7 +18,17 @@ initialize project by running: Select one of the three options to create a basic project. You can go for Javascript or typescript, but lets go for an empty file this time. -This is initialize an empty hardhat file in your directory. +Add the following code in the newly made "hardhat.config.js" file: + +```script +require("@nomicfoundation/hardhat-toolbox"); + +/** @type import('hardhat/config').HardhatUserConfig */ +module.exports = { + solidity: "0.8.18", +}; +``` + ### Step 04: Installing Dependencies @@ -35,13 +45,13 @@ Run the following for the full toolbox. ### Step 05: Make Folders and File Structure Make 3 folders in your main directory: -1. contract +1. contracts 2. script 3. test Now make three files: -1. Make a file in "contract" directory. Name it "MyTest.sol". +1. Make a file in "contracts" directory. Name it "MyTest.sol". 2. Make a file in "script" directory. Name it "deploy.js". 3. Make a file in "test" directory. Name it "test.js". @@ -50,7 +60,7 @@ Now make three files: ### Step 06: Let us start with writing our first contract -Add the following simple code to the file MyTest.sol file we just made in the contract folder. +Add the following simple code to the file "MyTest.sol" file we just made in the "contracts" folder. ```shell //SPDX-License-Identifier: UNLICENSED @@ -90,27 +100,122 @@ contract MyTest { } ``` -npx hardhat compile +Now for the script: + +Add the following self-explanatory code to the "deploy.js" file we made in the "script" folder. + +```script +const hre = require('hardhat') + +// RUN THE CONSOLE AS BELOW TO UNDERSTAND WHAT INFORMATION "hre" CARRIES +// console.log(hre) + +async function main() { + const currentTimstampInSeconds = Math.round(Date.now() / 1000); + const ONE_YEAR_IN_SECONDS = 365 * 24 * 60 * 60; + const unlockedTime = currentTimstampInSeconds + ONE_YEAR_IN_SECONDS; + // Whenever the contract is deployed, the owner can withdraw after one year. + + const lockedAmount = hre.ethers.utils.parseEther("1"); + + // RUN THE CONSOLE AS BELOW TO UNDERSTAND WHAT INFORMATION EACH CONSTANT CARRIES + // console.log(currentTimstampInSeconds); + // console.log(ONE_YEAR_IN_SECONDS); + // console.log(unlockedTime); + // console.log(lockedAmount) + + const MyTest = await hre.ethers.getContractFactory("MyTest"); + const myTest = await MyTest.deploy(unlockedTime, { value: lockedAmount}); -npx hardhat test + await myTest.deployed(); + console.log(`Contract contains '1' ETH & address: ${myTest.address}`) +} + +main().catch((error) => { + console.log(error); + process.exitCode = 1; +}) +``` +Then run the following command. + npx hardhat run script/deploy.js + +And let the magic happen. You will see two new folder being made: +1. Artifact +2. cache +These carry the information about the contract. +The contract data can also be fetched by "console.log(myTest)" in the deploy.js file. +This data can be fetched and used in the front end when we get there. + +Congradulations. You have deployed your first smart contract with hardhat. But it is not over yet. +### STEP 07: Test our contract: -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. +We have made and deployed our contract, but testing our contract is crucial as well. Lets make a function that we will be using for testing our contract. Note that there should be several tests that we have to perform during any such transaction, but this time, we will suffice for just one. -Try running some of the following tasks: +Add the following self-explanatory code to the "test.js" file we made in the "test" folder. ```shell -npx hardhat accounts -npx hardhat compile -npx hardhat clean -npx hardhat test -npx hardhat node -node scripts/sample-script.js -npx hardhat help +const {time, loadFixture} = require("@nomicfoundation/hardhat-network-helpers") + +// RUN THE CONSOLE AS BELOW TO UNDERSTAND WHAT INFORMATION EACH CONSTANT CARRIES +// console.log(time, loadFixture) + +const {anyValue} = require("@nomicfoundation/hardhat-chai-matchers") +// console.log(anyValue) + +const {expect} = require("chai") +const {ethers} = require("hardhat") +// console.log(expect, ethers) + +describe("MyTest", function () { + async function runEveryTime() { + const ONE_YEAR_IN_SECONDS = 365 * 24 * 60 * 60; + const ONE_GEWI = 1000000000; + const lockedAmount = ONE_GEWI; + const unlockedTime = (await time.latest()) + ONE_YEAR_IN_SECONDS; + // GET ACCOUNTS + const [owner, otherAccount] = await Promise.all([ethers.getSigner(), ethers.getSigner(1)]); + // ethers.getSigner can provide you upto 20 accounts. + + const MyTest = await ethers.getContractFactory("MyTest"); + const myTest = await MyTest.deploy(unlockedTime, {value: lockedAmount}) + + // CONSOLE.LOG EVERYTHING TO UNDERSTAND THE DATA EACH CARRY + + return { + myTest, + unlockedTime, + lockedAmount, + owner, + otherAccount + } + } + + // TESTING OUR FIRSST TEST: DEPLOYMENT + // describe allows you to check a specefic condition in a smart contract. + describe("Deployment", function () { // CHECKING TIME + it("Should check unlocked time", async function () { + const {myTest, unlockedTime} = await loadFixture(runEveryTime) + // console.log(unlockedTime, myTest) + expect(await myTest.unlockedTime()).to.equal(unlockedTime) + // comparing actual time and our contract time. + // you can sage the expect in a variable and console.log it to understand its value + }); + }) + runEveryTime() +}) ``` +To run this test, run the following in your terminal. + + npx hardhat test + +Congradulations. You have successfully written, deployed and tested your first smart contract through Hardhat. + + +### Go through the following links if you want to learn more. [Before starting development please go through the Ethereum Developer Documentation in detail](https://ethereum.org/en/developers/docs/) From bad257c3a0f4d8b14c54d2a2c9a0b57267884176 Mon Sep 17 00:00:00 2001 From: SMAshhar <63975782+SMAshhar@users.noreply.github.com> Date: Tue, 25 Apr 2023 22:08:58 -0700 Subject: [PATCH 07/16] Delete Greeter.sol --- .../contracts/Greeter.sol | 22 ------------------- 1 file changed, 22 deletions(-) delete mode 100644 step00_hardhat_helloworld/contracts/Greeter.sol diff --git a/step00_hardhat_helloworld/contracts/Greeter.sol b/step00_hardhat_helloworld/contracts/Greeter.sol deleted file mode 100644 index efffb8f..0000000 --- a/step00_hardhat_helloworld/contracts/Greeter.sol +++ /dev/null @@ -1,22 +0,0 @@ -//SPDX-License-Identifier: Unlicense -pragma solidity ^0.8.0; - -import "hardhat/console.sol"; - -contract Greeter { - string private greeting; - - constructor(string memory _greeting) { - console.log("Deploying a Greeter with greeting:", _greeting); - greeting = _greeting; - } - - function greet() public view returns (string memory) { - return greeting; - } - - function setGreeting(string memory _greeting) public { - console.log("Changing greeting from '%s' to '%s'", greeting, _greeting); - greeting = _greeting; - } -} From 7a42b1a5f6b9534c79776d64477bd6d34f4f54ab Mon Sep 17 00:00:00 2001 From: SMAshhar <63975782+SMAshhar@users.noreply.github.com> Date: Tue, 25 Apr 2023 22:09:13 -0700 Subject: [PATCH 08/16] Delete sample-script.js --- .../scripts/sample-script.js | 32 ------------------- 1 file changed, 32 deletions(-) delete mode 100644 step00_hardhat_helloworld/scripts/sample-script.js diff --git a/step00_hardhat_helloworld/scripts/sample-script.js b/step00_hardhat_helloworld/scripts/sample-script.js deleted file mode 100644 index 90cd819..0000000 --- a/step00_hardhat_helloworld/scripts/sample-script.js +++ /dev/null @@ -1,32 +0,0 @@ -// We require the Hardhat Runtime Environment explicitly here. This is optional -// but useful for running the script in a standalone fashion through `node