From 8080f4f84d84015613fc38e9477f04ec9c765ee8 Mon Sep 17 00:00:00 2001 From: folex1275 Date: Sun, 27 Jul 2025 15:15:50 +0100 Subject: [PATCH 1/8] chore: adding more tests --- test/Counter.js | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) diff --git a/test/Counter.js b/test/Counter.js index 99b2931..1eeb062 100644 --- a/test/Counter.js +++ b/test/Counter.js @@ -15,7 +15,8 @@ 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 + let count = await counter.count() + expect(count).to.eq(0); // assert that count = 0 upon deployment }) }) @@ -32,17 +33,53 @@ describe("Counter Test Suite", () => { }) it("Should set appropriate values for multiple setCount txns", async () => { - + const counter = await loadFixture(deployCounter); // extract deployed counter instace + let count3 = await counter.getCount(); // check initial count value before txn + expect(count3).to.eq(0); + await counter.setCount(20) + + let count4 = await counter.getCount() + expect(count4).to.eq(20) + await counter.setCount(30) + + let count5 = await counter.getCount() + expect(count5).to.eq(30) + await counter.setCount(40) + + let count6 = await counter.getCount() + expect(count6).to.eq(40) }) }) describe("IncreaseCountByOne", () => { it("Should set appropriate increaseCountByOne value", async () => { - + const counter = await loadFixture(deployCounter); + let count = await counter.getCount() + expect(count).to.eq(0) + await counter.setCount(40) + await counter.increaseCountByOne() + + let count2 = await counter.getCount() + expect(count2).to.eq(41) }) it("Should set appropriate values for multiple increaseCountByOne txns", async () => { - + const counter = await loadFixture(deployCounter); + let count = await counter.getCount() + expect(count).to.eq(0) + await counter.setCount(40) + await counter.increaseCountByOne() + + let count2 = await counter.getCount() + expect(count2).to.eq(41) + await counter.increaseCountByOne() + + let count3 = await counter.getCount() + expect(count3).to.eq(42) + await counter.increaseCountByOne() + + let count4 = await counter.getCount() + expect(count4).to.eq(43) }) }) }) From 3dfabe33c43955dd840ca194db1c41c0e5d9e0d5 Mon Sep 17 00:00:00 2001 From: folex1275 Date: Mon, 28 Jul 2025 00:36:25 +0100 Subject: [PATCH 2/8] chore: adding CounterV2.sol, CounterV2Caller.sol, CounterV2.js --- contracts/CounterV2.sol | 45 +++++++++++ contracts/CounterV2Caller.sol | 34 ++++++++ test/CounterV2.js | 148 ++++++++++++++++++++++++++++++++++ 3 files changed, 227 insertions(+) create mode 100644 contracts/CounterV2.sol create mode 100644 contracts/CounterV2Caller.sol create mode 100644 test/CounterV2.js diff --git a/contracts/CounterV2.sol b/contracts/CounterV2.sol new file mode 100644 index 0000000..8b105cd --- /dev/null +++ b/contracts/CounterV2.sol @@ -0,0 +1,45 @@ +// SPDX-License-Identifier: SEE LICENSE IN LICENSE +pragma solidity ^0.8.0; + +interface ICounterV2 { + function getCount() external view returns(uint); + function setCount(uint) external; + function resetCount() external; + function increaseCountByOne() external; + function decreaseCountByOne() external; +} + +contract CounterV2 is ICounterV2{ + uint public count; + address owner; + constructor(){ + owner = msg.sender; + } + + function getCount() external view returns(uint){ + return count; + } + + function setCount(uint _count) external{ + require(owner == msg.sender, "unauthorized address"); + require(_count >= 0); + count = _count; + } + + function resetCount() external { + require(owner == msg.sender, "unauthorized address"); + require(count > 0, "cannot input default value"); + count = 0; + } + + function increaseCountByOne() external{ + require(owner == msg.sender, "unauthorized address"); + count += 1; + } + + function decreaseCountByOne() external{ + require(owner == msg.sender, "unauthorized address"); + require(count > 0, "count cannot go below zero"); + count -= 1; + } +} \ No newline at end of file diff --git a/contracts/CounterV2Caller.sol b/contracts/CounterV2Caller.sol new file mode 100644 index 0000000..d94c144 --- /dev/null +++ b/contracts/CounterV2Caller.sol @@ -0,0 +1,34 @@ +// SPDX-License-Identifier: SEE LICENSE IN LICENSE +pragma solidity ^0.8.0; + +import "./CounterV2.sol"; + +contract CounterV2Caller is ICounterV2{ + ICounterV2 icv2; + address contractAddr; + + constructor(address _contractaddr){ + contractAddr = _contractaddr; + icv2 = ICounterV2(_contractaddr); + } + + function setCount(uint _count) public { + icv2.setCount(_count); + } + + function getCount() public view returns(uint) { + return icv2.getCount(); + } + + function increaseCountByOne() public { + icv2.increaseCountByOne(); + } + + function resetCount() public { + icv2.resetCount(); + } + + function decreaseCountByOne() public{ + icv2.decreaseCountByOne(); + } +} \ No newline at end of file diff --git a/test/CounterV2.js b/test/CounterV2.js new file mode 100644 index 0000000..b063acf --- /dev/null +++ b/test/CounterV2.js @@ -0,0 +1,148 @@ +const {loadFixture } = require("@nomicfoundation/hardhat-toolbox/network-helpers"); +// const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs"); +const { expect } = require("chai"); + +// util functon +const deployCounter = async () => { + // target the Counter contract within our contract folder + const CounterV2Contract = await ethers.getContractFactory("CounterV2"); // target CounterV2.sol + const [owner, otherAccount] = await ethers.getSigners(); + const counter = await CounterV2Contract.deploy(); // deploy the Counter contract + return {counter, owner, otherAccount}; +} + + +describe("CounterV2 Test Suite", () => { + describe("Deployment", () => { + it("Should return default values upon deployment", async () => { + const {counter} = await loadFixture(deployCounter); + let count = await counter.getCount(); + expect(count).to.eq(0); + }) + }) + + describe("Transactions", () => { + describe("SetCount", () => { + it("Should set appropriate count values", async () => { + const {counter, owner} = await loadFixture(deployCounter); + let count1 = await counter.getCount(); + expect(count1).to.eq(0); + await counter.connect(owner).setCount(10) // make owner explicit + + let count2 = await counter.getCount(); + expect(count2).to.eq(10) + }) + + 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 address"); + }) + + it("Should set appropriate values for multiple setCount txns", async () => { + const {counter, owner} = await loadFixture(deployCounter); + let count3 = await counter.getCount(); + expect(count3).to.eq(0); + await counter.connect(owner).setCount(20) + + let count4 = await counter.getCount() + expect(count4).to.eq(20) + await counter.connect(owner).setCount(30) + + let count5 = await counter.getCount() + expect(count5).to.eq(30) + await counter.connect(owner).setCount(40) + + let count6 = await counter.getCount() + expect(count6).to.eq(40) + }) + }) + + describe("IncreaseCountByOne", () => { + it("Should set appropriate increaseCountByOne value", async () => { + const {counter, owner} = await loadFixture(deployCounter); + let count = await counter.getCount() + expect(count).to.eq(0) + await counter.connect(owner).setCount(40) + await counter.connect(owner).increaseCountByOne() + + let count2 = await counter.getCount() + expect(count2).to.eq(41) + }) + + it("Should revert if invalid owner calls increasecountByOne", async () => { + const {counter, otherAccount} = await loadFixture(deployCounter); + await expect(counter.connect(otherAccount).increaseCountByOne()).to.be.revertedWith("unauthorized address"); + }) + + it("Should set appropriate values for multiple increaseCountByOne txns", async () => { + const {counter, owner} = await loadFixture(deployCounter); + let count = await counter.getCount() + expect(count).to.eq(0) + await counter.connect(owner).setCount(40) + await counter.connect(owner).increaseCountByOne() + + let count2 = await counter.getCount() + expect(count2).to.eq(41) + await counter.connect(owner).increaseCountByOne() + + let count3 = await counter.getCount() + expect(count3).to.eq(42) + await counter.connect(owner).increaseCountByOne() + + let count4 = await counter.getCount() + expect(count4).to.eq(43) + }) + }) + + describe("Reset Count", () => { + it("Should reset appropriate value by owner", async () => { + const {counter, owner} = await loadFixture(deployCounter); + await counter.connect(owner).setCount(5); + let count1 = await counter.getCount(); + expect(count1).to.eq(5); + await counter.connect(owner).resetCount(); + let count2 = await counter.getCount(); + expect(count2).to.eq(0); + }) + + it("should revert if invalid owner calls resetCount", async () => { + const {counter, otherAccount} = await loadFixture(deployCounter); + await expect(counter.connect(otherAccount).resetCount()).to.be.revertedWith("unauthorized address"); + }) + }) + + describe("DecreasecountByOne", () => { + it("Should set appropriate decreaseCountByOne value by owner", async () => { + const {counter, owner} = await loadFixture(deployCounter); + await counter.connect(owner).setCount(10); + let count1 = await counter.getCount(); + expect(count1).to.eq(10); + await counter.connect(owner).decreaseCountByOne(); + let count2 = await counter.getCount(); + expect(count2).to.eq(9); + }) + + it("Should revert if invalid owner calls decreaseCountByOne", async () => { + const {counter, otherAccount} = await loadFixture(deployCounter); + await expect(counter.connect(otherAccount).decreaseCountByOne()).to.be.revertedWith("unauthorized address"); + }) + + it("Should set appropriate values for multiple decreaseCountByOne txns", async () => { + const {counter, owner} = await loadFixture(deployCounter); + await counter.connect(owner).setCount(40) + await counter.connect(owner).decreaseCountByOne() + + let count2 = await counter.getCount() + expect(count2).to.eq(39) + await counter.connect(owner).decreaseCountByOne() + + let count3 = await counter.getCount() + expect(count3).to.eq(38) + await counter.connect(owner).decreaseCountByOne() + + let count4 = await counter.getCount() + expect(count4).to.eq(37) + }) + }) + }) +}) \ No newline at end of file From ee17e27f6e8193e5068ca8187f51fcd3bf9f553f Mon Sep 17 00:00:00 2001 From: folex1275 Date: Tue, 29 Jul 2025 14:02:04 +0100 Subject: [PATCH 3/8] chore: add CounterV2Caller --- test/CounterV2Caller.js | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 test/CounterV2Caller.js diff --git a/test/CounterV2Caller.js b/test/CounterV2Caller.js new file mode 100644 index 0000000..7b200d4 --- /dev/null +++ b/test/CounterV2Caller.js @@ -0,0 +1,37 @@ +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 counterV2.getAddress(); + + const CounterV2CallerContract = await ethers.getContractFactory("CounterV2Caller"); + const counterV2Caller = await CounterV2CallerContract.deploy(counterV2address); + + return { counterV2, counterV2Caller, owner, otherAccount}; +}; + +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); + }); + }); + + describe("Transactions", async () => { + + it("Should revert if invalid owner calls decreaseCountByOne", async () => { + const { counterV2Caller, otherAccount} = await loadFixture(deployCounter); + await expect(counterV2Caller.connect(otherAccount).decreaseCountByOne()).to.be.revertedWith("unauthorized address"); + }) + }); +}); \ No newline at end of file From b1356609121cc2961048ca566327c0d0ab569565 Mon Sep 17 00:00:00 2001 From: folex1275 Date: Tue, 29 Jul 2025 14:34:11 +0100 Subject: [PATCH 4/8] Change made to Counter.js, CounterV2Caller.js, CounterV2Caller.sol --- contracts/CounterV2Caller.sol | 1 - test/Counter.js | 22 +++++++--------------- test/CounterV2Caller.js | 1 + 3 files changed, 8 insertions(+), 16 deletions(-) diff --git a/contracts/CounterV2Caller.sol b/contracts/CounterV2Caller.sol index d94c144..ff62fa5 100644 --- a/contracts/CounterV2Caller.sol +++ b/contracts/CounterV2Caller.sol @@ -8,7 +8,6 @@ contract CounterV2Caller is ICounterV2{ address contractAddr; constructor(address _contractaddr){ - contractAddr = _contractaddr; icv2 = ICounterV2(_contractaddr); } diff --git a/test/Counter.js b/test/Counter.js index 1eeb062..2ec91df 100644 --- a/test/Counter.js +++ b/test/Counter.js @@ -65,21 +65,13 @@ describe("Counter Test Suite", () => { it("Should set appropriate values for multiple increaseCountByOne txns", async () => { const counter = await loadFixture(deployCounter); - let count = await counter.getCount() - expect(count).to.eq(0) - await counter.setCount(40) - await counter.increaseCountByOne() - - let count2 = await counter.getCount() - expect(count2).to.eq(41) - await counter.increaseCountByOne() - - let count3 = await counter.getCount() - expect(count3).to.eq(42) - await counter.increaseCountByOne() - - let count4 = await counter.getCount() - expect(count4).to.eq(43) + await counter.setCount(1) + + for(let i = 0; i < 4; i++){ + await counter.increaseCountByOne() + } + expect(await counter.getCount()).to.eq(5) + }) }) }) diff --git a/test/CounterV2Caller.js b/test/CounterV2Caller.js index 7b200d4..990c4c7 100644 --- a/test/CounterV2Caller.js +++ b/test/CounterV2Caller.js @@ -33,5 +33,6 @@ describe("CounterV2Caller Test Suite", () => { const { counterV2Caller, otherAccount} = await loadFixture(deployCounter); await expect(counterV2Caller.connect(otherAccount).decreaseCountByOne()).to.be.revertedWith("unauthorized address"); }) + }); }); \ No newline at end of file From 19bc1e0df71932d13c935ee0034a1cdf8f517e47 Mon Sep 17 00:00:00 2001 From: folex1275 Date: Tue, 29 Jul 2025 23:00:25 +0100 Subject: [PATCH 5/8] feat: add BlockToken.sol, test: add BlockToken.js --- contracts/BlockToken.sol | 49 ++++++++ test/BlockToken.js | 235 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 284 insertions(+) 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..0f1a394 --- /dev/null +++ b/contracts/BlockToken.sol @@ -0,0 +1,49 @@ +// 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); + } + + function transfertk(address _to, uint256 _amount) notAmount0(_amount) external { + _transfer(msg.sender,_to, _amount); + } + + function transferFromtk(address _from, address _to, uint256 _amount) notAmount0(_amount) external { + _transfer(_from, _to, _amount); + } + + function approveTxn(address _spender, uint256 _amount) notAmount0(_amount) external{ + _approve(msg.sender, _spender, _amount); + } + + +} \ No newline at end of file diff --git a/test/BlockToken.js b/test/BlockToken.js new file mode 100644 index 0000000..6c8f08d --- /dev/null +++ b/test/BlockToken.js @@ -0,0 +1,235 @@ + +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 () => { + const BlockTokenContract = await ethers.getContractFactory("BlockToken"); + let ZeroAddress = "0x0000000000000000000000000000000000000000"; + 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", () => { + describe("Burn Txn", () => { + 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); + }); + + it("Should revert if burning more than balance", async () => { + const { BlockToken, owner_ } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, owner_); + expect(await BlockToken.connect(owner_).balanceOf(owner_)).to.eq(1000); + + await expect(BlockToken.connect(owner_).burn(2000)).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); + }); + + it("Should revert if burning zero balance", async () => { + const { BlockToken, owner_ } = await loadFixture(deployBlockToken); + await expect(BlockToken.connect(owner_).burn(0)).to.be.revertedWith("BlockToken:: Zero amount not supported") + }) + }); + + describe("BurningFrom", () => { + it("Should Burn From User's 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_).transfertk(addr1, 200); + expect(await BlockToken.balanceOf(owner_)).to.eq(800); + + await BlockToken.connect(owner_).burnFrom(addr1, 50); + expect(await BlockToken.balanceOf(addr1)).to.eq(150); + }) + + it("Should revert if User burns more than balance", async () => { + const { BlockToken, owner_ } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, owner_); + await expect(BlockToken.connect(owner_).burn(2000)).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance") + }); + + it("Should revert if burning from address zero", async () => { + const { BlockToken, owner_ } = await loadFixture(deployBlockToken); + let ZeroAddress = "0x0000000000000000000000000000000000000000"; + await BlockToken.connect(owner_).mint(1000, owner_); + expect(await BlockToken.balanceOf(owner_)).to.eq(1000); + + await expect(BlockToken.connect(owner_).burnFrom(ZeroAddress, 50)).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidSender"); + }); + + it("Should revert if burnining called by different address", async () => { + const { BlockToken, addr2, addr1 } = await loadFixture(deployBlockToken); + let malicioustxn = BlockToken.connect(addr1).burnFrom(addr2, 1000); + await expect(malicioustxn).to.be.revertedWith("BlockToken:: Unauthorized User"); + }); + }); + + }); + + describe("Transferring", () => { + describe("Transfer txn", () => { + it("Should not transfer if user doesn't have tokens", async () => { + const { BlockToken, addr1, addr2 } = await loadFixture(deployBlockToken); + await expect(BlockToken.connect(addr1).transfer(addr2, 100)).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); + }) + + it("Should Transfer Successfully", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, owner_); + await BlockToken.connect(owner_).transfertk(addr1, 500); + expect(await BlockToken.balanceOf(owner_)).to.eq(500); + expect(await BlockToken.balanceOf(addr1)).to.eq(500); + + await BlockToken.connect(addr1).transfertk(addr2, 300); + expect(await BlockToken.balanceOf(addr1)).to.eq(200); + expect(await BlockToken.balanceOf(addr2)).to.eq(300); + }); + + it("Should revert if tranferring to zero address", async () => { + const { BlockToken, owner_ } = await loadFixture(deployBlockToken); + let ZeroAddress = "0x0000000000000000000000000000000000000000"; + await BlockToken.connect(owner_).mint(1000, owner_); + + await expect(BlockToken.connect(owner_).transfertk(ZeroAddress, 300)).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidReceiver"); + }); + + it("Should revert if tranferring more than balance", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, owner_); + await expect(BlockToken.connect(owner_).transfertk(addr1, 2000)).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); + }); + + }); + + describe("TransferFrom Txn", () => { + it("Should revert if owner address doesn't have tokens", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + await BlockToken.connect(addr1).approveTxn(addr2, 1000); + await expect(BlockToken.connect(addr2).transferFromtk(addr1, owner_, 50)).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); + }); + + it("Should revert when transfering zero tokens", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken) + await BlockToken.connect(addr1).approveTxn(addr2, 1000) + await expect(BlockToken.connect(addr2).transferFromtk(addr1, owner_, 0)).to.be.revertedWith("BlockToken:: Zero amount not supported"); + }); + + it("Should revert when transfering more than balance", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, addr1); + await BlockToken.connect(addr1).approveTxn(addr2, 3000) + await expect(BlockToken.connect(addr2).transferFromtk(addr1, owner_, 2000)).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); + }); + + it("Should revert if transfering to address zero", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + let ZeroAddress = "0x0000000000000000000000000000000000000000"; + await BlockToken.connect(owner_).mint(1000, addr1); + await BlockToken.connect(addr1).approveTxn(addr2, 1000) + await expect(BlockToken.connect(addr2).transferFromtk(addr1, ZeroAddress, 500)).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidReceiver"); + }); + + it("Should Transfer Tokens Successfully Using TranasferFrom", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, addr1); + await BlockToken.connect(addr1).approveTxn(addr2, 1000) + + await BlockToken.connect(addr2).transferFromtk(addr1, addr2, 400); + + expect(await BlockToken.balanceOf(addr1)).to.eq(600); + expect(await BlockToken.balanceOf(addr2)).to.eq(400); + + }); + }); + }); + + describe("Approvals", () => { + describe("Approve", () => { + it("Should revert when approving zero tokens", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, addr1); + await expect(BlockToken.connect(addr1).approveTxn(addr2, 0)).to.be.revertedWith("BlockToken:: Zero amount not supported"); + }); + + it("Should revert if approving to address zero", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + let ZeroAddress = "0x0000000000000000000000000000000000000000"; + + await BlockToken.connect(owner_).mint(500, addr1) + await expect(BlockToken.connect(addr1).approveTxn(ZeroAddress, 50)).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidSpender"); + }); + + it("Should Approve Tokens Successfully", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + + await BlockToken.connect(addr1).approveTxn(addr2, 300); + expect(await BlockToken.allowance(addr1, addr2)).to.eq(300); + }); + }); + }) +}); From 088d40be5352d5e86d519ad8469e331b14bb02f0 Mon Sep 17 00:00:00 2001 From: folex1275 Date: Tue, 29 Jul 2025 23:36:40 +0100 Subject: [PATCH 6/8] Merge erc-testing and Erc20-testing branches for BlockToken and Counter test suites --- test/BlockToken.js | 442 +++++++++++++++++++++++---------------------- test/Counter.js | 129 ++++++------- 2 files changed, 279 insertions(+), 292 deletions(-) diff --git a/test/BlockToken.js b/test/BlockToken.js index 6c8f08d..8fd3fa0 100644 --- a/test/BlockToken.js +++ b/test/BlockToken.js @@ -1,235 +1,239 @@ - -const { - loadFixture, -} = require("@nomicfoundation/hardhat-toolbox/network-helpers"); -// const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs"); +const { loadFixture } = require("@nomicfoundation/hardhat-toolbox/network-helpers"); const { expect } = require("chai"); -// util functon +// Utility function to deploy BlockToken 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 + 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 }; // 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 () => { - const BlockTokenContract = await ethers.getContractFactory("BlockToken"); - let ZeroAddress = "0x0000000000000000000000000000000000000000"; - await expect( - BlockTokenContract.deploy("hh", "tt", ZeroAddress) - ).to.be.revertedWith("BlockToken:: Zero address not supported"); - }); + describe("Deployment", () => { + it("Should return set values upon deployment", async () => { + const { BlockToken, name_, symbol_, owner_ } = await loadFixture(deployBlockToken); + expect(await BlockToken.name()).to.equal(name_); + expect(await BlockToken.symbol()).to.equal(symbol_); + expect(await BlockToken.owner()).to.equal(owner_); }); - 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"); - }); + it("Should revert if owner is zero address", async () => { + const BlockTokenContract = await ethers.getContractFactory("BlockToken"); + const ZeroAddress = "0x0000000000000000000000000000000000000000"; + await expect( + BlockTokenContract.deploy("hh", "tt", ZeroAddress) + ).to.be.revertedWith("BlockToken:: Zero address not supported"); }); + }); - describe("Burning", () => { - describe("Burn Txn", () => { - 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); - }); - - it("Should revert if burning more than balance", async () => { - const { BlockToken, owner_ } = await loadFixture(deployBlockToken); - await BlockToken.connect(owner_).mint(1000, owner_); - expect(await BlockToken.connect(owner_).balanceOf(owner_)).to.eq(1000); - - await expect(BlockToken.connect(owner_).burn(2000)).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); - }); - - it("Should revert if burning zero balance", async () => { - const { BlockToken, owner_ } = await loadFixture(deployBlockToken); - await expect(BlockToken.connect(owner_).burn(0)).to.be.revertedWith("BlockToken:: Zero amount not supported") - }) - }); - - describe("BurningFrom", () => { - it("Should Burn From User's 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_).transfertk(addr1, 200); - expect(await BlockToken.balanceOf(owner_)).to.eq(800); - - await BlockToken.connect(owner_).burnFrom(addr1, 50); - expect(await BlockToken.balanceOf(addr1)).to.eq(150); - }) - - it("Should revert if User burns more than balance", async () => { - const { BlockToken, owner_ } = await loadFixture(deployBlockToken); - await BlockToken.connect(owner_).mint(1000, owner_); - await expect(BlockToken.connect(owner_).burn(2000)).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance") - }); - - it("Should revert if burning from address zero", async () => { - const { BlockToken, owner_ } = await loadFixture(deployBlockToken); - let ZeroAddress = "0x0000000000000000000000000000000000000000"; - await BlockToken.connect(owner_).mint(1000, owner_); - expect(await BlockToken.balanceOf(owner_)).to.eq(1000); - - await expect(BlockToken.connect(owner_).burnFrom(ZeroAddress, 50)).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidSender"); - }); - - it("Should revert if burnining called by different address", async () => { - const { BlockToken, addr2, addr1 } = await loadFixture(deployBlockToken); - let malicioustxn = BlockToken.connect(addr1).burnFrom(addr2, 1000); - await expect(malicioustxn).to.be.revertedWith("BlockToken:: Unauthorized User"); - }); - }); + describe("Minting", () => { + it("Should allow onlyOwner Mint", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, addr1); + expect(await BlockToken.balanceOf(addr1)).to.equal(1000); + const maliciousTxn = BlockToken.connect(addr1).mint(1000, addr1); + await expect(maliciousTxn).to.be.revertedWith("BlockToken:: Unauthorized User"); }); - describe("Transferring", () => { - describe("Transfer txn", () => { - it("Should not transfer if user doesn't have tokens", async () => { - const { BlockToken, addr1, addr2 } = await loadFixture(deployBlockToken); - await expect(BlockToken.connect(addr1).transfer(addr2, 100)).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); - }) - - it("Should Transfer Successfully", async () => { - const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); - await BlockToken.connect(owner_).mint(1000, owner_); - await BlockToken.connect(owner_).transfertk(addr1, 500); - expect(await BlockToken.balanceOf(owner_)).to.eq(500); - expect(await BlockToken.balanceOf(addr1)).to.eq(500); - - await BlockToken.connect(addr1).transfertk(addr2, 300); - expect(await BlockToken.balanceOf(addr1)).to.eq(200); - expect(await BlockToken.balanceOf(addr2)).to.eq(300); - }); - - it("Should revert if tranferring to zero address", async () => { - const { BlockToken, owner_ } = await loadFixture(deployBlockToken); - let ZeroAddress = "0x0000000000000000000000000000000000000000"; - await BlockToken.connect(owner_).mint(1000, owner_); - - await expect(BlockToken.connect(owner_).transfertk(ZeroAddress, 300)).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidReceiver"); - }); - - it("Should revert if tranferring more than balance", async () => { - const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); - await BlockToken.connect(owner_).mint(1000, owner_); - await expect(BlockToken.connect(owner_).transfertk(addr1, 2000)).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); - }); - - }); - - describe("TransferFrom Txn", () => { - it("Should revert if owner address doesn't have tokens", async () => { - const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); - await BlockToken.connect(addr1).approveTxn(addr2, 1000); - await expect(BlockToken.connect(addr2).transferFromtk(addr1, owner_, 50)).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); - }); - - it("Should revert when transfering zero tokens", async () => { - const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken) - await BlockToken.connect(addr1).approveTxn(addr2, 1000) - await expect(BlockToken.connect(addr2).transferFromtk(addr1, owner_, 0)).to.be.revertedWith("BlockToken:: Zero amount not supported"); - }); - - it("Should revert when transfering more than balance", async () => { - const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); - await BlockToken.connect(owner_).mint(1000, addr1); - await BlockToken.connect(addr1).approveTxn(addr2, 3000) - await expect(BlockToken.connect(addr2).transferFromtk(addr1, owner_, 2000)).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); - }); - - it("Should revert if transfering to address zero", async () => { - const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); - let ZeroAddress = "0x0000000000000000000000000000000000000000"; - await BlockToken.connect(owner_).mint(1000, addr1); - await BlockToken.connect(addr1).approveTxn(addr2, 1000) - await expect(BlockToken.connect(addr2).transferFromtk(addr1, ZeroAddress, 500)).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidReceiver"); - }); - - it("Should Transfer Tokens Successfully Using TranasferFrom", async () => { - const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); - await BlockToken.connect(owner_).mint(1000, addr1); - await BlockToken.connect(addr1).approveTxn(addr2, 1000) - - await BlockToken.connect(addr2).transferFromtk(addr1, addr2, 400); - - expect(await BlockToken.balanceOf(addr1)).to.eq(600); - expect(await BlockToken.balanceOf(addr2)).to.eq(400); - - }); - }); + 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", () => { + describe("Burn Txn", () => { + it("Should not burn if user doesn't have tokens", async () => { + const { BlockToken, 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_ } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, owner_); + expect(await BlockToken.balanceOf(owner_)).to.equal(1000); + + await BlockToken.connect(owner_).burn(100); + expect(await BlockToken.balanceOf(owner_)).to.equal(900); + }); + + it("Should revert if burning more than balance", async () => { + const { BlockToken, owner_ } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, owner_); + expect(await BlockToken.balanceOf(owner_)).to.equal(1000); + + await expect( + BlockToken.connect(owner_).burn(2000) + ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); + }); + + it("Should revert if burning zero balance", async () => { + const { BlockToken, owner_ } = await loadFixture(deployBlockToken); + await expect( + BlockToken.connect(owner_).burn(0) + ).to.be.revertedWith("BlockToken:: Zero amount not supported"); + }); }); - describe("Approvals", () => { - describe("Approve", () => { - it("Should revert when approving zero tokens", async () => { - const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); - await BlockToken.connect(owner_).mint(1000, addr1); - await expect(BlockToken.connect(addr1).approveTxn(addr2, 0)).to.be.revertedWith("BlockToken:: Zero amount not supported"); - }); - - it("Should revert if approving to address zero", async () => { - const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); - let ZeroAddress = "0x0000000000000000000000000000000000000000"; - - await BlockToken.connect(owner_).mint(500, addr1) - await expect(BlockToken.connect(addr1).approveTxn(ZeroAddress, 50)).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidSpender"); - }); - - it("Should Approve Tokens Successfully", async () => { - const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); - - await BlockToken.connect(addr1).approveTxn(addr2, 300); - expect(await BlockToken.allowance(addr1, addr2)).to.eq(300); - }); - }); - }) -}); + describe("BurningFrom", () => { + it("Should Burn From User's Tokens Successfully", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, owner_); + expect(await BlockToken.balanceOf(owner_)).to.equal(1000); + + await BlockToken.connect(owner_).transfertk(addr1, 200); + expect(await BlockToken.balanceOf(owner_)).to.equal(800); + + await BlockToken.connect(owner_).burnFrom(addr1, 50); + expect(await BlockToken.balanceOf(addr1)).to.equal(150); + }); + + it("Should revert if User burns more than balance", async () => { + const { BlockToken, owner_ } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, owner_); + await expect( + BlockToken.connect(owner_).burn(2000) + ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); + }); + + it("Should revert if burning from address zero", async () => { + const { BlockToken, owner_ } = await loadFixture(deployBlockToken); + const ZeroAddress = "0x0000000000000000000000000000000000000000"; + await BlockToken.connect(owner_).mint(1000, owner_); + expect(await BlockToken.balanceOf(owner_)).to.equal(1000); + + await expect( + BlockToken.connect(owner_).burnFrom(ZeroAddress, 50) + ).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidSender"); + }); + + it("Should revert if burning called by different address", async () => { + const { BlockToken, addr1, addr2 } = await loadFixture(deployBlockToken); + const maliciousTxn = BlockToken.connect(addr1).burnFrom(addr2, 1000); + await expect(maliciousTxn).to.be.revertedWith("BlockToken:: Unauthorized User"); + }); + }); + }); + + describe("Transferring", () => { + describe("Transfer Txn", () => { + it("Should not transfer if user doesn't have tokens", async () => { + const { BlockToken, addr1, addr2 } = await loadFixture(deployBlockToken); + await expect( + BlockToken.connect(addr1).transfertk(addr2, 100) + ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); + }); + + it("Should Transfer Successfully", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, owner_); + await BlockToken.connect(owner_).transfertk(addr1, 500); + expect(await BlockToken.balanceOf(owner_)).to.equal(500); + expect(await BlockToken.balanceOf(addr1)).to.equal(500); + + await BlockToken.connect(addr1).transfertk(addr2, 300); + expect(await BlockToken.balanceOf(addr1)).to.equal(200); + expect(await BlockToken.balanceOf(addr2)).to.equal(300); + }); + + it("Should revert if transferring to zero address", async () => { + const { BlockToken, owner_ } = await loadFixture(deployBlockToken); + const ZeroAddress = "0x0000000000000000000000000000000000000000"; + await BlockToken.connect(owner_).mint(1000, owner_); + await expect( + BlockToken.connect(owner_).transfertk(ZeroAddress, 300) + ).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidReceiver"); + }); + + it("Should revert if transferring more than balance", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, owner_); + await expect( + BlockToken.connect(owner_).transfertk(addr1, 2000) + ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); + }); + }); + + describe("TransferFrom Txn", () => { + it("Should revert if owner address doesn't have tokens", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + await BlockToken.connect(addr1).approveTxn(addr2, 1000); + await expect( + BlockToken.connect(addr2).transferFromtk(addr1, owner_, 50) + ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); + }); + + it("Should revert when transferring zero tokens", async () => { + const { BlockToken, addr1, addr2 } = await loadFixture(deployBlockToken); + await BlockToken.connect(addr1).approveTxn(addr2, 1000); + await expect( + BlockToken.connect(addr2).transferFromtk(addr1, owner_, 0) + ).to.be.revertedWith("BlockToken:: Zero amount not supported"); + }); + + it("Should revert when transferring more than balance", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, addr1); + await BlockToken.connect(addr1).approveTxn(addr2, 3000); + await expect( + BlockToken.connect(addr2).transferFromtk(addr1, owner_, 2000) + ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); + }); + + it("Should revert if transferring to address zero", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + const ZeroAddress = "0x0000000000000000000000000000000000000000"; + await BlockToken.connect(owner_).mint(1000, addr1); + await BlockToken.connect(addr1).approveTxn(addr2, 1000); + await expect( + BlockToken.connect(addr2).transferFromtk(addr1, ZeroAddress, 500) + ).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidReceiver"); + }); + + it("Should Transfer Tokens Successfully Using TransferFrom", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, addr1); + await BlockToken.connect(addr1).approveTxn(addr2, 1000); + await BlockToken.connect(addr2).transferFromtk(addr1, addr2, 400); + expect(await BlockToken.balanceOf(addr1)).to.equal(600); + expect(await BlockToken.balanceOf(addr2)).to.equal(400); + }); + }); + }); + + describe("Approvals", () => { + describe("Approve", () => { + it("Should revert when approving zero tokens", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, addr1); + await expect( + BlockToken.connect(addr1).approveTxn(addr2, 0) + ).to.be.revertedWith("BlockToken:: Zero amount not supported"); + }); + + it("Should revert if approving to address zero", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + const ZeroAddress = "0x0000000000000000000000000000000000000000"; + await BlockToken.connect(owner_).mint(500, addr1); + await expect( + BlockToken.connect(addr1).approveTxn(ZeroAddress, 50) + ).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidSpender"); + }); + + it("Should Approve Tokens Successfully", async () => { + const { BlockToken, addr1, addr2 } = await loadFixture(deployBlockToken); + await BlockToken.connect(addr1).approveTxn(addr2, 300); + expect(await BlockToken.allowance(addr1, addr2)).to.equal(300); + }); + }); + }); +}); \ No newline at end of file diff --git a/test/Counter.js b/test/Counter.js index 2ec91df..7ef17f9 100644 --- a/test/Counter.js +++ b/test/Counter.js @@ -1,78 +1,61 @@ -const {loadFixture } = require("@nomicfoundation/hardhat-toolbox/network-helpers"); -// const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs"); +const { loadFixture } = require("@nomicfoundation/hardhat-toolbox/network-helpers"); const { expect } = require("chai"); -// util functon +// Utility function to deploy Counter 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 CounterContract = await ethers.getContractFactory("Counter"); // Target Counter.sol + const counter = await CounterContract.deploy(); // Deploy the Counter contract + return counter; // Return the deployed instance +}; -// Counter Test Suite +// Counter Test Suite describe("Counter Test Suite", () => { - describe("Deployment", () => { - it("Should return default values upon deployment", async () => { - const counter = await loadFixture(deployCounter); - let count = await counter.count() - expect(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 () => { - const counter = await loadFixture(deployCounter); // extract deployed counter instace - let count3 = await counter.getCount(); // check initial count value before txn - expect(count3).to.eq(0); - await counter.setCount(20) - - let count4 = await counter.getCount() - expect(count4).to.eq(20) - await counter.setCount(30) - - let count5 = await counter.getCount() - expect(count5).to.eq(30) - await counter.setCount(40) - - let count6 = await counter.getCount() - expect(count6).to.eq(40) - }) - }) - - describe("IncreaseCountByOne", () => { - it("Should set appropriate increaseCountByOne value", async () => { - const counter = await loadFixture(deployCounter); - let count = await counter.getCount() - expect(count).to.eq(0) - await counter.setCount(40) - await counter.increaseCountByOne() - - let count2 = await counter.getCount() - expect(count2).to.eq(41) - }) - - it("Should set appropriate values for multiple increaseCountByOne txns", async () => { - const counter = await loadFixture(deployCounter); - await counter.setCount(1) - - for(let i = 0; i < 4; i++){ - await counter.increaseCountByOne() - } - expect(await counter.getCount()).to.eq(5) - - }) - }) - }) -}) \ No newline at end of file + describe("Deployment", () => { + it("Should return default values upon deployment", async () => { + const counter = await loadFixture(deployCounter); + expect(await counter.count()).to.equal(0); // Assert that count = 0 upon deployment + }); + }); + + describe("Transactions", () => { + describe("SetCount", () => { + it("Should set appropriate count values", async () => { + const counter = await loadFixture(deployCounter); + expect(await counter.getCount()).to.equal(0); + await counter.setCount(10); + expect(await counter.getCount()).to.equal(10); + }); + + it("Should set appropriate values for multiple setCount txns", async () => { + const counter = await loadFixture(deployCounter); + expect(await counter.getCount()).to.equal(0); + await counter.setCount(20); + expect(await counter.getCount()).to.equal(20); + await counter.setCount(30); + expect(await counter.getCount()).to.equal(30); + await counter.setCount(40); + expect(await counter.getCount()).to.equal(40); + }); + }); + + describe("IncreaseCountByOne", () => { + it("Should set appropriate values for multiple increaseCountByOne txns", async () => { + const counter = await loadFixture(deployCounter); + await counter.setCount(10); + expect(await counter.getCount()).to.equal(10); + + // Use a while loop to increment until count reaches 25 + while ((await counter.getCount()) < 25) { + await counter.increaseCountByOne(); + } + expect(await counter.getCount()).to.equal(25); + + // Additional increments using a for loop + for (let i = 0; i < 10; i++) { + await counter.increaseCountByOne(); + } + expect(await counter.getCount()).to.equal(35); + }); + }); + }); +}); \ No newline at end of file From ddbf4e4cec81f4537e41a6924b138e602f9d0789 Mon Sep 17 00:00:00 2001 From: folex1275 Date: Tue, 29 Jul 2025 23:45:11 +0100 Subject: [PATCH 7/8] test: updating transferfrom test --- test/BlockToken.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/BlockToken.js b/test/BlockToken.js index 8fd3fa0..a469da9 100644 --- a/test/BlockToken.js +++ b/test/BlockToken.js @@ -173,7 +173,7 @@ describe("BlockToken Test Suite", () => { }); it("Should revert when transferring zero tokens", async () => { - const { BlockToken, addr1, addr2 } = await loadFixture(deployBlockToken); + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); await BlockToken.connect(addr1).approveTxn(addr2, 1000); await expect( BlockToken.connect(addr2).transferFromtk(addr1, owner_, 0) From 4e8b149d8bcb831d880cd65ff1eb77a9390b397a Mon Sep 17 00:00:00 2001 From: folex1275 Date: Wed, 30 Jul 2025 10:19:42 +0100 Subject: [PATCH 8/8] test: update made to BlockToken test --- test/BlockToken.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/BlockToken.js b/test/BlockToken.js index a469da9..fcc1189 100644 --- a/test/BlockToken.js +++ b/test/BlockToken.js @@ -46,6 +46,12 @@ describe("BlockToken Test Suite", () => { BlockToken.connect(owner_).mint(0, addr1) ).to.be.revertedWith("BlockToken:: Zero amount not supported"); }); + + it("Should revert if minting to address zero", async () => { + const { BlockToken, owner_ } = await loadFixture(deployBlockToken); + let ZeroAddress = "0x0000000000000000000000000000000000000000"; + await expect(BlockToken.connect(owner_).mint(1000, ZeroAddress)).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidReceiver"); + }) }); describe("Burning", () => { @@ -121,6 +127,13 @@ describe("BlockToken Test Suite", () => { const maliciousTxn = BlockToken.connect(addr1).burnFrom(addr2, 1000); await expect(maliciousTxn).to.be.revertedWith("BlockToken:: Unauthorized User"); }); + + it("Should revert if burning zero amount", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, owner_); + await BlockToken.connect(owner_).transfertk(addr1, 400); + await expect(BlockToken.connect(owner_).burnFrom(addr1, 0)).to.be.revertedWith("BlockToken:: Zero amount not supported") + }) }); }); @@ -161,6 +174,11 @@ describe("BlockToken Test Suite", () => { BlockToken.connect(owner_).transfertk(addr1, 2000) ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); }); + it("Should revert if transferring zero tokens", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, owner_); + await expect(BlockToken.connect(owner_).transfertk(addr1, 0)).to.be.revertedWith("BlockToken:: Zero amount not supported"); + }) }); describe("TransferFrom Txn", () => {