Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions contracts/BlockToken.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

contract BlockToken is ERC20{

address public owner;

modifier onlyOwner {
require(msg.sender == owner, "BlockToken:: Unauthorized User");
_;
}

modifier notAmount0(uint256 _amount){
require(_amount != 0, "BlockToken:: Zero amount not supported");
_;
}
constructor(string memory _name, string memory _symbol, address _owner) ERC20(_name, _symbol){
require(_owner != address(0), "BlockToken:: Zero address not supported");
owner = _owner;
}

function mint(uint256 _amount, address _recepient) onlyOwner notAmount0(_amount) external {
_mint(_recepient, _amount);
}

function burn(uint256 _amount) notAmount0(_amount) external {
_burn(msg.sender, _amount);
}

function burnFrom(address _user, uint256 _amount)onlyOwner notAmount0(_amount) external {
_burn(_user, _amount);
}


}
48 changes: 0 additions & 48 deletions contracts/Counter.sol

This file was deleted.

63 changes: 63 additions & 0 deletions contracts/CounterV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;

import "../interfaces/ICounterV2.sol";

// interface ICounterV2{
// function setCount(uint count) external;
// function getCount() external returns (uint);
// function resetCount() external;
// function decreaseCount() external;
// }

// contract CounterV2 {
contract CounterV2 is ICounterV2{

uint count;
address owner;

constructor(){
owner = msg.sender;
}

// modifier onlyOwner() {
// require (owner == msg.sender, "unauthorized");
// _;
// }

function setCount(uint _count) public {
require(owner == msg.sender, "unauthorized");
require(_count<=10, "count should not be greater than 10");
count = _count;
}

function getCount() public view returns(uint){
return count;
}

function resetCount() public{
require(owner == msg.sender, "unauthorized");
count = 0;
}

function decreaseCount() public{
count -= 1;
}

function increaseCount() public{
count+=1;
}

}

// contract CounterV2Caller{
// ICounterV2 public icounterv2;

// constructor(address _counterV2address){
// icounterv2 = ICounterV2(_counterV2address);
// }

// function decreaseCount() public{
// icounterv2.decreaseCount();
// }
// }
14 changes: 14 additions & 0 deletions contracts/CounterV2Caller.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.28;
import "../interfaces/ICounterV2.sol";
contract CounterV2Caller{
ICounterV2 public icounterv2;

constructor(address _counterV2address){
icounterv2 = ICounterV2(_counterV2address);
}

function decreaseCount() public{
icounterv2.decreaseCount();
}
}
9 changes: 9 additions & 0 deletions interfaces/ICounterV2.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.28;

interface ICounterV2{
function setCount(uint count) external;
function getCount() external returns (uint);
function resetCount() external;
function decreaseCount() external;
}
9 changes: 9 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
"devDependencies": {
"@nomicfoundation/hardhat-toolbox": "^5.0.0",
"hardhat": "^2.26.1"
},
},
"scripts": {
"test": "npx hardhat test",
"compile": "npx hardhat compile",
"test": "npx hardhat test",
"compile": "npx hardhat compile",
"node": "npx hardhat node"
},
"dependencies": {
"@openzeppelin/contracts": "^5.4.0"
}
}
162 changes: 162 additions & 0 deletions test/BlockToken.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
const {
loadFixture,
} = require("@nomicfoundation/hardhat-toolbox/network-helpers");
// const { anyValue } = require("@nomicfoundation/hardhat-chai-matchers/withArgs");
const { expect } = require("chai");

// util functon
const deployBlockToken = async () => {
// target the BlockToken contract within our contract folder
let name_ = "BlockToken";
let symbol_ = "BCT";
const [owner_, addr1, addr2] = await ethers.getSigners();
const BlockTokenContract = await ethers.getContractFactory("BlockToken"); // target BlockToken.sol
const BlockToken = await BlockTokenContract.deploy(
name_,
symbol_,
owner_.address
); // deploy the BlockToken contract
return { BlockToken, owner_, addr1, addr2, name_, symbol_ }; // return the deployed instance of our BlockToken contract
};

// BlockToken Test Suite
describe("BlockToken Test Suite", () => {
describe("Deployment", () => {
it("Should return set values upon deployment", async () => {
const { BlockToken, name_, symbol_, owner_ } = await loadFixture(
deployBlockToken
);
expect(await BlockToken.name()).to.eq(name_);
expect(await BlockToken.symbol()).to.eq(symbol_);
expect(await BlockToken.owner()).to.eq(owner_);
});

it("Should revert if owner is zero address", async () => {
let ZeroAddress = "0x0000000000000000000000000000000000000000";
const BlockTokenContract = await ethers.getContractFactory("BlockToken");
await expect(
BlockTokenContract.deploy("hh", "tt", ZeroAddress)
).to.be.revertedWith("BlockToken:: Zero address not supported");
});
});

describe("Minting", () => {
it("Should allow onlyOwner Mint", async () => {
const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken);
// test owner mints successfully
await BlockToken.connect(owner_).mint(1000, addr1);
expect(await BlockToken.balanceOf(addr1)).to.eq(1000);

// test that another user cant call successfully
let malicioustxn = BlockToken.connect(addr1).mint(1000, addr1);
await expect(malicioustxn).to.be.revertedWith(
"BlockToken:: Unauthorized User"
);
});

it("Should revert if minting amount is zero", async () => {
const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken);
await expect(
BlockToken.connect(owner_).mint(0, addr1)
).to.be.revertedWith("BlockToken:: Zero amount not supported");
});
});

describe("Burning", () => {
it("Should not burn if user doesn't have tokens", async () => {
const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken);
await expect(
BlockToken.connect(addr1).burn(1000)
).to.be.revertedWithCustomError(BlockToken, "ERC20InsufficientBalance");
});

it("Should Burn Tokens Successfully", async () => {
const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken);
await BlockToken.connect(owner_).mint(1000, owner_);
expect(await BlockToken.balanceOf(owner_)).to.eq(1000);

await BlockToken.connect(owner_).burn(100);
expect(await BlockToken.balanceOf(owner_)).to.eq(900);
});
});

describe("Burning From", () => {
it("Should only allow owner", async () => {
const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken);
await BlockToken.connect(owner_).mint(10000, addr1);
// expect(await BlockToken.balanceOf(addr1)).to.eq(10000);
const authorizedUser = await BlockToken.connect(owner_).burnFrom(
addr1,
1000
);
expect(await BlockToken.balanceOf(addr1)).to.eq(9000);
const unauthorizedUser = BlockToken.connect(addr1).burnFrom(addr1, 1000);
await expect(unauthorizedUser).to.be.revertedWith(
"BlockToken:: Unauthorized User"
);
});
it("Should not send zero", async () => {
const { BlockToken, addr1, owner_ } = await loadFixture(deployBlockToken);
await expect(
BlockToken.connect(owner_).burnFrom(addr1, 0)
).to.be.revertedWith("BlockToken:: Zero amount not supported");
});
});

describe("Transfer", () => {
it("should be called by owner", async () => {
const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken);
await BlockToken.connect(owner_).mint(10000, owner_);
await BlockToken.connect(owner_).transfer(addr1, 1000);
const addrBalance = await BlockToken.balanceOf(addr1);
const ownerBalance = await BlockToken.balanceOf(owner_);

expect(addrBalance).to.eq(1000);
expect(ownerBalance).to.eq(9000);
});
it("amount to be sent should be less than the balance of the sender", async () => {
const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken);
await BlockToken.connect(owner_).mint(10000, owner_);
const ownerBalance = await BlockToken.balanceOf(owner_);
expect(ownerBalance).to.be.greaterThanOrEqual(1000);
await BlockToken.connect(owner_).transfer(addr1, 1000);
const addrBalance = await BlockToken.balanceOf(addr1);
const ownerBalanceAfter = await BlockToken.balanceOf(owner_);

expect(addrBalance).to.eq(1000);
expect(ownerBalanceAfter).to.eq(9000);
});
it("should revert if amount to be sent is greter than the amount the sender has", async () => {
const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken);
await expect(BlockToken.connect(owner_).transfer(addr1, 10000)).to.be
.reverted;
});
it("address 0 should not be the receiver", async () => {
const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken);
let ZeroAddress = "0x0000000000000000000000000000000000000000";
await expect(BlockToken.connect(owner_).transfer(ZeroAddress, 1000)).to.be
.reverted;
});
});

describe("Approve", () => {
it("address zero should not be the receiver", async () => {
const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken);
let ZeroAddress = "0x0000000000000000000000000000000000000000";
await expect(BlockToken.connect(owner_).approve(ZeroAddress, 1000)).to.be
.reverted;
});
it("should send moeny to allowances", async () => {
const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken);
await BlockToken.connect(owner_).approve(addr1, 1000);
const allowance = await BlockToken.allowance(owner_, addr1);

expect(allowance).to.eq(1000);
});
// it("address zero should not be the sender", async () => {
// const { BlockToken, owner_, addr1 } = await loadFixture(deployBlockToken);
// let ZeroAddress = "0x0000000000000000000000000000000000000000";
// await expect(BlockToken.connect(ZeroAddress)).to.be.reverted;
// });
});
});
Loading