-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounterContract.sol
33 lines (26 loc) · 903 Bytes
/
CounterContract.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.2 <0.9.0;
contract CounterContract {
uint256 private _count;
// Events
event EmitResults(uint256 oldValue, uint256 newValue);
constructor() {}
// Increment function
function increment() public {
_count = _updateCount(_count, true);
emit EmitResults(_count - 1, _count);
}
// Decrement function
function decrement() public {
require(_count > 0, "Counter: cannot decrement below 0");
_count = _updateCount(_count, false);
emit EmitResults(_count + 1, _count);
}
function getCount() public view returns (uint256) {
return _count;
}
// Private function to update the count
function _updateCount(uint256 currentCount, bool isIncrement) private pure returns (uint256) {
return isIncrement ? currentCount + 1 : currentCount - 1;
}
}