-
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| // 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; | ||
| } | ||
|
|
||
| function _getERC721SequentialEnumerableStorage() | ||
| private | ||
| pure | ||
| returns (ERC721SequentialEnumerableStorage storage $) | ||
| { | ||
| bytes32 slot = keccak256(abi.encode("ethscriptions.storage.ERC721SequentialEnumerable")); | ||
| assembly { | ||
| $.slot := slot | ||
| } | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: ERC-7201 Storage Slot MismatchThe |
||
|
|
||
| /// @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); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.