ERC721.sol 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. pragma solidity ^0.4.24;
  2. import "./IERC721.sol";
  3. import "./ERC721Basic.sol";
  4. import "../../introspection/ERC165.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 ERC721 is ERC165, ERC721Basic, IERC721 {
  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[]) private ownedTokens_;
  18. // Mapping from token ID to index of the owner tokens list
  19. mapping(uint256 => uint256) private ownedTokensIndex_;
  20. // Array with all token ids, used for enumeration
  21. uint256[] private allTokens_;
  22. // Mapping from token id to position in the allTokens array
  23. mapping(uint256 => uint256) private allTokensIndex_;
  24. // Optional mapping for token URIs
  25. mapping(uint256 => string) private tokenURIs_;
  26. bytes4 private constant InterfaceId_ERC721Enumerable = 0x780e9d63;
  27. /**
  28. * 0x780e9d63 ===
  29. * bytes4(keccak256('totalSupply()')) ^
  30. * bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) ^
  31. * bytes4(keccak256('tokenByIndex(uint256)'))
  32. */
  33. bytes4 private constant InterfaceId_ERC721Metadata = 0x5b5e139f;
  34. /**
  35. * 0x5b5e139f ===
  36. * bytes4(keccak256('name()')) ^
  37. * bytes4(keccak256('symbol()')) ^
  38. * bytes4(keccak256('tokenURI(uint256)'))
  39. */
  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. // To prevent a gap in the array, we store the last token in the index of the token to delete, and
  136. // then delete the last slot.
  137. uint256 tokenIndex = ownedTokensIndex_[_tokenId];
  138. uint256 lastTokenIndex = ownedTokens_[_from].length.sub(1);
  139. uint256 lastToken = ownedTokens_[_from][lastTokenIndex];
  140. ownedTokens_[_from][tokenIndex] = lastToken;
  141. // This also deletes the contents at the last position of the array
  142. ownedTokens_[_from].length--;
  143. // Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to
  144. // be zero. Then we can make sure that we will remove _tokenId from the ownedTokens list since we are first swapping
  145. // the lastToken to the first position, and then dropping the element placed in the last position of the list
  146. ownedTokensIndex_[_tokenId] = 0;
  147. ownedTokensIndex_[lastToken] = tokenIndex;
  148. }
  149. /**
  150. * @dev Internal function to mint a new token
  151. * Reverts if the given token ID already exists
  152. * @param _to address the beneficiary that will own the minted token
  153. * @param _tokenId uint256 ID of the token to be minted by the msg.sender
  154. */
  155. function _mint(address _to, uint256 _tokenId) internal {
  156. super._mint(_to, _tokenId);
  157. allTokensIndex_[_tokenId] = allTokens_.length;
  158. allTokens_.push(_tokenId);
  159. }
  160. /**
  161. * @dev Internal function to burn a specific token
  162. * Reverts if the token does not exist
  163. * @param _owner owner of the token to burn
  164. * @param _tokenId uint256 ID of the token being burned by the msg.sender
  165. */
  166. function _burn(address _owner, uint256 _tokenId) internal {
  167. super._burn(_owner, _tokenId);
  168. // Clear metadata (if any)
  169. if (bytes(tokenURIs_[_tokenId]).length != 0) {
  170. delete tokenURIs_[_tokenId];
  171. }
  172. // Reorg all tokens array
  173. uint256 tokenIndex = allTokensIndex_[_tokenId];
  174. uint256 lastTokenIndex = allTokens_.length.sub(1);
  175. uint256 lastToken = allTokens_[lastTokenIndex];
  176. allTokens_[tokenIndex] = lastToken;
  177. allTokens_[lastTokenIndex] = 0;
  178. allTokens_.length--;
  179. allTokensIndex_[_tokenId] = 0;
  180. allTokensIndex_[lastToken] = tokenIndex;
  181. }
  182. }