-
Notifications
You must be signed in to change notification settings - Fork 86
/
Asset.sol
388 lines (353 loc) · 15.6 KB
/
Asset.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
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
//SPDX-License-Identifier: MIT
pragma solidity 0.8.18;
import {
AccessControlUpgradeable,
ContextUpgradeable
} from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import {Address} from "@openzeppelin/contracts/utils/Address.sol";
import {
ERC1155BurnableUpgradeable,
ERC1155Upgradeable
} from "@openzeppelin/contracts-upgradeable/token/ERC1155/extensions/ERC1155BurnableUpgradeable.sol";
import {
ERC1155SupplyUpgradeable
} from "@openzeppelin/contracts-upgradeable/token/ERC1155/extensions/ERC1155SupplyUpgradeable.sol";
import {
ERC1155URIStorageUpgradeable
} from "@openzeppelin/contracts-upgradeable/token/ERC1155/extensions/ERC1155URIStorageUpgradeable.sol";
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {
ERC2771HandlerUpgradeable
} from "@sandbox-smart-contracts/dependency-metatx/contracts/ERC2771HandlerUpgradeable.sol";
import {
MultiRoyaltyDistributor
} from "@sandbox-smart-contracts/dependency-royalty-management/contracts/MultiRoyaltyDistributor.sol";
import {
OperatorFiltererUpgradeable
} from "@sandbox-smart-contracts/dependency-operator-filter/contracts/OperatorFiltererUpgradeable.sol";
import {TokenIdUtils} from "./libraries/TokenIdUtils.sol";
import {IAsset} from "./interfaces/IAsset.sol";
import {ITokenUtils, IRoyaltyUGC} from "./interfaces/ITokenUtils.sol";
/// @title Asset
/// @author The Sandbox
/// @notice ERC1155 asset token contract
/// @notice Minting and burning tokens is only allowed through separate authorized contracts
/// @dev This contract is final and should not be inherited
contract Asset is
IAsset,
Initializable,
ERC2771HandlerUpgradeable,
ERC1155BurnableUpgradeable,
AccessControlUpgradeable,
ERC1155SupplyUpgradeable,
ERC1155URIStorageUpgradeable,
OperatorFiltererUpgradeable,
MultiRoyaltyDistributor,
ITokenUtils
{
using TokenIdUtils for uint256;
using Address for address;
bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
bytes32 public constant BURNER_ROLE = keccak256("BURNER_ROLE");
bytes32 public constant MODERATOR_ROLE = keccak256("MODERATOR_ROLE");
// mapping of ipfs metadata token hash to token id
mapping(string => uint256) public hashUsed;
/// @custom:oz-upgrades-unsafe-allow constructor
constructor() {
_disableInitializers();
}
/// @notice Initialize the contract
/// @param forwarder The address of the trusted forwarder
/// @param assetAdmin The address of the asset admin
/// @param baseUri The base URI for the token metadata
/// @param commonSubscription The address of the operator filter subscription
/// @param manager The address of the royalty manager
function initialize(
address forwarder,
address assetAdmin,
string memory baseUri,
address commonSubscription,
address manager
) external initializer {
__ERC2771Handler_init(forwarder);
_grantRole(DEFAULT_ADMIN_ROLE, assetAdmin);
_setBaseURI(baseUri);
__OperatorFilterer_init(commonSubscription, true);
__MultiRoyaltyDistributor_init(manager);
__AccessControl_init();
__ERC1155Supply_init();
__ERC1155Burnable_init();
}
/// @notice Mint new tokens
/// @dev Only callable by the minter role
/// @param to The address of the recipient
/// @param id The id of the token to mint
/// @param amount The amount of the token to mint
/// @param metadataHash The metadata hash of the token to mint
function mint(
address to,
uint256 id,
uint256 amount,
string memory metadataHash
) external onlyRole(MINTER_ROLE) {
_setMetadataHash(id, metadataHash);
_mint(to, id, amount, "");
address creator = id.getCreatorAddress();
_setTokenRoyalties(id, payable(creator), creator);
}
/// @notice Mint new tokens with catalyst tier chosen by the creator
/// @dev Only callable by the minter role
/// @param to The address of the recipient
/// @param ids The ids of the tokens to mint
/// @param amounts The amounts of the tokens to mint
/// @param metadataHashes The metadata hashes of the tokens to mint
function mintBatch(
address to,
uint256[] memory ids,
uint256[] memory amounts,
string[] memory metadataHashes
) external onlyRole(MINTER_ROLE) {
require(ids.length == metadataHashes.length, "Asset: 1-Array mismatch");
require(ids.length == amounts.length, "Asset: 2-Array mismatch");
for (uint256 i = 0; i < ids.length; i++) {
_setMetadataHash(ids[i], metadataHashes[i]);
}
_mintBatch(to, ids, amounts, "");
for (uint256 i; i < ids.length; i++) {
address creator = ids[i].getCreatorAddress();
_setTokenRoyalties(ids[i], payable(creator), creator);
}
}
/// @notice Burn a token from a given account
/// @dev Only the minter role can burn tokens
/// @dev This function was added with token recycling and bridging in mind but may have other use cases
/// @param account The account to burn tokens from
/// @param id The token id to burn
/// @param amount The amount of tokens to burn
function burnFrom(
address account,
uint256 id,
uint256 amount
) external onlyRole(BURNER_ROLE) {
_burn(account, id, amount);
}
/// @notice Burn a batch of tokens from a given account
/// @dev Only the minter role can burn tokens
/// @dev This function was added with token recycling and bridging in mind but may have other use cases
/// @dev The length of the ids and amounts arrays must be the same
/// @param account The account to burn tokens from
/// @param ids An array of token ids to burn
/// @param amounts An array of amounts of tokens to burn
function burnBatchFrom(
address account,
uint256[] memory ids,
uint256[] memory amounts
) external onlyRole(BURNER_ROLE) {
_burnBatch(account, ids, amounts);
}
/// @notice Set a new URI for specific tokenid
/// @dev The metadata hash should be the IPFS CIDv1 base32 encoded hash
/// @param tokenId The token id to set URI for
/// @param metadata The new URI for asset's metadata
function setTokenURI(uint256 tokenId, string memory metadata) external onlyRole(MODERATOR_ROLE) {
_setURI(tokenId, metadata);
}
/// @notice Set a new base URI
/// @param baseURI The new base URI
function setBaseURI(string memory baseURI) external onlyRole(DEFAULT_ADMIN_ROLE) {
_setBaseURI(baseURI);
}
/// @notice returns full token URI, including baseURI and token metadata URI
/// @param tokenId The token id to get URI for
/// @return tokenURI the URI of the token
function uri(uint256 tokenId)
public
view
override(ERC1155Upgradeable, ERC1155URIStorageUpgradeable)
returns (string memory tokenURI)
{
return ERC1155URIStorageUpgradeable.uri(tokenId);
}
/// @notice returns the tokenId associated with provided metadata hash
/// @param metadataHash The metadata hash to get tokenId for
/// @return tokenId the tokenId associated with the metadata hash
function getTokenIdByMetadataHash(string memory metadataHash) public view returns (uint256 tokenId) {
return hashUsed[metadataHash];
}
/// @notice sets the metadata hash for a given tokenId
/// @param tokenId The tokenId to set metadata hash for
/// @param metadataHash The metadata hash to set
function _setMetadataHash(uint256 tokenId, string memory metadataHash) internal {
if (hashUsed[metadataHash] != 0) {
require(hashUsed[metadataHash] == tokenId, "Asset: Hash already used");
} else {
hashUsed[metadataHash] = tokenId;
_setURI(tokenId, metadataHash);
}
}
/// @notice Set a new trusted forwarder address, limited to DEFAULT_ADMIN_ROLE only
/// @dev Change the address of the trusted forwarder for meta-TX
/// @param trustedForwarder The new trustedForwarder
function setTrustedForwarder(address trustedForwarder) external onlyRole(DEFAULT_ADMIN_ROLE) {
require(trustedForwarder.isContract(), "Asset: Bad forwarder address");
_setTrustedForwarder(trustedForwarder);
}
/// @notice Query if a contract implements interface `id`.
/// @param id the interface identifier, as specified in ERC-165.
/// @return supported `true` if the contract implements `id`.
function supportsInterface(bytes4 id)
public
view
virtual
override(ERC1155Upgradeable, AccessControlUpgradeable, MultiRoyaltyDistributor)
returns (bool supported)
{
return id == type(IRoyaltyUGC).interfaceId || super.supportsInterface(id);
}
function _msgSender()
internal
view
virtual
override(ContextUpgradeable, ERC2771HandlerUpgradeable)
returns (address sender)
{
return ERC2771HandlerUpgradeable._msgSender();
}
function _msgData()
internal
view
virtual
override(ContextUpgradeable, ERC2771HandlerUpgradeable)
returns (bytes calldata msgData)
{
return ERC2771HandlerUpgradeable._msgData();
}
function _beforeTokenTransfer(
address operator,
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal override(ERC1155Upgradeable, ERC1155SupplyUpgradeable) {
super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
}
/// @notice Transfers `values` tokens of type `ids` from `from` to `to` (with safety call).
/// @dev call data should be optimized to order ids so packedBalance can be used efficiently.
/// @param from address from which tokens are transfered.
/// @param to address to which the token will be transfered.
/// @param ids ids of each token type transfered.
/// @param amounts amount of each token type transfered.
/// @param data additional data accompanying the transfer.
function safeBatchTransferFrom(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) public virtual override onlyAllowedOperator(from) {
super.safeBatchTransferFrom(from, to, ids, amounts, data);
}
/// @notice Enable or disable approval for `operator` to manage all of the caller's tokens.
/// @param operator address which will be granted rights to transfer all tokens of the caller.
/// @param approved whether to approve or revoke
function setApprovalForAll(address operator, bool approved)
public
virtual
override
onlyAllowedOperatorApproval(operator)
{
_setApprovalForAll(_msgSender(), operator, approved);
}
/// @notice Transfers `value` tokens of type `id` from `from` to `to` (with safety call).
/// @param from address from which tokens are transfered.
/// @param to address to which the token will be transfered.
/// @param id the token type transfered.
/// @param amount amount of token transfered.
/// @param data additional data accompanying the transfer.
function safeTransferFrom(
address from,
address to,
uint256 id,
uint256 amount,
bytes memory data
) public virtual override onlyAllowedOperator(from) {
require(from == _msgSender() || isApprovedForAll(from, _msgSender()), "Asset: Transfer error");
_safeTransferFrom(from, to, id, amount, data);
}
/// @notice could be used to deploy splitter and set tokens royalties
/// @param tokenId the id of the token for which the EIP2981 royalty is set for.
/// @param recipient the royalty recipient for the splitter of the creator.
/// @param creator the creator of the tokens.
function setTokenRoyalties(
uint256 tokenId,
address payable recipient,
address creator
) external override onlyRole(DEFAULT_ADMIN_ROLE) {
_setTokenRoyalties(tokenId, recipient, creator);
}
/// @notice Extracts the creator address from a given token id
/// @param tokenId The token id to extract the creator address from
/// @return creator The asset creator address
function getCreatorAddress(uint256 tokenId) external pure returns (address creator) {
return TokenIdUtils.getCreatorAddress(tokenId);
}
/// @notice Extracts the tier from a given token id
/// @param tokenId The token id to extract the tier from
/// @return tier The asset tier, determined by the catalyst used to create it
function getTier(uint256 tokenId) external pure returns (uint8 tier) {
return TokenIdUtils.getTier(tokenId);
}
/// @notice Extracts the revealed flag from a given token id
/// @param tokenId The token id to extract the revealed flag from
/// @return revealed Whether the asset is revealed or not
function isRevealed(uint256 tokenId) external pure returns (bool revealed) {
return TokenIdUtils.isRevealed(tokenId);
}
/// @notice Extracts the asset nonce from a given token id
/// @param tokenId The token id to extract the asset nonce from
/// @return creatorNonce The asset creator nonce
function getCreatorNonce(uint256 tokenId) external pure returns (uint16 creatorNonce) {
return TokenIdUtils.getCreatorNonce(tokenId);
}
/// @notice Extracts the abilities and enhancements hash from a given token id
/// @param tokenId The token id to extract reveal nonce from
/// @return revealNonce The reveal nonce of the asset
function getRevealNonce(uint256 tokenId) external pure returns (uint16 revealNonce) {
return TokenIdUtils.getRevealNonce(tokenId);
}
/// @notice Extracts the bridged flag from a given token id
/// @param tokenId The token id to extract the bridged flag from
/// @return bridged Whether the asset is bridged or not
function isBridged(uint256 tokenId) external pure returns (bool bridged) {
return TokenIdUtils.isBridged(tokenId);
}
/// @notice This function is used to register Asset contract on the Operator Filterer Registry of OpenSea. Can only be called by admin.
/// @dev used to register contract and subscribe to the subscriptionOrRegistrantToCopy's black list.
/// @param subscriptionOrRegistrantToCopy registration address of the list to subscribe.
/// @param subscribe bool to signify subscription "true"" or to copy the list "false".
function registerAndSubscribe(address subscriptionOrRegistrantToCopy, bool subscribe)
external
onlyRole(DEFAULT_ADMIN_ROLE)
{
require(subscriptionOrRegistrantToCopy.isContract(), "Asset: Bad subscription address");
_registerAndSubscribe(subscriptionOrRegistrantToCopy, subscribe);
}
/// @notice sets the operator filter registry address
/// @param registry the address of the registry
function setOperatorRegistry(address registry) external onlyRole(DEFAULT_ADMIN_ROLE) {
require(registry.isContract(), "Asset: Bad registry address");
OperatorFiltererUpgradeable._setOperatorFilterRegistry(registry);
}
/// @notice A descriptive name for the collection of tokens in this contract.
/// @return _name the name of the tokens.
function name() external pure returns (string memory _name) {
return "The Sandbox's ASSETs";
}
/// @notice An abbreviated name for the collection of tokens in this contract.
/// @return _symbol the symbol of the tokens.
function symbol() external pure returns (string memory _symbol) {
return "ASSET";
}
uint256[49] private __gap;
}