ERC721Token.sol 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. pragma solidity ^0.4.24;
  2. import "./ERC721.sol";
  3. import "./ERC721BasicToken.sol";
  4. import "../../introspection/SupportsInterfaceWithLookup.sol";
  5. /**
  6. * @title Full ERC721 Token
  7. * This implementation includes all the required and some optional functionality of the ERC721 standard
  8. * Moreover, it includes approve all functionality using operator terminology
  9. * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
  10. */
  11. contract ERC721Token is SupportsInterfaceWithLookup, ERC721BasicToken, ERC721 {
  12. // Token name
  13. string internal name_;
  14. // Token symbol
  15. string internal symbol_;
  16. // Mapping from owner to list of owned token IDs
  17. mapping(address => uint256[]) internal ownedTokens;
  18. // Mapping from token ID to index of the owner tokens list
  19. mapping(uint256 => uint256) internal ownedTokensIndex;
  20. // Array with all token ids, used for enumeration
  21. uint256[] internal allTokens;
  22. // Mapping from token id to position in the allTokens array
  23. mapping(uint256 => uint256) internal allTokensIndex;
  24. // Optional mapping for token URIs
  25. mapping(uint256 => string) internal tokenURIs;
  26. /**
  27. * @dev Constructor function
  28. */
  29. constructor(string _name, string _symbol) public {
  30. name_ = _name;
  31. symbol_ = _symbol;
  32. // register the supported interfaces to conform to ERC721 via ERC165
  33. _registerInterface(InterfaceId_ERC721Enumerable);
  34. _registerInterface(InterfaceId_ERC721Metadata);
  35. }
  36. /**
  37. * @dev Gets the token name
  38. * @return string representing the token name
  39. */
  40. function name() external view returns (string) {
  41. return name_;
  42. }
  43. /**
  44. * @dev Gets the token symbol
  45. * @return string representing the token symbol
  46. */
  47. function symbol() external view returns (string) {
  48. return symbol_;
  49. }
  50. /**
  51. * @dev Returns an URI for a given token ID
  52. * Throws if the token ID does not exist. May return an empty string.
  53. * @param _tokenId uint256 ID of the token to query
  54. */
  55. function tokenURI(uint256 _tokenId) public view returns (string) {
  56. require(exists(_tokenId));
  57. return tokenURIs[_tokenId];
  58. }
  59. /**
  60. * @dev Gets the token ID at a given index of the tokens list of the requested owner
  61. * @param _owner address owning the tokens list to be accessed
  62. * @param _index uint256 representing the index to be accessed of the requested tokens list
  63. * @return uint256 token ID at the given index of the tokens list owned by the requested address
  64. */
  65. function tokenOfOwnerByIndex(
  66. address _owner,
  67. uint256 _index
  68. )
  69. public
  70. view
  71. returns (uint256)
  72. {
  73. require(_index < balanceOf(_owner));
  74. return ownedTokens[_owner][_index];
  75. }
  76. /**
  77. * @dev Gets the total amount of tokens stored by the contract
  78. * @return uint256 representing the total amount of tokens
  79. */
  80. function totalSupply() public view returns (uint256) {
  81. return allTokens.length;
  82. }
  83. /**
  84. * @dev Gets the token ID at a given index of all the tokens in this contract
  85. * Reverts if the index is greater or equal to the total number of tokens
  86. * @param _index uint256 representing the index to be accessed of the tokens list
  87. * @return uint256 token ID at the given index of the tokens list
  88. */
  89. function tokenByIndex(uint256 _index) public view returns (uint256) {
  90. require(_index < totalSupply());
  91. return allTokens[_index];
  92. }
  93. /**
  94. * @dev Internal function to set the token URI for a given token
  95. * Reverts if the token ID does not exist
  96. * @param _tokenId uint256 ID of the token to set its URI
  97. * @param _uri string URI to assign
  98. */
  99. function _setTokenURI(uint256 _tokenId, string _uri) internal {
  100. require(exists(_tokenId));
  101. tokenURIs[_tokenId] = _uri;
  102. }
  103. /**
  104. * @dev Internal function to add a token ID to the list of a given address
  105. * @param _to address representing the new owner of the given token ID
  106. * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address
  107. */
  108. function addTokenTo(address _to, uint256 _tokenId) internal {
  109. super.addTokenTo(_to, _tokenId);
  110. uint256 length = ownedTokens[_to].length;
  111. ownedTokens[_to].push(_tokenId);
  112. ownedTokensIndex[_tokenId] = length;
  113. }
  114. /**
  115. * @dev Internal function to remove a token ID from the list of a given address
  116. * @param _from address representing the previous owner of the given token ID
  117. * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address
  118. */
  119. function removeTokenFrom(address _from, uint256 _tokenId) internal {
  120. super.removeTokenFrom(_from, _tokenId);
  121. // To prevent a gap in the array, we store the last token in the index of the token to delete, and
  122. // then delete the last slot.
  123. uint256 tokenIndex = ownedTokensIndex[_tokenId];
  124. uint256 lastTokenIndex = ownedTokens[_from].length.sub(1);
  125. uint256 lastToken = ownedTokens[_from][lastTokenIndex];
  126. ownedTokens[_from][tokenIndex] = lastToken;
  127. // This also deletes the contents at the last position of the array
  128. ownedTokens[_from].length--;
  129. // Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to
  130. // be zero. Then we can make sure that we will remove _tokenId from the ownedTokens list since we are first swapping
  131. // the lastToken to the first position, and then dropping the element placed in the last position of the list
  132. ownedTokensIndex[_tokenId] = 0;
  133. ownedTokensIndex[lastToken] = tokenIndex;
  134. }
  135. /**
  136. * @dev Internal function to mint a new token
  137. * Reverts if the given token ID already exists
  138. * @param _to address the beneficiary that will own the minted token
  139. * @param _tokenId uint256 ID of the token to be minted by the msg.sender
  140. */
  141. function _mint(address _to, uint256 _tokenId) internal {
  142. super._mint(_to, _tokenId);
  143. allTokensIndex[_tokenId] = allTokens.length;
  144. allTokens.push(_tokenId);
  145. }
  146. /**
  147. * @dev Internal function to burn a specific token
  148. * Reverts if the token does not exist
  149. * @param _owner owner of the token to burn
  150. * @param _tokenId uint256 ID of the token being burned by the msg.sender
  151. */
  152. function _burn(address _owner, uint256 _tokenId) internal {
  153. super._burn(_owner, _tokenId);
  154. // Clear metadata (if any)
  155. if (bytes(tokenURIs[_tokenId]).length != 0) {
  156. delete tokenURIs[_tokenId];
  157. }
  158. // Reorg all tokens array
  159. uint256 tokenIndex = allTokensIndex[_tokenId];
  160. uint256 lastTokenIndex = allTokens.length.sub(1);
  161. uint256 lastToken = allTokens[lastTokenIndex];
  162. allTokens[tokenIndex] = lastToken;
  163. allTokens[lastTokenIndex] = 0;
  164. allTokens.length--;
  165. allTokensIndex[_tokenId] = 0;
  166. allTokensIndex[lastToken] = tokenIndex;
  167. }
  168. }