-
Notifications
You must be signed in to change notification settings - Fork 477
/
DropERC20.sol
535 lines (440 loc) · 20.6 KB
/
DropERC20.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
// SPDX-License-Identifier: Apache-2.0
pragma solidity ^0.8.11;
// ========== External imports ==========
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20BurnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20VotesUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/AccessControlEnumerableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/structs/BitMapsUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/utils/MulticallUpgradeable.sol";
// ========== Internal imports ==========
import "../interfaces/IThirdwebContract.sol";
// ========== Features ==========
import "../extension/interface/IPlatformFee.sol";
import "../extension/interface/IPrimarySale.sol";
import { IDropERC20 } from "../interfaces/drop/IDropERC20.sol";
import { ITWFee } from "../interfaces/ITWFee.sol";
import "../openzeppelin-presets/metatx/ERC2771ContextUpgradeable.sol";
import "../lib/MerkleProof.sol";
import "../lib/CurrencyTransferLib.sol";
import "../lib/FeeType.sol";
contract DropERC20 is
Initializable,
IThirdwebContract,
IPrimarySale,
IPlatformFee,
ReentrancyGuardUpgradeable,
ERC2771ContextUpgradeable,
MulticallUpgradeable,
AccessControlEnumerableUpgradeable,
ERC20BurnableUpgradeable,
ERC20VotesUpgradeable,
IDropERC20
{
using BitMapsUpgradeable for BitMapsUpgradeable.BitMap;
/*///////////////////////////////////////////////////////////////
State variables
//////////////////////////////////////////////////////////////*/
bytes32 private constant MODULE_TYPE = bytes32("DropERC20");
uint128 private constant VERSION = 1;
/// @dev Only transfers to or from TRANSFER_ROLE holders are valid, when transfers are restricted.
bytes32 private constant TRANSFER_ROLE = keccak256("TRANSFER_ROLE");
/// @dev Contract level metadata.
string public contractURI;
/// @dev Max bps in the thirdweb system.
uint128 internal constant MAX_BPS = 10_000;
/// @dev The % of primary sales collected as platform fees.
uint128 internal platformFeeBps;
/// @dev The address that receives all platform fees from all sales.
address internal platformFeeRecipient;
/// @dev The address that receives all primary sales value.
address public primarySaleRecipient;
/// @dev The max number of tokens a wallet can claim.
uint256 public maxWalletClaimCount;
/// @dev Global max total supply of tokens.
uint256 public maxTotalSupply;
/// @dev The set of all claim conditions, at any given moment.
ClaimConditionList public claimCondition;
/*///////////////////////////////////////////////////////////////
Mappings
//////////////////////////////////////////////////////////////*/
/// @dev Mapping from address => number of tokens a wallet has claimed.
mapping(address => uint256) public walletClaimCount;
/*///////////////////////////////////////////////////////////////
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 _primarySaleRecipient,
address _platformFeeRecipient,
uint256 _platformFeeBps
) external initializer {
// Initialize inherited contracts, most base-like -> most derived.
__ERC2771Context_init_unchained(_trustedForwarders);
__ERC20Permit_init(_name);
__ERC20_init_unchained(_name, _symbol);
// Initialize this contract's state.
contractURI = _contractURI;
primarySaleRecipient = _primarySaleRecipient;
platformFeeRecipient = _platformFeeRecipient;
platformFeeBps = uint128(_platformFeeBps);
_setupRole(DEFAULT_ADMIN_ROLE, _defaultAdmin);
_setupRole(TRANSFER_ROLE, _defaultAdmin);
_setupRole(TRANSFER_ROLE, address(0));
}
/*///////////////////////////////////////////////////////////////
Generic contract logic
//////////////////////////////////////////////////////////////*/
/// @dev Returns the type of the contract.
function contractType() external pure returns (bytes32) {
return MODULE_TYPE;
}
/// @dev Returns the version of the contract.
function contractVersion() external pure returns (uint8) {
return uint8(VERSION);
}
/*///////////////////////////////////////////////////////////////
ERC 165 + ERC20 transfer hooks
//////////////////////////////////////////////////////////////*/
/// @dev See ERC 165
function supportsInterface(bytes4 interfaceId)
public
view
virtual
override(AccessControlEnumerableUpgradeable)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
function _afterTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual override(ERC20Upgradeable, ERC20VotesUpgradeable) {
super._afterTokenTransfer(from, to, amount);
}
/// @dev Runs on every transfer.
function _beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal override(ERC20Upgradeable) {
super._beforeTokenTransfer(from, to, amount);
if (!hasRole(TRANSFER_ROLE, address(0)) && from != address(0) && to != address(0)) {
require(hasRole(TRANSFER_ROLE, from) || hasRole(TRANSFER_ROLE, to), "transfers restricted.");
}
}
/*///////////////////////////////////////////////////////////////
Claim logic
//////////////////////////////////////////////////////////////*/
/// @dev Lets an account claim tokens.
function claim(
address _receiver,
uint256 _quantity,
address _currency,
uint256 _pricePerToken,
bytes32[] calldata _proofs,
uint256 _proofMaxQuantityPerTransaction
) external payable nonReentrant {
require(isTrustedForwarder(msg.sender) || _msgSender() == tx.origin, "BOT");
// Get the claim conditions.
uint256 activeConditionId = getActiveClaimConditionId();
/**
* We make allowlist checks (i.e. verifyClaimMerkleProof) before verifying the claim's general
* validity (i.e. verifyClaim) because we give precedence to the check of allow list quantity
* restriction over the check of the general claim condition's quantityLimitPerTransaction
* restriction.
*/
// Verify inclusion in allowlist.
(bool validMerkleProof, uint256 merkleProofIndex) = verifyClaimMerkleProof(
activeConditionId,
_msgSender(),
_quantity,
_proofs,
_proofMaxQuantityPerTransaction
);
// Verify claim validity. If not valid, revert.
// when there's allowlist present --> verifyClaimMerkleProof will verify the _proofMaxQuantityPerTransaction value with hashed leaf in the allowlist
// when there's no allowlist, this check is true --> verifyClaim will check for _quantity being less/equal than the limit
bool toVerifyMaxQuantityPerTransaction = _proofMaxQuantityPerTransaction == 0 ||
claimCondition.phases[activeConditionId].merkleRoot == bytes32(0);
verifyClaim(
activeConditionId,
_msgSender(),
_quantity,
_currency,
_pricePerToken,
toVerifyMaxQuantityPerTransaction
);
if (validMerkleProof && _proofMaxQuantityPerTransaction > 0) {
/**
* Mark the claimer's use of their position in the allowlist. A spot in an allowlist
* can be used only once.
*/
claimCondition.limitMerkleProofClaim[activeConditionId].set(merkleProofIndex);
}
// If there's a price, collect price.
collectClaimPrice(_quantity, _currency, _pricePerToken);
// Mint the relevant NFTs to claimer.
transferClaimedTokens(_receiver, activeConditionId, _quantity);
emit TokensClaimed(activeConditionId, _msgSender(), _receiver, _quantity);
}
/// @dev Lets a contract admin (account with `DEFAULT_ADMIN_ROLE`) set claim conditions.
function setClaimConditions(ClaimCondition[] calldata _phases, bool _resetClaimEligibility)
external
onlyRole(DEFAULT_ADMIN_ROLE)
{
uint256 existingStartIndex = claimCondition.currentStartId;
uint256 existingPhaseCount = claimCondition.count;
/**
* `limitLastClaimTimestamp` and `limitMerkleProofClaim` are mappings that use a
* claim condition's UID as a key.
*
* If `_resetClaimEligibility == true`, we assign completely new UIDs to the claim
* conditions in `_phases`, effectively resetting the restrictions on claims expressed
* by `limitLastClaimTimestamp` and `limitMerkleProofClaim`.
*/
uint256 newStartIndex = existingStartIndex;
if (_resetClaimEligibility) {
newStartIndex = existingStartIndex + existingPhaseCount;
}
claimCondition.count = _phases.length;
claimCondition.currentStartId = newStartIndex;
uint256 lastConditionStartTimestamp;
for (uint256 i = 0; i < _phases.length; i++) {
require(
i == 0 || lastConditionStartTimestamp < _phases[i].startTimestamp,
"startTimestamp must be in ascending order."
);
uint256 supplyClaimedAlready = claimCondition.phases[newStartIndex + i].supplyClaimed;
require(supplyClaimedAlready <= _phases[i].maxClaimableSupply, "max supply claimed already");
claimCondition.phases[newStartIndex + i] = _phases[i];
claimCondition.phases[newStartIndex + i].supplyClaimed = supplyClaimedAlready;
lastConditionStartTimestamp = _phases[i].startTimestamp;
}
/**
* Gas refunds (as much as possible)
*
* If `_resetClaimEligibility == true`, we assign completely new UIDs to the claim
* conditions in `_phases`. So, we delete claim conditions with UID < `newStartIndex`.
*
* If `_resetClaimEligibility == false`, and there are more existing claim conditions
* than in `_phases`, we delete the existing claim conditions that don't get replaced
* by the conditions in `_phases`.
*/
if (_resetClaimEligibility) {
for (uint256 i = existingStartIndex; i < newStartIndex; i++) {
delete claimCondition.phases[i];
delete claimCondition.limitMerkleProofClaim[i];
}
} else {
if (existingPhaseCount > _phases.length) {
for (uint256 i = _phases.length; i < existingPhaseCount; i++) {
delete claimCondition.phases[newStartIndex + i];
delete claimCondition.limitMerkleProofClaim[newStartIndex + i];
}
}
}
emit ClaimConditionsUpdated(_phases);
}
/// @dev Collects and distributes the primary sale value of tokens being claimed.
function collectClaimPrice(
uint256 _quantityToClaim,
address _currency,
uint256 _pricePerToken
) internal {
if (_pricePerToken == 0) {
return;
}
// `_pricePerToken` is interpreted as price per 1 ether unit of the ERC20 tokens.
uint256 totalPrice = (_quantityToClaim * _pricePerToken) / 1 ether;
require(totalPrice > 0, "quantity too low");
uint256 platformFees = (totalPrice * platformFeeBps) / MAX_BPS;
if (_currency == CurrencyTransferLib.NATIVE_TOKEN) {
require(msg.value == totalPrice, "must send total price.");
}
CurrencyTransferLib.transferCurrency(_currency, _msgSender(), platformFeeRecipient, platformFees);
CurrencyTransferLib.transferCurrency(_currency, _msgSender(), primarySaleRecipient, totalPrice - platformFees);
}
/// @dev Transfers the tokens being claimed.
function transferClaimedTokens(
address _to,
uint256 _conditionId,
uint256 _quantityBeingClaimed
) internal {
// Update the supply minted under mint condition.
claimCondition.phases[_conditionId].supplyClaimed += _quantityBeingClaimed;
// if transfer claimed tokens is called when to != msg.sender, it'd use msg.sender's limits.
// behavior would be similar to msg.sender mint for itself, then transfer to `to`.
claimCondition.limitLastClaimTimestamp[_conditionId][_msgSender()] = block.timestamp;
walletClaimCount[_msgSender()] += _quantityBeingClaimed;
_mint(_to, _quantityBeingClaimed);
}
/// @dev Checks a request to claim tokens against the active claim condition's criteria.
function verifyClaim(
uint256 _conditionId,
address _claimer,
uint256 _quantity,
address _currency,
uint256 _pricePerToken,
bool verifyMaxQuantityPerTransaction
) public view {
ClaimCondition memory currentClaimPhase = claimCondition.phases[_conditionId];
require(
_currency == currentClaimPhase.currency && _pricePerToken == currentClaimPhase.pricePerToken,
"invalid currency or price specified."
);
// If we're checking for an allowlist quantity restriction, ignore the general quantity restriction.
require(
_quantity > 0 &&
(!verifyMaxQuantityPerTransaction || _quantity <= currentClaimPhase.quantityLimitPerTransaction),
"invalid quantity claimed."
);
require(
currentClaimPhase.supplyClaimed + _quantity <= currentClaimPhase.maxClaimableSupply,
"exceed max mint supply."
);
uint256 _maxTotalSupply = maxTotalSupply;
uint256 _maxWalletClaimCount = maxWalletClaimCount;
require(_maxTotalSupply == 0 || totalSupply() + _quantity <= _maxTotalSupply, "exceed max total supply.");
require(
_maxWalletClaimCount == 0 || walletClaimCount[_claimer] + _quantity <= _maxWalletClaimCount,
"exceed claim limit for wallet"
);
(, uint256 nextValidClaimTimestamp) = getClaimTimestamp(_conditionId, _claimer);
require(block.timestamp >= nextValidClaimTimestamp, "cannot claim yet.");
}
/// @dev Checks whether a claimer meets the claim condition's allowlist criteria.
function verifyClaimMerkleProof(
uint256 _conditionId,
address _claimer,
uint256 _quantity,
bytes32[] calldata _proofs,
uint256 _proofMaxQuantityPerTransaction
) public view returns (bool validMerkleProof, uint256 merkleProofIndex) {
ClaimCondition memory currentClaimPhase = claimCondition.phases[_conditionId];
if (currentClaimPhase.merkleRoot != bytes32(0)) {
(validMerkleProof, merkleProofIndex) = MerkleProof.verify(
_proofs,
currentClaimPhase.merkleRoot,
keccak256(abi.encodePacked(_claimer, _proofMaxQuantityPerTransaction))
);
require(validMerkleProof, "not in whitelist.");
require(!claimCondition.limitMerkleProofClaim[_conditionId].get(merkleProofIndex), "proof claimed.");
require(
_proofMaxQuantityPerTransaction == 0 || _quantity <= _proofMaxQuantityPerTransaction,
"invalid quantity proof."
);
}
}
/*///////////////////////////////////////////////////////////////
Getter functions
//////////////////////////////////////////////////////////////*/
/// @dev At any given moment, returns the uid for the active claim condition.
function getActiveClaimConditionId() public view returns (uint256) {
for (uint256 i = claimCondition.currentStartId + claimCondition.count; i > claimCondition.currentStartId; i--) {
if (block.timestamp >= claimCondition.phases[i - 1].startTimestamp) {
return i - 1;
}
}
revert("no active mint condition.");
}
/// @dev Returns the timestamp for when a claimer is eligible for claiming tokens again.
function getClaimTimestamp(uint256 _conditionId, address _claimer)
public
view
returns (uint256 lastClaimTimestamp, uint256 nextValidClaimTimestamp)
{
lastClaimTimestamp = claimCondition.limitLastClaimTimestamp[_conditionId][_claimer];
if(lastClaimTimestamp != 0) {
unchecked {
nextValidClaimTimestamp =
lastClaimTimestamp +
claimCondition.phases[_conditionId].waitTimeInSecondsBetweenClaims;
if (nextValidClaimTimestamp < lastClaimTimestamp) {
nextValidClaimTimestamp = type(uint256).max;
}
}
}
}
/// @dev Returns the claim condition at the given uid.
function getClaimConditionById(uint256 _conditionId) external view returns (ClaimCondition memory condition) {
condition = claimCondition.phases[_conditionId];
}
/// @dev Returns the platform fee recipient and bps.
function getPlatformFeeInfo() external view returns (address, uint16) {
return (platformFeeRecipient, uint16(platformFeeBps));
}
/*///////////////////////////////////////////////////////////////
Setter functions
//////////////////////////////////////////////////////////////*/
/// @dev Lets a contract admin set a claim count for a wallet.
function setWalletClaimCount(address _claimer, uint256 _count) external onlyRole(DEFAULT_ADMIN_ROLE) {
walletClaimCount[_claimer] = _count;
emit WalletClaimCountUpdated(_claimer, _count);
}
/// @dev Lets a contract admin set a maximum number of tokens that can be claimed by any wallet.
function setMaxWalletClaimCount(uint256 _count) external onlyRole(DEFAULT_ADMIN_ROLE) {
maxWalletClaimCount = _count;
emit MaxWalletClaimCountUpdated(_count);
}
/// @dev Lets a contract admin set the global maximum supply of tokens.
function setMaxTotalSupply(uint256 _maxTotalSupply) external onlyRole(DEFAULT_ADMIN_ROLE) {
maxTotalSupply = _maxTotalSupply;
emit MaxTotalSupplyUpdated(_maxTotalSupply);
}
/// @dev Lets a contract admin set the recipient for all primary sales.
function setPrimarySaleRecipient(address _saleRecipient) external onlyRole(DEFAULT_ADMIN_ROLE) {
primarySaleRecipient = _saleRecipient;
emit PrimarySaleRecipientUpdated(_saleRecipient);
}
/// @dev Lets a contract admin update the platform fee recipient and bps
function setPlatformFeeInfo(address _platformFeeRecipient, uint256 _platformFeeBps)
external
onlyRole(DEFAULT_ADMIN_ROLE)
{
require(_platformFeeBps <= MAX_BPS, "bps <= 10000.");
platformFeeBps = uint64(_platformFeeBps);
platformFeeRecipient = _platformFeeRecipient;
emit PlatformFeeInfoUpdated(_platformFeeRecipient, _platformFeeBps);
}
/// @dev Lets a contract admin set the URI for contract-level metadata.
function setContractURI(string calldata _uri) external onlyRole(DEFAULT_ADMIN_ROLE) {
string memory prevURI = contractURI;
contractURI = _uri;
emit ContractURIUpdated(prevURI, _uri);
}
/*///////////////////////////////////////////////////////////////
Miscellaneous
//////////////////////////////////////////////////////////////*/
function _mint(address account, uint256 amount) internal virtual override(ERC20Upgradeable, ERC20VotesUpgradeable) {
super._mint(account, amount);
}
function _burn(address account, uint256 amount) internal virtual override(ERC20Upgradeable, ERC20VotesUpgradeable) {
super._burn(account, amount);
}
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();
}
}