ERC721Metadata.sol 2.6 KB

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