|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +import "../interface/IDropSinglePhase.sol"; |
| 5 | +import "../../lib/MerkleProof.sol"; |
| 6 | +import "./ExecutionContext.sol"; |
| 7 | +import "@openzeppelin/contracts-upgradeable/utils/structs/BitMapsUpgradeable.sol"; |
| 8 | + |
| 9 | +abstract contract DropSinglePhase is IDropSinglePhase, ExecutionContext { |
| 10 | + using BitMapsUpgradeable for BitMapsUpgradeable.BitMap; |
| 11 | + |
| 12 | + /*/////////////////////////////////////////////////////////////// |
| 13 | + State variables |
| 14 | + //////////////////////////////////////////////////////////////*/ |
| 15 | + |
| 16 | + /// @dev The active conditions for claiming tokens. |
| 17 | + ClaimCondition public claimCondition; |
| 18 | + |
| 19 | + /// @dev The ID for the active claim condition. |
| 20 | + bytes32 private conditionId; |
| 21 | + |
| 22 | + /*/////////////////////////////////////////////////////////////// |
| 23 | + Mappings |
| 24 | + //////////////////////////////////////////////////////////////*/ |
| 25 | + |
| 26 | + /** |
| 27 | + * @dev Map from an account and uid for a claim condition, to the last timestamp |
| 28 | + * at which the account claimed tokens under that claim condition. |
| 29 | + */ |
| 30 | + mapping(bytes32 => mapping(address => uint256)) private lastClaimTimestamp; |
| 31 | + |
| 32 | + /** |
| 33 | + * @dev Map from a claim condition uid to whether an address in an allowlist |
| 34 | + * has already claimed tokens i.e. used their place in the allowlist. |
| 35 | + */ |
| 36 | + mapping(bytes32 => BitMapsUpgradeable.BitMap) private usedAllowlistSpot; |
| 37 | + |
| 38 | + /*/////////////////////////////////////////////////////////////// |
| 39 | + Drop logic |
| 40 | + //////////////////////////////////////////////////////////////*/ |
| 41 | + |
| 42 | + /// @dev Lets an account claim tokens. |
| 43 | + function claim( |
| 44 | + address _receiver, |
| 45 | + uint256 _quantity, |
| 46 | + address _currency, |
| 47 | + uint256 _pricePerToken, |
| 48 | + AllowlistProof calldata _allowlistProof, |
| 49 | + bytes memory _data |
| 50 | + ) public payable virtual { |
| 51 | + _beforeClaim(_receiver, _quantity, _currency, _pricePerToken, _allowlistProof, _data); |
| 52 | + |
| 53 | + bytes32 activeConditionId = conditionId; |
| 54 | + |
| 55 | + /** |
| 56 | + * We make allowlist checks (i.e. verifyClaimMerkleProof) before verifying the claim's general |
| 57 | + * validity (i.e. verifyClaim) because we give precedence to the check of allow list quantity |
| 58 | + * restriction over the check of the general claim condition's quantityLimitPerTransaction |
| 59 | + * restriction. |
| 60 | + */ |
| 61 | + |
| 62 | + // Verify inclusion in allowlist. |
| 63 | + (bool validMerkleProof, uint256 merkleProofIndex) = verifyClaimMerkleProof( |
| 64 | + _msgSender(), |
| 65 | + _quantity, |
| 66 | + _allowlistProof |
| 67 | + ); |
| 68 | + |
| 69 | + // Verify claim validity. If not valid, revert. |
| 70 | + bool toVerifyMaxQuantityPerTransaction = _allowlistProof.maxQuantityInAllowlist == 0; |
| 71 | + |
| 72 | + verifyClaim(_msgSender(), _quantity, _currency, _pricePerToken, toVerifyMaxQuantityPerTransaction); |
| 73 | + |
| 74 | + if (validMerkleProof && _allowlistProof.maxQuantityInAllowlist > 0) { |
| 75 | + /** |
| 76 | + * Mark the claimer's use of their position in the allowlist. A spot in an allowlist |
| 77 | + * can be used only once. |
| 78 | + */ |
| 79 | + usedAllowlistSpot[activeConditionId].set(merkleProofIndex); |
| 80 | + } |
| 81 | + |
| 82 | + // Update contract state. |
| 83 | + claimCondition.supplyClaimed += _quantity; |
| 84 | + lastClaimTimestamp[activeConditionId][_msgSender()] = block.timestamp; |
| 85 | + |
| 86 | + // If there's a price, collect price. |
| 87 | + collectPriceOnClaim(_quantity, _currency, _pricePerToken); |
| 88 | + |
| 89 | + // Mint the relevant NFTs to claimer. |
| 90 | + uint256 startTokenId = transferTokensOnClaim(_receiver, _quantity); |
| 91 | + |
| 92 | + emit TokensClaimed(claimCondition, _msgSender(), _receiver, _quantity, startTokenId); |
| 93 | + |
| 94 | + _afterClaim(_receiver, _quantity, _currency, _pricePerToken, _allowlistProof, _data); |
| 95 | + } |
| 96 | + |
| 97 | + /// @dev Lets a contract admin set claim conditions. |
| 98 | + function setClaimConditions( |
| 99 | + ClaimCondition calldata _condition, |
| 100 | + bool _resetClaimEligibility, |
| 101 | + bytes memory |
| 102 | + ) external { |
| 103 | + bytes32 targetConditionId = conditionId; |
| 104 | + uint256 supplyClaimedAlready = claimCondition.supplyClaimed; |
| 105 | + |
| 106 | + if (_resetClaimEligibility) { |
| 107 | + supplyClaimedAlready = 0; |
| 108 | + targetConditionId = keccak256(abi.encodePacked(msg.sender, block.number)); |
| 109 | + } |
| 110 | + |
| 111 | + require(supplyClaimedAlready <= _condition.maxClaimableSupply, "max supply claimed already"); |
| 112 | + |
| 113 | + claimCondition = ClaimCondition({ |
| 114 | + startTimestamp: block.timestamp, |
| 115 | + maxClaimableSupply: _condition.maxClaimableSupply, |
| 116 | + supplyClaimed: supplyClaimedAlready, |
| 117 | + quantityLimitPerTransaction: _condition.supplyClaimed, |
| 118 | + waitTimeInSecondsBetweenClaims: _condition.waitTimeInSecondsBetweenClaims, |
| 119 | + merkleRoot: _condition.merkleRoot, |
| 120 | + pricePerToken: _condition.pricePerToken, |
| 121 | + currency: _condition.currency |
| 122 | + }); |
| 123 | + conditionId = targetConditionId; |
| 124 | + |
| 125 | + emit ClaimConditionUpdated(_condition, _resetClaimEligibility); |
| 126 | + } |
| 127 | + |
| 128 | + /// @dev Checks a request to claim NFTs against the active claim condition's criteria. |
| 129 | + function verifyClaim( |
| 130 | + address _claimer, |
| 131 | + uint256 _quantity, |
| 132 | + address _currency, |
| 133 | + uint256 _pricePerToken, |
| 134 | + bool verifyMaxQuantityPerTransaction |
| 135 | + ) public view { |
| 136 | + ClaimCondition memory currentClaimPhase = claimCondition; |
| 137 | + |
| 138 | + require( |
| 139 | + _currency == currentClaimPhase.currency && _pricePerToken == currentClaimPhase.pricePerToken, |
| 140 | + "invalid currency or price." |
| 141 | + ); |
| 142 | + |
| 143 | + // If we're checking for an allowlist quantity restriction, ignore the general quantity restriction. |
| 144 | + require( |
| 145 | + _quantity > 0 && |
| 146 | + (!verifyMaxQuantityPerTransaction || _quantity <= currentClaimPhase.quantityLimitPerTransaction), |
| 147 | + "invalid quantity." |
| 148 | + ); |
| 149 | + require( |
| 150 | + currentClaimPhase.supplyClaimed + _quantity <= currentClaimPhase.maxClaimableSupply, |
| 151 | + "exceed max claimable supply." |
| 152 | + ); |
| 153 | + |
| 154 | + uint256 timestampOfLastClaim = lastClaimTimestamp[conditionId][_claimer]; |
| 155 | + require( |
| 156 | + timestampOfLastClaim == 0 || |
| 157 | + block.timestamp >= timestampOfLastClaim + currentClaimPhase.waitTimeInSecondsBetweenClaims, |
| 158 | + "cannot claim." |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + /// @dev Checks whether a claimer meets the claim condition's allowlist criteria. |
| 163 | + function verifyClaimMerkleProof( |
| 164 | + address _claimer, |
| 165 | + uint256 _quantity, |
| 166 | + AllowlistProof calldata _allowlistProof |
| 167 | + ) public view returns (bool validMerkleProof, uint256 merkleProofIndex) { |
| 168 | + ClaimCondition memory currentClaimPhase = claimCondition; |
| 169 | + |
| 170 | + if (currentClaimPhase.merkleRoot != bytes32(0)) { |
| 171 | + (validMerkleProof, merkleProofIndex) = MerkleProof.verify( |
| 172 | + _allowlistProof.proof, |
| 173 | + currentClaimPhase.merkleRoot, |
| 174 | + keccak256(abi.encodePacked(_claimer, _allowlistProof.maxQuantityInAllowlist)) |
| 175 | + ); |
| 176 | + require(validMerkleProof, "not in whitelist."); |
| 177 | + require(!usedAllowlistSpot[conditionId].get(merkleProofIndex), "proof claimed."); |
| 178 | + require( |
| 179 | + _allowlistProof.maxQuantityInAllowlist == 0 || _quantity <= _allowlistProof.maxQuantityInAllowlist, |
| 180 | + "invalid quantity proof." |
| 181 | + ); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + /*/////////////////////////////////////////////////////////////// |
| 186 | + Virtual functions: to be implemented in derived contract |
| 187 | + //////////////////////////////////////////////////////////////*/ |
| 188 | + |
| 189 | + /// @dev Runs before every `claim` function call. |
| 190 | + function _beforeClaim( |
| 191 | + address _receiver, |
| 192 | + uint256 _quantity, |
| 193 | + address _currency, |
| 194 | + uint256 _pricePerToken, |
| 195 | + AllowlistProof calldata _allowlistProof, |
| 196 | + bytes memory _data |
| 197 | + ) internal virtual {} |
| 198 | + |
| 199 | + /// @dev Runs after every `claim` function call. |
| 200 | + function _afterClaim( |
| 201 | + address _receiver, |
| 202 | + uint256 _quantity, |
| 203 | + address _currency, |
| 204 | + uint256 _pricePerToken, |
| 205 | + AllowlistProof calldata _allowlistProof, |
| 206 | + bytes memory _data |
| 207 | + ) internal virtual {} |
| 208 | + |
| 209 | + /// @dev Collects and distributes the primary sale value of NFTs being claimed. |
| 210 | + function collectPriceOnClaim( |
| 211 | + uint256 _quantityToClaim, |
| 212 | + address _currency, |
| 213 | + uint256 _pricePerToken |
| 214 | + ) internal virtual; |
| 215 | + |
| 216 | + /// @dev Transfers the NFTs being claimed. |
| 217 | + function transferTokensOnClaim(address _to, uint256 _quantityBeingClaimed) |
| 218 | + internal |
| 219 | + virtual |
| 220 | + returns (uint256 startTokenId); |
| 221 | +} |
0 commit comments