/
TieredDropLogic.sol
598 lines (478 loc) · 22.2 KB
/
TieredDropLogic.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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.11;
import { TieredDropStorage } from "./TieredDropStorage.sol";
import { ERC721AStorage } from "../../eip/ERC721AUpgradeable.sol";
import "../../lib/TWStrings.sol";
import "../../lib/CurrencyTransferLib.sol";
import { IERC2981 } from "../../eip/interface/IERC2981.sol";
import { Context, ERC721AUpgradeable } from "../../eip/ERC721AUpgradeable.sol";
import { IERC2771Context } from "../../extension/interface/IERC2771Context.sol";
import { ERC2771ContextUpgradeable } from "../../extension/ERC2771ContextUpgradeable.sol";
import { DelayedReveal } from "../../extension/DelayedReveal.sol";
import { PrimarySale } from "../../extension/PrimarySale.sol";
import { Royalty, IERC165 } from "../../extension/Royalty.sol";
import { Permissions } from "../../extension/Permissions.sol";
import { LazyMintWithTier } from "../../extension/LazyMintWithTier.sol";
import { ContractMetadata } from "../../extension/ContractMetadata.sol";
import { Ownable } from "../../extension/Ownable.sol";
import { SignatureActionUpgradeable } from "../../extension/SignatureActionUpgradeable.sol";
import { DefaultOperatorFiltererUpgradeable } from "../../extension/DefaultOperatorFiltererUpgradeable.sol";
contract TieredDropLogic is
Royalty,
PrimarySale,
DelayedReveal,
LazyMintWithTier,
ContractMetadata,
Ownable,
SignatureActionUpgradeable,
DefaultOperatorFiltererUpgradeable,
ERC2771ContextUpgradeable,
ERC721AUpgradeable
{
using TWStrings for uint256;
/*///////////////////////////////////////////////////////////////
Constants
//////////////////////////////////////////////////////////////*/
/// @dev Default admin role for all roles. Only accounts with this role can grant/revoke other roles.
bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
/// @dev Only transfers to or from TRANSFER_ROLE holders are valid, when transfers are restricted.
bytes32 private constant transferRole = keccak256("TRANSFER_ROLE");
/// @dev Only MINTER_ROLE holders can sign off on `MintRequest`s and lazy mint tokens.
bytes32 private constant minterRole = keccak256("MINTER_ROLE");
/// @dev Only transfers initiated by operator role hodlers are valid, when operator-initated transfers are restricted.
bytes32 private constant operatorRole = keccak256("OPERATOR_ROLE");
/*///////////////////////////////////////////////////////////////
Events
//////////////////////////////////////////////////////////////*/
/// @notice Emitted when tokens are claimed via `claimWithSignature`.
event TokensClaimed(
address indexed claimer,
address indexed receiver,
uint256 startTokenId,
uint256 quantityClaimed,
string[] tiersInPriority
);
/*///////////////////////////////////////////////////////////////
ERC 165 / 721 / 2981 logic
//////////////////////////////////////////////////////////////*/
/// @dev Returns the URI for a given tokenId.
function tokenURI(uint256 _tokenId) public view override returns (string memory) {
// Retrieve metadata ID for token.
uint256 metadataId = _getMetadataId(_tokenId);
// Use metadata ID to return token metadata.
(uint256 batchId, uint256 index) = _getBatchId(metadataId);
string memory batchUri = _getBaseURI(metadataId);
if (isEncryptedBatch(batchId)) {
return string(abi.encodePacked(batchUri, "0"));
} else {
uint256 fairMetadataId = _getFairMetadataId(metadataId, batchId, index);
return string(abi.encodePacked(batchUri, fairMetadataId.toString()));
}
}
/// @dev See ERC 165
function supportsInterface(bytes4 interfaceId)
public
view
virtual
override(ERC721AUpgradeable, IERC165)
returns (bool)
{
return super.supportsInterface(interfaceId) || type(IERC2981).interfaceId == interfaceId;
}
/*///////////////////////////////////////////////////////////////
Lazy minting + delayed-reveal logic
//////////////////////////////////////////////////////////////*
/**
* @dev Lets an account with `MINTER_ROLE` lazy mint 'n' NFTs.
* The URIs for each token is the provided `_baseURIForTokens` + `{tokenId}`.
*/
function lazyMint(
uint256 _amount,
string calldata _baseURIForTokens,
string calldata _tier,
bytes calldata _data
) public override returns (uint256 batchId) {
TieredDropStorage.Data storage data = TieredDropStorage.tieredDropStorage();
uint256 nextId = nextTokenIdToLazyMint();
if (_data.length > 0) {
(bytes memory encryptedURI, bytes32 provenanceHash) = abi.decode(_data, (bytes, bytes32));
if (encryptedURI.length != 0 && provenanceHash != "") {
_setEncryptedData(nextId + _amount, _data);
}
}
data.totalRemainingInTier[_tier] += _amount;
uint256 startId = nextId;
if (isTierEmpty(_tier) || data.nextMetadataIdToMapFromTier[_tier] == type(uint256).max) {
data.nextMetadataIdToMapFromTier[_tier] = startId;
}
return super.lazyMint(_amount, _baseURIForTokens, _tier, _data);
}
/// @dev Lets an account with `MINTER_ROLE` reveal the URI for a batch of 'delayed-reveal' NFTs.
function reveal(uint256 _index, bytes calldata _key) external returns (string memory revealedURI) {
require(Permissions(address(this)).hasRole(minterRole, _msgSender()), "not minter.");
uint256 batchId = getBatchIdAtIndex(_index);
revealedURI = getRevealURI(batchId, _key);
_setEncryptedData(batchId, "");
_setBaseURI(batchId, revealedURI);
_scrambleOffset(batchId, _key);
emit TokenURIRevealed(_index, revealedURI);
}
/*///////////////////////////////////////////////////////////////
Claiming lazy minted tokens logic
//////////////////////////////////////////////////////////////*/
/// @dev Claim lazy minted tokens via signature.
function claimWithSignature(GenericRequest calldata _req, bytes calldata _signature)
external
payable
returns (address signer)
{
ERC721AStorage.Data storage data = ERC721AStorage.erc721AStorage();
(
string[] memory tiersInPriority,
address to,
address royaltyRecipient,
uint256 royaltyBps,
address primarySaleRecipient,
uint256 quantity,
uint256 totalPrice,
address currency
) = abi.decode(_req.data, (string[], address, address, uint256, address, uint256, uint256, address));
if (quantity == 0) {
revert("0 qty");
}
uint256 tokenIdToMint = data._currentIndex;
if (tokenIdToMint + quantity > nextTokenIdToLazyMint()) {
revert("!Tokens");
}
// Verify and process payload.
signer = _processRequest(_req, _signature);
// Collect price
collectPriceOnClaim(primarySaleRecipient, currency, totalPrice);
// Set royalties, if applicable.
if (royaltyRecipient != address(0) && royaltyBps != 0) {
_setupRoyaltyInfoForToken(tokenIdToMint, royaltyRecipient, royaltyBps);
}
// Mint tokens.
transferTokensOnClaim(to, quantity, tiersInPriority);
emit TokensClaimed(_msgSender(), to, tokenIdToMint, quantity, tiersInPriority);
}
/*///////////////////////////////////////////////////////////////
Internal functions
//////////////////////////////////////////////////////////////*/
/// @dev Collects and distributes the primary sale value of NFTs being claimed.
function collectPriceOnClaim(
address _primarySaleRecipient,
address _currency,
uint256 _totalPrice
) internal {
if (_totalPrice == 0) {
return;
}
address saleRecipient = _primarySaleRecipient == address(0) ? primarySaleRecipient() : _primarySaleRecipient;
if (_currency == CurrencyTransferLib.NATIVE_TOKEN) {
if (msg.value != _totalPrice) {
revert("!Price");
}
}
CurrencyTransferLib.transferCurrency(_currency, _msgSender(), saleRecipient, _totalPrice);
}
/// @dev Transfers the NFTs being claimed.
function transferTokensOnClaim(
address _to,
uint256 _totalQuantityBeingClaimed,
string[] memory _tiers
) internal {
TieredDropStorage.Data storage data = TieredDropStorage.tieredDropStorage();
ERC721AStorage.Data storage dataERC721A = ERC721AStorage.erc721AStorage();
uint256 startTokenIdToMint = dataERC721A._currentIndex;
uint256 startIdToMap = startTokenIdToMint;
uint256 remaningToDistribute = _totalQuantityBeingClaimed;
for (uint256 i = 0; i < _tiers.length; i += 1) {
string memory tier = _tiers[i];
uint256 qtyFulfilled = _getQuantityFulfilledByTier(tier, remaningToDistribute);
if (qtyFulfilled == 0) {
continue;
}
remaningToDistribute -= qtyFulfilled;
_mapTokensToTier(tier, startIdToMap, qtyFulfilled);
data.totalRemainingInTier[tier] -= qtyFulfilled;
data.totalsForTier[keccak256(abi.encodePacked(tier, "minted"))] += qtyFulfilled;
if (remaningToDistribute > 0) {
startIdToMap += qtyFulfilled;
} else {
break;
}
}
require(remaningToDistribute == 0, "Insufficient tokens in tiers.");
_safeMint(_to, _totalQuantityBeingClaimed);
}
/// @dev Maps lazy minted metadata to NFT tokenIds.
function _mapTokensToTier(
string memory _tier,
uint256 _startIdToMap,
uint256 _quantity
) private {
TieredDropStorage.Data storage data = TieredDropStorage.tieredDropStorage();
uint256 nextIdFromTier = data.nextMetadataIdToMapFromTier[_tier];
uint256 startTokenId = _startIdToMap;
TokenRange[] memory tokensInTier = tokensInTier(_tier);
uint256 len = tokensInTier.length;
uint256 qtyRemaining = _quantity;
for (uint256 i = 0; i < len; i += 1) {
TokenRange memory range = tokensInTier[i];
uint256 gap = 0;
if (range.startIdInclusive <= nextIdFromTier && nextIdFromTier < range.endIdNonInclusive) {
uint256 proxyStartId = nextIdFromTier;
uint256 proxyEndId = proxyStartId + qtyRemaining <= range.endIdNonInclusive
? proxyStartId + qtyRemaining
: range.endIdNonInclusive;
gap = proxyEndId - proxyStartId;
uint256 endTokenId = startTokenId + gap;
data.endIdsAtMint[data.lengthEndIdsAtMint] = endTokenId;
data.lengthEndIdsAtMint += 1;
data.tierAtEndId[endTokenId] = _tier;
data.proxyTokenRange[endTokenId] = TokenRange(proxyStartId, proxyEndId);
startTokenId += gap;
qtyRemaining -= gap;
if (nextIdFromTier + gap < range.endIdNonInclusive) {
nextIdFromTier += gap;
} else if (i < (len - 1)) {
nextIdFromTier = tokensInTier[i + 1].startIdInclusive;
} else {
nextIdFromTier = type(uint256).max;
}
}
if (qtyRemaining == 0) {
data.nextMetadataIdToMapFromTier[_tier] = nextIdFromTier;
break;
}
}
}
/// @dev Returns how much of the total-quantity-to-distribute can come from the given tier.
function _getQuantityFulfilledByTier(string memory _tier, uint256 _quantity)
private
view
returns (uint256 fulfilled)
{
TieredDropStorage.Data storage data = TieredDropStorage.tieredDropStorage();
uint256 total = data.totalRemainingInTier[_tier];
if (total >= _quantity) {
fulfilled = _quantity;
} else {
fulfilled = total;
}
}
/// @dev Returns the tier that the given token is associated with.
function getTierForToken(uint256 _tokenId) external view returns (string memory) {
TieredDropStorage.Data storage data = TieredDropStorage.tieredDropStorage();
uint256 len = data.lengthEndIdsAtMint;
for (uint256 i = 0; i < len; i += 1) {
uint256 endId = data.endIdsAtMint[i];
if (_tokenId < endId) {
return data.tierAtEndId[endId];
}
}
revert("!Tier");
}
/// @dev Returns the max `endIndex` that can be used with getTokensInTier.
function getTokensInTierLen() external view returns (uint256) {
TieredDropStorage.Data storage data = TieredDropStorage.tieredDropStorage();
return data.lengthEndIdsAtMint;
}
/// @dev Returns all tokenIds that belong to the given tier.
function getTokensInTier(
string memory _tier,
uint256 _startIdx,
uint256 _endIdx
) external view returns (TokenRange[] memory ranges) {
TieredDropStorage.Data storage data = TieredDropStorage.tieredDropStorage();
uint256 len = data.lengthEndIdsAtMint;
require(_startIdx < _endIdx && _endIdx <= len, "TieredDrop: invalid indices.");
uint256 numOfRangesForTier = 0;
bytes32 hashOfTier = keccak256(abi.encodePacked(_tier));
for (uint256 i = _startIdx; i < _endIdx; i += 1) {
bytes32 hashOfStoredTier = keccak256(abi.encodePacked(data.tierAtEndId[data.endIdsAtMint[i]]));
if (hashOfStoredTier == hashOfTier) {
numOfRangesForTier += 1;
}
}
ranges = new TokenRange[](numOfRangesForTier);
uint256 idx = 0;
for (uint256 i = _startIdx; i < _endIdx; i += 1) {
bytes32 hashOfStoredTier = keccak256(abi.encodePacked(data.tierAtEndId[data.endIdsAtMint[i]]));
if (hashOfStoredTier == hashOfTier) {
uint256 end = data.endIdsAtMint[i];
uint256 start = 0;
if (i > 0) {
start = data.endIdsAtMint[i - 1];
}
ranges[idx] = TokenRange(start, end);
idx += 1;
}
}
}
/// @dev Returns the metadata ID for the given tokenID.
function _getMetadataId(uint256 _tokenId) private view returns (uint256) {
TieredDropStorage.Data storage data = TieredDropStorage.tieredDropStorage();
uint256 len = data.lengthEndIdsAtMint;
for (uint256 i = 0; i < len; i += 1) {
if (_tokenId < data.endIdsAtMint[i]) {
uint256 targetEndId = data.endIdsAtMint[i];
uint256 diff = targetEndId - _tokenId;
TokenRange memory range = data.proxyTokenRange[targetEndId];
return range.endIdNonInclusive - diff;
}
}
revert("!Metadata-ID");
}
/// @dev Returns the fair metadata ID for a given tokenId.
function _getFairMetadataId(
uint256 _metadataId,
uint256 _batchId,
uint256 _indexOfBatchId
) private view returns (uint256 fairMetadataId) {
TieredDropStorage.Data storage data = TieredDropStorage.tieredDropStorage();
bytes32 bytesRandom = data.tokenIdOffset[_batchId];
if (bytesRandom == bytes32(0)) {
return _metadataId;
}
uint256 randomness = uint256(bytesRandom);
uint256 prevBatchId;
if (_indexOfBatchId > 0) {
prevBatchId = getBatchIdAtIndex(_indexOfBatchId - 1);
}
uint256 batchSize = _batchId - prevBatchId;
uint256 offset = randomness % batchSize;
fairMetadataId = prevBatchId + ((_metadataId + offset) % batchSize);
}
/// @dev Scrambles tokenId offset for a given batchId.
function _scrambleOffset(uint256 _batchId, bytes calldata _seed) private {
TieredDropStorage.Data storage data = TieredDropStorage.tieredDropStorage();
data.tokenIdOffset[_batchId] = keccak256(abi.encodePacked(_seed, block.timestamp, blockhash(block.number - 1)));
}
/// @dev Returns whether a given address is authorized to sign mint requests.
function _isAuthorizedSigner(address _signer) internal view override returns (bool) {
return Permissions(address(this)).hasRole(minterRole, _signer);
}
/// @dev Returns whether lazy minting can be done in the given execution context.
function _canLazyMint() internal view virtual override returns (bool) {
return Permissions(address(this)).hasRole(minterRole, _msgSender());
}
/// @dev Returns whether the operator restriction can be set within the given execution context.
function _canSetOperatorRestriction() internal virtual override returns (bool) {
return Permissions(address(this)).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 Permissions(address(this)).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 Permissions(address(this)).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 Permissions(address(this)).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 Permissions(address(this)).hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
}
/*///////////////////////////////////////////////////////////////
Miscellaneous
//////////////////////////////////////////////////////////////*/
/**
* @dev Returns the total amount of tokens minted in the contract.
*/
function totalMinted() external view returns (uint256) {
ERC721AStorage.Data storage data = ERC721AStorage.erc721AStorage();
unchecked {
return data._currentIndex - _startTokenId();
}
}
/// @dev Returns the total number of tokens minted from the given tier.
function totalMintedInTier(string memory _tier) external view returns (uint256) {
TieredDropStorage.Data storage data = TieredDropStorage.tieredDropStorage();
return data.totalsForTier[keccak256(abi.encodePacked(_tier, "minted"))];
}
/// @dev The tokenId of the next NFT that will be minted / lazy minted.
function nextTokenIdToMint() external view returns (uint256) {
return nextTokenIdToLazyMint();
}
/// @dev Burns `tokenId`. See {ERC721-_burn}.
function burn(uint256 tokenId) external virtual {
// note: ERC721AUpgradeable's `_burn(uint256,bool)` internally checks for token approvals.
_burn(tokenId, true);
}
/// @dev See {ERC721-_beforeTokenTransfer}.
function _beforeTokenTransfers(
address from,
address to,
uint256 startTokenId,
uint256 quantity
) internal virtual override {
super._beforeTokenTransfers(from, to, startTokenId, quantity);
// if transfer is restricted on the contract, we still want to allow burning and minting
if (!Permissions(address(this)).hasRole(transferRole, address(0)) && from != address(0) && to != address(0)) {
if (
!Permissions(address(this)).hasRole(transferRole, from) &&
!Permissions(address(this)).hasRole(transferRole, to)
) {
revert("!TRANSFER");
}
}
}
/// @dev See {ERC721-isApprovedForAll}.
function getApproved(uint256 tokenId) public view override returns (address) {
address operator = super.getApproved(tokenId);
bool operatorRoleApproval = Permissions(address(this)).hasRoleWithSwitch(operatorRole, operator);
return operatorRoleApproval ? operator : address(0);
}
/// @dev See {ERC721-isApprovedForAll}.
function isApprovedForAll(address account, address operator) public view virtual override returns (bool) {
bool operatorRoleApproval = true;
if (account != operator) {
operatorRoleApproval = Permissions(address(this)).hasRoleWithSwitch(operatorRole, operator);
}
return operatorRoleApproval && super.isApprovedForAll(account, operator);
}
/// @dev See {ERC721-setApprovalForAll}.
function setApprovalForAll(address operator, bool approved) public override onlyAllowedOperatorApproval(operator) {
super.setApprovalForAll(operator, approved);
}
/// @dev See {ERC721-approve}.
function approve(address operator, uint256 tokenId) public override onlyAllowedOperatorApproval(operator) {
super.approve(operator, tokenId);
}
/// @dev See {ERC721-_transferFrom}.
function transferFrom(
address from,
address to,
uint256 tokenId
) public override(ERC721AUpgradeable) onlyAllowedOperator(from) {
super.transferFrom(from, to, tokenId);
}
/// @dev See {ERC721-_safeTransferFrom}.
function safeTransferFrom(
address from,
address to,
uint256 tokenId
) public override(ERC721AUpgradeable) onlyAllowedOperator(from) {
super.safeTransferFrom(from, to, tokenId);
}
/// @dev See {ERC721-_safeTransferFrom}.
function safeTransferFrom(
address from,
address to,
uint256 tokenId,
bytes memory data
) public override(ERC721AUpgradeable) onlyAllowedOperator(from) {
super.safeTransferFrom(from, to, tokenId, data);
}
function _msgSender() internal view override(Context, ERC2771ContextUpgradeable) returns (address sender) {
return ERC2771ContextUpgradeable._msgSender();
}
function _msgData() internal view override(Context, ERC2771ContextUpgradeable) returns (bytes calldata) {
return ERC2771ContextUpgradeable._msgData();
}
}