From 5112a590b7ec40ad5d3409c9f0ab7d4bf811069e Mon Sep 17 00:00:00 2001 From: Cletusgizo Date: Fri, 25 Jul 2025 12:43:48 +0100 Subject: [PATCH 1/6] Update: CounterTest --- package.json | 6 +++--- test/Counter.js | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/package.json b/package.json index 0586079..b6abcf5 100644 --- a/package.json +++ b/package.json @@ -3,10 +3,10 @@ "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" } } diff --git a/test/Counter.js b/test/Counter.js index 99b2931..0d52e9d 100644 --- a/test/Counter.js +++ b/test/Counter.js @@ -21,6 +21,7 @@ describe("Counter Test Suite", () => { 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 @@ -29,20 +30,55 @@ describe("Counter Test Suite", () => { 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 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); + await counter.setCount(20) + + let count3 = await counter.getCount(); // check initial count value before txn + expect(count3).to.eq(20); + await counter.setCount(30) + + let count4 = await counter.getCount(); // check initial count value before txn + expect(count4).to.eq(30); + await counter.setCount(40) + }) }) describe("IncreaseCountByOne", () => { it("Should set appropriate increaseCountByOne value", 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); + + let count2 = await counter.getCount(); // check initial count value before txn + expect(count1).to.eq(0); + }) it("Should set appropriate values for multiple increaseCountByOne txns", 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); + + let count2 = await counter.getCount(); // check initial count value before txn + expect(count1).to.eq(0); + + let count3 = await counter.getCount(); // check initial count value before txn + expect(count1).to.eq(0); + + let count4 = await counter.getCount(); // check initial count value before txn + expect(count1).to.eq(0); }) }) }) From 2b6aea6001aac6e2929fc1c202831c2d51f7b72e Mon Sep 17 00:00:00 2001 From: Cletusgizo Date: Tue, 29 Jul 2025 09:03:50 +0100 Subject: [PATCH 2/6] UPDATE: Assignment V2 --- contracts/CounterV2.sol | 63 +++++++++++++++++++++++++ contracts/Lock.sol | 50 ++++++++++---------- test/CounterV2.js | 101 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 189 insertions(+), 25 deletions(-) create mode 100644 contracts/CounterV2.sol create mode 100644 test/CounterV2.js diff --git a/contracts/CounterV2.sol b/contracts/CounterV2.sol new file mode 100644 index 0000000..7b8c354 --- /dev/null +++ b/contracts/CounterV2.sol @@ -0,0 +1,63 @@ +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.28; + +interface MeCounterV2 { + function resetCount() external; + function setCount(uint256 _count) external; + function decreaseCount() external; + function increaseCountByOne() external; + function getCount() external view returns(uint256); + +} + +contract CounterV2 is MeCounterV2 { + uint256 public count; + address owner; + + constructor() { + owner = msg.sender; + } + + function setCount(uint256 _count) public { + require(_count > 0, "Cannot pass in 0 value as argument"); + require(msg.sender == owner, "Unauthorized User: Only owner can alter the count"); + count = _count; + } + + + function increaseCountByOne() public { + require(msg.sender == owner, "Unauthorized User"); + count += 1; + } + + function getCount() public view returns(uint256) { + // require(msg.sender == owner, "Unauthorized User"); + return count; + } + + function resetCount() public { + require(msg.sender == owner, "Unauthorized User"); + if (count > 0) { + count = 0; + } + } + + function decreaseCount() external { + count -= 1; + } +} + +contract CounterV2Caller{ + MeCounterV2 public _MeCounterV2; + address public contractCountV2Add; + + constructor (address _contractCountV2Add) { + contractCountV2Add = _contractCountV2Add; + _MeCounterV2 = MeCounterV2 (_contractCountV2Add); + } + + function decreementCaller() external { + _MeCounterV2.decreaseCount(); + } + +} \ No newline at end of file diff --git a/contracts/Lock.sol b/contracts/Lock.sol index 2f385f7..7c6b906 100644 --- a/contracts/Lock.sol +++ b/contracts/Lock.sol @@ -1,34 +1,34 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.28; +// // SPDX-License-Identifier: UNLICENSED +// pragma solidity ^0.8.28; -// Uncomment this line to use console.log -// import "hardhat/console.sol"; +// // Uncomment this line to use console.log +// // import "hardhat/console.sol"; -contract Lock { - uint public unlockTime; - address payable public owner; +// contract Lock { +// uint public unlockTime; +// address payable public owner; - event Withdrawal(uint amount, uint when); +// event Withdrawal(uint amount, uint when); - constructor(uint _unlockTime) payable { - require( - block.timestamp < _unlockTime, - "Unlock time should be in the future" - ); +// constructor(uint _unlockTime) payable { +// require( +// block.timestamp < _unlockTime, +// "Unlock time should be in the future" +// ); - unlockTime = _unlockTime; - owner = payable(msg.sender); - } +// unlockTime = _unlockTime; +// owner = payable(msg.sender); +// } - function withdraw() public { - // Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal - // console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp); +// function withdraw() public { +// // Uncomment this line, and the import of "hardhat/console.sol", to print a log in your terminal +// // console.log("Unlock time is %o and block timestamp is %o", unlockTime, block.timestamp); - require(block.timestamp >= unlockTime, "You can't withdraw yet"); - require(msg.sender == owner, "You aren't the owner"); +// require(block.timestamp >= unlockTime, "You can't withdraw yet"); +// require(msg.sender == owner, "You aren't the owner"); - emit Withdrawal(address(this).balance, block.timestamp); +// emit Withdrawal(address(this).balance, block.timestamp); - owner.transfer(address(this).balance); - } -} +// owner.transfer(address(this).balance); +// } +// } diff --git a/test/CounterV2.js b/test/CounterV2.js new file mode 100644 index 0000000..b8c1e3b --- /dev/null +++ b/test/CounterV2.js @@ -0,0 +1,101 @@ +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 CounterContract = await ethers.getContractFactory("CounterV2"); // target Counter.sol + const counterV2 = await CounterContract.deploy(); // deploy the Counter contract + + + const CounterContract1 = await ethers.getContractFactory("CounterV2Caller"); + const targetAddress = await counterV2.getAddress(); //const targetAddress = await counterV2.getAddress(); + + const counterV2caller = await CounterContract1.deploy(targetAddress); + + + return {counterV2, counterV2caller} ; // return the deployed instance of our counter contract +} + + +//Counter Test +describe("Testing Counter Suite Contract.sol", () => { + describe("Deployment", async() => { + it("Should return default values upon deployment", async () => { + const {counterV2} = await loadFixture(deployCounter); + expect(await counterV2.count()).to.eq(0); + }); + }); + + describe("Transaction", () => { + describe("SetCount", () => { + it("Should set appropriate count values", async () => { + const {counterV2} = await loadFixture(deployCounter); + let count1 = await counterV2.getCount(); + expect(count1).to.eq(0); + await counterV2.setCount(10); + }) + it("Should set appropriate values for multiple setCount txns", async () => { + const {counterV2} = await loadFixture(deployCounter); + let count1 = await counterV2.getCount(); + expect(count1).to.eq(0); + await counterV2.setCount(1); + + let count2 = await counterV2.getCount(); + expect(count2).to.eq(1); + await counterV2.setCount(2); + }) + }) + + describe("resetCount", () => { + it("Should reset the value to a default", async () => { + const {counterV2} = await loadFixture(deployCounter); + let count1 = await counterV2.getCount(); + expect(count1).to.eq(0); + await counterV2.setCount(10); + + let count2 = await counterV2.getCount(); + expect(count2).to.eq(10); + await counterV2.setCount(20); + + let count3 = await counterV2.getCount(); + expect(count3).to.eq(20); + await counterV2.resetCount(); + + }) + }) + + describe("IncreaseCountByOne", () => { + it("Should set appropriate increaseCountByOne value", async () => { + const {counterV2} = await loadFixture(deployCounter); // extract deployed counter instace + let count1 = await counterV2.getCount(); // check initial count value before txn + expect(count1).to.eq(0); + + let count2 = await counterV2.getCount(); // check initial count value before txn + expect(count1).to.eq(0); + + }) + + }) + + describe("DecreaseCount", () => { + it("Should decrease Count by one value", async () => { + const {counterV2, counterV2caller} = await loadFixture(deployCounter); // extract deployed counter instace + let count1 = await counterV2.getCount(); // check initial count value before txn + expect(count1).to.eq(0); + await counterV2.setCount(10) + + let count2 = await counterV2.getCount(); // check initial count value before txn + expect(count2).to.eq(10); + await counterV2caller.decreementCaller(); + + let count3 = await counterV2.getCount(); // check initial count value before txn + expect(count3).to.eq(9); + + }) + + }) + }) +}) \ No newline at end of file From 5fadfc96564a47a00ed68a815279114730a8558f Mon Sep 17 00:00:00 2001 From: Cletusgizo Date: Wed, 30 Jul 2025 01:44:26 +0100 Subject: [PATCH 3/6] Added burnFrom, transferToken, TransferFromToken, approveToken --- contracts/BlockHeaderToken.sol | 48 +++++++ contracts/Counter.sol | 78 +++++----- contracts/CounterV2.sol | 124 ++++++++-------- package-lock.json | 22 +-- package.json | 4 + test/BlockToken.js | 251 +++++++++++++++++++++++++++++++++ test/Counter.js | 153 +++++++++++--------- test/CounterV2.js | 180 +++++++++++------------ tsconfig.json | 3 +- 9 files changed, 593 insertions(+), 270 deletions(-) create mode 100644 contracts/BlockHeaderToken.sol create mode 100644 test/BlockToken.js diff --git a/contracts/BlockHeaderToken.sol b/contracts/BlockHeaderToken.sol new file mode 100644 index 0000000..f7b755f --- /dev/null +++ b/contracts/BlockHeaderToken.sol @@ -0,0 +1,48 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.28; + +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 transferToken(address _recepient, uint256 _amount) notAmount0(_amount) external { + _transfer(msg.sender, _recepient, _amount); + } + + function transferFromToken(address _sender, address _recepient, uint256 _amount) notAmount0(_amount) external { + _transfer(_sender, _recepient, _amount); + } + + function approveToken(address spender, uint256 _amount) notAmount0(_amount) external{ + _approve(msg.sender, spender, _amount); + } + +} diff --git a/contracts/Counter.sol b/contracts/Counter.sol index fa8560c..a74eb6d 100644 --- a/contracts/Counter.sol +++ b/contracts/Counter.sol @@ -1,48 +1,48 @@ -// SPDX-License-Identifier: MIT -pragma solidity 0.8.28; +// // 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); +// interface ICounter { +// function setCount(uint256 _count) external; +// function increaseCountByOne() external; +// function getCount() external view returns(uint256); -} +// } -contract Counter is ICounter { - uint256 public count; +// contract Counter is ICounter { +// uint256 public count; - function setCount(uint256 _count) external { - count = _count; - } - - function increaseCountByOne() public { - count += 1; - } - - function getCount() public view returns(uint256) { - return count; - } -} - - -// 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) external { +// count = _count; +// } -// function setCount(uint256 _count) public { -// _ic.setCount(_count); +// function increaseCountByOne() public { +// count += 1; // } // function getCount() public view returns(uint256) { -// return _ic.getCount(); +// return count; // } -// } \ No newline at end of file +// } + + +// // // 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 index 7b8c354..903993b 100644 --- a/contracts/CounterV2.sol +++ b/contracts/CounterV2.sol @@ -1,63 +1,63 @@ -//SPDX-License-Identifier: MIT -pragma solidity ^0.8.28; - -interface MeCounterV2 { - function resetCount() external; - function setCount(uint256 _count) external; - function decreaseCount() external; - function increaseCountByOne() external; - function getCount() external view returns(uint256); +// //SPDX-License-Identifier: MIT +// pragma solidity ^0.8.28; + +// interface MeCounterV2 { +// function resetCount() external; +// function setCount(uint256 _count) external; +// function decreaseCount() external; +// function increaseCountByOne() external; +// function getCount() external view returns(uint256); -} - -contract CounterV2 is MeCounterV2 { - uint256 public count; - address owner; - - constructor() { - owner = msg.sender; - } - - function setCount(uint256 _count) public { - require(_count > 0, "Cannot pass in 0 value as argument"); - require(msg.sender == owner, "Unauthorized User: Only owner can alter the count"); - count = _count; - } - - - function increaseCountByOne() public { - require(msg.sender == owner, "Unauthorized User"); - count += 1; - } - - function getCount() public view returns(uint256) { - // require(msg.sender == owner, "Unauthorized User"); - return count; - } - - function resetCount() public { - require(msg.sender == owner, "Unauthorized User"); - if (count > 0) { - count = 0; - } - } - - function decreaseCount() external { - count -= 1; - } -} - -contract CounterV2Caller{ - MeCounterV2 public _MeCounterV2; - address public contractCountV2Add; - - constructor (address _contractCountV2Add) { - contractCountV2Add = _contractCountV2Add; - _MeCounterV2 = MeCounterV2 (_contractCountV2Add); - } - - function decreementCaller() external { - _MeCounterV2.decreaseCount(); - } - -} \ No newline at end of file +// } + +// contract CounterV2 is MeCounterV2 { +// uint256 public count; +// address owner; + +// constructor() { +// owner = msg.sender; +// } + +// function setCount(uint256 _count) public { +// require(_count > 0, "Cannot pass in 0 value as argument"); +// require(msg.sender == owner, "Unauthorized User: Only owner can alter the count"); +// count = _count; +// } + + +// function increaseCountByOne() public { +// require(msg.sender == owner, "Unauthorized User"); +// count += 1; +// } + +// function getCount() public view returns(uint256) { +// // require(msg.sender == owner, "Unauthorized User"); +// return count; +// } + +// function resetCount() public { +// require(msg.sender == owner, "Unauthorized User"); +// if (count > 0) { +// count = 0; +// } +// } + +// function decreaseCount() external { +// count -= 1; +// } +// } + +// contract CounterV2Caller{ +// MeCounterV2 public _MeCounterV2; +// address public contractCountV2Add; + +// constructor (address _contractCountV2Add) { +// contractCountV2Add = _contractCountV2Add; +// _MeCounterV2 = MeCounterV2 (_contractCountV2Add); +// } + +// function decreementCaller() external { +// _MeCounterV2.decreaseCount(); +// } + +// } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 656a34f..d2e9473 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5,8 +5,12 @@ "packages": { "": { "name": "hardhat-project", + "dependencies": { + "@openzeppelin/contracts": "^5.4.0" + }, "devDependencies": { "@nomicfoundation/hardhat-toolbox": "^5.0.0", + "@types/minimatch": "^5.1.2", "hardhat": "^2.26.1" } }, @@ -1305,6 +1309,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", @@ -1678,15 +1688,11 @@ } }, "node_modules/@types/minimatch": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-6.0.0.tgz", - "integrity": "sha512-zmPitbQ8+6zNutpwgcQuLcsEpn/Cj54Kbn7L5pX0Os5kdWplB7xPgEh/g+SWOB/qmows2gpuCaPyduq8ZZRnxA==", - "deprecated": "This is a stub types definition. minimatch provides its own type definitions, so you do not need this installed.", + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/@types/minimatch/-/minimatch-5.1.2.tgz", + "integrity": "sha512-K0VQKziLUWkVKiRVrx4a40iPaxTUefQmjtkQofBkYRcoaaL/8rhwDWww9qWbrgicNOgnpIsMxyNIUM4+n6dUIA==", "dev": true, - "peer": true, - "dependencies": { - "minimatch": "*" - } + "license": "MIT" }, "node_modules/@types/mocha": { "version": "10.0.10", diff --git a/package.json b/package.json index b6abcf5..3af3ab2 100644 --- a/package.json +++ b/package.json @@ -2,11 +2,15 @@ "name": "hardhat-project", "devDependencies": { "@nomicfoundation/hardhat-toolbox": "^5.0.0", + "@types/minimatch": "^5.1.2", "hardhat": "^2.26.1" }, "scripts": { "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..a24e14b --- /dev/null +++ b/test/BlockToken.js @@ -0,0 +1,251 @@ +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", () => { + 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 not burn if user doesn't have tokens", async () => { + const {BlockToken, owner_, addr1} = await loadFixture(deployBlockToken); + await expect(BlockToken.connect(owner_).burnFrom(addr1, 1000) + ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); + }); + + it("Shound burn from an address successfully", async () => { + const {BlockToken, owner_, addr1} = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(2000, addr1); + expect(await BlockToken.balanceOf(addr1)).to.eq(2000); + + await BlockToken.connect(owner_).burnFrom(addr1,1000); + expect(await BlockToken.balanceOf(addr1)).to.eq(1000); + }); + + it("Should revert when burning zero tokens", async () => { + const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken); + await expect( + BlockToken.connect(owner_).burnFrom(addr1, 0) + ).to.be.revertedWith("BlockToken:: Zero amount not supported"); + }); + + it("Should revert when burning more than balance", async () => { + const { BlockToken, owner_ ,addr1} = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(500, addr1); + await expect( + BlockToken.connect(owner_).burnFrom(addr1, 1000) + ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); + }); + + it("Should revert if burnining called by different address", async () =>{ + const { BlockToken,addr2, addr1 } = await loadFixture(deployBlockToken); + // test that another user cant call successfully + let malicioustxn = BlockToken.connect(addr1).burnFrom(addr2, 1000); + await expect(malicioustxn).to.be.revertedWith( + "BlockToken:: Unauthorized User" + ); + }); +}); + +describe("Approve token transfer", () =>{ + 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).approveToken(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).approveToken(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).approveToken(addr2, 300); + expect(await BlockToken.allowance(addr1,addr2)).to.eq(300); + + }); + }); + describe("Transfer Token ", () => { + describe("TransferToken Transaction", () => { + it("Should revert if from address doesn't have tokens", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + // await BlockToken.connect(owner_).mint(1000, addr1); + await expect( + BlockToken.connect(addr1).transferToken(addr2, 1000) + ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); + }); + + it("Should revert when transfering zero tokens", async () => { + const { BlockToken, owner_,addr1,addr2 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, addr1); + + await expect( + BlockToken.connect(addr1).transferToken(addr2, 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(500, addr1); + await expect( + BlockToken.connect(addr1).transferToken(addr2, 1000) + ).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(500, addr1) + await expect( + BlockToken.connect(addr1).transferToken(ZeroAddress, 50) + ).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidReceiver"); + }); + + it("Should Burn Tokens Successfully", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + await BlockToken.connect(owner_).mint(1000, addr1); + expect(await BlockToken.balanceOf(addr1)).to.eq(1000); + + await BlockToken.connect(addr1).transferToken(addr2, 300); + expect(await BlockToken.balanceOf(addr1)).to.eq(700); + expect(await BlockToken.balanceOf(addr2)).to.eq(300); + + }); + }); +}); + +describe("TransferFrom Token", () => { + it("Should revert if owner address doesn't have tokens", async () => { + const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); + await BlockToken.connect(addr1).approveToken(addr2,100); + await expect( + BlockToken.connect(addr2).transferFromToken(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(owner_).mint(1000, addr1); + await BlockToken.connect(addr1).approveToken(addr2,500) + await expect( + BlockToken.connect(addr2).transferFromToken(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).approveToken(addr2,5000) + await expect( + BlockToken.connect(addr2).transferFromToken(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).approveToken(addr2,5000) + await expect( + BlockToken.connect(addr2).transferFromToken(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).approveToken(addr2,5000) + + await BlockToken.connect(addr2).transferFromToken(addr1,addr2, 400); + + expect(await BlockToken.balanceOf(addr1)).to.eq(600); + expect(await BlockToken.balanceOf(addr2)).to.eq(400); + + }); + }); + }) \ No newline at end of file diff --git a/test/Counter.js b/test/Counter.js index 0d52e9d..f7228ed 100644 --- a/test/Counter.js +++ b/test/Counter.js @@ -1,85 +1,98 @@ -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 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 -} - -// Counter 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 +// 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 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 +// } + +// // Counter 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); // A demo disployment of an instance, 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 +// 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 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 +// it("Should set appropriate values for multiple setCount txns", 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); - await counter.setCount(20) +// let count2 = await counter.getCount(); // check initial count value before txn +// expect(count2).to.eq(10); +// await counter.setCount(20) - let count3 = await counter.getCount(); // check initial count value before txn - expect(count3).to.eq(20); - await counter.setCount(30) +// let count3 = await counter.getCount(); // check initial count value before txn +// expect(count3).to.eq(20); +// await counter.setCount(30) - let count4 = await counter.getCount(); // check initial count value before txn - expect(count4).to.eq(30); - await counter.setCount(40) +// let count4 = await counter.getCount(); // check initial count value before txn +// expect(count4).to.eq(30); +// await counter.setCount(40) - }) - }) +// }) +// }) - describe("IncreaseCountByOne", () => { - it("Should set appropriate increaseCountByOne value", 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); +// describe("IncreaseCountByOne", () => { +// // it("Should set appropriate increaseCountByOne value", 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); - let count2 = await counter.getCount(); // check initial count value before txn - expect(count1).to.eq(0); +// // await counter.increaseCountByOne(); // assert that count = 0 upon deployment +// // let count2 = await counter.getCount(); // check initial count value before txn +// // expect(count1).to.eq(0); - }) +// // }) - it("Should set appropriate values for multiple increaseCountByOne txns", 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); +// it("Should set appropriate values for multiple increaseCountByOne txns", 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(50) - let count2 = await counter.getCount(); // check initial count value before txn - expect(count1).to.eq(0); +// // await counter.increaseCountByOne(s); +// let count2 = 50 +// // expect(count2).to.eq(50); + +// while (count2 < 50) { +// console.log(count2); +// count2++; +// } +// // for (let index = 0; index < 51; index++) { +// await counter.increaseCountByOne(); + +// // }, - let count3 = await counter.getCount(); // check initial count value before txn - expect(count1).to.eq(0); +// // let count3 = await counter.getCount(); // check initial count value before txn +// // expect(count3).to.eq(101); + + + - let count4 = await counter.getCount(); // check initial count value before txn - expect(count1).to.eq(0); - }) - }) - }) -}) \ No newline at end of file +// }) +// }) +// }) +// }) \ No newline at end of file diff --git a/test/CounterV2.js b/test/CounterV2.js index b8c1e3b..e54114e 100644 --- a/test/CounterV2.js +++ b/test/CounterV2.js @@ -1,101 +1,101 @@ -const {loadFixture } = require("@nomicfoundation/hardhat-toolbox/network-helpers"); -// const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs"); -const { expect } = require("chai"); +// 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 CounterContract = await ethers.getContractFactory("CounterV2"); // target Counter.sol - const counterV2 = await CounterContract.deploy(); // deploy the Counter contract +// // util functon +// const deployCounter = async () => { +// // target the Counter contract within our contract folder +// const CounterContract = await ethers.getContractFactory("CounterV2"); // target Counter.sol +// const counterV2 = await CounterContract.deploy(); // deploy the Counter contract - const CounterContract1 = await ethers.getContractFactory("CounterV2Caller"); - const targetAddress = await counterV2.getAddress(); //const targetAddress = await counterV2.getAddress(); - - const counterV2caller = await CounterContract1.deploy(targetAddress); - - - return {counterV2, counterV2caller} ; // return the deployed instance of our counter contract -} - - -//Counter Test -describe("Testing Counter Suite Contract.sol", () => { - describe("Deployment", async() => { - it("Should return default values upon deployment", async () => { - const {counterV2} = await loadFixture(deployCounter); - expect(await counterV2.count()).to.eq(0); - }); - }); - - describe("Transaction", () => { - describe("SetCount", () => { - it("Should set appropriate count values", async () => { - const {counterV2} = await loadFixture(deployCounter); - let count1 = await counterV2.getCount(); - expect(count1).to.eq(0); - await counterV2.setCount(10); - }) - it("Should set appropriate values for multiple setCount txns", async () => { - const {counterV2} = await loadFixture(deployCounter); - let count1 = await counterV2.getCount(); - expect(count1).to.eq(0); - await counterV2.setCount(1); - - let count2 = await counterV2.getCount(); - expect(count2).to.eq(1); - await counterV2.setCount(2); - }) - }) - - describe("resetCount", () => { - it("Should reset the value to a default", async () => { - const {counterV2} = await loadFixture(deployCounter); - let count1 = await counterV2.getCount(); - expect(count1).to.eq(0); - await counterV2.setCount(10); - - let count2 = await counterV2.getCount(); - expect(count2).to.eq(10); - await counterV2.setCount(20); - - let count3 = await counterV2.getCount(); - expect(count3).to.eq(20); - await counterV2.resetCount(); - - }) - }) - - describe("IncreaseCountByOne", () => { - it("Should set appropriate increaseCountByOne value", async () => { - const {counterV2} = await loadFixture(deployCounter); // extract deployed counter instace - let count1 = await counterV2.getCount(); // check initial count value before txn - expect(count1).to.eq(0); - - let count2 = await counterV2.getCount(); // check initial count value before txn - expect(count1).to.eq(0); +// const CounterContract1 = await ethers.getContractFactory("CounterV2Caller"); +// const targetAddress = await counterV2.getAddress(); //const targetAddress = await counterV2.getAddress(); + +// const counterV2caller = await CounterContract1.deploy(targetAddress); + + +// return {counterV2, counterV2caller} ; // return the deployed instance of our counter contract +// } + + +// //Counter Test +// describe("Testing Counter Suite Contract.sol", () => { +// describe("Deployment", async() => { +// it("Should return default values upon deployment", async () => { +// const {counterV2} = await loadFixture(deployCounter); +// expect(await counterV2.count()).to.eq(0); +// }); +// }); + +// describe("Transaction", () => { +// describe("SetCount", () => { +// it("Should set appropriate count values", async () => { +// const {counterV2} = await loadFixture(deployCounter); +// let count1 = await counterV2.getCount(); +// expect(count1).to.eq(0); +// await counterV2.setCount(10); +// }) +// it("Should set appropriate values for multiple setCount txns", async () => { +// const {counterV2} = await loadFixture(deployCounter); +// let count1 = await counterV2.getCount(); +// expect(count1).to.eq(0); +// await counterV2.setCount(1); + +// let count2 = await counterV2.getCount(); +// expect(count2).to.eq(1); +// await counterV2.setCount(2); +// }) +// }) + +// describe("resetCount", () => { +// it("Should reset the value to a default", async () => { +// const {counterV2} = await loadFixture(deployCounter); +// let count1 = await counterV2.getCount(); +// expect(count1).to.eq(0); +// await counterV2.setCount(10); + +// let count2 = await counterV2.getCount(); +// expect(count2).to.eq(10); +// await counterV2.setCount(20); + +// let count3 = await counterV2.getCount(); +// expect(count3).to.eq(20); +// await counterV2.resetCount(); + +// }) +// }) + +// describe("IncreaseCountByOne", () => { +// it("Should set appropriate increaseCountByOne value", async () => { +// const {counterV2} = await loadFixture(deployCounter); // extract deployed counter instace +// let count1 = await counterV2.getCount(); // check initial count value before txn +// expect(count1).to.eq(0); + +// let count2 = await counterV2.getCount(); // check initial count value before txn +// expect(count1).to.eq(0); - }) +// }) - }) +// }) - describe("DecreaseCount", () => { - it("Should decrease Count by one value", async () => { - const {counterV2, counterV2caller} = await loadFixture(deployCounter); // extract deployed counter instace - let count1 = await counterV2.getCount(); // check initial count value before txn - expect(count1).to.eq(0); - await counterV2.setCount(10) +// describe("DecreaseCount", () => { +// it("Should decrease Count by one value", async () => { +// const {counterV2, counterV2caller} = await loadFixture(deployCounter); // extract deployed counter instace +// let count1 = await counterV2.getCount(); // check initial count value before txn +// expect(count1).to.eq(0); +// await counterV2.setCount(10) - let count2 = await counterV2.getCount(); // check initial count value before txn - expect(count2).to.eq(10); - await counterV2caller.decreementCaller(); +// let count2 = await counterV2.getCount(); // check initial count value before txn +// expect(count2).to.eq(10); +// await counterV2caller.decreementCaller(); - let count3 = await counterV2.getCount(); // check initial count value before txn - expect(count3).to.eq(9); +// let count3 = await counterV2.getCount(); // check initial count value before txn +// expect(count3).to.eq(9); - }) +// }) - }) - }) -}) \ No newline at end of file +// }) +// }) +// }) \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index 574e785..10d7742 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,7 @@ "forceConsistentCasingInFileNames": true, "strict": true, "skipLibCheck": true, - "resolveJsonModule": true + "resolveJsonModule": true, + // "types": ["minimatch"] } } From d3d7c34ae55030e8e25bc32d37e73b1faf7d3eda Mon Sep 17 00:00:00 2001 From: Cletusgizo Date: Wed, 30 Jul 2025 01:58:23 +0100 Subject: [PATCH 4/6] Ecounter Conflit --- contracts/BlockHeaderToken.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/BlockHeaderToken.sol b/contracts/BlockHeaderToken.sol index f7b755f..b80b3e6 100644 --- a/contracts/BlockHeaderToken.sol +++ b/contracts/BlockHeaderToken.sol @@ -1,5 +1,5 @@ // SPDX-License-Identifier: MIT -pragma solidity ^0.8.28; +pragma solidity ^0.8.0; import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol"; From f421c3acb3d7bd11c0c37ae7e6310dfa3d980f1a Mon Sep 17 00:00:00 2001 From: Cletusgizo Date: Wed, 30 Jul 2025 02:24:32 +0100 Subject: [PATCH 5/6] Assignment Complete --- contracts/BlockHeaderToken.sol | 2 +- contracts/Counter.sol | 96 ++++++++++++------------- contracts/CounterV2.sol | 124 ++++++++++++++++----------------- test/BlockToken.js | 10 +-- 4 files changed, 116 insertions(+), 116 deletions(-) diff --git a/contracts/BlockHeaderToken.sol b/contracts/BlockHeaderToken.sol index b80b3e6..bbcf239 100644 --- a/contracts/BlockHeaderToken.sol +++ b/contracts/BlockHeaderToken.sol @@ -37,7 +37,7 @@ contract BlockToken is ERC20{ _transfer(msg.sender, _recepient, _amount); } - function transferFromToken(address _sender, address _recepient, uint256 _amount) notAmount0(_amount) external { + function transferFromAdd(address _sender, address _recepient, uint256 _amount) notAmount0(_amount) external { _transfer(_sender, _recepient, _amount); } diff --git a/contracts/Counter.sol b/contracts/Counter.sol index a74eb6d..3a2a4a4 100644 --- a/contracts/Counter.sol +++ b/contracts/Counter.sol @@ -1,48 +1,48 @@ -// // 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) external { -// count = _count; -// } - -// function increaseCountByOne() public { -// count += 1; -// } - -// function getCount() public view returns(uint256) { -// return count; -// } -// } - - -// // // 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 +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +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) external { + count = _count; + } + + function increaseCountByOne() public { + count += 1; + } + + function getCount() public view returns(uint256) { + return count; + } +} + + +// // 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 index 903993b..43a6428 100644 --- a/contracts/CounterV2.sol +++ b/contracts/CounterV2.sol @@ -1,63 +1,63 @@ -// //SPDX-License-Identifier: MIT -// pragma solidity ^0.8.28; - -// interface MeCounterV2 { -// function resetCount() external; -// function setCount(uint256 _count) external; -// function decreaseCount() external; -// function increaseCountByOne() external; -// function getCount() external view returns(uint256); +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +interface MeCounterV2 { + function resetCount() external; + function setCount(uint256 _count) external; + function decreaseCount() external; + function increaseCountByOne() external; + function getCount() external view returns(uint256); -// } - -// contract CounterV2 is MeCounterV2 { -// uint256 public count; -// address owner; - -// constructor() { -// owner = msg.sender; -// } - -// function setCount(uint256 _count) public { -// require(_count > 0, "Cannot pass in 0 value as argument"); -// require(msg.sender == owner, "Unauthorized User: Only owner can alter the count"); -// count = _count; -// } - - -// function increaseCountByOne() public { -// require(msg.sender == owner, "Unauthorized User"); -// count += 1; -// } - -// function getCount() public view returns(uint256) { -// // require(msg.sender == owner, "Unauthorized User"); -// return count; -// } - -// function resetCount() public { -// require(msg.sender == owner, "Unauthorized User"); -// if (count > 0) { -// count = 0; -// } -// } - -// function decreaseCount() external { -// count -= 1; -// } -// } - -// contract CounterV2Caller{ -// MeCounterV2 public _MeCounterV2; -// address public contractCountV2Add; - -// constructor (address _contractCountV2Add) { -// contractCountV2Add = _contractCountV2Add; -// _MeCounterV2 = MeCounterV2 (_contractCountV2Add); -// } - -// function decreementCaller() external { -// _MeCounterV2.decreaseCount(); -// } - -// } \ No newline at end of file +} + +contract CounterV2 is MeCounterV2 { + uint256 public count; + address owner; + + constructor() { + owner = msg.sender; + } + + function setCount(uint256 _count) public { + require(_count > 0, "Cannot pass in 0 value as argument"); + require(msg.sender == owner, "Unauthorized User: Only owner can alter the count"); + count = _count; + } + + + function increaseCountByOne() public { + require(msg.sender == owner, "Unauthorized User"); + count += 1; + } + + function getCount() public view returns(uint256) { + // require(msg.sender == owner, "Unauthorized User"); + return count; + } + + function resetCount() public { + require(msg.sender == owner, "Unauthorized User"); + if (count > 0) { + count = 0; + } + } + + function decreaseCount() external { + count -= 1; + } +} + +contract CounterV2Caller{ + MeCounterV2 public _MeCounterV2; + address public contractCountV2Add; + + constructor (address _contractCountV2Add) { + contractCountV2Add = _contractCountV2Add; + _MeCounterV2 = MeCounterV2 (_contractCountV2Add); + } + + function decreementCaller() external { + _MeCounterV2.decreaseCount(); + } + +} \ No newline at end of file diff --git a/test/BlockToken.js b/test/BlockToken.js index a24e14b..5914421 100644 --- a/test/BlockToken.js +++ b/test/BlockToken.js @@ -204,7 +204,7 @@ describe("TransferFrom Token", () => { const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); await BlockToken.connect(addr1).approveToken(addr2,100); await expect( - BlockToken.connect(addr2).transferFromToken(addr1,owner_,50) + BlockToken.connect(addr2).transferFromAdd(addr1,owner_,50) ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); }); @@ -213,7 +213,7 @@ describe("TransferFrom Token", () => { await BlockToken.connect(owner_).mint(1000, addr1); await BlockToken.connect(addr1).approveToken(addr2,500) await expect( - BlockToken.connect(addr2).transferFromToken(addr1,owner_, 0) + BlockToken.connect(addr2).transferFromAdd(addr1,owner_, 0) ).to.be.revertedWith("BlockToken:: Zero amount not supported"); }); @@ -222,7 +222,7 @@ describe("TransferFrom Token", () => { await BlockToken.connect(owner_).mint(1000, addr1); await BlockToken.connect(addr1).approveToken(addr2,5000) await expect( - BlockToken.connect(addr2).transferFromToken(addr1,owner_, 2000) + BlockToken.connect(addr2).transferFromAdd(addr1,owner_, 2000) ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); }); @@ -232,7 +232,7 @@ describe("TransferFrom Token", () => { await BlockToken.connect(owner_).mint(1000, addr1); await BlockToken.connect(addr1).approveToken(addr2,5000) await expect( - BlockToken.connect(addr2).transferFromToken(addr1, ZeroAddress, 500) + BlockToken.connect(addr2).transferFromAdd(addr1, ZeroAddress, 500) ).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidReceiver"); }); @@ -241,7 +241,7 @@ describe("TransferFrom Token", () => { await BlockToken.connect(owner_).mint(1000, addr1); await BlockToken.connect(addr1).approveToken(addr2,5000) - await BlockToken.connect(addr2).transferFromToken(addr1,addr2, 400); + await BlockToken.connect(addr2).transferFromAdd(addr1,addr2, 400); expect(await BlockToken.balanceOf(addr1)).to.eq(600); expect(await BlockToken.balanceOf(addr2)).to.eq(400); From 6eb6f866bb6421809b1bf8d12e45ef4152baaca0 Mon Sep 17 00:00:00 2001 From: Cletusgizo Date: Wed, 30 Jul 2025 02:34:51 +0100 Subject: [PATCH 6/6] Assignment Completed --- contracts/BlockHeaderToken.sol | 6 ++--- test/BlockToken.js | 40 +++++++++++++++++----------------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/contracts/BlockHeaderToken.sol b/contracts/BlockHeaderToken.sol index bbcf239..0744d82 100644 --- a/contracts/BlockHeaderToken.sol +++ b/contracts/BlockHeaderToken.sol @@ -33,15 +33,15 @@ contract BlockToken is ERC20{ _burn(_user, _amount); } - function transferToken(address _recepient, uint256 _amount) notAmount0(_amount) external { + function tokenTransfer(address _recepient, uint256 _amount) notAmount0(_amount) external { _transfer(msg.sender, _recepient, _amount); } - function transferFromAdd(address _sender, address _recepient, uint256 _amount) notAmount0(_amount) external { + function transferTokenFrom(address _sender, address _recepient, uint256 _amount) notAmount0(_amount) external { _transfer(_sender, _recepient, _amount); } - function approveToken(address spender, uint256 _amount) notAmount0(_amount) external{ + function tokenApprove(address spender, uint256 _amount) notAmount0(_amount) external{ _approve(msg.sender, spender, _amount); } diff --git a/test/BlockToken.js b/test/BlockToken.js index 5914421..28af895 100644 --- a/test/BlockToken.js +++ b/test/BlockToken.js @@ -127,7 +127,7 @@ describe("Approve token transfer", () =>{ await BlockToken.connect(owner_).mint(1000, addr1); await expect( - BlockToken.connect(addr1).approveToken(addr2, 0) + BlockToken.connect(addr1).tokenApprove(addr2, 0) ).to.be.revertedWith("BlockToken:: Zero amount not supported"); }); @@ -137,25 +137,25 @@ describe("Approve token transfer", () =>{ await BlockToken.connect(owner_).mint(500, addr1) await expect( - BlockToken.connect(addr1).approveToken(ZeroAddress, 50) + BlockToken.connect(addr1).tokenApprove(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).approveToken(addr2, 300); + await BlockToken.connect(addr1).tokenApprove(addr2, 300); expect(await BlockToken.allowance(addr1,addr2)).to.eq(300); }); }); - describe("Transfer Token ", () => { - describe("TransferToken Transaction", () => { + describe("Token Transfer ", () => { + describe("TOken transfer Transaction", () => { it("Should revert if from address doesn't have tokens", async () => { const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); // await BlockToken.connect(owner_).mint(1000, addr1); await expect( - BlockToken.connect(addr1).transferToken(addr2, 1000) + BlockToken.connect(addr1).tokenTransfer(addr2, 1000) ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); }); @@ -164,7 +164,7 @@ describe("Approve token transfer", () =>{ await BlockToken.connect(owner_).mint(1000, addr1); await expect( - BlockToken.connect(addr1).transferToken(addr2, 0) + BlockToken.connect(addr1).tokenTransfer(addr2, 0) ).to.be.revertedWith("BlockToken:: Zero amount not supported"); }); @@ -172,7 +172,7 @@ describe("Approve token transfer", () =>{ const { BlockToken, owner_, addr1,addr2 } = await loadFixture(deployBlockToken); await BlockToken.connect(owner_).mint(500, addr1); await expect( - BlockToken.connect(addr1).transferToken(addr2, 1000) + BlockToken.connect(addr1).tokenTransfer(addr2, 1000) ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); }); @@ -182,7 +182,7 @@ describe("Approve token transfer", () =>{ await BlockToken.connect(owner_).mint(500, addr1) await expect( - BlockToken.connect(addr1).transferToken(ZeroAddress, 50) + BlockToken.connect(addr1).tokenTransfer(ZeroAddress, 50) ).to.be.revertedWithCustomError(BlockToken, "ERC20InvalidReceiver"); }); @@ -191,7 +191,7 @@ describe("Approve token transfer", () =>{ await BlockToken.connect(owner_).mint(1000, addr1); expect(await BlockToken.balanceOf(addr1)).to.eq(1000); - await BlockToken.connect(addr1).transferToken(addr2, 300); + await BlockToken.connect(addr1).tokenTransfer(addr2, 300); expect(await BlockToken.balanceOf(addr1)).to.eq(700); expect(await BlockToken.balanceOf(addr2)).to.eq(300); @@ -202,27 +202,27 @@ describe("Approve token transfer", () =>{ describe("TransferFrom Token", () => { it("Should revert if owner address doesn't have tokens", async () => { const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); - await BlockToken.connect(addr1).approveToken(addr2,100); + await BlockToken.connect(addr1).tokenApprove(addr2,100); await expect( - BlockToken.connect(addr2).transferFromAdd(addr1,owner_,50) + BlockToken.connect(addr2).transferTokenFrom(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(owner_).mint(1000, addr1); - await BlockToken.connect(addr1).approveToken(addr2,500) + await BlockToken.connect(addr1).tokenApprove(addr2,500) await expect( - BlockToken.connect(addr2).transferFromAdd(addr1,owner_, 0) + BlockToken.connect(addr2).transferTokenFrom(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).approveToken(addr2,5000) + await BlockToken.connect(addr1).tokenApprove(addr2,5000) await expect( - BlockToken.connect(addr2).transferFromAdd(addr1,owner_, 2000) + BlockToken.connect(addr2).transferTokenFrom(addr1,owner_, 2000) ).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance"); }); @@ -230,18 +230,18 @@ describe("TransferFrom Token", () => { const { BlockToken, owner_, addr1, addr2 } = await loadFixture(deployBlockToken); let ZeroAddress = "0x0000000000000000000000000000000000000000"; await BlockToken.connect(owner_).mint(1000, addr1); - await BlockToken.connect(addr1).approveToken(addr2,5000) + await BlockToken.connect(addr1).tokenApprove(addr2,5000) await expect( - BlockToken.connect(addr2).transferFromAdd(addr1, ZeroAddress, 500) + BlockToken.connect(addr2).transferTokenFrom(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).approveToken(addr2,5000) + await BlockToken.connect(addr1).tokenApprove(addr2,5000) - await BlockToken.connect(addr2).transferFromAdd(addr1,addr2, 400); + await BlockToken.connect(addr2).transferTokenFrom(addr1,addr2, 400); expect(await BlockToken.balanceOf(addr1)).to.eq(600); expect(await BlockToken.balanceOf(addr2)).to.eq(400);