-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathERC1155m.sol
More file actions
137 lines (122 loc) · 4.14 KB
/
ERC1155m.sol
File metadata and controls
137 lines (122 loc) · 4.14 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "@molecule-protocol/v2/interfaces/IMoleculeController.sol";
// custom errors
error AccountNotAllowedToMint(address minter);
error AccountNotAllowedToBurn(address burner);
error SenderNotAllowedToTransfer(address sender);
error RecipientNotAllowedToReceive(address receipient);
error OwnerNotAllowedToApprove(address owner);
// Molecule ERC1155 token
contract ERC1155m is ERC1155, Ownable {
enum MoleculeType {
Approve,
Burn,
Mint,
Transfer
}
// Base URI for metadata
string public _baseURI;
// Molecule address
address public _moleculeApprove;
address public _moleculeBurn;
address public _moleculeMint;
address public _moleculeTransfer;
event MoleculeUpdated(address molecule, MoleculeType mtype);
event BaseUriUpdated(string baseURI);
constructor(string memory baseURI) ERC1155("") {
setBaseURI(baseURI);
}
// Set base URI for metadata, needed for displaying correctly on OpenSea
function uri(
uint256 id
) public view virtual override returns (string memory) {
return string.concat(_baseURI, Strings.toString(id), ".json");
}
function mint(
address account,
uint256 id,
uint256 amount,
bytes memory data
) external {
if (_moleculeMint != address(0)) {
if (!IMoleculeController(_moleculeMint).check(account)) {
revert AccountNotAllowedToMint(account);
}
}
_mint(account, id, amount, data);
}
function burn(address account, uint256 id, uint256 amount) external {
if (_moleculeBurn != address(0)) {
if (!IMoleculeController(_moleculeBurn).check(account)) {
revert AccountNotAllowedToBurn(account);
}
}
_burn(account, id, amount);
}
// Transfer function: note this is used by all mint/burn/transfer functions
function _beforeTokenTransfer(
address operator,
address sender,
address recipient,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual override {
if (_moleculeTransfer != address(0)) {
if (!IMoleculeController(_moleculeTransfer).check(sender)) {
revert SenderNotAllowedToTransfer(sender);
}
if (!IMoleculeController(_moleculeTransfer).check(recipient)) {
revert RecipientNotAllowedToReceive(recipient);
}
}
super._beforeTokenTransfer(
operator,
sender,
recipient,
ids,
amounts,
data
);
}
// internal approve function
function _setApprovalForAll(
address tokenOwner,
address operator,
bool approved
) internal virtual override {
if (_moleculeApprove != address(0)) {
if (!IMoleculeController(_moleculeApprove).check(operator)) {
revert OwnerNotAllowedToApprove(operator);
}
}
super._setApprovalForAll(tokenOwner, operator, approved);
}
// Owner only functions
// Update molecule address
function updateMolecule(
address molecule,
MoleculeType mtype
) external onlyOwner {
// allows 0x0 address to be set to remove molecule access control
if (mtype == MoleculeType.Approve) {
_moleculeApprove = molecule;
} else if (mtype == MoleculeType.Burn) {
_moleculeBurn = molecule;
} else if (mtype == MoleculeType.Mint) {
_moleculeMint = molecule;
} else if (mtype == MoleculeType.Transfer) {
_moleculeTransfer = molecule;
}
emit MoleculeUpdated(molecule, mtype);
}
// Allow metadata uri to be updated by owner
function setBaseURI(string memory baseURI) public onlyOwner {
_baseURI = baseURI;
emit BaseUriUpdated(baseURI);
}
}