Repository navigation
Expand file tree
/
Copy pathERC1155NFTProduct.sol
More file actions
418 lines (367 loc) · 14 KB
/
Copy pathERC1155NFTProduct.sol
File metadata and controls
418 lines (367 loc) · 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts-upgradeable/token/ERC1155/ERC1155Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
import {IERC2981} from "@openzeppelin/contracts/token/common/ERC2981.sol";
import "@openzeppelin/contracts/utils/Strings.sol";
import "../lib/GranularRoles.sol";
import "../lib/Base64.sol";
import "../lib/Config.sol";
import "../lib/ITemplate.sol";
/*
* ERC-1155 proxy contract, meaning it does not make use of a constructor,
* but rather uses `initialize` with `initializer` modifier, see {Initializable}
*
* Minting and other write transactions only supported for accounts with relevant access rights.
*/
contract ERC1155NFTProduct is
ERC1155Upgradeable,
GranularRoles,
ITemplate,
ReentrancyGuardUpgradeable
{
/*******************************
* Extensions, structs, events *
*******************************/
using Strings for uint256;
/*
* Event emitted to show opensea that metadata of a token is frozen,
* see https://docs.opensea.io/docs/metadata-standards
*/
event PermanentURI(string _value, uint256 indexed _id);
// Event emitted to show that all tokens have their metadata frozen
event PermanentURIGlobal();
/*************
* Constants *
*************/
// Template name
string public constant NAME = "ERC1155NFTProduct";
// Template version
uint256 public constant VERSION = 1_00_01;
// Basis for calculating royalties.
// This has to be 10k for royaltiesBps to be in basis points.
uint16 public constant ROYALTIES_BASIS = 10000;
// Default URI for tokens, each minted token will have a token URI, so default URI is empty
string public constant DEFAULT_URI = "";
/********************
* Public variables *
********************/
// If true then tokens metadata can be updated
bool public metadataUpdatable;
// If true then tokens can be burned by their owners
bool public tokensBurnable;
// If true then tokens can be transferred by having the correct access rights {GranularRoles-TRANSFER_ROLE}
// if the token is owned by {GranularRoles-_owner} address
bool public tokensTransferable;
// Token name
string public name;
// Token symbol
string public symbol;
// Token IDs are returned as `baseURI` + `tokenURI`
string public baseURI;
// Address where royalties will be transferred to
address public royaltiesAddress;
// Secondary market royalties in basis points (100 bps = 1%). Royalties use ERC2981 standard and support
// OpenSea standard.
uint256 public royaltiesBasisPoints;
// Mapping of individually frozen tokens
mapping(uint256 => bool) public freezeTokenUris;
// Mapping of token ID to supply
mapping(uint256 => uint256) public tokenSupply;
/***************************
* Contract initialization *
***************************/
constructor() initializer {
// solhint-disable-previous-line no-empty-blocks
}
// Can only be called once, used because constructors cannot be used for proxy contracts
function initialize(
Config.Deployment memory deploymentConfig,
Config.Runtime memory runtimeConfig,
RolesAddresses[] memory rolesAddresses
) public initializer {
__ERC1155_init(DEFAULT_URI);
__ReentrancyGuard_init();
_setRoyalties(
runtimeConfig.royaltiesAddress,
runtimeConfig.royaltiesBps
);
metadataUpdatable = runtimeConfig.metadataUpdatable;
tokensBurnable = deploymentConfig.tokensBurnable;
tokensTransferable = runtimeConfig.tokensTransferable;
name = deploymentConfig.name;
symbol = deploymentConfig.symbol;
baseURI = runtimeConfig.baseURI;
_initRoles(deploymentConfig.owner, rolesAddresses);
}
/*******************
* Write functions *
*******************/
// Allows to set a default URI for tokens, which is only returned if token URI does not exist for given token ID
function setURI(string memory _newURI)
public
onlyRole(UPDATE_CONTRACT_ROLE)
{
_setURI(_newURI);
}
/*
* Allows to update token URI for given `_tokenId` and freeze it.
* For the transaction to succeed either `_newUri` or `_isFreezeTokenUri` as true must be specified, or both.
* The `_newURI` cannot be the same as the current URI for the token.
*
* Token ID must exist, `metadataUpdatable` must be true and token URI for given token cannot be frozen.
* Only callable by accounts with `UPDATE_TOKEN_ROLE` or `ADMIN_ROLE`.
*/
function updateTokenUri(
uint256 _tokenId,
string memory _newUri,
bool _isFreezeTokenUri
) public onlyRole(UPDATE_TOKEN_ROLE) {
require(_exists(_tokenId), "Token does not exist");
require(metadataUpdatable, "Metadata is frozen");
require(freezeTokenUris[_tokenId] != true, "Token is frozen");
require(
_isFreezeTokenUri || (bytes(_newUri).length != 0),
"Either _newUri or _isFreezeTokenUri=true required"
);
if (bytes(_newUri).length != 0) {
require(
keccak256(bytes(_tokenURIs[_tokenId])) !=
keccak256(bytes(string(abi.encodePacked(_newUri)))),
"New token URI is same as updated"
);
_tokenURIs[_tokenId] = _newUri;
emit URI(_newUri, _tokenId);
}
if (_isFreezeTokenUri) {
freezeTokenUris[_tokenId] = true;
emit PermanentURI(_tokenURIs[_tokenId], _tokenId);
}
}
/*
* Allows to burn given `value` amount of tokens with `id`.
* `tokensBurnable` must be true in order for the transaction to succeed and
* at least `value` amount of tokens must exist.
* Only callable by accounts with `BURN_ROLE` or `ADMIN_ROLE`.
*/
function burn(uint256 id, uint256 value) public onlyRole(BURN_ROLE) {
require(tokensBurnable, "Burns are disabled");
_burn(_owner, id, value);
tokenSupply[id] -= value;
}
/*
* Same functionality as `burn` but for a batch of tokens.
* Input `ids` and `values` must be in direct correlation,
* an index in both lists referring to the same token.
*/
function burnBatch(uint256[] memory ids, uint256[] memory values)
public
onlyRole(BURN_ROLE)
{
require(tokensBurnable, "Burns are disabled");
_burnBatch(_owner, ids, values);
for (uint256 i = 0; i < ids.length; i++) {
tokenSupply[ids[i]] -= values[i];
}
}
/*
* Allows to transfer given `value` amount of tokens with `id`.
* `tokensTransferable` must be true and the tokens to be transferred must be owned by the `_owner`,
* at least `value` amount of tokens must exist.
* Only callable by accounts with `TRANSFER_ROLE` or `ADMIN_ROLE`.
*/
function transferByOwner(
address to,
uint256 id,
uint256 amount
) public onlyRole(TRANSFER_ROLE) {
require(tokensTransferable, "Transfers are disabled");
_safeTransferFrom(_owner, to, id, amount, "");
}
/*
* Same functionality as `transferByOwner` but for a batch of tokens.
* Input `ids` and `values` must be in direct correlation,
* an index in both lists referring to the same token.
*/
function transferByOwnerBatch(
address[] memory to,
uint256[] memory ids,
uint256[] memory amounts
) public onlyRole(TRANSFER_ROLE) {
require(tokensTransferable, "Transfers are disabled");
require(
to.length == ids.length && ids.length == amounts.length,
"Mismatched input arrays"
);
for (uint256 i = 0; i < ids.length; i++) {
_safeTransferFrom(_owner, to[i], ids[i], amounts[i], "");
}
}
/*
* Allows to update contract configuration and revoke NFTPort's access to the contract.
* `baseURI` can only be updated if `metadataUpdatable` is true.
* Tokens can only be made to not be transferable or updatable, not vice-versa.
* Only callable by accounts with `UPDATE_CONTRACT_ROLE` or `ADMIN_ROLE`.
*/
function update(
Config.Runtime calldata newConfig,
RolesAddresses[] memory rolesAddresses,
bool isRevokeNFTPortPermissions
) public onlyRole(UPDATE_CONTRACT_ROLE) {
// If metadata is frozen, baseURI cannot be updated
require(
metadataUpdatable ||
(keccak256(abi.encodePacked(newConfig.baseURI)) ==
keccak256(abi.encodePacked(baseURI))),
"Metadata is frozen"
);
baseURI = newConfig.baseURI;
_setRoyalties(newConfig.royaltiesAddress, newConfig.royaltiesBps);
if (!newConfig.tokensTransferable) {
tokensTransferable = false;
}
if (!newConfig.metadataUpdatable && metadataUpdatable) {
metadataUpdatable = false;
emit PermanentURIGlobal();
}
_updateRoles(rolesAddresses);
if (isRevokeNFTPortPermissions) {
revokeNFTPortPermissions();
}
}
/*
* Allows to mint tokens by the contract `_owner`.
* Only callable by accounts with `MINT_ROLE` or `ADMIN_ROLE`.
*/
function mintByOwner(
address account,
uint256 id,
uint256 amount,
string memory tokenUri
) public onlyRole(MINT_ROLE) nonReentrant {
require(!_exists(id), "NFT: token already minted");
if (bytes(tokenUri).length > 0) {
_tokenURIs[id] = tokenUri;
emit URI(tokenUri, id);
}
tokenSupply[id] += amount;
_mint(account, id, amount, "");
}
/*
* Same functionality as `mintByOwner` but for a batch of tokens.
* Input `ids` and `values` must be in direct correlation,
* an index in both lists referring to the same token.
*/
function mintByOwnerBatch(
address[] memory to,
uint256[] memory ids,
uint256[] memory amounts,
string[] memory uris
) public onlyRole(MINT_ROLE) nonReentrant {
require(
to.length == ids.length &&
ids.length == amounts.length &&
amounts.length == uris.length,
"Mismatched input arrays"
);
for (uint256 i = 0; i < ids.length; i++) {
require(!_exists(ids[i]), "One of tokens is already minted");
require(
to[i] == address(to[i]),
"NFT: one of addresses is invalid"
);
require(amounts[i] > 0, "NFT: all amounts must be > 0");
tokenSupply[ids[i]] += amounts[i];
if (bytes(uris[i]).length > 0) {
_tokenURIs[ids[i]] = uris[i];
emit URI(uris[i], ids[i]);
}
_mint(to[i], ids[i], amounts[i], "");
}
}
/******************
* View functions *
******************/
// Returns total supply for the given token ID
function totalSupply(uint256 _id) public view returns (uint256) {
return tokenSupply[_id];
}
/*
* Returns token URI for the given token ID.
* If token URI is not empty and base URI is not empty then returns base URI + token URI,
* if base URI is empty and token URI is not empty then returns just token URI.
* If the token URI for the given token ID is empty then returns the default token URI.
*/
function uri(uint256 _id) public view override returns (string memory) {
if (bytes(_tokenURIs[_id]).length > 0) {
if (bytes(baseURI).length > 0) {
return string(abi.encodePacked(baseURI, _tokenURIs[_id]));
} else {
return _tokenURIs[_id];
}
} else {
return super.uri(_id);
}
}
// @dev ERC2981 token royalty info
function royaltyInfo(uint256, uint256 salePrice)
external
view
returns (address, uint256)
{
return (
royaltiesAddress,
(royaltiesBasisPoints * salePrice) / ROYALTIES_BASIS
);
}
/**
* @dev OpenSea contract metadata, returns a base64 encoded JSON string containing royalties basis points
* and royalties address
*/
function contractURI() external view returns (string memory) {
string memory json = Base64.encode(
bytes(
string(
abi.encodePacked(
// solium-disable-next-line quotes
'{"seller_fee_basis_points": ', // solhint-disable-line
royaltiesBasisPoints.toString(),
// solium-disable-next-line quotes
', "fee_recipient": "', // solhint-disable-line
uint256(uint160(royaltiesAddress)).toHexString(20),
// solium-disable-next-line quotes
'"}' // solhint-disable-line
)
)
)
);
string memory output = string(
abi.encodePacked("data:application/json;base64,", json)
);
return output;
}
function supportsInterface(bytes4 interfaceId)
public
view
override(ERC1155Upgradeable, AccessControlUpgradeable)
returns (bool)
{
return
ERC1155Upgradeable.supportsInterface(interfaceId) ||
interfaceId == type(IERC2981).interfaceId;
}
/*************
* Internals *
*************/
// Mapping of token ID to URI
mapping(uint256 => string) private _tokenURIs;
// Used for checking if token with given ID exists
function _exists(uint256 _tokenId) internal view virtual returns (bool) {
return tokenSupply[_tokenId] > 0;
}
function _setRoyalties(address newAddress, uint256 newBps) internal {
require(newBps <= ROYALTIES_BASIS, "Cannot set royalties to over 100%");
royaltiesAddress = newAddress;
royaltiesBasisPoints = newBps;
}
}