Skip to content

Task 1 #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: task-1
Choose a base branch
from
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
50 changes: 50 additions & 0 deletions contracts/simpleCounter.sol
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
3 changes: 1 addition & 2 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -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",
};
2 changes: 2 additions & 0 deletions submissions/assignment-1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Assignment 1
Here's a link to [assignment 1](../contracts/simpleCounter.sol)
50 changes: 50 additions & 0 deletions test/Counter.js
Original file line number Diff line number Diff line change
@@ -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);
});
});