-
Notifications
You must be signed in to change notification settings - Fork 29
counter contract #16
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
fatiudeen
wants to merge
2
commits into
BlockheaderWeb3-Community:task-1
Choose a base branch
from
fatiudeen:assignment-1
base: task-1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
counter contract #16
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are not supposed to delete this file