-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDropERC1155.sol
394 lines (325 loc) · 13.7 KB
/
DropERC1155.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
389
390
391
392
393
394
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.11;
/// @author thirdweb
import "@openzeppelin/contracts-upgradeable/token/ERC1155/ERC1155Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/MulticallUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/StringsUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/interfaces/IERC2981Upgradeable.sol";
// ========== Internal imports ==========
import "../openzeppelin-presets/metatx/ERC2771ContextUpgradeable.sol";
import "../lib/CurrencyTransferLib.sol";
// ========== Features ==========
import "../extension/ContractMetadata.sol";
import "../extension/PlatformFee.sol";
import "../extension/Royalty.sol";
import "../extension/PrimarySale.sol";
import "../extension/Ownable.sol";
import "../extension/LazyMint.sol";
import "../extension/PermissionsEnumerable.sol";
import "../extension/Drop1155.sol";
// OpenSea operator filter
import "../extension/DefaultOperatorFiltererUpgradeable.sol";
contract DropERC1155 is
Initializable,
ContractMetadata,
PlatformFee,
Royalty,
PrimarySale,
Ownable,
LazyMint,
PermissionsEnumerable,
Drop1155,
ERC2771ContextUpgradeable,
MulticallUpgradeable,
DefaultOperatorFiltererUpgradeable,
ERC1155Upgradeable
{
using StringsUpgradeable for uint256;
/*///////////////////////////////////////////////////////////////
State variables
//////////////////////////////////////////////////////////////*/
// Token name
string public name;
// Token symbol
string public symbol;
/// @dev Only transfers to or from TRANSFER_ROLE holders are valid, when transfers are restricted.
bytes32 private transferRole;
/// @dev Only MINTER_ROLE holders can sign off on `MintRequest`s and lazy mint tokens.
bytes32 private minterRole;
/// @dev Max bps in the thirdweb system.
uint256 private constant MAX_BPS = 10_000;
/*///////////////////////////////////////////////////////////////
Mappings
//////////////////////////////////////////////////////////////*/
/// @dev Mapping from token ID => total circulating supply of tokens with that ID.
mapping(uint256 => uint256) public totalSupply;
/// @dev Mapping from token ID => maximum possible total circulating supply of tokens with that ID.
mapping(uint256 => uint256) public maxTotalSupply;
/// @dev Mapping from token ID => the address of the recipient of primary sales.
mapping(uint256 => address) public saleRecipient;
/*///////////////////////////////////////////////////////////////
Events
//////////////////////////////////////////////////////////////*/
/// @dev Emitted when the global max supply of a token is updated.
event MaxTotalSupplyUpdated(uint256 tokenId, uint256 maxTotalSupply);
/// @dev Emitted when the sale recipient for a particular tokenId is updated.
event SaleRecipientForTokenUpdated(uint256 indexed tokenId, address saleRecipient);
/*///////////////////////////////////////////////////////////////
Constructor + initializer logic
//////////////////////////////////////////////////////////////*/
constructor() initializer {}
/// @dev Initiliazes the contract, like a constructor.
function initialize(
address _defaultAdmin,
string memory _name,
string memory _symbol,
string memory _contractURI,
address[] memory _trustedForwarders,
address _saleRecipient,
address _royaltyRecipient,
uint128 _royaltyBps,
uint128 _platformFeeBps,
address _platformFeeRecipient
) external initializer {
bytes32 _transferRole = keccak256("TRANSFER_ROLE");
bytes32 _minterRole = keccak256("MINTER_ROLE");
// Initialize inherited contracts, most base-like -> most derived.
__ERC2771Context_init(_trustedForwarders);
__ERC1155_init_unchained("");
__DefaultOperatorFilterer_init();
// Initialize this contract's state.
_setupContractURI(_contractURI);
_setupOwner(_defaultAdmin);
_setOperatorRestriction(true);
_setupRole(DEFAULT_ADMIN_ROLE, _defaultAdmin);
_setupRole(_minterRole, _defaultAdmin);
_setupRole(_transferRole, _defaultAdmin);
_setupRole(_transferRole, address(0));
_setupPlatformFeeInfo(_platformFeeRecipient, _platformFeeBps);
_setupDefaultRoyaltyInfo(_royaltyRecipient, _royaltyBps);
_setupPrimarySaleRecipient(_saleRecipient);
transferRole = _transferRole;
minterRole = _minterRole;
name = _name;
symbol = _symbol;
}
/*///////////////////////////////////////////////////////////////
ERC 165 / 1155 / 2981 logic
//////////////////////////////////////////////////////////////*/
/// @dev Returns the uri for a given tokenId.
function uri(uint256 _tokenId) public view override returns (string memory) {
string memory batchUri = _getBaseURI(_tokenId);
return string(abi.encodePacked(batchUri, _tokenId.toString()));
}
/// @dev See ERC 165
function supportsInterface(bytes4 interfaceId)
public
view
virtual
override(ERC1155Upgradeable, IERC165)
returns (bool)
{
return super.supportsInterface(interfaceId) || type(IERC2981Upgradeable).interfaceId == interfaceId;
}
/*///////////////////////////////////////////////////////////////
Contract identifiers
//////////////////////////////////////////////////////////////*/
function contractType() external pure returns (bytes32) {
return bytes32("DropERC1155");
}
function contractVersion() external pure returns (uint8) {
return uint8(4);
}
/*///////////////////////////////////////////////////////////////
Setter functions
//////////////////////////////////////////////////////////////*/
/// @dev Lets a module admin set a max total supply for token.
function setMaxTotalSupply(uint256 _tokenId, uint256 _maxTotalSupply) external onlyRole(DEFAULT_ADMIN_ROLE) {
maxTotalSupply[_tokenId] = _maxTotalSupply;
emit MaxTotalSupplyUpdated(_tokenId, _maxTotalSupply);
}
/// @dev Lets a contract admin set the recipient for all primary sales.
function setSaleRecipientForToken(uint256 _tokenId, address _saleRecipient) external onlyRole(DEFAULT_ADMIN_ROLE) {
saleRecipient[_tokenId] = _saleRecipient;
emit SaleRecipientForTokenUpdated(_tokenId, _saleRecipient);
}
/*///////////////////////////////////////////////////////////////
Internal functions
//////////////////////////////////////////////////////////////*/
/// @dev Runs before every `claim` function call.
function _beforeClaim(
uint256 _tokenId,
address,
uint256 _quantity,
address,
uint256,
AllowlistProof calldata,
bytes memory
) internal view override {
require(
maxTotalSupply[_tokenId] == 0 || totalSupply[_tokenId] + _quantity <= maxTotalSupply[_tokenId],
"exceed max total supply"
);
}
/// @dev Collects and distributes the primary sale value of NFTs being claimed.
function collectPriceOnClaim(
uint256 _tokenId,
address _primarySaleRecipient,
uint256 _quantityToClaim,
address _currency,
uint256 _pricePerToken
) internal override {
if (_pricePerToken == 0) {
return;
}
(address platformFeeRecipient, uint16 platformFeeBps) = getPlatformFeeInfo();
address _saleRecipient = _primarySaleRecipient == address(0)
? (saleRecipient[_tokenId] == address(0) ? primarySaleRecipient() : saleRecipient[_tokenId])
: _primarySaleRecipient;
uint256 totalPrice = _quantityToClaim * _pricePerToken;
uint256 platformFees = (totalPrice * platformFeeBps) / MAX_BPS;
if (_currency == CurrencyTransferLib.NATIVE_TOKEN) {
if (msg.value != totalPrice) {
revert("!Price");
}
}
CurrencyTransferLib.transferCurrency(_currency, _msgSender(), platformFeeRecipient, platformFees);
CurrencyTransferLib.transferCurrency(_currency, _msgSender(), _saleRecipient, totalPrice - platformFees);
}
/// @dev Transfers the NFTs being claimed.
function transferTokensOnClaim(
address _to,
uint256 _tokenId,
uint256 _quantityBeingClaimed
) internal override {
_mint(_to, _tokenId, _quantityBeingClaimed, "");
}
/// @dev Checks whether platform fee info can be set in the given execution context.
function _canSetPlatformFeeInfo() internal view override returns (bool) {
return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
}
/// @dev Checks whether primary sale recipient can be set in the given execution context.
function _canSetPrimarySaleRecipient() internal view
override returns (bool) {
return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
}
/// @dev Checks whether owner can be set in the given execution context.
function _canSetOwner() internal view override returns (bool) {
return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
}
/// @dev Checks whether royalty info can be set in the given execution context.
function _canSetRoyaltyInfo() internal view override returns (bool) {
return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
}
/// @dev Checks whether contract metadata can be set in the given execution context.
function _canSetContractURI() internal view override returns (bool) {
return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
}
/// @dev Checks whether platform fee info can be set in the given execution context.
function _canSetClaimConditions() internal view override returns (bool) {
return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
}
/// @dev Returns whether lazy minting can be done in the given execution context.
function _canLazyMint() internal view virtual override returns (bool) {
return hasRole(minterRole, _msgSender());
}
/// @dev Returns whether operator restriction can be set in the given execution context.
function _canSetOperatorRestriction() internal virtual override returns (bool) {
return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
}
/*///////////////////////////////////////////////////////////////
Miscellaneous
//////////////////////////////////////////////////////////////*/
/// @dev The tokenId of the next NFT that will be minted / lazy minted.
function nextTokenIdToMint() external view returns (uint256) {
return nextTokenIdToLazyMint;
}
/// @dev Lets a token owner burn multiple tokens they own at once (i.e. destroy for good)
function burnBatch(
address account,
uint256[] memory ids,
uint256[] memory values
) public virtual {
require(
account == _msgSender() || isApprovedForAll(account, _msgSender()),
"ERC1155: caller is not owner nor approved."
);
_burnBatch(account, ids, values);
}
/**
* @dev See {ERC1155-_beforeTokenTransfer}.
*/
function _beforeTokenTransfer(
address operator,
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) internal virtual override {
super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
// if transfer is restricted on the contract, we still want to allow burning and minting
if (!hasRole(transferRole, address(0)) && from != address(0) && to != address(0)) {
require(hasRole(transferRole, from) || hasRole(transferRole, to), "restricted to TRANSFER_ROLE holders.");
}
if (from == address(0)) {
for (uint256 i = 0; i < ids.length; ++i) {
totalSupply[ids[i]] += amounts[i];
}
}
if (to == address(0)) {
for (uint256 i = 0; i < ids.length; ++i) {
totalSupply[ids[i]] -= amounts[i];
}
}
}
/// @dev See {ERC1155-setApprovalForAll}
function setApprovalForAll(address operator, bool approved) public override onlyAllowedOperatorApproval(operator) {
super.setApprovalForAll(operator, approved);
}
/**
* @dev See {IERC1155-safeTransferFrom}.
*/
function safeTransferFrom(
address from,
address to,
uint256 id,
uint256 amount,
bytes memory data
) public override(ERC1155Upgradeable) onlyAllowedOperator(from) {
super.safeTransferFrom(from, to, id, amount, data);
}
/**
* @dev See {IERC1155-safeBatchTransferFrom}.
*/
function safeBatchTransferFrom(
address from,
address to,
uint256[] memory ids,
uint256[] memory amounts,
bytes memory data
) public override(ERC1155Upgradeable) onlyAllowedOperator(from) {
super.safeBatchTransferFrom(from, to, ids, amounts, data);
}
function _dropMsgSender() internal view virtual override returns (address) {
return _msgSender();
}
function _msgSender()
internal
view
virtual
override(ContextUpgradeable, ERC2771ContextUpgradeable)
returns (address sender)
{
return ERC2771ContextUpgradeable._msgSender();
}
function _msgData()
internal
view
virtual
override(ContextUpgradeable, ERC2771ContextUpgradeable)
returns (bytes calldata)
{
return ERC2771ContextUpgradeable._msgData();
}
}