ERC721Token.sol 6.2 KB

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