ERC721URIStorage.sol 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.7.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. _requireMinted(tokenId);
  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 See {ERC721-_burn}. This override additionally checks to see if a
  42. * token-specific URI was set for the token, and if so, it deletes the token URI from
  43. * the storage mapping.
  44. */
  45. function _burn(uint256 tokenId) internal virtual override {
  46. super._burn(tokenId);
  47. if (bytes(_tokenURIs[tokenId]).length != 0) {
  48. delete _tokenURIs[tokenId];
  49. }
  50. }
  51. }