ERC721Token.sol 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. pragma solidity ^0.4.18;
  2. import "./ERC721.sol";
  3. import "./DeprecatedERC721.sol";
  4. import "./ERC721BasicToken.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 ERC721, ERC721BasicToken {
  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. function ERC721Token(string _name, string _symbol) public {
  30. name_ = _name;
  31. symbol_ = _symbol;
  32. }
  33. /**
  34. * @dev Gets the token name
  35. * @return string representing the token name
  36. */
  37. function name() public view returns (string) {
  38. return name_;
  39. }
  40. /**
  41. * @dev Gets the token symbol
  42. * @return string representing the token symbol
  43. */
  44. function symbol() public view returns (string) {
  45. return symbol_;
  46. }
  47. /**
  48. * @dev Returns an URI for a given token ID
  49. * @dev Throws if the token ID does not exist. May return an empty string.
  50. * @param _tokenId uint256 ID of the token to query
  51. */
  52. function tokenURI(uint256 _tokenId) public view returns (string) {
  53. require(exists(_tokenId));
  54. return tokenURIs[_tokenId];
  55. }
  56. /**
  57. * @dev Internal function to set the token URI for a given token
  58. * @dev Reverts if the token ID does not exist
  59. * @param _tokenId uint256 ID of the token to set its URI
  60. * @param _uri string URI to assign
  61. */
  62. function _setTokenURI(uint256 _tokenId, string _uri) internal {
  63. require(exists(_tokenId));
  64. tokenURIs[_tokenId] = _uri;
  65. }
  66. /**
  67. * @dev Gets the token ID at a given index of the tokens list of the requested owner
  68. * @param _owner address owning the tokens list to be accessed
  69. * @param _index uint256 representing the index to be accessed of the requested tokens list
  70. * @return uint256 token ID at the given index of the tokens list owned by the requested address
  71. */
  72. function tokenOfOwnerByIndex(address _owner, uint256 _index) public view returns (uint256) {
  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. * @dev 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 add a token ID to the list of a given address
  95. * @param _to address representing the new owner of the given token ID
  96. * @param _tokenId uint256 ID of the token to be added to the tokens list of the given address
  97. */
  98. function addTokenTo(address _to, uint256 _tokenId) internal {
  99. super.addTokenTo(_to, _tokenId);
  100. uint256 length = ownedTokens[_to].length;
  101. ownedTokens[_to].push(_tokenId);
  102. ownedTokensIndex[_tokenId] = length;
  103. }
  104. /**
  105. * @dev Internal function to remove a token ID from the list of a given address
  106. * @param _from address representing the previous owner of the given token ID
  107. * @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address
  108. */
  109. function removeTokenFrom(address _from, uint256 _tokenId) internal {
  110. super.removeTokenFrom(_from, _tokenId);
  111. uint256 tokenIndex = ownedTokensIndex[_tokenId];
  112. uint256 lastTokenIndex = ownedTokens[_from].length.sub(1);
  113. uint256 lastToken = ownedTokens[_from][lastTokenIndex];
  114. ownedTokens[_from][tokenIndex] = lastToken;
  115. ownedTokens[_from][lastTokenIndex] = 0;
  116. // Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to
  117. // be zero. Then we can make sure that we will remove _tokenId from the ownedTokens list since we are first swapping
  118. // the lastToken to the first position, and then dropping the element placed in the last position of the list
  119. ownedTokens[_from].length--;
  120. ownedTokensIndex[_tokenId] = 0;
  121. ownedTokensIndex[lastToken] = tokenIndex;
  122. }
  123. /**
  124. * @dev Internal function to mint a new token
  125. * @dev Reverts if the given token ID already exists
  126. * @param _to address the beneficiary that will own the minted token
  127. * @param _tokenId uint256 ID of the token to be minted by the msg.sender
  128. */
  129. function _mint(address _to, uint256 _tokenId) internal {
  130. super._mint(_to, _tokenId);
  131. allTokensIndex[_tokenId] = allTokens.length;
  132. allTokens.push(_tokenId);
  133. }
  134. /**
  135. * @dev Internal function to burn a specific token
  136. * @dev Reverts if the token does not exist
  137. * @param _owner owner of the token to burn
  138. * @param _tokenId uint256 ID of the token being burned by the msg.sender
  139. */
  140. function _burn(address _owner, uint256 _tokenId) internal {
  141. super._burn(_owner, _tokenId);
  142. // Clear metadata (if any)
  143. if (bytes(tokenURIs[_tokenId]).length != 0) {
  144. delete tokenURIs[_tokenId];
  145. }
  146. // Reorg all tokens array
  147. uint256 tokenIndex = allTokensIndex[_tokenId];
  148. uint256 lastTokenIndex = allTokens.length.sub(1);
  149. uint256 lastToken = allTokens[lastTokenIndex];
  150. allTokens[tokenIndex] = lastToken;
  151. allTokens[lastTokenIndex] = 0;
  152. allTokens.length--;
  153. allTokensIndex[_tokenId] = 0;
  154. allTokensIndex[lastToken] = tokenIndex;
  155. }
  156. }