-
Notifications
You must be signed in to change notification settings - Fork 11.8k
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 Counters.sol
#4233
Comments
All in! Just wanted to add gas efficiency as another good reason to remove it. |
@QuentinMerabet Can you tell more about the gas inefficiency you noticed? Is that related to storage packing (counter could be smaller than uint256) ? |
Please provide an example on how to migrate Counters to the new way to achieve its functionality |
@Garito, if we remove it we'll provide a guide for migrating to 5.0. Curious to know, would you like the migration example to be in another contract abstraction or to be a guide to implement it with raw solidity? We're removing it because we have the feeling that multiple people in the community feel it like an unnecessary abstraction, so having your input on what you expect as a migration guide will help us understand better how to approach this removal. |
Let us consider an example of creating an ERC721 contract and making use of Counters library.
Here we see that Counters library is particularly used to get
|
@balajipachai Without Counters the code would be as follows: contract MyToken is ERC721, Pausable, Ownable {
uint256 private _tokenIdCounter;
function safeMint(address receiver) public onlyOwner {
uint256 tokenId = _tokenIdCounter;
_safeMint(_receiver, tokenId);
_tokenIdCounter += 1;
}
} Alternatively, simply |
Giving this examples: what was the point of Counters then? |
Got it, let me make the necessary changes wherever Counters was used, update the tests and submit a PR for the same. |
The point of Counters was to provide an "object" that was guaranteed to behave in a way that will not overflow, therefore can be safely incremented in an "unchecked" way. It was introduced before Solidity had native checked arithmetic, so it made slightly more sense back then. There are a few issues though. This "guarantee" was always a soft guarantee, because the inner struct members are directly modifiable bypassing the struct interface. Additionally, a Counter being a uint256 value occupies an entire storage slot whereas in most occasions packing a counter in storage with other values will be a far larger optimization than removing overflow checks; the significance of this has also changed since the introduction of Counters due to changes in the EVM gas prices. |
You're looking at 4.x docs, notice the URL. 5.x is here: https://docs.openzeppelin.com/contracts/5.x/ |
no wonder i couldn't resolve
no matter how many times i reinstalled @openzeppelin/contracts, it simply doesn't exist since version 5.0.0 anymore, which consequently makes this ethereum guide deprecated because its MyNFT.sol code is:
so i tweeted the guide's last editor to suggest an update on it |
|
Noted. Thanks for letting me know! :) Btw - I think this dropdown can be improved - tried pressing 'Current version' probably 3 or 4 times before realizing Version 5 is the current version. I think the first dropdown should instead look like this (not current version implicitly being older version) Version 5 (Current version) etc. |
Still, there are huge of guides with Counters.sol. |
@otarasovqs You can simply use a simple uint256 variable and increment or decrement it.
Note that even though we usually put ++ and -- after the variable, they're slightly more gas efficient when placed before the variable. And of course...
|
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol"; i have these imports but only the Counters import is not accepted |
Same to me, I have imported the that but in openzeppelin file it does not have any counter.sol file. please help with that anyone. |
any luck here |
for anyone having error with |
use
it'll work but the older versions have some vulnerabilities |
🧐 Motivation
The
Counters.sol
contract initially served as an abstraction for a number that guarantees two main properties:Also, it allows to use
unchecked
arithmetic assuming no overflowHowever, its usage has been reduced across OpenZeppelin Contracts and there are some relevant reasons to remove it, such as:
Counters.sol
contract (eg. User Defined Types)📝 Details
We're proposing removing it for Contracts 5.0, but we want to hear comments against removing it (if any), so we can have a clear view of what to do in the future. An alternative we're strongly considering is removing it for 5.0 but re-adding in the future some variation if people consider it useful and it makes sense.
The text was updated successfully, but these errors were encountered: