ERC721Metadata.sol 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. pragma solidity ^0.5.0;
  2. import "./ERC721.sol";
  3. import "./IERC721Metadata.sol";
  4. import "../../introspection/ERC165.sol";
  5. contract ERC721Metadata is ERC165, ERC721, IERC721Metadata {
  6. // Token name
  7. string private _name;
  8. // Token symbol
  9. string private _symbol;
  10. // Optional mapping for token URIs
  11. mapping(uint256 => string) private _tokenURIs;
  12. bytes4 private constant _INTERFACE_ID_ERC721_METADATA = 0x5b5e139f;
  13. /**
  14. * 0x5b5e139f ===
  15. * bytes4(keccak256('name()')) ^
  16. * bytes4(keccak256('symbol()')) ^
  17. * bytes4(keccak256('tokenURI(uint256)'))
  18. */
  19. /**
  20. * @dev Constructor function
  21. */
  22. constructor (string memory name, string memory symbol) public {
  23. _name = name;
  24. _symbol = symbol;
  25. // register the supported interfaces to conform to ERC721 via ERC165
  26. _registerInterface(_INTERFACE_ID_ERC721_METADATA);
  27. }
  28. /**
  29. * @dev Gets the token name
  30. * @return string representing the token name
  31. */
  32. function name() external view returns (string memory) {
  33. return _name;
  34. }
  35. /**
  36. * @dev Gets the token symbol
  37. * @return string representing the token symbol
  38. */
  39. function symbol() external view returns (string memory) {
  40. return _symbol;
  41. }
  42. /**
  43. * @dev Returns an URI for a given token ID
  44. * Throws if the token ID does not exist. May return an empty string.
  45. * @param tokenId uint256 ID of the token to query
  46. */
  47. function tokenURI(uint256 tokenId) external view returns (string memory) {
  48. require(_exists(tokenId));
  49. return _tokenURIs[tokenId];
  50. }
  51. /**
  52. * @dev Internal function to set the token URI for a given token
  53. * Reverts if the token ID does not exist
  54. * @param tokenId uint256 ID of the token to set its URI
  55. * @param uri string URI to assign
  56. */
  57. function _setTokenURI(uint256 tokenId, string memory uri) internal {
  58. require(_exists(tokenId));
  59. _tokenURIs[tokenId] = uri;
  60. }
  61. /**
  62. * @dev Internal function to burn a specific token
  63. * Reverts if the token does not exist
  64. * Deprecated, use _burn(uint256) instead
  65. * @param owner owner of the token to burn
  66. * @param tokenId uint256 ID of the token being burned by the msg.sender
  67. */
  68. function _burn(address owner, uint256 tokenId) internal {
  69. super._burn(owner, tokenId);
  70. // Clear metadata (if any)
  71. if (bytes(_tokenURIs[tokenId]).length != 0) {
  72. delete _tokenURIs[tokenId];
  73. }
  74. }
  75. }