ERC721Enumerable.sol 5.7 KB

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