ERC721Enumerable.sol 6.8 KB

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