-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathERC721NFTProduct.sol
More file actions
303 lines (258 loc) · 9.7 KB
/
Copy pathERC721NFTProduct.sol
File metadata and controls
303 lines (258 loc) · 9.7 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
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {IERC2981} from "@openzeppelin/contracts/token/common/ERC2981.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC721/extensions/ERC721URIStorageUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
import "../lib/Base64.sol";
import "../lib/GranularRoles.sol";
import "../lib/Config.sol";
import "../lib/ITemplate.sol";
/*
* ERC-721 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 ERC721NFTProduct is
ERC721URIStorageUpgradeable,
GranularRoles,
ITemplate,
ReentrancyGuardUpgradeable
{
/*******************************
* Extensions, structs, events *
*******************************/
using StringsUpgradeable 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 = "ERC721NFTProduct";
// Template version
uint256 public constant VERSION = 1_01_00;
// Basis for calculating royalties.
// This has to be 10k for royaltiesBps to be in basis points.
uint16 public constant ROYALTIES_BASIS = 10000;
/********************
* 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;
// Mapping of individually frozen tokens
mapping(uint256 => bool) public freezeTokenUris;
// Base URI of the tokens, token URIs are calculated 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;
// Counter for the total number of tokens minted in this contract
uint256 public totalSupply;
/***************************
* 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 {
// @dev Initializes the contract by setting a `name` and a `symbol` to the token collection.
__ERC721_init(deploymentConfig.name, deploymentConfig.symbol);
__ReentrancyGuard_init();
_setRoyalties(
runtimeConfig.royaltiesAddress,
runtimeConfig.royaltiesBps
);
metadataUpdatable = runtimeConfig.metadataUpdatable;
tokensBurnable = deploymentConfig.tokensBurnable;
tokensTransferable = runtimeConfig.tokensTransferable;
baseURI = runtimeConfig.baseURI;
_initRoles(deploymentConfig.owner, rolesAddresses);
}
/*******************
* Write functions *
*******************/
// Mint a token to input `caller` address
function mintToCaller(
address caller,
uint256 tokenId,
string memory tokenURI
) public onlyRole(MINT_ROLE) nonReentrant returns (uint256) {
_safeMint(caller, tokenId);
totalSupply += 1;
_setTokenURI(tokenId, tokenURI);
return tokenId;
}
/*
* Function to update token URIs for individual tokens,
* can be used to update and optionally freeze token URIs
*
* only callable if `metadataUpdatable` is true, the medatadata
* for the token has not been frozen previously and the caller has `UPDATE_TOKEN_ROLE` (or `ADMIN_ROLE`) role
*/
function updateTokenUri(
uint256 _tokenId,
string memory _tokenUri,
bool _isFreezeTokenUri
) public onlyRole(UPDATE_TOKEN_ROLE) {
require(_exists(_tokenId), "Token: Token does not exist");
require(metadataUpdatable, "Token: Metadata is frozen");
require(freezeTokenUris[_tokenId] != true, "Token: Token is frozen");
require(
_isFreezeTokenUri || (bytes(_tokenUri).length != 0),
"Token: Token URI is missing"
);
if (bytes(_tokenUri).length != 0) {
_setTokenURI(_tokenId, _tokenUri);
}
if (_isFreezeTokenUri) {
freezeTokenUris[_tokenId] = true;
emit PermanentURI(tokenURI(_tokenId), _tokenId);
}
}
/*
* Function to transfer tokens owned by the `_owner` address.
*
* only callable if `tokensTransferable` is true, the token to be transferred
* is owned by `_owner` and the caller has `TRANSFER_ROLE` (or `ADMIN_ROLE`) role
*/
function transferByOwner(address _to, uint256 _tokenId)
public
onlyRole(TRANSFER_ROLE)
{
require(tokensTransferable, "Transfer: Transfers are disabled");
_safeTransfer(_owner, _to, _tokenId, "");
}
/*
* Function to burn tokens owned by the `_owner` address.
*
* only callable if `tokensBurnable` is true, the token to be burned
* is owned by `_owner` and the caller has `BURN_ROLE` (or `ADMIN_ROLE`) role
*/
function burn(uint256 _tokenId) public onlyRole(BURN_ROLE) {
require(tokensBurnable, "Burn: Burns are disabled");
require(_exists(_tokenId), "Burn: Token does not exist");
require(
ERC721Upgradeable.ownerOf(_tokenId) == _owner,
"Burn: not held by contract owner"
);
_burn(_tokenId);
totalSupply -= 1;
}
/*
* Function to update the collection configuration.
*
* Only callable if `metadataUpdatable` is true, or `baseURI` is not updated.
* The ability to transfer tokens or update metadata can only be turned OFF with this, not vice-versa.
*
* This can also be used to revoke NFTPort access to the contract,
* meaning access rights for NFTPort account will be removed.
*/
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))),
"Update: 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();
}
}
/******************
* View functions *
******************/
function supportsInterface(bytes4 interfaceId)
public
view
override(ERC721Upgradeable, AccessControlUpgradeable)
returns (bool)
{
return
ERC721Upgradeable.supportsInterface(interfaceId) ||
interfaceId == type(IERC2981).interfaceId;
}
// @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(
'{"seller_fee_basis_points": ', // solhint-disable-line quotes
royaltiesBasisPoints.toString(),
', "fee_recipient": "', // solhint-disable-line quotes
uint256(uint160(royaltiesAddress)).toHexString(20),
'"}' // solhint-disable-line quotes
)
)
)
);
string memory output = string(
abi.encodePacked("data:application/json;base64,", json)
);
return output;
}
/*************
* Internals *
*************/
function _baseURI()
internal
view
virtual
override(ERC721Upgradeable)
returns (string memory)
{
return baseURI;
}
function _setRoyalties(address newAddress, uint newBps) internal {
require(newBps <= ROYALTIES_BASIS, "Cannot set royalties to over 100%");
royaltiesAddress = newAddress;
royaltiesBasisPoints = newBps;
}
}