ERC721Enumerable.sol 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. pragma solidity ^0.4.24;
  2. import "./IERC721Enumerable.sol";
  3. import "./ERC721.sol";
  4. import "../../introspection/ERC165.sol";
  5. contract ERC721Enumerable is ERC165, ERC721, IERC721Enumerable {
  6. // Mapping from owner to list of owned token IDs
  7. mapping(address => uint256[]) private _ownedTokens;
  8. // Mapping from token ID to index of the owner tokens list
  9. mapping(uint256 => uint256) private _ownedTokensIndex;
  10. // Array with all token ids, used for enumeration
  11. uint256[] private _allTokens;
  12. // Mapping from token id to position in the allTokens array
  13. mapping(uint256 => uint256) private _allTokensIndex;
  14. bytes4 private constant _InterfaceId_ERC721Enumerable = 0x780e9d63;
  15. /**
  16. * 0x780e9d63 ===
  17. * bytes4(keccak256('totalSupply()')) ^
  18. * bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) ^
  19. * bytes4(keccak256('tokenByIndex(uint256)'))
  20. */
  21. /**
  22. * @dev Constructor function
  23. */
  24. constructor() public {
  25. // register the supported interface to conform to ERC721 via ERC165
  26. _registerInterface(_InterfaceId_ERC721Enumerable);
  27. }
  28. /**
  29. * @dev Gets the token ID at a given index of the tokens list of the requested owner
  30. * @param owner address owning the tokens list to be accessed
  31. * @param index uint256 representing the index to be accessed of the requested tokens list
  32. * @return uint256 token ID at the given index of the tokens list owned by the requested address
  33. */
  34. function tokenOfOwnerByIndex(
  35. address owner,
  36. uint256 index
  37. )
  38. public
  39. view
  40. returns (uint256)
  41. {
  42. require(index < balanceOf(owner));
  43. return _ownedTokens[owner][index];
  44. }
  45. /**
  46. * @dev Gets the total amount of tokens stored by the contract
  47. * @return uint256 representing the total amount of tokens
  48. */
  49. function totalSupply() public view returns (uint256) {
  50. return _allTokens.length;
  51. }
  52. /**
  53. * @dev Gets the token ID at a given index of all the tokens in this contract
  54. * Reverts if the index is greater or equal to the total number of tokens
  55. * @param index uint256 representing the index to be accessed of the tokens list
  56. * @return uint256 token ID at the given index of the tokens list
  57. */
  58. function tokenByIndex(uint256 index) public view returns (uint256) {
  59. require(index < totalSupply());
  60. return _allTokens[index];
  61. }
  62. /**
  63. * @dev Internal function to add a token ID to the list of a given address
  64. * @param to address representing the new owner of the given token ID
  65. * @param tokenId uint256 ID of the token to be added to the tokens list of the given address
  66. */
  67. function _addTokenTo(address to, uint256 tokenId) internal {
  68. super._addTokenTo(to, tokenId);
  69. uint256 length = _ownedTokens[to].length;
  70. _ownedTokens[to].push(tokenId);
  71. _ownedTokensIndex[tokenId] = length;
  72. }
  73. /**
  74. * @dev Internal function to remove a token ID from the list of a given address
  75. * @param from address representing the previous owner of the given token ID
  76. * @param tokenId uint256 ID of the token to be removed from the tokens list of the given address
  77. */
  78. function _removeTokenFrom(address from, uint256 tokenId) internal {
  79. super._removeTokenFrom(from, tokenId);
  80. // To prevent a gap in the array, we store the last token in the index of the token to delete, and
  81. // then delete the last slot.
  82. uint256 tokenIndex = _ownedTokensIndex[tokenId];
  83. uint256 lastTokenIndex = _ownedTokens[from].length.sub(1);
  84. uint256 lastToken = _ownedTokens[from][lastTokenIndex];
  85. _ownedTokens[from][tokenIndex] = lastToken;
  86. // This also deletes the contents at the last position of the array
  87. _ownedTokens[from].length--;
  88. // Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to
  89. // be zero. Then we can make sure that we will remove tokenId from the ownedTokens list since we are first swapping
  90. // the lastToken to the first position, and then dropping the element placed in the last position of the list
  91. _ownedTokensIndex[tokenId] = 0;
  92. _ownedTokensIndex[lastToken] = tokenIndex;
  93. }
  94. /**
  95. * @dev Internal function to mint a new token
  96. * Reverts if the given token ID already exists
  97. * @param to address the beneficiary that will own the minted token
  98. * @param tokenId uint256 ID of the token to be minted by the msg.sender
  99. */
  100. function _mint(address to, uint256 tokenId) internal {
  101. super._mint(to, tokenId);
  102. _allTokensIndex[tokenId] = _allTokens.length;
  103. _allTokens.push(tokenId);
  104. }
  105. /**
  106. * @dev Internal function to burn a specific token
  107. * Reverts if the token does not exist
  108. * @param owner owner of the token to burn
  109. * @param tokenId uint256 ID of the token being burned by the msg.sender
  110. */
  111. function _burn(address owner, uint256 tokenId) internal {
  112. super._burn(owner, tokenId);
  113. // Reorg all tokens array
  114. uint256 tokenIndex = _allTokensIndex[tokenId];
  115. uint256 lastTokenIndex = _allTokens.length.sub(1);
  116. uint256 lastToken = _allTokens[lastTokenIndex];
  117. _allTokens[tokenIndex] = lastToken;
  118. _allTokens[lastTokenIndex] = 0;
  119. _allTokens.length--;
  120. _allTokensIndex[tokenId] = 0;
  121. _allTokensIndex[lastToken] = tokenIndex;
  122. }
  123. }