From 2add8df77e8fb2b9de1c53296c8dad9822133010 Mon Sep 17 00:00:00 2001 From: Tumi Date: Mon, 28 Jul 2025 12:07:10 +0100 Subject: [PATCH 1/3] refac: modify counter.sol and add counter.js test --- contracts/Counter.sol | 17 +++++- test/Counter.js | 127 ++++++++++++++++++++++++++++-------------- 2 files changed, 100 insertions(+), 44 deletions(-) diff --git a/contracts/Counter.sol b/contracts/Counter.sol index fa8560c..98b72b0 100644 --- a/contracts/Counter.sol +++ b/contracts/Counter.sol @@ -11,7 +11,7 @@ interface ICounter { contract Counter is ICounter { uint256 public count; - function setCount(uint256 _count) external { + function setCount(uint256 _count) public { count = _count; } @@ -24,6 +24,21 @@ contract Counter is ICounter { } } +// // contract D is C { +// // string public myName; + +// // function getTotalSupply() public view returns(uint256) { +// // return totalSupply(); +// // } + +// // function setName(string memory _myName) external { +// // myName = _myName; +// // } +// // } + +// // contract E is D { + +// // } // contract F { // // Initializing interface IC diff --git a/test/Counter.js b/test/Counter.js index 99b2931..d187d3c 100644 --- a/test/Counter.js +++ b/test/Counter.js @@ -1,49 +1,90 @@ -const {loadFixture } = require("@nomicfoundation/hardhat-toolbox/network-helpers"); +const { + loadFixture, +} = require("@nomicfoundation/hardhat-toolbox/network-helpers"); // const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs"); const { expect } = require("chai"); -// util functon +// DEPLOY const deployCounter = async () => { - // target the Counter contract within our contract folder - const CounterContract = await ethers.getContractFactory("Counter"); // target Counter.sol - const counter = await CounterContract.deploy(); // deploy the Counter contract - return counter ; // return the deployed instance of our counter contract -} + // const [owner, otherAccount] = await ethers.getSigners(); -// Counter Test Suite + const CounterContract = await ethers.getContractFactory("Counter"); //targeting counter.sol + const counter = await CounterContract.deploy(); //if no constructor no need to put anything in the depl0y place + + return counter; //return the deployed instance of our counter contract +}; + +// TEST SUITE describe("Counter Test Suite", () => { - describe("Deployment", () => { - it("Should return default values upon deployment", async () => { - const counter = await loadFixture(deployCounter); - expect(await counter.count()).to.eq(0); // assert that count = 0 upon deployment - }) - }) - - describe("Transactions", () => { - describe("SetCount", () => { - it("Should set appropriate count values", async () => { - const counter = await loadFixture(deployCounter); // extract deployed counter instace - let count1 = await counter.getCount(); // check initial count value before txn - expect(count1).to.eq(0); - await counter.setCount(10) // assert that count = 0 upon deployment - - let count2 = await counter.getCount(); // check initial count value before txn - expect(count2).to.eq(10) // check final count = 10 - }) - - it("Should set appropriate values for multiple setCount txns", async () => { - - }) - }) - - describe("IncreaseCountByOne", () => { - it("Should set appropriate increaseCountByOne value", async () => { - - }) - - it("Should set appropriate values for multiple increaseCountByOne txns", async () => { - - }) - }) - }) -}) \ No newline at end of file + describe("Deployment", async () => { + it("Should return default values upon deployment", async () => { + const counter = await loadFixture(deployCounter); + expect(await counter.getCount()).to.eq(0); //assert count equal to 0 upon deployment + // console.log("counter here: ___", counter); + }); + }); + describe("Transaction", async () => { + describe("SetCount", async () => { + it("Should set appropriate count values", async () => { + const counter = await loadFixture(deployCounter); //extract deployed counter instance + let count1 = await counter.getCount(); //check initial value before transaction + expect(count1).to.eq(0); + await counter.setCount(10); + let count2 = await counter.getCount(); + expect(count2).to.eq(10); //check final count equal to 10 + // expect(await counter.getCount()).to.eq(0); //assert count equal to 0 upon deployment + // console.log("counter here: ___", counter); + }); + it("Should set appropriate values for multiple setCount txns", async () => { + const counter = await loadFixture(deployCounter); //extract deployed counter instance + let count1 = await counter.getCount(); //check initial value before transaction + expect(count1).to.eq(0); + await counter.setCount(10); + let count2 = await counter.getCount(); + expect(count2).to.eq(10); //check final count equal to 10 + + let count3 = await counter.getCount(); + expect(count3).to.eq(10); + await counter.setCount(11); + let count4 = await counter.getCount(); + expect(count4).to.eq(11); //check final count equal to 10 + + let count5 = await counter.getCount(); + expect(count5).to.eq(11); + await counter.setCount(12); + let count6 = await counter.getCount(); + expect(count6).to.eq(12); //check final count equal to 10 + }); + }); + describe("Increase count by one", async () => { + it("Should set appropriate count value to increase by one", async () => { + const counter = await loadFixture(deployCounter); + let count1 = await counter.getCount(); + expect(count1).to.eq(0); + await counter.increaseCountByOne(); + let count2 = await counter.getCount(); + expect(count2).to.eq(1); + }); + it("Should set appropriate count value to increase by one for multiple increasebyone function", async () => { + const counter = await loadFixture(deployCounter); + let count1 = await counter.getCount(); + expect(count1).to.eq(0); + await counter.increaseCountByOne(); + let count2 = await counter.getCount(); + expect(count2).to.eq(1); + + let count3 = await counter.getCount(); + expect(count3).to.eq(1); + await counter.increaseCountByOne(); + let count4 = await counter.getCount(); + expect(count4).to.eq(2); + + let count5 = await counter.getCount(); + expect(count5).to.eq(2); + await counter.increaseCountByOne(); + let count6 = await counter.getCount(); + expect(count6).to.eq(3); + }); + }); + }); +}); From 95cf16f91e50eade45c1e382e8a33f6317392b2d Mon Sep 17 00:00:00 2001 From: Tumi Date: Mon, 28 Jul 2025 12:19:13 +0100 Subject: [PATCH 2/3] feat: implement counterV2 and counterV2caller and test for functionality --- contracts/Counter.sol | 63 ------------------------ contracts/CounterV2.sol | 63 ++++++++++++++++++++++++ contracts/CounterV2Caller.sol | 14 ++++++ interfaces/ICounterV2.sol | 9 ++++ test/Counter.js | 90 ----------------------------------- test/CounterV2.js | 74 ++++++++++++++++++++++++++++ test/CounterV2Caller.js | 50 +++++++++++++++++++ 7 files changed, 210 insertions(+), 153 deletions(-) delete mode 100644 contracts/Counter.sol create mode 100644 contracts/CounterV2.sol create mode 100644 contracts/CounterV2Caller.sol create mode 100644 interfaces/ICounterV2.sol delete mode 100644 test/Counter.js create mode 100644 test/CounterV2.js create mode 100644 test/CounterV2Caller.js diff --git a/contracts/Counter.sol b/contracts/Counter.sol deleted file mode 100644 index 98b72b0..0000000 --- a/contracts/Counter.sol +++ /dev/null @@ -1,63 +0,0 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.28; - -interface ICounter { - function setCount(uint256 _count) external; - function increaseCountByOne() external; - function getCount() external view returns(uint256); - -} - -contract Counter is ICounter { - uint256 public count; - - function setCount(uint256 _count) public { - count = _count; - } - - function increaseCountByOne() public { - count += 1; - } - - function getCount() public view returns(uint256) { - return count; - } -} - -// // contract D is C { -// // string public myName; - -// // function getTotalSupply() public view returns(uint256) { -// // return totalSupply(); -// // } - -// // function setName(string memory _myName) external { -// // myName = _myName; -// // } -// // } - -// // contract E is D { - -// // } - -// contract F { -// // Initializing interface IC -// IC public _ic; -// // Initializing the contract address -// address public contractCAddress; - -// constructor(address _contractCAddress) { -// // Set the contract address to the state variable contract address -// contractCAddress = _contractCAddress; -// // Passing the contract address into interface using the address instance of another contract -// _ic = IC(_contractCAddress); -// } - -// function setCount(uint256 _count) public { -// _ic.setCount(_count); -// } - -// function getCount() public view returns(uint256) { -// return _ic.getCount(); -// } -// } \ No newline at end of file diff --git a/contracts/CounterV2.sol b/contracts/CounterV2.sol new file mode 100644 index 0000000..7c895b9 --- /dev/null +++ b/contracts/CounterV2.sol @@ -0,0 +1,63 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.28; + +import "../interfaces/ICounterV2.sol"; + +// interface ICounterV2{ +// function setCount(uint count) external; +// function getCount() external returns (uint); +// function resetCount() external; +// function decreaseCount() external; +// } + +// contract CounterV2 { +contract CounterV2 is ICounterV2{ + + uint count; + address owner; + + constructor(){ + owner = msg.sender; + } + + // modifier onlyOwner() { + // require (owner == msg.sender, "unauthorized"); + // _; + // } + + function setCount(uint _count) public { + require(owner == msg.sender, "unauthorized"); + require(_count<=10, "count should not be greater than 10"); + count = _count; + } + + function getCount() public view returns(uint){ + return count; + } + + function resetCount() public{ + require(owner == msg.sender, "unauthorized"); + count = 0; + } + + function decreaseCount() public{ + count -= 1; + } + + function increaseCount() public{ + count+=1; + } + +} + +// contract CounterV2Caller{ +// ICounterV2 public icounterv2; + +// constructor(address _counterV2address){ +// icounterv2 = ICounterV2(_counterV2address); +// } + +// function decreaseCount() public{ +// icounterv2.decreaseCount(); +// } +// } \ No newline at end of file diff --git a/contracts/CounterV2Caller.sol b/contracts/CounterV2Caller.sol new file mode 100644 index 0000000..3615e2e --- /dev/null +++ b/contracts/CounterV2Caller.sol @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.28; +import "../interfaces/ICounterV2.sol"; +contract CounterV2Caller{ + ICounterV2 public icounterv2; + + constructor(address _counterV2address){ + icounterv2 = ICounterV2(_counterV2address); + } + + function decreaseCount() public{ + icounterv2.decreaseCount(); + } +} \ No newline at end of file diff --git a/interfaces/ICounterV2.sol b/interfaces/ICounterV2.sol new file mode 100644 index 0000000..5b76f53 --- /dev/null +++ b/interfaces/ICounterV2.sol @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: MIT +pragma solidity 0.8.28; + +interface ICounterV2{ + function setCount(uint count) external; + function getCount() external returns (uint); + function resetCount() external; + function decreaseCount() external; +} \ No newline at end of file diff --git a/test/Counter.js b/test/Counter.js deleted file mode 100644 index d187d3c..0000000 --- a/test/Counter.js +++ /dev/null @@ -1,90 +0,0 @@ -const { - loadFixture, -} = require("@nomicfoundation/hardhat-toolbox/network-helpers"); -// const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs"); -const { expect } = require("chai"); - -// DEPLOY -const deployCounter = async () => { - // const [owner, otherAccount] = await ethers.getSigners(); - - const CounterContract = await ethers.getContractFactory("Counter"); //targeting counter.sol - const counter = await CounterContract.deploy(); //if no constructor no need to put anything in the depl0y place - - return counter; //return the deployed instance of our counter contract -}; - -// TEST SUITE -describe("Counter Test Suite", () => { - describe("Deployment", async () => { - it("Should return default values upon deployment", async () => { - const counter = await loadFixture(deployCounter); - expect(await counter.getCount()).to.eq(0); //assert count equal to 0 upon deployment - // console.log("counter here: ___", counter); - }); - }); - describe("Transaction", async () => { - describe("SetCount", async () => { - it("Should set appropriate count values", async () => { - const counter = await loadFixture(deployCounter); //extract deployed counter instance - let count1 = await counter.getCount(); //check initial value before transaction - expect(count1).to.eq(0); - await counter.setCount(10); - let count2 = await counter.getCount(); - expect(count2).to.eq(10); //check final count equal to 10 - // expect(await counter.getCount()).to.eq(0); //assert count equal to 0 upon deployment - // console.log("counter here: ___", counter); - }); - it("Should set appropriate values for multiple setCount txns", async () => { - const counter = await loadFixture(deployCounter); //extract deployed counter instance - let count1 = await counter.getCount(); //check initial value before transaction - expect(count1).to.eq(0); - await counter.setCount(10); - let count2 = await counter.getCount(); - expect(count2).to.eq(10); //check final count equal to 10 - - let count3 = await counter.getCount(); - expect(count3).to.eq(10); - await counter.setCount(11); - let count4 = await counter.getCount(); - expect(count4).to.eq(11); //check final count equal to 10 - - let count5 = await counter.getCount(); - expect(count5).to.eq(11); - await counter.setCount(12); - let count6 = await counter.getCount(); - expect(count6).to.eq(12); //check final count equal to 10 - }); - }); - describe("Increase count by one", async () => { - it("Should set appropriate count value to increase by one", async () => { - const counter = await loadFixture(deployCounter); - let count1 = await counter.getCount(); - expect(count1).to.eq(0); - await counter.increaseCountByOne(); - let count2 = await counter.getCount(); - expect(count2).to.eq(1); - }); - it("Should set appropriate count value to increase by one for multiple increasebyone function", async () => { - const counter = await loadFixture(deployCounter); - let count1 = await counter.getCount(); - expect(count1).to.eq(0); - await counter.increaseCountByOne(); - let count2 = await counter.getCount(); - expect(count2).to.eq(1); - - let count3 = await counter.getCount(); - expect(count3).to.eq(1); - await counter.increaseCountByOne(); - let count4 = await counter.getCount(); - expect(count4).to.eq(2); - - let count5 = await counter.getCount(); - expect(count5).to.eq(2); - await counter.increaseCountByOne(); - let count6 = await counter.getCount(); - expect(count6).to.eq(3); - }); - }); - }); -}); diff --git a/test/CounterV2.js b/test/CounterV2.js new file mode 100644 index 0000000..983e5fb --- /dev/null +++ b/test/CounterV2.js @@ -0,0 +1,74 @@ +const { + loadFixture, +} = require("@nomicfoundation/hardhat-toolbox/network-helpers"); +// const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs"); +const { expect } = require("chai"); + +// DEPLOY +const deployCounter = async () => { + const [owner, otherAccount] = await ethers.getSigners(); + + const CounterV2Contract = await ethers.getContractFactory("CounterV2"); //targeting counter.sol + const counter = await CounterV2Contract.deploy(); //if no constructor no need to put anything in the depl0y place + + return { counter, owner, otherAccount }; //return the deployed instance of our counter contract +}; + +describe("CounterV2 Test Suite", () => { + describe("Deployment", async () => { + it("Should return default values upon deployment", async () => { + const { counter } = await loadFixture(deployCounter); + expect(await counter.getCount()).to.eq(0); //assert count equal to 0 upon deployment + // console.log("counter here: ___", counter); + }); + }); + describe("Transactions", async () => { + it("Should set appropriate count values", async () => { + const { counter } = await loadFixture(deployCounter); + const counter1 = await counter.getCount(); + expect(counter1).to.eq(0); + await counter.setCount(10); + const counter2 = await counter.getCount(); + expect(counter2).to.eq(10); + }); + it("Should reset count values to 0", async () => { + const { counter } = await loadFixture(deployCounter); + // const counter1 = await counter.getCount(); + // expect(counter1).to.eq(0); + await counter.resetCount(); + const counter2 = await counter.getCount(); + expect(counter2).to.eq(0); + }); + it("Should increase count ", async () => { + const { counter } = await loadFixture(deployCounter); + const counter1 = await counter.getCount(); + expect(counter1).to.eq(0); + await counter.increaseCount(); + const counter2 = await counter.getCount(); + expect(counter2).to.eq(1); + }); + it("Should decrease count ", async () => { + const { counter } = await loadFixture(deployCounter); + // const counter1 = await counter.getCount(); + // expect(counter1).to.eq(0); + await counter.setCount(6); + await counter.decreaseCount(); + const counter2 = await counter.getCount(); + expect(counter2).to.eq(5); + }); + describe("Reverts", () => { + it("should revert if invalid owner calls setCount", async () => { + const { counter, otherAccount } = await loadFixture(deployCounter); + await expect( + counter.connect(otherAccount).setCount(10) + ).to.be.revertedWith("unauthorized"); + }); + it("should revert if invalid calls resetCount", async () => { + const { counter, otherAccount } = await loadFixture(deployCounter); + await expect( + counter.connect(otherAccount).resetCount() + ).to.be.revertedWith("unauthorized"); + }); + }); + }); +}); diff --git a/test/CounterV2Caller.js b/test/CounterV2Caller.js new file mode 100644 index 0000000..060cc26 --- /dev/null +++ b/test/CounterV2Caller.js @@ -0,0 +1,50 @@ +const { + loadFixture, +} = require("@nomicfoundation/hardhat-toolbox/network-helpers"); +// const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs"); +const { expect } = require("chai"); + +// DEPLOY +const deployCounter = async () => { + // const [owner, otherAccount] = await ethers.getSigners(); + + const CounterV2Contract = await ethers.getContractFactory("CounterV2"); + const counterV2 = await CounterV2Contract.deploy(); //if no constructor no need to put anything in the depl0y place + const counterV2address = await counter.getAddress(); + + const CounterV2CallerContract = await ethers.getContractFactory( + "CounterV2Caller" + ); //targeting counter.sol + const counterV2Caller = await CounterV2CallerContract.deploy( + counterV2address + ); + + return { counterV2, counterV2Caller }; //return the deployed instance of our counter contract +}; + +describe("CounterV2Caller Test Suite", () => { + describe("Deployment", () => { + it("Should return default values upon deployment", async () => { + const { counterV2 } = await loadFixture(deployCounter); + expect(await counterV2.getCount()).to.eq(0); //assert count equal to 0 upon deployment + // console.log("counter here: ___", counter); + }); + }); + describe("Transactions", async () => { + it("Should successfully decrement", async () => { + const { counterV2, counterV2Caller } = await loadFixture(deployCounter); + await counterV2.setCount(10); + await counterV2Caller.decreaseCount(); + + const countAfterChnage = await counterV2.getCount(); + expect(countAfterChnage).to.eq(9); + }); + // it("Should decrease count ", async () => { + // const { counter, counterV2 } = await loadFixture(deployCounter); + // await counter.setCount(9); + // await counterV2.decreaseCount(); + // const counter2 = await counter.getCount(); + // expect(counter2).to.eq(8); + // }); + }); +}); From 91fe953d4355aca4446d269353b897acf5ac7bb1 Mon Sep 17 00:00:00 2001 From: Tumi Date: Tue, 29 Jul 2025 21:13:33 +0100 Subject: [PATCH 3/3] test: test all public functions of erc20 --- contracts/BlockToken.sol | 37 +++++++++ package-lock.json | 9 +++ package.json | 9 ++- test/BlockToken.js | 162 +++++++++++++++++++++++++++++++++++++++ test/CounterV2Caller.js | 3 +- 5 files changed, 215 insertions(+), 5 deletions(-) create mode 100644 contracts/BlockToken.sol create mode 100644 test/BlockToken.js diff --git a/contracts/BlockToken.sol b/contracts/BlockToken.sol new file mode 100644 index 0000000..c2fc742 --- /dev/null +++ b/contracts/BlockToken.sol @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; + +contract BlockToken is ERC20{ + + address public owner; + + modifier onlyOwner { + require(msg.sender == owner, "BlockToken:: Unauthorized User"); + _; + } + + modifier notAmount0(uint256 _amount){ + require(_amount != 0, "BlockToken:: Zero amount not supported"); + _; + } + constructor(string memory _name, string memory _symbol, address _owner) ERC20(_name, _symbol){ + require(_owner != address(0), "BlockToken:: Zero address not supported"); + owner = _owner; + } + + function mint(uint256 _amount, address _recepient) onlyOwner notAmount0(_amount) external { + _mint(_recepient, _amount); + } + + function burn(uint256 _amount) notAmount0(_amount) external { + _burn(msg.sender, _amount); + } + + function burnFrom(address _user, uint256 _amount)onlyOwner notAmount0(_amount) external { + _burn(_user, _amount); + } + + +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 656a34f..cc2bf83 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,6 +5,9 @@ "packages": { "": { "name": "hardhat-project", + "dependencies": { + "@openzeppelin/contracts": "^5.4.0" + }, "devDependencies": { "@nomicfoundation/hardhat-toolbox": "^5.0.0", "hardhat": "^2.26.1" @@ -1305,6 +1308,12 @@ "node": ">= 12" } }, + "node_modules/@openzeppelin/contracts": { + "version": "5.4.0", + "resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-5.4.0.tgz", + "integrity": "sha512-eCYgWnLg6WO+X52I16TZt8uEjbtdkgLC0SUX/xnAksjjrQI4Xfn4iBRoI5j55dmlOhDv1Y7BoR3cU7e3WWhC6A==", + "license": "MIT" + }, "node_modules/@scure/base": { "version": "1.2.6", "resolved": "https://registry.npmjs.org/@scure/base/-/base-1.2.6.tgz", diff --git a/package.json b/package.json index 0586079..a7c3ef4 100644 --- a/package.json +++ b/package.json @@ -3,10 +3,13 @@ "devDependencies": { "@nomicfoundation/hardhat-toolbox": "^5.0.0", "hardhat": "^2.26.1" - }, + }, "scripts": { - "test": "npx hardhat test", - "compile": "npx hardhat compile", + "test": "npx hardhat test", + "compile": "npx hardhat compile", "node": "npx hardhat node" + }, + "dependencies": { + "@openzeppelin/contracts": "^5.4.0" } } diff --git a/test/BlockToken.js b/test/BlockToken.js new file mode 100644 index 0000000..3eb454d --- /dev/null +++ b/test/BlockToken.js @@ -0,0 +1,162 @@ +const { + loadFixture, +} = require("@nomicfoundation/hardhat-toolbox/network-helpers"); +// const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs"); +const { expect } = require("chai"); + +// util functon +const deployBlockToken = async () => { + // target the BlockToken contract within our contract folder + let name_ = "BlockToken"; + let symbol_ = "BCT"; + const [owner_, addr1, addr2] = await ethers.getSigners(); + const BlockTokenContract = await ethers.getContractFactory("BlockToken"); // target BlockToken.sol + const BlockToken = await BlockTokenContract.deploy( + name_, + symbol_, + owner_.address + ); // deploy the BlockToken contract + return { BlockToken, owner_, addr1, addr2, name_, symbol_ }; // return the deployed instance of our BlockToken contract +}; + +// BlockToken Test Suite +describe("BlockToken Test Suite", () => { + describe("Deployment", () => { + it("Should return set values upon deployment", async () => { + const { BlockToken, name_, symbol_, owner_ } = await loadFixture( + deployBlockToken + ); + expect(await BlockToken.name()).to.eq(name_); + expect(await BlockToken.symbol()).to.eq(symbol_); + expect(await BlockToken.owner()).to.eq(owner_); + }); + + it("Should revert if owner is zero address", async () => { + let ZeroAddress = "0x0000000000000000000000000000000000000000"; + const BlockTokenContract = await ethers.getContractFactory("BlockToken"); + await expect( + BlockTokenContract.deploy("hh", "tt", ZeroAddress) + ).to.be.revertedWith("BlockToken:: Zero address not supported"); + }); + }); + + describe("Minting", () => { + it("Should allow onlyOwner Mint", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + // test owner mints successfully + await BlockToken.connect(owner_).mint(1000, addr1); + expect(await BlockToken.balanceOf(addr1)).to.eq(1000); + + // test that another user cant call successfully + let malicioustxn = BlockToken.connect(addr1).mint(1000, addr1); + await expect(malicioustxn).to.be.revertedWith( + "BlockToken:: Unauthorized User" + ); + }); + + it("Should revert if minting amount is zero", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + await expect( + BlockToken.connect(owner_).mint(0, addr1) + ).to.be.revertedWith("BlockToken:: Zero amount not supported"); + }); + }); + + describe("Burning", () => { + it("Should not burn if user doesn't have tokens", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + await expect( + BlockToken.connect(addr1).burn(1000) + ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); + }); + + it("Should Burn Tokens Successfully", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, owner_); + expect(await BlockToken.balanceOf(owner_)).to.eq(1000); + + await BlockToken.connect(owner_).burn(100); + expect(await BlockToken.balanceOf(owner_)).to.eq(900); + }); + }); + + describe("Burning From", () => { + it("Should only allow owner", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(10000, addr1); + // expect(await BlockToken.balanceOf(addr1)).to.eq(10000); + const authorizedUser = await BlockToken.connect(owner_).burnFrom( + addr1, + 1000 + ); + expect(await BlockToken.balanceOf(addr1)).to.eq(9000); + const unauthorizedUser = BlockToken.connect(addr1).burnFrom(addr1, 1000); + await expect(unauthorizedUser).to.be.revertedWith( + "BlockToken:: Unauthorized User" + ); + }); + it("Should not send zero", async () => { + const { BlockToken, addr1, owner_ } = await loadFixture(deployBlockToken); + await expect( + BlockToken.connect(owner_).burnFrom(addr1, 0) + ).to.be.revertedWith("BlockToken:: Zero amount not supported"); + }); + }); + + describe("Transfer", () => { + it("should be called by owner", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(10000, owner_); + await BlockToken.connect(owner_).transfer(addr1, 1000); + const addrBalance = await BlockToken.balanceOf(addr1); + const ownerBalance = await BlockToken.balanceOf(owner_); + + expect(addrBalance).to.eq(1000); + expect(ownerBalance).to.eq(9000); + }); + it("amount to be sent should be less than the balance of the sender", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(10000, owner_); + const ownerBalance = await BlockToken.balanceOf(owner_); + expect(ownerBalance).to.be.greaterThanOrEqual(1000); + await BlockToken.connect(owner_).transfer(addr1, 1000); + const addrBalance = await BlockToken.balanceOf(addr1); + const ownerBalanceAfter = await BlockToken.balanceOf(owner_); + + expect(addrBalance).to.eq(1000); + expect(ownerBalanceAfter).to.eq(9000); + }); + it("should revert if amount to be sent is greter than the amount the sender has", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + await expect(BlockToken.connect(owner_).transfer(addr1, 10000)).to.be + .reverted; + }); + it("address 0 should not be the receiver", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + let ZeroAddress = "0x0000000000000000000000000000000000000000"; + await expect(BlockToken.connect(owner_).transfer(ZeroAddress, 1000)).to.be + .reverted; + }); + }); + + describe("Approve", () => { + it("address zero should not be the receiver", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + let ZeroAddress = "0x0000000000000000000000000000000000000000"; + await expect(BlockToken.connect(owner_).approve(ZeroAddress, 1000)).to.be + .reverted; + }); + it("should send moeny to allowances", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).approve(addr1, 1000); + const allowance = await BlockToken.allowance(owner_, addr1); + + expect(allowance).to.eq(1000); + }); + // it("address zero should not be the sender", async () => { + // const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + // let ZeroAddress = "0x0000000000000000000000000000000000000000"; + // await expect(BlockToken.connect(ZeroAddress)).to.be.reverted; + // }); + }); +}); diff --git a/test/CounterV2Caller.js b/test/CounterV2Caller.js index 060cc26..8e74c72 100644 --- a/test/CounterV2Caller.js +++ b/test/CounterV2Caller.js @@ -10,7 +10,7 @@ const deployCounter = async () => { const CounterV2Contract = await ethers.getContractFactory("CounterV2"); const counterV2 = await CounterV2Contract.deploy(); //if no constructor no need to put anything in the depl0y place - const counterV2address = await counter.getAddress(); + const counterV2address = await counterV2.getAddress(); const CounterV2CallerContract = await ethers.getContractFactory( "CounterV2Caller" @@ -35,7 +35,6 @@ describe("CounterV2Caller Test Suite", () => { const { counterV2, counterV2Caller } = await loadFixture(deployCounter); await counterV2.setCount(10); await counterV2Caller.decreaseCount(); - const countAfterChnage = await counterV2.getCount(); expect(countAfterChnage).to.eq(9); });