ERC721Enumerable.sol 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.8.0) (token/ERC721/extensions/ERC721Enumerable.sol)
  3. pragma solidity ^0.8.19;
  4. import "../ERC721.sol";
  5. import "./IERC721Enumerable.sol";
  6. /**
  7. * @dev This implements an optional extension of {ERC721} defined in the EIP that adds
  8. * enumerability of all the token ids in the contract as well as all token ids owned by each
  9. * account.
  10. */
  11. abstract contract ERC721Enumerable is ERC721, IERC721Enumerable {
  12. // Mapping from owner to list of owned token IDs
  13. mapping(address => mapping(uint256 => uint256)) private _ownedTokens;
  14. // Mapping from token ID to index of the owner tokens list
  15. mapping(uint256 => uint256) private _ownedTokensIndex;
  16. // Array with all token ids, used for enumeration
  17. uint256[] private _allTokens;
  18. // Mapping from token id to position in the allTokens array
  19. mapping(uint256 => uint256) private _allTokensIndex;
  20. /**
  21. * @dev An `owner`'s token query was out of bounds for `index`.
  22. *
  23. * NOTE: The owner being `address(0)` indicates a global out of bounds index.
  24. */
  25. error ERC721OutOfBoundsIndex(address owner, uint256 index);
  26. /**
  27. * @dev Batch mint is not allowed.
  28. */
  29. error ERC721EnumerableForbiddenBatchMint();
  30. /**
  31. * @dev See {IERC165-supportsInterface}.
  32. */
  33. function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165, ERC721) returns (bool) {
  34. return interfaceId == type(IERC721Enumerable).interfaceId || super.supportsInterface(interfaceId);
  35. }
  36. /**
  37. * @dev See {IERC721Enumerable-tokenOfOwnerByIndex}.
  38. */
  39. function tokenOfOwnerByIndex(address owner, uint256 index) public view virtual override returns (uint256) {
  40. if (index >= balanceOf(owner)) {
  41. revert ERC721OutOfBoundsIndex(owner, index);
  42. }
  43. return _ownedTokens[owner][index];
  44. }
  45. /**
  46. * @dev See {IERC721Enumerable-totalSupply}.
  47. */
  48. function totalSupply() public view virtual override returns (uint256) {
  49. return _allTokens.length;
  50. }
  51. /**
  52. * @dev See {IERC721Enumerable-tokenByIndex}.
  53. */
  54. function tokenByIndex(uint256 index) public view virtual override returns (uint256) {
  55. if (index >= ERC721Enumerable.totalSupply()) {
  56. revert ERC721OutOfBoundsIndex(address(0), index);
  57. }
  58. return _allTokens[index];
  59. }
  60. /**
  61. * @dev See {ERC721-_beforeTokenTransfer}.
  62. */
  63. function _beforeTokenTransfer(
  64. address from,
  65. address to,
  66. uint256 firstTokenId,
  67. uint256 batchSize
  68. ) internal virtual override {
  69. super._beforeTokenTransfer(from, to, firstTokenId, batchSize);
  70. if (batchSize > 1) {
  71. // Will only trigger during construction. Batch transferring (minting) is not available afterwards.
  72. revert ERC721EnumerableForbiddenBatchMint();
  73. }
  74. uint256 tokenId = firstTokenId;
  75. if (from == address(0)) {
  76. _addTokenToAllTokensEnumeration(tokenId);
  77. } else if (from != to) {
  78. _removeTokenFromOwnerEnumeration(from, tokenId);
  79. }
  80. if (to == address(0)) {
  81. _removeTokenFromAllTokensEnumeration(tokenId);
  82. } else if (to != from) {
  83. _addTokenToOwnerEnumeration(to, tokenId);
  84. }
  85. }
  86. /**
  87. * @dev Private function to add a token to this extension's ownership-tracking data structures.
  88. * @param to address representing the new owner of the given token ID
  89. * @param tokenId uint256 ID of the token to be added to the tokens list of the given address
  90. */
  91. function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private {
  92. uint256 length = balanceOf(to);
  93. _ownedTokens[to][length] = tokenId;
  94. _ownedTokensIndex[tokenId] = length;
  95. }
  96. /**
  97. * @dev Private function to add a token to this extension's token tracking data structures.
  98. * @param tokenId uint256 ID of the token to be added to the tokens list
  99. */
  100. function _addTokenToAllTokensEnumeration(uint256 tokenId) private {
  101. _allTokensIndex[tokenId] = _allTokens.length;
  102. _allTokens.push(tokenId);
  103. }
  104. /**
  105. * @dev Private function to remove a token from this extension's ownership-tracking data structures. Note that
  106. * while the token is not assigned a new owner, the `_ownedTokensIndex` mapping is _not_ updated: this allows for
  107. * gas optimizations e.g. when performing a transfer operation (avoiding double writes).
  108. * This has O(1) time complexity, but alters the order of the _ownedTokens array.
  109. * @param from address representing the previous owner of the given token ID
  110. * @param tokenId uint256 ID of the token to be removed from the tokens list of the given address
  111. */
  112. function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private {
  113. // To prevent a gap in from's tokens array, we store the last token in the index of the token to delete, and
  114. // then delete the last slot (swap and pop).
  115. uint256 lastTokenIndex = ERC721.balanceOf(from) - 1;
  116. uint256 tokenIndex = _ownedTokensIndex[tokenId];
  117. // When the token to delete is the last token, the swap operation is unnecessary
  118. if (tokenIndex != lastTokenIndex) {
  119. uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];
  120. _ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
  121. _ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
  122. }
  123. // This also deletes the contents at the last position of the array
  124. delete _ownedTokensIndex[tokenId];
  125. delete _ownedTokens[from][lastTokenIndex];
  126. }
  127. /**
  128. * @dev Private function to remove a token from this extension's token tracking data structures.
  129. * This has O(1) time complexity, but alters the order of the _allTokens array.
  130. * @param tokenId uint256 ID of the token to be removed from the tokens list
  131. */
  132. function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private {
  133. // To prevent a gap in the tokens array, we store the last token in the index of the token to delete, and
  134. // then delete the last slot (swap and pop).
  135. uint256 lastTokenIndex = _allTokens.length - 1;
  136. uint256 tokenIndex = _allTokensIndex[tokenId];
  137. // When the token to delete is the last token, the swap operation is unnecessary. However, since this occurs so
  138. // rarely (when the last minted token is burnt) that we still do the swap here to avoid the gas cost of adding
  139. // an 'if' statement (like in _removeTokenFromOwnerEnumeration)
  140. uint256 lastTokenId = _allTokens[lastTokenIndex];
  141. _allTokens[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
  142. _allTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
  143. // This also deletes the contents at the last position of the array
  144. delete _allTokensIndex[tokenId];
  145. _allTokens.pop();
  146. }
  147. }