Skip to content

Commit

Permalink
Remove utils/Counters.sol (#4289)
Browse files Browse the repository at this point in the history
Co-authored-by: Francisco Giordano <fg@frang.io>
  • Loading branch information
Amxx and frangio authored May 31, 2023
1 parent 4c713f8 commit 2ee1da1
Show file tree
Hide file tree
Showing 9 changed files with 20 additions and 156 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

### Removals

The following contracts were removed:
The following contracts and libraries were removed:

- `Counters`
- `ERC20Snapshot`
- `ERC20VotesComp`
- `GovernorVotesComp`
Expand Down
7 changes: 3 additions & 4 deletions contracts/mocks/proxy/UUPSUpgradeableMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
pragma solidity ^0.8.19;

import "../../proxy/utils/UUPSUpgradeable.sol";
import "../../utils/Counters.sol";

contract NonUpgradeableMock {
Counters.Counter internal _counter;
uint256 internal _counter;

function current() external view returns (uint256) {
return Counters.current(_counter);
return _counter;
}

function increment() external {
return Counters.increment(_counter);
++_counter;
}
}

Expand Down
43 changes: 0 additions & 43 deletions contracts/utils/Counters.sol

This file was deleted.

19 changes: 9 additions & 10 deletions contracts/utils/Nonces.sol
Original file line number Diff line number Diff line change
@@ -1,31 +1,30 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;

import "./Counters.sol";

/**
* @dev Provides tracking nonces for addresses. Nonces will only increment.
*/
abstract contract Nonces {
using Counters for Counters.Counter;

mapping(address => Counters.Counter) private _nonces;
mapping(address => uint256) private _nonces;

/**
* @dev Returns an address nonce.
*/
function nonces(address owner) public view virtual returns (uint256) {
return _nonces[owner].current();
return _nonces[owner];
}

/**
* @dev Consumes a nonce.
*
* Returns the current value and increments nonce.
*/
function _useNonce(address owner) internal virtual returns (uint256 current) {
Counters.Counter storage nonce = _nonces[owner];
current = nonce.current();
nonce.increment();
function _useNonce(address owner) internal virtual returns (uint256) {
// For each account, the nonce has an initial value of 0, can only be incremented by one, and cannot be
// decremented or reset. This guarantees that the nonce never overflows.
unchecked {
// It is important to do x++ and not ++x here.
return _nonces[owner]++;
}
}
}
3 changes: 0 additions & 3 deletions contracts/utils/README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ The {Address}, {Arrays}, {Base64} and {Strings} libraries provide more operation

For new data types:

* {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 Expand Up @@ -75,8 +74,6 @@ Ethereum contracts have no native concept of an interface, so applications must

{{Base64}}

{{Counters}}

{{Strings}}

{{ShortStrings}}
Expand Down
13 changes: 5 additions & 8 deletions docs/modules/ROOT/pages/erc721.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,21 @@ Here's what a contract for tokenized items might look like:
pragma solidity ^0.8.19;
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import "@openzeppelin/contracts/utils/Counters.sol";
contract GameItem is ERC721URIStorage {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
uint256 private _nextTokenId;
constructor() ERC721("GameItem", "ITM") {}
function awardItem(address player, string memory tokenURI)
public
returns (uint256)
{
uint256 newItemId = _tokenIds.current();
_mint(player, newItemId);
_setTokenURI(newItemId, tokenURI);
uint256 tokenId = _nextTokenId++;
_mint(player, tokenId);
_setTokenURI(tokenId, tokenURI);
_tokenIds.increment();
return newItemId;
return tokenId;
}
}
----
Expand Down
2 changes: 0 additions & 2 deletions docs/modules/ROOT/pages/utilities.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,6 @@ If you need support for more powerful collections than Solidity's native arrays
[[misc]]
== Misc

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].

=== Base64

xref:api:utils.adoc#Base64[`Base64`] util allows you to transform `bytes32` data into its Base64 `string` representation.
Expand Down
84 changes: 0 additions & 84 deletions test/utils/Counters.test.js

This file was deleted.

2 changes: 1 addition & 1 deletion test/utils/Nonces.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ contract('Nonces', function (accounts) {
expect(await this.nonces.nonces(sender)).to.be.bignumber.equal('0');

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

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

0 comments on commit 2ee1da1

Please sign in to comment.