ERC721Token.sol 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. pragma solidity ^0.4.21;
  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. function ERC721Token(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(address _owner, uint256 _index) public view returns (uint256) {
  62. require(_index < balanceOf(_owner));
  63. return ownedTokens[_owner][_index];
  64. }
  65. /**
  66. * @dev Gets the total amount of tokens stored by the contract
  67. * @return uint256 representing the total amount of tokens
  68. */
  69. function totalSupply() public view returns (uint256) {
  70. return allTokens.length;
  71. }
  72. /**
  73. * @dev Gets the token ID at a given index of all the tokens in this contract
  74. * @dev Reverts if the index is greater or equal to the total number of tokens
  75. * @param _index uint256 representing the index to be accessed of the tokens list
  76. * @return uint256 token ID at the given index of the tokens list
  77. */
  78. function tokenByIndex(uint256 _index) public view returns (uint256) {
  79. require(_index < totalSupply());
  80. return allTokens[_index];
  81. }
  82. /**
  83. * @dev Internal function to set the token URI for a given token
  84. * @dev Reverts if the token ID does not exist
  85. * @param _tokenId uint256 ID of the token to set its URI
  86. * @param _uri string URI to assign
  87. */
  88. function _setTokenURI(uint256 _tokenId, string _uri) internal {
  89. require(exists(_tokenId));
  90. tokenURIs[_tokenId] = _uri;
  91. }
  92. /**
  93. * @dev Internal function to add a token ID to the list of a given address
  94. * @param _to address representing the new owner of the given token ID
  95. * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address
  96. */
  97. function addTokenTo(address _to, uint256 _tokenId) internal {
  98. super.addTokenTo(_to, _tokenId);
  99. uint256 length = ownedTokens[_to].length;
  100. ownedTokens[_to].push(_tokenId);
  101. ownedTokensIndex[_tokenId] = length;
  102. }
  103. /**
  104. * @dev Internal function to remove a token ID from the list of a given address
  105. * @param _from address representing the previous owner of the given token ID
  106. * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address
  107. */
  108. function removeTokenFrom(address _from, uint256 _tokenId) internal {
  109. super.removeTokenFrom(_from, _tokenId);
  110. uint256 tokenIndex = ownedTokensIndex[_tokenId];
  111. uint256 lastTokenIndex = ownedTokens[_from].length.sub(1);
  112. uint256 lastToken = ownedTokens[_from][lastTokenIndex];
  113. ownedTokens[_from][tokenIndex] = lastToken;
  114. ownedTokens[_from][lastTokenIndex] = 0;
  115. // Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to
  116. // be zero. Then we can make sure that we will remove _tokenId from the ownedTokens list since we are first swapping
  117. // the lastToken to the first position, and then dropping the element placed in the last position of the list
  118. ownedTokens[_from].length--;
  119. ownedTokensIndex[_tokenId] = 0;
  120. ownedTokensIndex[lastToken] = tokenIndex;
  121. }
  122. /**
  123. * @dev Internal function to mint a new token
  124. * @dev Reverts if the given token ID already exists
  125. * @param _to address the beneficiary that will own the minted token
  126. * @param _tokenId uint256 ID of the token to be minted by the msg.sender
  127. */
  128. function _mint(address _to, uint256 _tokenId) internal {
  129. super._mint(_to, _tokenId);
  130. allTokensIndex[_tokenId] = allTokens.length;
  131. allTokens.push(_tokenId);
  132. }
  133. /**
  134. * @dev Internal function to burn a specific token
  135. * @dev Reverts if the token does not exist
  136. * @param _owner owner of the token to burn
  137. * @param _tokenId uint256 ID of the token being burned by the msg.sender
  138. */
  139. function _burn(address _owner, uint256 _tokenId) internal {
  140. super._burn(_owner, _tokenId);
  141. // Clear metadata (if any)
  142. if (bytes(tokenURIs[_tokenId]).length != 0) {
  143. delete tokenURIs[_tokenId];
  144. }
  145. // Reorg all tokens array
  146. uint256 tokenIndex = allTokensIndex[_tokenId];
  147. uint256 lastTokenIndex = allTokens.length.sub(1);
  148. uint256 lastToken = allTokens[lastTokenIndex];
  149. allTokens[tokenIndex] = lastToken;
  150. allTokens[lastTokenIndex] = 0;
  151. allTokens.length--;
  152. allTokensIndex[_tokenId] = 0;
  153. allTokensIndex[lastToken] = tokenIndex;
  154. }
  155. }