// SPDX-License-Identifier: Apache-2.0 pragma solidity ^0.8.16; import "./interface/IDrop1155.sol"; import "../lib/MerkleProof.sol"; import "@openzeppelin/contracts/utils/Context.sol"; abstract contract Drop1155 is Context, IDrop1155 { using TWBitMaps for TWBitMaps.BitMap; /*/////////////////////////////////////////////////////////////// State variables //////////////////////////////////////////////////////////////*/ /// @dev Mapping from token ID => the set of all claim conditions, at any given moment, for tokens of the token ID. mapping(uint256 => ClaimConditionList) public claimCondition; /*/////////////////////////////////////////////////////////////// Drop logic //////////////////////////////////////////////////////////////*/ /// @dev Lets an account claim tokens. function claim(address _receiver, uint256 _editionId, uint256 _quantity, bytes32[] calldata _proof) public payable virtual override { _beforeClaim(_editionId, _receiver, _quantity, _proof); uint256 activeConditionId = getActiveClaimConditionId(_editionId); /** * 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(), _editionId, _proof); bool toVerifyMaxQuantityPerTransaction = _proof.length == 0 || claimCondition[_editionId].conditions[activeConditionId].merkleRoot == bytes32(0); verifyClaim(activeConditionId, _msgSender(), _editionId, _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[_editionId].usedAllowlistSpot[activeConditionId].set(merkleProofIndex); } // Update contract state. claimCondition[_editionId].conditions[activeConditionId].supplyClaimed += _quantity; claimCondition[_editionId].lastClaimTimestamp[activeConditionId][_msgSender()] = block.timestamp; // If there's a price, collect price. collectPriceOnClaim(_editionId, _quantity, claimCondition[_editionId].conditions[activeConditionId].pricePerToken); // Mint the relevant NFTs to claimer. transferTokensOnClaim(_receiver, _editionId, _quantity); emit TokensClaimed(activeConditionId, _msgSender(), _receiver, _editionId, _quantity); _afterClaim(_editionId, _receiver, _quantity, _proof); } /// @dev Lets a contract admin set claim conditions. function setClaimConditions(uint256 _editionId, ClaimCondition[] calldata _conditions, bool _resetClaimEligibility) public virtual override { ClaimConditionList storage conditionList = claimCondition[_editionId]; uint256 existingStartIndex = conditionList.currentStartId; uint256 existingPhaseCount = conditionList.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; } conditionList.count = _conditions.length; conditionList.currentStartId = newStartIndex; uint256 lastConditionEndTimestamp; for (uint256 i = 0; i < _conditions.length; i++) { if (i != 0 && lastConditionEndTimestamp >= _conditions[i].startsAt) { revert StartTimeStampError({ index: i, lastConditionStartTimestamp: lastConditionEndTimestamp, startsAt: _conditions[i].startsAt }); } uint256 supplyClaimedAlready = conditionList.conditions[newStartIndex + i].supplyClaimed; if (supplyClaimedAlready > _conditions[i].maxClaimableSupply) { revert MaxSupplyClaimedAlreadyError({ index: i, supplyClaimedAlready: supplyClaimedAlready, maxClaimableSupply: _conditions[i].maxClaimableSupply }); } conditionList.conditions[newStartIndex + i] = _conditions[i]; conditionList.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 conditionList.conditions[i]; delete conditionList.usedAllowlistSpot[newStartIndex + i]; } } else { if (existingPhaseCount > _conditions.length) { for (uint256 i = _conditions.length; i < existingPhaseCount; i++) { delete conditionList.conditions[newStartIndex + i]; delete conditionList.usedAllowlistSpot[newStartIndex + i]; } } } emit ClaimConditionsUpdated(_editionId, _conditions, _resetClaimEligibility); } /// @dev Checks whether a claimer meets the claim condition's allowlist criteria. function verifyClaimMerkleProof(uint256 _conditionId, address _claimer, uint256 _editionId, bytes32[] calldata _proof) public view returns (bool validMerkleProof, uint256 merkleProofIndex) { ClaimCondition memory currentClaimPhase = claimCondition[_editionId].conditions[_conditionId]; if (currentClaimPhase.merkleRoot != bytes32(0)) { (validMerkleProof, merkleProofIndex) = MerkleProof.verify(_proof, currentClaimPhase.merkleRoot, keccak256(abi.encodePacked(_claimer))); if (!validMerkleProof) { revert NotInWhitelistError(); } if (claimCondition[_editionId].usedAllowlistSpot[_conditionId].get(merkleProofIndex)) { revert ProofClaimedError(); } } } /// @dev Checks a request to claim NFTs against the active claim condition's criteria. function verifyClaim( uint256 _conditionId, address _claimer, uint256 _editionId, uint256 _quantity, bool verifyMaxQuantityPerTransaction ) public view { ClaimCondition memory currentClaimPhase = claimCondition[_editionId].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(_editionId, _conditionId, _claimer); if (lastClaimTimestamp != 0 && block.timestamp < nextValidClaimTimestamp) { revert CannotClaimYetError({ lastClaimTimestamp: lastClaimTimestamp, blockTimestamp: block.timestamp, nextValidClaimTimestamp: nextValidClaimTimestamp }); } } /// @dev At any given moment, returns the uid for the active claim condition. function getActiveClaimConditionId(uint256 _editionId) public view returns (uint256) { ClaimConditionList storage conditionList = claimCondition[_editionId]; for (uint256 i = conditionList.currentStartId + conditionList.count; i > conditionList.currentStartId; i--) { if ( block.timestamp >= conditionList.conditions[i - 1].startsAt && ( block.timestamp <= conditionList.conditions[i - 1].endsAt || conditionList.conditions[i - 1].endsAt == 0 ) ) { return i - 1; } } revert NoActiveClaimConditionError(); } /// @dev Returns the claim condition at the given uid. function getClaimConditionById(uint256 _editionId, uint256 _conditionId) external view returns (ClaimCondition memory condition) { condition = claimCondition[_editionId].conditions[_conditionId]; } function getUsedAllowListSpot(uint256 _editionId, uint256 _conditionId, uint256 _index) external view returns (bool) { return claimCondition[_editionId].usedAllowlistSpot[_conditionId].get(_index); } /// @dev Returns the timestamp for when a claimer is eligible for claiming NFTs again. function getClaimTimestamp(uint256 _editionId, uint256 _conditionId, address _claimer) public view returns (uint256 lastClaimTimestamp, uint256 nextValidClaimTimestamp) { lastClaimTimestamp = claimCondition[_editionId].lastClaimTimestamp[_conditionId][_claimer]; unchecked { nextValidClaimTimestamp = lastClaimTimestamp + claimCondition[_editionId].conditions[_conditionId].waitTimeInSecondsBetweenClaims; if (nextValidClaimTimestamp < lastClaimTimestamp) { nextValidClaimTimestamp = type(uint256).max; } } } /*//////////////////////////////////////////////////////////////////// Optional hooks that can be implemented in the derived contract ///////////////////////////////////////////////////////////////////*/ /// @dev Runs before every `claim` function call. function _beforeClaim(uint256 _editionId, address _receiver, uint256 _quantity, bytes32[] calldata _proof) internal virtual {} /// @dev Runs after every `claim` function call. function _afterClaim(uint256 _editionId, 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 _editionId, uint256 _quantityToClaim, uint256 _pricePerToken) internal virtual; /// @dev Transfers the NFTs being claimed. function transferTokensOnClaim(address _to, uint256 _editionId, uint256 _quantityBeingClaimed) internal virtual; }