ERC721Metadata.sol 2.7 KB

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