// SPDX-License-Identifier: Apache-2.0 pragma solidity ^0.8.16; import "./interface/IDrop.sol"; import "../lib/MerkleProof.sol"; import "@openzeppelin/contracts/utils/Context.sol"; abstract contract Drop is Context, IDrop { using TWBitMaps for TWBitMaps.BitMap; /*/////////////////////////////////////////////////////////////// State variables //////////////////////////////////////////////////////////////*/ /// @dev The active conditions for claiming tokens. ClaimConditionList public claimCondition; /*/////////////////////////////////////////////////////////////// Drop logic //////////////////////////////////////////////////////////////*/ /// @dev Lets an account claim tokens. function claim(address _receiver, uint256 _quantity, bytes32[] calldata _proof) public payable virtual override { _beforeClaim(_receiver, _quantity, _proof); 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(), _proof); // Verify claim validity. If not valid, revert. // when there's allowlist present --> verifyClaimMerkleProof will verify the maxQuantityInAllowlist value with hashed leaf in the allowlist // when there's no allowlist, this check is true --> verifyClaim will check for _quantity being equal/less than the limit bool toVerifyMaxQuantityPerTransaction = _proof.length == 0 || claimCondition.conditions[activeConditionId].merkleRoot == bytes32(0); verifyClaim(activeConditionId, _msgSender(), _quantity, toVerifyMaxQuantityPerTransaction); if (validMerkleProof) { /** * Mark the claimer's use of their position in the allowlist. A spot in an allowlist * can be used only once. */ claimCondition.usedAllowlistSpot[activeConditionId].set(merkleProofIndex); } // Update contract state. claimCondition.conditions[activeConditionId].supplyClaimed += _quantity; claimCondition.lastClaimTimestamp[activeConditionId][_msgSender()] = block.timestamp; // If there's a price, collect price. collectPriceOnClaim(_quantity, claimCondition.conditions[activeConditionId].pricePerToken); // Mint the relevant NFTs to claimer. uint256 startTokenId = transferTokensOnClaim(_receiver, _quantity); emit TokensClaimed(activeConditionId, _msgSender(), _receiver, startTokenId, _quantity); _afterClaim(_receiver, _quantity, _proof); } /// @dev Lets a contract admin set claim conditions. function setClaimConditions(ClaimCondition[] calldata _conditions, bool _resetClaimEligibility) public virtual override { uint256 existingStartIndex = claimCondition.currentStartId; uint256 existingPhaseCount = claimCondition.count; /** * The mapping `supplyClaimedByWallet` uses a claim condition's UID as a key. * * If `_resetClaimEligibility == true`, we assign completely new UIDs to the claim * conditions in `_conditions`, effectively resetting the restrictions on claims expressed * by `supplyClaimedByWallet`. */ uint256 newStartIndex = existingStartIndex; if (_resetClaimEligibility) { newStartIndex = existingStartIndex + existingPhaseCount; } claimCondition.count = _conditions.length; claimCondition.currentStartId = newStartIndex; uint256 lastConditionEndTimestamp; for (uint256 i = 0; i < _conditions.length; i++) { if (i != 0 && lastConditionEndTimestamp >= _conditions[i].startsAt) { revert StartTimeStampError({ index: i, lastConditionEndTimestamp: lastConditionEndTimestamp, startsAt: _conditions[i].startsAt }); } uint256 supplyClaimedAlready = claimCondition.conditions[newStartIndex + i].supplyClaimed; if (supplyClaimedAlready > _conditions[i].maxClaimableSupply) { revert MaxSupplyClaimedAlreadyError({ index: i, supplyClaimedAlready: supplyClaimedAlready, maxClaimableSupply: _conditions[i].maxClaimableSupply }); } claimCondition.conditions[newStartIndex + i] = _conditions[i]; claimCondition.conditions[newStartIndex + i].supplyClaimed = supplyClaimedAlready; lastConditionEndTimestamp = _conditions[i].endsAt; } /** * Gas refunds (as much as possible) * * If `_resetClaimEligibility == true`, we assign completely new UIDs to the claim * conditions in `_conditions`. So, we delete claim conditions with UID < `newStartIndex`. * * If `_resetClaimEligibility == false`, and there are more existing claim conditions * than in `_conditions`, we delete the existing claim conditions that don't get replaced * by the conditions in `_conditions`. */ if (_resetClaimEligibility) { for (uint256 i = existingStartIndex; i < newStartIndex; i++) { delete claimCondition.conditions[i]; delete claimCondition.usedAllowlistSpot[i]; } } else { if (existingPhaseCount > _conditions.length) { for (uint256 i = _conditions.length; i < existingPhaseCount; i++) { delete claimCondition.conditions[newStartIndex + i]; delete claimCondition.usedAllowlistSpot[newStartIndex + i]; } } } emit ClaimConditionsUpdated(_conditions, _resetClaimEligibility); } /// @notice Checks a request to claim NFTs against the active claim condition's criteria. /// @param _conditionId id collection where you want to claim /// @param _claimer address wallet of claimer /// @param _quantity quantity of token you want to claim /// @param verifyMaxQuantityPerTransaction verify max quantity per transaction function verifyClaim( uint256 _conditionId, address _claimer, uint256 _quantity, bool verifyMaxQuantityPerTransaction ) public view { ClaimCondition memory currentClaimPhase = claimCondition.conditions[_conditionId]; // If we're checking for an allowlist quantity restriction, ignore the general quantity restriction. if ( _quantity == 0 || (verifyMaxQuantityPerTransaction && _quantity > currentClaimPhase.quantityLimitPerTransaction) ) { revert InvalidQuantityClaimedError({ quantity: _quantity, verifyMaxQuantityPerTransaction: verifyMaxQuantityPerTransaction, quantityLimitPerTransaction: currentClaimPhase.quantityLimitPerTransaction }); } if (currentClaimPhase.supplyClaimed + _quantity > currentClaimPhase.maxClaimableSupply) { revert ExceedMaxClaimableSupplyError({ quantity: _quantity, supplyClaimed: currentClaimPhase.supplyClaimed, maxClaimableSupply: currentClaimPhase.maxClaimableSupply }); } (uint256 lastClaimTimestamp, uint256 nextValidClaimTimestamp) = getClaimTimestamp(_conditionId, _claimer); if (lastClaimTimestamp != 0 && block.timestamp < nextValidClaimTimestamp) { revert CannotClaimYetError({ lastClaimTimestamp: lastClaimTimestamp, blockTimestamp: block.timestamp, nextValidClaimTimestamp: nextValidClaimTimestamp }); } } /// @dev Checks whether a claimer meets the claim condition's allowlist criteria. function verifyClaimMerkleProof(uint256 _conditionId, address _claimer, bytes32[] calldata _proof) public view returns (bool validMerkleProof, uint256 merkleProofIndex) { ClaimCondition memory currentClaimPhase = claimCondition.conditions[_conditionId]; if (currentClaimPhase.merkleRoot != bytes32(0)) { (validMerkleProof, merkleProofIndex) = MerkleProof.verify(_proof, currentClaimPhase.merkleRoot, keccak256(abi.encodePacked(_claimer))); if (!validMerkleProof) { revert NotInWhitelistError(); } if (claimCondition.usedAllowlistSpot[_conditionId].get(merkleProofIndex)) { revert ProofClaimedError(); } } } /// @notice Return current claim condition function getActiveClaimConditionId() public view returns (uint256) { for (uint256 i = claimCondition.count; i > 0; i--) { if ( block.timestamp >= claimCondition.conditions[i - 1].startsAt && ( block.timestamp <= claimCondition.conditions[i - 1].endsAt || claimCondition.conditions[i - 1].endsAt == 0 ) ) { return i - 1; } } revert NoActiveClaimConditionError(); } /// @dev Returns the claim condition at the given uid. function getClaimConditionById(uint256 _conditionId) external view returns (ClaimCondition memory condition) { condition = claimCondition.conditions[_conditionId]; } function getUsedAllowListSpot(uint256 _conditionId, uint256 _index) external view returns (bool) { return claimCondition.usedAllowlistSpot[_conditionId].get(_index); } /// @notice Return timestamp for next valid claim for only one claimer /// @param _conditionId id of collection you wan to get claim timestamp /// @param _claimer address wallet of one claimer function getClaimTimestamp(uint256 _conditionId, address _claimer) public view returns (uint256 lastClaimTimestamp, uint256 nextValidClaimTimestamp) { lastClaimTimestamp = claimCondition.lastClaimTimestamp[_conditionId][_claimer]; unchecked { nextValidClaimTimestamp = lastClaimTimestamp + claimCondition.conditions[_conditionId].waitTimeInSecondsBetweenClaims; if (nextValidClaimTimestamp < lastClaimTimestamp) { nextValidClaimTimestamp = type(uint256).max; } } } /*//////////////////////////////////////////////////////////////////// Optional hooks that can be implemented in the derived contract ///////////////////////////////////////////////////////////////////*/ /// @dev Exposes the ability to override the msg sender. /// @dev Runs before every `claim` function call. function _beforeClaim(address _receiver, uint256 _quantity, bytes32[] calldata _proof) internal virtual {} /// @dev Runs after every `claim` function call. function _afterClaim(address _receiver, uint256 _quantity, bytes32[] calldata _proof) internal virtual {} /*/////////////////////////////////////////////////////////////// Virtual functions: to be implemented in derived contract //////////////////////////////////////////////////////////////*/ /// @dev Collects and distributes the primary sale value of NFTs being claimed. function collectPriceOnClaim(uint256 _quantityToClaim, uint256 _pricePerToken) internal virtual; /// @dev Transfers the NFTs being claimed. function transferTokensOnClaim(address _to, uint256 _quantityBeingClaimed) internal virtual returns (uint256 startTokenId); }