12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- // SPDX-License-Identifier: MIT
- // OpenZeppelin Contracts (last updated v5.0.0-rc.0) (token/ERC721/extensions/ERC721URIStorage.sol)
- pragma solidity ^0.8.20;
- import {ERC721} from "../ERC721.sol";
- import {Strings} from "../../../utils/Strings.sol";
- import {IERC4906} from "../../../interfaces/IERC4906.sol";
- import {IERC165} from "../../../interfaces/IERC165.sol";
- /**
- * @dev ERC721 token with storage based token URI management.
- */
- abstract contract ERC721URIStorage is IERC4906, ERC721 {
- using Strings for uint256;
- // Interface ID as defined in ERC-4906. This does not correspond to a traditional interface ID as ERC-4906 only
- // defines events and does not include any external function.
- bytes4 private constant ERC4906_INTERFACE_ID = bytes4(0x49064906);
- // Optional mapping for token URIs
- mapping(uint256 tokenId => string) private _tokenURIs;
- /**
- * @dev See {IERC165-supportsInterface}
- */
- function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721, IERC165) returns (bool) {
- return interfaceId == ERC4906_INTERFACE_ID || super.supportsInterface(interfaceId);
- }
- /**
- * @dev See {IERC721Metadata-tokenURI}.
- */
- function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
- _requireOwned(tokenId);
- string memory _tokenURI = _tokenURIs[tokenId];
- string memory base = _baseURI();
- // If there is no base URI, return the token URI.
- if (bytes(base).length == 0) {
- return _tokenURI;
- }
- // If both are set, concatenate the baseURI and tokenURI (via string.concat).
- if (bytes(_tokenURI).length > 0) {
- return string.concat(base, _tokenURI);
- }
- return super.tokenURI(tokenId);
- }
- /**
- * @dev Sets `_tokenURI` as the tokenURI of `tokenId`.
- *
- * Emits {MetadataUpdate}.
- */
- function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
- _tokenURIs[tokenId] = _tokenURI;
- emit MetadataUpdate(tokenId);
- }
- }
|