ERC721Token.sol 6.9 KB

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