├── .gitignore ├── package.json ├── hardhat.config.js ├── scripts └── deploy.js ├── test └── test.js ├── README.md └── contracts └── firstContract.sol /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | 3 | 4 | # hardhat files 5 | artifacts 6 | cache 7 | 8 | # env file 9 | .env 10 | 11 | # yarn lock 12 | yarn.lock -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "my-first-contract", 3 | "version": "1.0.0", 4 | "main": "index.js", 5 | "license": "MIT", 6 | "dependencies": { 7 | "@nomiclabs/hardhat-ethers": "^2.0.5", 8 | "@nomiclabs/hardhat-waffle": "^2.0.3", 9 | "chai": "^4.3.6", 10 | "dotenv": "^16.0.0", 11 | "ethers": "^5.6.2", 12 | "hardhat": "^2.9.2" 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /hardhat.config.js: -------------------------------------------------------------------------------- 1 | require('dotenv').config(); 2 | require("@nomiclabs/hardhat-waffle"); 3 | require("@nomiclabs/hardhat-ethers"); 4 | 5 | module.exports = { 6 | solidity: "0.8.7", 7 | defaultNetwork: "rinkeby", 8 | networks: { 9 | hardhat: { 10 | }, 11 | rinkeby: { 12 | url: process.env.MY_ALCHEMY_URL, 13 | accounts: [process.env.MY_METAMASK_PRIVATE_KEY] 14 | } 15 | }, 16 | }; 17 | -------------------------------------------------------------------------------- /scripts/deploy.js: -------------------------------------------------------------------------------- 1 | const hre = require("hardhat"); 2 | 3 | async function main() { 4 | const firstContract = await hre.ethers.getContractFactory("firstContract"); 5 | const deployFirstContract = await firstContract.deploy(); 6 | 7 | await deployFirstContract.deployed(); 8 | 9 | console.log("My first contract deployed to:", deployFirstContract.address); 10 | } 11 | 12 | ;(async () => { 13 | try { 14 | await main(); 15 | process.exit(0); 16 | } catch (error) { 17 | console.error(error); 18 | process.exit(1); 19 | } 20 | })(); -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | const { expect } = require("chai"); 2 | const { ethers } = require("hardhat"); 3 | 4 | describe("firstContract", function () { 5 | it("should store student information", async function () { 6 | const firstContract = await ethers.getContractFactory("firstContract"); 7 | const myFirstContract = await firstContract.deploy(); 8 | await myFirstContract.deployed(); 9 | 10 | const myFirstContractInfo = await myFirstContract.storeStudentInfo("171014051", "Md. Muhtasim Fuad Fahim", "ULAB", "CSE"); 11 | 12 | // wait until the transaction is mined 13 | await myFirstContractInfo.wait(); 14 | 15 | expect(await myFirstContract.getStudentInfo("171014051")); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # My First Smart Contract Deployment and Testing using Hardhat 2 | 3 | This project demonstrates a basic Hardhat use case. It comes with a sample contract for any registration process, 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. For any registration process this smart contract can be used. 4 | 5 | 6 | ## Clone the repository and download dependencies 7 | 8 | ```shell 9 | git clone https://github.com/mdmuhtasimfuadfahim/My-hardhat-smart-contract 10 | cd My-hardhat-smart-contract 11 | yarn install 12 | # or 13 | npm install 14 | ``` 15 | 16 | ## Environment variables 17 | Create an .env file and set these settings 18 | 19 | ```shell 20 | MY_ALCHEMY_URL = alchemy_url 21 | MY_METAMASK_PRIVATE_KEY = "metamask_private_key" 22 | ``` 23 | 24 | ## Deploy contract 25 | ```shell 26 | npm hardhat compile 27 | npx run scripts/deploy.js --network rinkeby 28 | # or 29 | node scripts/deploy.js 30 | ``` 31 | 32 | ## Testing 33 | 34 | ```shell 35 | npx hardhat test 36 | ``` 37 | 38 | ## Some Basic Commands of Hardhat 39 | ```shell 40 | npx hardhat compile 41 | npx hardhat clean 42 | npx hardhat accounts 43 | npx hardhat node 44 | npx hardhat help 45 | node scripts/file_name.js 46 | npx hardhat test 47 | ``` 48 | 49 | ## License 50 | - MIT 51 | 52 | ## Contribution 53 | 54 | Please feel free to contact me for any contribution. 55 | 56 | ##### Thank you 57 | -------------------------------------------------------------------------------- /contracts/firstContract.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | pragma solidity ^0.8.7; 3 | 4 | contract firstContract{ 5 | struct studentInfo{ 6 | string id; 7 | string name; 8 | string university; 9 | string department; 10 | } 11 | 12 | address public admin; 13 | address public student; 14 | 15 | constructor(){ 16 | admin = msg.sender; 17 | } 18 | 19 | modifier isAdmin(){ 20 | require(admin == msg.sender, "You cannot sign transaction until you have an admin account"); 21 | _; 22 | } 23 | 24 | modifier notStudent(){ 25 | require(student != msg.sender, "You are a student"); 26 | _; 27 | } 28 | 29 | mapping(string => studentInfo) studentInformation; 30 | 31 | event logStudentInfo( 32 | string id, 33 | string name, 34 | string university, 35 | string department, 36 | string status 37 | ); 38 | 39 | 40 | function storeStudentInfo(string memory _id, string memory _name, string memory _university, string memory _department) public 41 | isAdmin() 42 | { 43 | studentInformation[_id].id = _id; 44 | studentInformation[_id].name = _name; 45 | studentInformation[_id].university = _university; 46 | studentInformation[_id].department = _department; 47 | emit logStudentInfo(_id, _name, _university, _department, "Registration Successful"); 48 | } 49 | 50 | function getStudentInfo(string memory _id) public view returns(string memory, string memory, string memory, string memory) 51 | { 52 | return(studentInformation[_id].id, studentInformation[_id].name, studentInformation[_id].university, studentInformation[_id].department); 53 | } 54 | 55 | function isAdmin_() public view returns(bool){ 56 | return msg.sender == admin; 57 | } 58 | 59 | function notStudent_() public view returns(bool){ 60 | return msg.sender == student; 61 | } 62 | } --------------------------------------------------------------------------------