diff --git a/assignments/fatiudeen/Counter.sol b/assignments/fatiudeen/Counter.sol new file mode 100644 index 00000000..86742549 --- /dev/null +++ b/assignments/fatiudeen/Counter.sol @@ -0,0 +1,79 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.24; + +/// @title Counter Contract +/// @notice A simple contract that implements a counter with increase, decrease and reset functionality +contract Counter { + /// @notice The current value of the counter + uint public count; + + /// @notice Emitted when the counter value is increased + /// @param amount The new value of the counter + /// @param when The timestamp when the increase occurred + event CountIncreased(uint amount, uint when); + + /// @notice Emitted when the counter value is decreased + /// @param amount The new value of the counter + /// @param when The timestamp when the decrease occurred + event CountDecreased(uint amount, uint when); + + /// @notice Increases the counter by one + /// @dev Reverts if the operation would cause an overflow + function increaseByOne() public { + require(count < type(uint).max, "cannot increase beyond max uint"); + count += 1; + emit CountIncreased(count, block.timestamp); + } + + /// @notice Increases the counter by a specified value + /// @param _value The amount to increase the counter by + /// @dev Reverts if the operation would cause an overflow + function increaseByValue(uint _value) public { + require(count + _value <= type(uint).max, "cannot increase beyond max uint"); + count += _value; + emit CountIncreased(count, block.timestamp); + } + + /// @notice Decreases the counter by one + /// @dev Reverts if the operation would cause an underflow + function decreaseByOne() public { + require(count > 0, "cannot decrease below 0"); + count -= 1; + emit CountDecreased(count, block.timestamp); + } + + /// @notice Decreases the counter by a specified value + /// @param _value The amount to decrease the counter by + /// @dev Reverts if the operation would cause an underflow + function decreaseByValue(uint _value) public { + require(count >= _value, "cannot decrease below 0"); + count -= _value; + emit CountDecreased(count, block.timestamp); + } + + /// @notice Resets the counter to zero + function resetCount() public { + uint oldCount = count; + if (oldCount != 0) { + count = 0; + emit CountDecreased(count, block.timestamp); + } + } + + /// @notice Returns the current value of the counter + /// @return The current counter value + function getCount() public view returns (uint) { + return count; + } + + /// @notice Returns the maximum value of uint256 + /// @return The maximum value that can be stored in a uint256 + /// @dev Uses unchecked arithmetic to intentionally underflow and get max uint + function getMaxUint256() public pure returns (uint) { + uint max; + unchecked { + max = 0 - 1; + } + return max; + } +} diff --git a/submissions/Assignment3/README.md b/submissions/Assignment3/README.md deleted file mode 100644 index 6f3a09c7..00000000 --- a/submissions/Assignment3/README.md +++ /dev/null @@ -1,3 +0,0 @@ -# Submissions for Assignment 3 ---- ---- \ No newline at end of file diff --git a/submissions/assignment-1.md b/submissions/assignment-1.md new file mode 100644 index 00000000..11dbdfbd --- /dev/null +++ b/submissions/assignment-1.md @@ -0,0 +1,12 @@ +# Assignment 1 Submission + +The implementation of the Counter Contract can be found at: +[Counter.sol](../../../assignments/fatiudeen/Counter.sol) + +This implementation includes: +- Complete counter functionality with increase/decrease operations +- Overflow and underflow protection +- Event emission for all counter changes +- Comprehensive error handling +- Clear NatSpec documentation +- Pure function for max uint256 calculation