/
ERC1155URIStorage.sol
54 lines (45 loc) · 1.7 KB
/
ERC1155URIStorage.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC1155/extensions/ERC1155URIStorage.sol)
pragma solidity ^0.8.0;
import "../../../utils/Strings.sol";
import "../ERC1155.sol";
import "./ERC1155Supply.sol";
/**
* @dev ERC1155 token with storage based token URI management.
* Inspired by the ERC721URIStorage extension
*/
abstract contract ERC1155URIStorage is ERC1155Supply {
using Strings for uint256;
// Optional mapping for token URIs
mapping(uint256 => string) private _tokenURIs;
/**
* @dev {IERC721Metadata-tokenURI} token URIs for ERC1155 tokens.
*/
function tokenURI(uint256 tokenId) public view virtual returns (string memory) {
require(exists(tokenId), "ERC1155URIStorage: URI query for nonexistent token");
string memory _tokenURI = _tokenURIs[tokenId];
string memory base = uri(0);
// If there is no base URI, return the token URI.
if (bytes(base).length == 0) {
return _tokenURI;
}
// If both are set, concatenate the base URI and tokenURI (via abi.encodePacked).
if (bytes(_tokenURI).length > 0) {
return string(abi.encodePacked(base, _tokenURI));
}
// If there is a base URI set but no tokenURI, return the base URI.
return base;
}
/**
* @dev Sets `_tokenURI` as the tokenURI of `tokenId`.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
require(exists(tokenId), "ERC1155URIStorage: URI set of nonexistent token");
_tokenURIs[tokenId] = _tokenURI;
emit URI(tokenURI(tokenId), tokenId);
}
}