Skip to content

Commit 2ee1da1

Browse files
Amxxfrangio
andauthored
Remove utils/Counters.sol (#4289)
Co-authored-by: Francisco Giordano <[email protected]>
1 parent 4c713f8 commit 2ee1da1

File tree

9 files changed

+20
-156
lines changed

9 files changed

+20
-156
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
### Removals
44

5-
The following contracts were removed:
5+
The following contracts and libraries were removed:
66

7+
- `Counters`
78
- `ERC20Snapshot`
89
- `ERC20VotesComp`
910
- `GovernorVotesComp`

contracts/mocks/proxy/UUPSUpgradeableMock.sol

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@
33
pragma solidity ^0.8.19;
44

55
import "../../proxy/utils/UUPSUpgradeable.sol";
6-
import "../../utils/Counters.sol";
76

87
contract NonUpgradeableMock {
9-
Counters.Counter internal _counter;
8+
uint256 internal _counter;
109

1110
function current() external view returns (uint256) {
12-
return Counters.current(_counter);
11+
return _counter;
1312
}
1413

1514
function increment() external {
16-
return Counters.increment(_counter);
15+
++_counter;
1716
}
1817
}
1918

contracts/utils/Counters.sol

Lines changed: 0 additions & 43 deletions
This file was deleted.

contracts/utils/Nonces.sol

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity ^0.8.19;
33

4-
import "./Counters.sol";
5-
64
/**
75
* @dev Provides tracking nonces for addresses. Nonces will only increment.
86
*/
97
abstract contract Nonces {
10-
using Counters for Counters.Counter;
11-
12-
mapping(address => Counters.Counter) private _nonces;
8+
mapping(address => uint256) private _nonces;
139

1410
/**
1511
* @dev Returns an address nonce.
1612
*/
1713
function nonces(address owner) public view virtual returns (uint256) {
18-
return _nonces[owner].current();
14+
return _nonces[owner];
1915
}
2016

2117
/**
2218
* @dev Consumes a nonce.
2319
*
2420
* Returns the current value and increments nonce.
2521
*/
26-
function _useNonce(address owner) internal virtual returns (uint256 current) {
27-
Counters.Counter storage nonce = _nonces[owner];
28-
current = nonce.current();
29-
nonce.increment();
22+
function _useNonce(address owner) internal virtual returns (uint256) {
23+
// For each account, the nonce has an initial value of 0, can only be incremented by one, and cannot be
24+
// decremented or reset. This guarantees that the nonce never overflows.
25+
unchecked {
26+
// It is important to do x++ and not ++x here.
27+
return _nonces[owner]++;
28+
}
3029
}
3130
}

contracts/utils/README.adoc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ The {Address}, {Arrays}, {Base64} and {Strings} libraries provide more operation
1010

1111
For new data types:
1212

13-
* {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.
1413
* {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`).
1514
* {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.
1615

@@ -75,8 +74,6 @@ Ethereum contracts have no native concept of an interface, so applications must
7574

7675
{{Base64}}
7776

78-
{{Counters}}
79-
8077
{{Strings}}
8178

8279
{{ShortStrings}}

docs/modules/ROOT/pages/erc721.adoc

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,21 @@ Here's what a contract for tokenized items might look like:
1717
pragma solidity ^0.8.19;
1818
1919
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
20-
import "@openzeppelin/contracts/utils/Counters.sol";
2120
2221
contract GameItem is ERC721URIStorage {
23-
using Counters for Counters.Counter;
24-
Counters.Counter private _tokenIds;
22+
uint256 private _nextTokenId;
2523
2624
constructor() ERC721("GameItem", "ITM") {}
2725
2826
function awardItem(address player, string memory tokenURI)
2927
public
3028
returns (uint256)
3129
{
32-
uint256 newItemId = _tokenIds.current();
33-
_mint(player, newItemId);
34-
_setTokenURI(newItemId, tokenURI);
30+
uint256 tokenId = _nextTokenId++;
31+
_mint(player, tokenId);
32+
_setTokenURI(tokenId, tokenURI);
3533
36-
_tokenIds.increment();
37-
return newItemId;
34+
return tokenId;
3835
}
3936
}
4037
----

docs/modules/ROOT/pages/utilities.adoc

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ If you need support for more powerful collections than Solidity's native arrays
9797
[[misc]]
9898
== Misc
9999

100-
Want to keep track of some numbers that increment by 1 every time you want another one? Check out xref:api:utils.adoc#Counters[`Counters`]. This is useful for lots of things, like creating incremental identifiers, as shown on the xref:erc721.adoc[ERC721 guide].
101-
102100
=== Base64
103101

104102
xref:api:utils.adoc#Base64[`Base64`] util allows you to transform `bytes32` data into its Base64 `string` representation.

test/utils/Counters.test.js

Lines changed: 0 additions & 84 deletions
This file was deleted.

test/utils/Nonces.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ contract('Nonces', function (accounts) {
1919
expect(await this.nonces.nonces(sender)).to.be.bignumber.equal('0');
2020

2121
const { receipt } = await this.nonces.$_useNonce(sender);
22-
expectEvent(receipt, 'return$_useNonce', { current: '0' });
22+
expectEvent(receipt, 'return$_useNonce', ['0']);
2323

2424
expect(await this.nonces.nonces(sender)).to.be.bignumber.equal('1');
2525
});

0 commit comments

Comments
 (0)