-
Notifications
You must be signed in to change notification settings - Fork 0
/
ERC1155Core.sol
85 lines (67 loc) · 2.8 KB
/
ERC1155Core.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
import {ERC1155Supply, ERC1155} from "@openzeppelin/contracts/token/ERC1155/extensions/ERC1155Supply.sol";
import {OwnerIsCreator} from "@chainlink/contracts-ccip/src/v0.8/shared/access/OwnerIsCreator.sol";
/**
* THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED VALUES FOR CLARITY.
* THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE.
* DO NOT USE THIS CODE IN PRODUCTION.
*/
contract ERC1155Core is ERC1155Supply, OwnerIsCreator {
address internal s_issuer;
// Optional mapping for token URIs
mapping(uint256 tokenId => string) private _tokenURIs;
event SetIssuer(address indexed issuer);
error ERC1155Core_CallerIsNotIssuerOrItself(address msgSender);
modifier onlyIssuerOrItself() {
if (msg.sender != address(this) && msg.sender != s_issuer) {
revert ERC1155Core_CallerIsNotIssuerOrItself(msg.sender);
}
_;
}
// Used as the URI for all token types by relying on ID substitution, e.g. https://token-cdn-domain/{id}.json
constructor(string memory uri_) ERC1155(uri_) {}
function setIssuer(address _issuer) external onlyOwner {
s_issuer = _issuer;
emit SetIssuer(_issuer);
}
function mint(address _to, uint256 _id, uint256 _amount, bytes memory _data, string memory _tokenUri)
public
onlyIssuerOrItself
{
_mint(_to, _id, _amount, _data);
_tokenURIs[_id] = _tokenUri;
}
function mintBatch(
address _to,
uint256[] memory _ids,
uint256[] memory _amounts,
bytes memory _data,
string[] memory _tokenUris
) public onlyIssuerOrItself {
_mintBatch(_to, _ids, _amounts, _data);
for (uint256 i = 0; i < _ids.length; ++i) {
_tokenURIs[_ids[i]] = _tokenUris[i];
}
}
function burn(address account, uint256 id, uint256 amount) public onlyIssuerOrItself {
if (account != _msgSender() && !isApprovedForAll(account, _msgSender())) {
revert ERC1155MissingApprovalForAll(_msgSender(), account);
}
_burn(account, id, amount);
}
function burnBatch(address account, uint256[] memory ids, uint256[] memory amounts) public onlyIssuerOrItself {
if (account != _msgSender() && !isApprovedForAll(account, _msgSender())) {
revert ERC1155MissingApprovalForAll(_msgSender(), account);
}
_burnBatch(account, ids, amounts);
}
function uri(uint256 tokenId) public view override returns (string memory) {
string memory tokenURI = _tokenURIs[tokenId];
return bytes(tokenURI).length > 0 ? tokenURI : super.uri(tokenId);
}
function _setURI(uint256 tokenId, string memory tokenURI) internal {
_tokenURIs[tokenId] = tokenURI;
emit URI(uri(tokenId), tokenId);
}
}