Skip to content
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

Add a Counter.reset function #2678

Merged
merged 3 commits into from
May 19, 2021
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* `ERC20Votes`: add a new extension of the `ERC20` token with support for voting snapshots and delegation. This extension is compatible with Compound's `Comp` token interface. ([#2632](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2632))
* Enumerables: Improve gas cost of removal in `EnumerableSet` and `EnumerableMap`.
* Enumerables: Improve gas cost of lookup in `EnumerableSet` and `EnumerableMap`.
* `Counter`: add a reset method. ([#2678](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2678))

## 4.1.0 (2021-04-29)

Expand Down
4 changes: 4 additions & 0 deletions contracts/mocks/CountersImpl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ contract CountersImpl {
function decrement() public {
_counter.decrement();
}

function reset() public {
_counter.reset();
}
}
6 changes: 5 additions & 1 deletion contracts/utils/Counters.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pragma solidity ^0.8.0;
/**
* @title Counters
* @author Matt Condon (@shrugs)
* @dev Provides counters that can only be incremented or decremented by one. This can be used e.g. to track the number
* @dev Provides counters that can only be incremented, decremented or reset. This can be used e.g. to track the number
* of elements in a mapping, issuing ERC721 ids, or counting request ids.
*
* Include with `using Counters for Counters.Counter;`
Expand Down Expand Up @@ -35,4 +35,8 @@ library Counters {
counter._value = value - 1;
}
}

function reset(Counter storage counter) internal {
counter._value = 0;
}
}
2 changes: 1 addition & 1 deletion contracts/utils/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The {Address}, {Arrays} and {Strings} libraries provide more operations related

For new data types:

* {Counters}: a simple way to get a counter that can only be incremented or decremented. Very useful for ID generation, counting contract activity, among others.
* {Counters}: a simple way to get a counter that can only be incremented, decremented or reset. Very useful for ID generation, counting contract activity, among others.
* {EnumerableMap}: like Solidity's https://solidity.readthedocs.io/en/latest/types.html#mapping-types[`mapping`] type, but with key-value _enumeration_: this will let you know how many entries a mapping has, and iterate over them (which is not possible with `mapping`).
* {EnumerableSet}: like {EnumerableMap}, but for https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets]. Can be used to store privileged accounts, issued IDs, etc.

Expand Down
20 changes: 20 additions & 0 deletions test/utils/Counters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,24 @@ contract('Counters', function (accounts) {
});
});
});

describe('reset', function () {
context('null counter', function () {
it('does not throw', async function () {
await this.counter.reset();
expect(await this.counter.current()).to.be.bignumber.equal('0');
});
});

context('non null counter', function () {
beforeEach(async function () {
await this.counter.increment();
expect(await this.counter.current()).to.be.bignumber.equal('1');
});
it('reset to 0', async function () {
await this.counter.reset();
expect(await this.counter.current()).to.be.bignumber.equal('0');
});
});
});
});