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

Remove utils/Counters.sol #4289

Merged
merged 5 commits into from
May 31, 2023
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
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 {
frangio marked this conversation as resolved.
Show resolved Hide resolved
// 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