diff --git a/contracts/simpleCounter.sol b/contracts/simpleCounter.sol new file mode 100644 index 00000000..dee6b314 --- /dev/null +++ b/contracts/simpleCounter.sol @@ -0,0 +1,50 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.24; + +contract Counter { + uint public count; + + event CountIncreased(uint amount, uint when); + event CountDecreased(uint amount, uint when); + + function increaseByOne() public { + require(count < type(uint).max, "cannot increase beyond max uint"); + count++; + emit CountIncreased(count, block.timestamp); + } + + function increaseByValue(uint _value) public { + require(count + _value >= count, "cannot increase beyond max uint"); + count += _value; + emit CountIncreased(count, block.timestamp); + } + + function decreaseByOne() public { + require(count > 0, "cannot decrease below 0"); + count--; + emit CountDecreased(count, block.timestamp); + } + + function decreaseByValue(uint _value) public { + require(count >= _value, "cannot decrease below 0"); + count -= _value; + emit CountDecreased(count, block.timestamp); + } + + function resetCount() public { + count = 0; + emit CountDecreased(count, block.timestamp); + } + + function getCount() public view returns (uint) { + return count; + } + + function getMaxUint256() public pure returns (uint) { + unchecked { + uint max = 0; + max--; // underflows to the maximum uint256 value + return max; + } + } +} diff --git a/hardhat.config.js b/hardhat.config.js index 26528f0e..b63f0c2f 100644 --- a/hardhat.config.js +++ b/hardhat.config.js @@ -1,7 +1,6 @@ require("@nomicfoundation/hardhat-toolbox"); -require("@nomicfoundation/hardhat-ignition-ethers"); /** @type import('hardhat/config').HardhatUserConfig */ module.exports = { - solidity: "0.8.25", + solidity: "0.8.28", }; diff --git a/submissions/assignment-1.md b/submissions/assignment-1.md new file mode 100644 index 00000000..cfee1be3 --- /dev/null +++ b/submissions/assignment-1.md @@ -0,0 +1,2 @@ +# Assignment 1 +Here's a link to [assignment 1](../contracts/simpleCounter.sol) \ No newline at end of file diff --git a/test/Counter.js b/test/Counter.js new file mode 100644 index 00000000..70dd8002 --- /dev/null +++ b/test/Counter.js @@ -0,0 +1,50 @@ +const { expect } = require("chai"); +const { ethers } = require("hardhat"); + + +describe("Counter Contract", function () { + let Counter, counter, owner, addr1; + + beforeEach(async function () { + [owner, addr1] = await ethers.getSigners(); + Counter = await ethers.getContractFactory("Counter"); + counter = await Counter.deploy(); + await counter.waitForDeployment(); + }); + + it("should start with a count of 0", async function () { + expect(await counter.getCount()).to.equal(0); + }); + + it("should increase count by one", async function () { + await counter.increaseByOne(); + expect(await counter.getCount()).to.equal(1); + }); + + it("should increase count by a specified value", async function () { + await counter.increaseByValue(10); + expect(await counter.getCount()).to.equal(10); + }); + + it("should decrease count by one", async function () { + await counter.increaseByOne(); + await counter.decreaseByOne(); + expect(await counter.getCount()).to.equal(0); + }); + + it("should decrease count by a specified value", async function () { + await counter.increaseByValue(20); + await counter.decreaseByValue(5); + expect(await counter.getCount()).to.equal(15); + }); + + it("should not decrease count below zero", async function () { + await expect(counter.decreaseByOne()).to.be.revertedWith("cannot decrease below 0"); + }); + + it("should reset count to zero", async function () { + await counter.increaseByValue(50); + await counter.resetCount(); + expect(await counter.getCount()).to.equal(0); + }); +}); \ No newline at end of file