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/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..ff62fa5 --- /dev/null +++ b/contracts/CounterV2Caller.sol @@ -0,0 +1,33 @@ +// 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){ + 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/BlockToken.js b/test/BlockToken.js new file mode 100644 index 0000000..fcc1189 --- /dev/null +++ b/test/BlockToken.js @@ -0,0 +1,257 @@ +const { loadFixture } = require("@nomicfoundation/hardhat-toolbox/network-helpers"); +const { expect } = require("chai"); + +// Utility function to deploy BlockToken +const deployBlockToken = async () => { + 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.equal(name_); + expect(await BlockToken.symbol()).to.equal(symbol_); + expect(await BlockToken.owner()).to.equal(owner_); + }); + + 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("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"); + }); + + 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 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", () => { + 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("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"); + }); + + 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") + }) + }); + }); + + 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"); + }); + 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", () => { + 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, 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 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 99b2931..7ef17f9 100644 --- a/test/Counter.js +++ b/test/Counter.js @@ -1,49 +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); - expect(await counter.count()).to.eq(0); // assert that count = 0 upon deployment - }) - }) + 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); // 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 - }) + 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 () => { - - }) - }) + 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 increaseCountByOne value", async () => { - - }) + 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); - it("Should set appropriate values for multiple increaseCountByOne txns", async () => { - - }) - }) - }) -}) \ No newline at end of file + // 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 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 diff --git a/test/CounterV2Caller.js b/test/CounterV2Caller.js new file mode 100644 index 0000000..990c4c7 --- /dev/null +++ b/test/CounterV2Caller.js @@ -0,0 +1,38 @@ +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