-
Notifications
You must be signed in to change notification settings - Fork 25
More efficient enumerable #129
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
contracts/src/ERC721EthscriptionsEnumerableUpgradeable.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity 0.8.24; | ||
|
|
||
| import "./ERC721EthscriptionsUpgradeable.sol"; | ||
| import {IERC721Enumerable} from "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol"; | ||
| import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol"; | ||
|
|
||
| /** | ||
| * @dev Enumerable mixin for collections that require general token ID tracking and burns. | ||
| */ | ||
| abstract contract ERC721EthscriptionsEnumerableUpgradeable is ERC721EthscriptionsUpgradeable, IERC721Enumerable { | ||
| /// @inheritdoc ERC165Upgradeable | ||
| function supportsInterface(bytes4 interfaceId) | ||
| public | ||
| view | ||
| virtual | ||
| override(ERC721EthscriptionsUpgradeable, IERC165) | ||
| returns (bool) | ||
| { | ||
| return interfaceId == type(IERC721Enumerable).interfaceId || super.supportsInterface(interfaceId); | ||
| } | ||
|
|
||
| /// @inheritdoc IERC721Enumerable | ||
| function tokenOfOwnerByIndex(address owner, uint256 index) | ||
| public | ||
| view | ||
| virtual | ||
| override(IERC721Enumerable, ERC721EthscriptionsUpgradeable) | ||
| returns (uint256) | ||
| { | ||
| return super.tokenOfOwnerByIndex(owner, index); | ||
| } | ||
|
|
||
| /// @inheritdoc IERC721Enumerable | ||
| function totalSupply() public view virtual override returns (uint256) { | ||
| ERC721EnumerableStorage storage $ = _getERC721EnumerableStorage(); | ||
| return $._allTokens.length; | ||
| } | ||
|
|
||
| /// @inheritdoc IERC721Enumerable | ||
| function tokenByIndex(uint256 index) public view virtual override returns (uint256) { | ||
| ERC721EnumerableStorage storage $ = _getERC721EnumerableStorage(); | ||
| if (index >= $._allTokens.length) { | ||
| revert ERC721OutOfBoundsIndex(address(0), index); | ||
| } | ||
| return $._allTokens[index]; | ||
| } | ||
|
|
||
| function _afterTokenMint(uint256 tokenId) internal virtual override { | ||
| ERC721EnumerableStorage storage $ = _getERC721EnumerableStorage(); | ||
| $._allTokensIndex[tokenId] = $._allTokens.length; | ||
| $._allTokens.push(tokenId); | ||
| } | ||
|
|
||
| function _beforeTokenRemoval(uint256 tokenId, address) internal virtual override { | ||
| ERC721EnumerableStorage storage $ = _getERC721EnumerableStorage(); | ||
| uint256 tokenIndex = $._allTokensIndex[tokenId]; | ||
| uint256 lastTokenIndex = $._allTokens.length - 1; | ||
| uint256 lastTokenId = $._allTokens[lastTokenIndex]; | ||
|
|
||
| $._allTokens[tokenIndex] = lastTokenId; | ||
| $._allTokensIndex[lastTokenId] = tokenIndex; | ||
|
|
||
| delete $._allTokensIndex[tokenId]; | ||
| $._allTokens.pop(); | ||
| } | ||
| } |
88 changes: 88 additions & 0 deletions
88
contracts/src/ERC721EthscriptionsSequentialEnumerableUpgradeable.sol
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| // SPDX-License-Identifier: MIT | ||
| pragma solidity 0.8.24; | ||
|
|
||
| import "./ERC721EthscriptionsUpgradeable.sol"; | ||
| import {IERC721Enumerable} from "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol"; | ||
| import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol"; | ||
|
|
||
| /** | ||
| * @dev Enumerable mixin for Ethscriptions-style collections where token IDs are | ||
| * sequential, start at zero, and tokens are never burned. | ||
| */ | ||
| abstract contract ERC721EthscriptionsSequentialEnumerableUpgradeable is ERC721EthscriptionsUpgradeable, IERC721Enumerable { | ||
| /// @dev Raised when a mint attempts to skip or reuse a token ID. | ||
| error ERC721SequentialEnumerableInvalidTokenId(uint256 expected, uint256 actual); | ||
| /// @dev Raised if a contract attempts to remove a token from supply. | ||
| error ERC721SequentialEnumerableTokenRemoval(uint256 tokenId); | ||
|
|
||
| /// @custom:storage-location erc7201:ethscriptions.storage.ERC721SequentialEnumerable | ||
| struct ERC721SequentialEnumerableStorage { | ||
| uint256 _mintCount; | ||
| } | ||
|
|
||
| // keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.ERC721SequentialEnumerableStorageLocation")) - 1)) & ~bytes32(uint256(0xff)) | ||
| bytes32 private constant ERC721SequentialEnumerableStorageLocation = 0x154e8d00bf5f00755eebdfa0d432d05cad242742a46a00bbdb15798f33342700; | ||
|
|
||
| function _getERC721SequentialEnumerableStorage() | ||
| private | ||
| pure | ||
| returns (ERC721SequentialEnumerableStorage storage $) | ||
| { | ||
| assembly { | ||
| $.slot := ERC721SequentialEnumerableStorageLocation | ||
| } | ||
| } | ||
|
|
||
| /// @inheritdoc ERC165Upgradeable | ||
| function supportsInterface(bytes4 interfaceId) | ||
| public | ||
| view | ||
| virtual | ||
| override(ERC721EthscriptionsUpgradeable, IERC165) | ||
| returns (bool) | ||
| { | ||
| return interfaceId == type(IERC721Enumerable).interfaceId || super.supportsInterface(interfaceId); | ||
| } | ||
|
|
||
| /// @inheritdoc IERC721Enumerable | ||
| function tokenOfOwnerByIndex(address owner, uint256 index) | ||
| public | ||
| view | ||
| virtual | ||
| override(IERC721Enumerable, ERC721EthscriptionsUpgradeable) | ||
| returns (uint256) | ||
| { | ||
| return super.tokenOfOwnerByIndex(owner, index); | ||
| } | ||
|
|
||
| /// @inheritdoc IERC721Enumerable | ||
| function totalSupply() public view virtual override returns (uint256) { | ||
| ERC721SequentialEnumerableStorage storage $ = _getERC721SequentialEnumerableStorage(); | ||
| return $._mintCount; | ||
| } | ||
|
|
||
| /// @inheritdoc IERC721Enumerable | ||
| function tokenByIndex(uint256 index) public view virtual override returns (uint256) { | ||
| if (index >= totalSupply()) { | ||
| revert ERC721OutOfBoundsIndex(address(0), index); | ||
| } | ||
| return index; | ||
| } | ||
|
|
||
| function _afterTokenMint(uint256 tokenId) internal virtual override { | ||
| ERC721SequentialEnumerableStorage storage $ = _getERC721SequentialEnumerableStorage(); | ||
|
|
||
| uint256 expectedId = $._mintCount; | ||
| if (tokenId != expectedId) { | ||
| revert ERC721SequentialEnumerableInvalidTokenId(expectedId, tokenId); | ||
| } | ||
|
|
||
| unchecked { | ||
| $._mintCount = expectedId + 1; | ||
| } | ||
| } | ||
|
|
||
| function _beforeTokenRemoval(uint256 tokenId, address) internal virtual override { | ||
| revert ERC721SequentialEnumerableTokenRemoval(tokenId); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: ERC-7201 Storage Slot Mismatch
The
_getERC721SequentialEnumerableStoragefunction calculates its storage slot using a simplekeccak256hash. This deviates from the ERC-7201 standard indicated by the@custom:storage-locationcomment, potentially causing storage slot collisions and inconsistencies with other ERC-7201 compliant implementations.