This repo was created to not be stucked in tutorial hell and use knowledge from cryptozombies in passionate project
- Clone project
git clone https://github.com/Kacper-Hernacki/Crypto-zombies-alternative.git
- Redirect path to folder
cd /Crypto-zombies-alternative
- Installing dependencies
npm isntall
- Compile contract by:
npm run build
- Run tests
npm run test
- Run local testnet:
npm run local-testnet
, - deploy to local:
npm run deploy:local
-
Create package.json
npm init -y
-
Installing hardhat globally:
npx hardhat
above commend createshardhat.config.json
file//\*_ @type import('hardhat/config').HardhatUserConfig _/ module.exports = { solidity: "0.8.9", };
-
Installing Hardhat locallly
npm install --save-dev hardhat
-
Creating Folder Struture:
contracts
- folder where Solidity files are attached.scripts
- place where are stored hardhat scripts.test
- there lands test files
-
Installing hardhat plugins:
npm install --save-dev @nomiclabs/hardhat-waffle @nomiclabs/hardhat-ethers ethereum-waffle chai ethers solidity-coverage
:@nomiclabs/hardhat-waffle
: This is a Hardhat plugin that enables waffle support.@nomiclabs/hardhat-ethers
: This is a Hardhat plugin that enables ethers support.ethereum-waffle
: Waffle is a Solidity testing library. It allows you to write tests for your contracts with JavaScript.chai
: Chai is an assertion library and provides functions like expect.ethers
: This is a popular Ethereum client library. It allows you to interface with blockchains that implement the Ethereum API.solidity-coverage
: This library gives you coverage reports on unit tests with the help of Istanbul.
-
Including plugins
require("@nomiclabs/hardhat-waffle"); require("solidity-coverage"); //\*_ @type import('hardhat/config').HardhatUserConfig _/ module.exports = { solidity: "0.8.9", };
-
Adding Commands: In
package.json
under script add these lines:"build": "hardhat compile", "test:light": "hardhat test", "test": "hardhat coverage",
Above lines of code should execute: 1.
build
run smart contracts files fromcontracts
folder. 2.test:light
it invokes Waffle for testing contracts. 3.test
it invokes Waffle tests and generates coverage. -
Implementing smart contract In
contracts/MyContract.sol
put code:// SPDX-License-Identifier: MIT pragma solidity >=0.7.0 <0.9.0; contract MyContract { string private name; constructor(string memory _name) { name = _name; } function changeName(string memory _name) public { name = _name; } function getName() public view returns (string memory) { return name; } }
-
Compile contract by:
npm run build
-
testing: In
test/MyContract.test.js
put code:const { expect } = require("chai"); describe("MyContract", () => { it("should return its name", async () => { const MyContract = await ethers.getContractFactory("MyContract"); const myContract = await MyContract.deploy("My Contract"); await myContract.deployed(); expect(await myContract.getName()).to.equal("My Contract"); }); it("should change its name when requested", async () => { const MyContract = await ethers.getContractFactory("MyContract"); const myContract = await MyContract.deploy("My Contract"); await myContract.changeName("Another Contract"); expect(await myContract.getName()).to.equal("Another Contract"); }); });
-
Run tests
npm run test
-
Deploying smart contract: In
scripts/deployMyContract.js
put:async function main() { const MyContract = await ethers.getContractFactory("MyContract"); const myContract = await MyContract.deploy("My Contract"); console.log("My Contract deployed to:", myContract.address); } main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); });
-
Add deploy command in
package.json
:"deploy:local": "hardhat run --network localhost scripts/deployMyContract.js"
, -
Add local-testnet script in
package.json
:"local-testnet": "hardhat node"
, -
Run local testnet:
npm run local-testnet
, -
deploy to local:
npm run deploy:local