ERC721URIStorage.sol 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.0 (token/ERC721/extensions/ERC721URIStorage.sol)
  3. pragma solidity ^0.8.0;
  4. import "../ERC721.sol";
  5. /**
  6. * @dev ERC721 token with storage based token URI management.
  7. */
  8. abstract contract ERC721URIStorage is ERC721 {
  9. using Strings for uint256;
  10. // Optional mapping for token URIs
  11. mapping(uint256 => string) private _tokenURIs;
  12. /**
  13. * @dev See {IERC721Metadata-tokenURI}.
  14. */
  15. function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
  16. require(_exists(tokenId), "ERC721URIStorage: URI query for nonexistent token");
  17. string memory _tokenURI = _tokenURIs[tokenId];
  18. string memory base = _baseURI();
  19. // If there is no base URI, return the token URI.
  20. if (bytes(base).length == 0) {
  21. return _tokenURI;
  22. }
  23. // If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked).
  24. if (bytes(_tokenURI).length > 0) {
  25. return string(abi.encodePacked(base, _tokenURI));
  26. }
  27. return super.tokenURI(tokenId);
  28. }
  29. /**
  30. * @dev Sets `_tokenURI` as the tokenURI of `tokenId`.
  31. *
  32. * Requirements:
  33. *
  34. * - `tokenId` must exist.
  35. */
  36. function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
  37. require(_exists(tokenId), "ERC721URIStorage: URI set of nonexistent token");
  38. _tokenURIs[tokenId] = _tokenURI;
  39. }
  40. /**
  41. * @dev Destroys `tokenId`.
  42. * The approval is cleared when the token is burned.
  43. *
  44. * Requirements:
  45. *
  46. * - `tokenId` must exist.
  47. *
  48. * Emits a {Transfer} event.
  49. */
  50. function _burn(uint256 tokenId) internal virtual override {
  51. super._burn(tokenId);
  52. if (bytes(_tokenURIs[tokenId]).length != 0) {
  53. delete _tokenURIs[tokenId];
  54. }
  55. }
  56. }