// SPDX-License-Identifier: Apache-2.0 pragma solidity ^0.8.0; // Interface import { ILazyMintERC721 } from "./ILazyMintERC721.sol"; // Token import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol"; // Protocol control center. import { ProtocolControl } from "../../ProtocolControl.sol"; // Royalties import "@openzeppelin/contracts/interfaces/IERC2981.sol"; // Access Control + security import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; import "@openzeppelin/contracts/access/AccessControlEnumerable.sol"; // Meta transactions import "@openzeppelin/contracts/metatx/ERC2771Context.sol"; // Utils import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol"; import "@openzeppelin/contracts/utils/Multicall.sol"; import "@openzeppelin/contracts/utils/Strings.sol"; // Helper interfaces import { IWETH } from "../../interfaces/IWETH.sol"; import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; contract LazyMintERC721 is ILazyMintERC721, ERC721Enumerable, ERC2771Context, IERC2981, AccessControlEnumerable, ReentrancyGuard, Multicall { using Strings for uint256; /// @dev Version of the contract. uint256 public constant VERSION = 2; /// @dev Only TRANSFER_ROLE holders can have tokens transferred from or to them, during restricted transfers. bytes32 public constant TRANSFER_ROLE = keccak256("TRANSFER_ROLE"); /// @dev Only MINTER_ROLE holders can lazy mint NFTs (i.e. can call functions prefixed with `lazyMint`). bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE"); /// @dev The address interpreted as native token of the chain. address private constant NATIVE_TOKEN = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE; /// @dev The address of the native token wrapper contract. address public immutable nativeTokenWrapper; /// @dev Owner of the contract (purpose: OpenSea compatibility, etc.) address private _owner; /// @dev The adress that receives all primary sales value. address public defaultSaleRecipient; /// @dev The next token ID of the NFT to "lazy mint". uint256 public nextTokenIdToMint; /// @dev The next token ID of the NFT that can be claimed. uint256 public nextTokenIdToClaim; /// @dev Contract interprets 10_000 as 100%. uint64 private constant MAX_BPS = 10_000; /// @dev The % of secondary sales collected as royalties. See EIP 2981. uint64 public royaltyBps; /// @dev The % of primary sales collected by the contract as fees. uint120 public feeBps; /// @dev Whether transfers on tokens are restricted. bool public transfersRestricted; /// @dev Contract level metadata. string public contractURI; /// @dev The protocol control center. ProtocolControl internal controlCenter; uint256[] public baseURIIndices; /// @dev End token Id => URI that overrides `baseURI + tokenId` convention. mapping(uint256 => string) private baseURI; /// @dev End token Id => info related to the delayed reveal of the baseURI mapping(uint256 => bytes) public encryptedBaseURI; /// @dev The claim conditions at any given moment. ClaimConditions public claimConditions; modifier onlyModuleAdmin() { require(hasRole(DEFAULT_ADMIN_ROLE, _msgSender()), "not module admin."); _; } /// @dev Checks whether caller has MINTER_ROLE. modifier onlyMinter() { require(hasRole(MINTER_ROLE, _msgSender()), "not minter."); _; } constructor( string memory _name, string memory _symbol, string memory _contractURI, address payable _controlCenter, address _trustedForwarder, address _nativeTokenWrapper, address _saleRecipient, uint128 _royaltyBps, uint128 _feeBps ) ERC721(_name, _symbol) ERC2771Context(_trustedForwarder) { // Set the protocol control center controlCenter = ProtocolControl(_controlCenter); nativeTokenWrapper = _nativeTokenWrapper; defaultSaleRecipient = _saleRecipient; contractURI = _contractURI; royaltyBps = uint64(_royaltyBps); feeBps = uint120(_feeBps); address deployer = _msgSender(); _owner = deployer; _setupRole(DEFAULT_ADMIN_ROLE, deployer); _setupRole(MINTER_ROLE, deployer); _setupRole(TRANSFER_ROLE, deployer); } /// ===== Public functions ===== /** * @dev Returns the address of the current owner. */ function owner() public view returns (address) { return hasRole(DEFAULT_ADMIN_ROLE, _owner) ? _owner : address(0); } /// @dev Returns the URI for a given tokenId. function tokenURI(uint256 _tokenId) public view override returns (string memory) { for (uint256 i = 0; i < baseURIIndices.length; i += 1) { if (_tokenId < baseURIIndices[i]) { if (encryptedBaseURI[baseURIIndices[i]].length != 0) { return string(abi.encodePacked(baseURI[baseURIIndices[i]], "0")); } else { return string(abi.encodePacked(baseURI[baseURIIndices[i]], _tokenId.toString())); } } } return ""; } /// @dev At any given moment, returns the uid for the active claim condition. function getIndexOfActiveCondition() public view returns (uint256) { uint256 totalConditionCount = claimConditions.totalConditionCount; require(totalConditionCount > 0, "no public mint condition."); for (uint256 i = totalConditionCount; i > 0; i -= 1) { if (block.timestamp >= claimConditions.claimConditionAtIndex[i - 1].startTimestamp) { return i - 1; } } revert("no active mint condition."); } /// @dev See: https://ethereum.stackexchange.com/questions/69825/decrypt-message-on-chain function encryptDecrypt(bytes memory data, bytes calldata key) public pure returns (bytes memory result) { // Store data length on stack for later use uint256 length = data.length; assembly { // Set result to free memory pointer result := mload(0x40) // Increase free memory pointer by lenght + 32 mstore(0x40, add(add(result, length), 32)) // Set result length mstore(result, length) } // Iterate over the data stepping by 32 bytes for (uint256 i = 0; i < length; i += 32) { // Generate hash of the key and offset bytes32 hash = keccak256(abi.encodePacked(key, i)); bytes32 chunk; assembly { // Read 32-bytes data chunk chunk := mload(add(data, add(i, 32))) } // XOR the chunk with hash chunk ^= hash; assembly { // Write 32-byte encrypted chunk mstore(add(result, add(i, 32)), chunk) } } } /// ===== External functions ===== function getBaseURICount() external view returns (uint256) { return baseURIIndices.length; } /** * @dev Lets an account with `MINTER_ROLE` mint tokens of ID from `nextTokenIdToMint` * to `nextTokenIdToMint + _amount - 1`. The URIs for these tokenIds is baseURI + `${tokenId}`. */ function lazyMint( uint256 _amount, string calldata _baseURIForTokens, bytes calldata _encryptedBaseURI ) external onlyMinter { uint256 startId = nextTokenIdToMint; uint256 baseURIIndex = startId + _amount; nextTokenIdToMint = baseURIIndex; baseURI[baseURIIndex] = _baseURIForTokens; baseURIIndices.push(baseURIIndex); if (_encryptedBaseURI.length != 0) { encryptedBaseURI[baseURIIndex] = _encryptedBaseURI; } emit LazyMintedTokens(startId, startId + _amount - 1, _baseURIForTokens, _encryptedBaseURI); } /// @dev Lets an account with `MINTER_ROLE` reveal the URI for the relevant NFTs. function reveal(uint256 index, bytes calldata _key) external onlyMinter returns (string memory revealedURI) { require(index < baseURIIndices.length, "invalid index."); uint256 _index = baseURIIndices[index]; bytes memory encryptedURI = encryptedBaseURI[_index]; require(encryptedURI.length != 0, "nothing to reveal."); revealedURI = string(encryptDecrypt(encryptedURI, _key)); baseURI[_index] = revealedURI; delete encryptedBaseURI[_index]; emit RevealedNFT(_index, revealedURI); return revealedURI; } /// @dev Lets an account claim a given quantity of tokens, of a single tokenId. function claim( address _receiver, uint256 _quantity, bytes32[] calldata _proofs ) external payable nonReentrant { uint256 tokenIdToClaim = nextTokenIdToClaim; // Get the claim conditions. uint256 activeConditionIndex = getIndexOfActiveCondition(); ClaimCondition memory condition = claimConditions.claimConditionAtIndex[activeConditionIndex]; // Verify claim validity. If not valid, revert. verifyClaim(_msgSender(), _quantity, _proofs, activeConditionIndex); // If there's a price, collect price. collectClaimPrice(condition, _quantity); // Mint the relevant tokens to claimer. transferClaimedTokens(_receiver, activeConditionIndex, _quantity); emit ClaimedTokens(activeConditionIndex, _msgSender(), _receiver, tokenIdToClaim, _quantity); } /// @dev Lets a module admin update mint conditions without resetting the restrictions. function updateClaimConditions(ClaimCondition[] calldata _conditions) external onlyModuleAdmin { resetClaimConditions(_conditions); emit NewClaimConditions(_conditions); } /// @dev Lets a module admin set mint conditions. function setClaimConditions(ClaimCondition[] calldata _conditions) external onlyModuleAdmin { uint256 numOfConditionsSet = resetClaimConditions(_conditions); resetTimestampRestriction(numOfConditionsSet); emit NewClaimConditions(_conditions); } /// @dev See EIP 2981 function royaltyInfo(uint256, uint256 salePrice) external view virtual override returns (address receiver, uint256 royaltyAmount) { receiver = controlCenter.getRoyaltyTreasury(address(this)); royaltyAmount = (salePrice * royaltyBps) / MAX_BPS; } // ===== Internal functions ===== /// @dev Overwrites the current claim conditions with new claim conditions function resetClaimConditions(ClaimCondition[] calldata _conditions) internal returns (uint256 indexForCondition) { // make sure the conditions are sorted in ascending order uint256 lastConditionStartTimestamp; for (uint256 i = 0; i < _conditions.length; i++) { require( lastConditionStartTimestamp == 0 || lastConditionStartTimestamp < _conditions[i].startTimestamp, "startTimestamp must be in ascending order." ); require(_conditions[i].maxClaimableSupply > 0, "max mint supply cannot be 0."); require(_conditions[i].quantityLimitPerTransaction > 0, "quantity limit cannot be 0."); claimConditions.claimConditionAtIndex[indexForCondition] = ClaimCondition({ startTimestamp: _conditions[i].startTimestamp, maxClaimableSupply: _conditions[i].maxClaimableSupply, supplyClaimed: 0, quantityLimitPerTransaction: _conditions[i].quantityLimitPerTransaction, waitTimeInSecondsBetweenClaims: _conditions[i].waitTimeInSecondsBetweenClaims, pricePerToken: _conditions[i].pricePerToken, currency: _conditions[i].currency, merkleRoot: _conditions[i].merkleRoot }); indexForCondition += 1; lastConditionStartTimestamp = _conditions[i].startTimestamp; } uint256 totalConditionCount = claimConditions.totalConditionCount; if (indexForCondition < totalConditionCount) { for (uint256 j = indexForCondition; j < totalConditionCount; j += 1) { delete claimConditions.claimConditionAtIndex[j]; } } claimConditions.totalConditionCount = indexForCondition; } /// @dev Updates the `timstampLimitIndex` to reset the time restriction between claims, for a claim condition. function resetTimestampRestriction(uint256 _factor) internal { claimConditions.timstampLimitIndex += _factor; } /// @dev Checks whether a request to claim tokens obeys the active mint condition. function verifyClaim( address _claimer, uint256 _quantity, bytes32[] calldata _proofs, uint256 _conditionIndex ) public view { ClaimCondition memory _claimCondition = claimConditions.claimConditionAtIndex[_conditionIndex]; require(_quantity > 0 && _quantity <= _claimCondition.quantityLimitPerTransaction, "invalid quantity claimed."); require( _claimCondition.supplyClaimed + _quantity <= _claimCondition.maxClaimableSupply, "exceed max mint supply." ); require(nextTokenIdToClaim + _quantity <= nextTokenIdToMint, "not enough minted tokens."); uint256 timestampIndex = _conditionIndex + claimConditions.timstampLimitIndex; uint256 timestampOfLastClaim = claimConditions.timestampOfLastClaim[_claimer][timestampIndex]; uint256 nextValidTimestampForClaim = getTimestampForNextValidClaim(_conditionIndex, _claimer); require(timestampOfLastClaim == 0 || block.timestamp >= nextValidTimestampForClaim, "cannot claim yet."); if (_claimCondition.merkleRoot != bytes32(0)) { bytes32 leaf = keccak256(abi.encodePacked(_claimer)); require(MerkleProof.verify(_proofs, _claimCondition.merkleRoot, leaf), "not in whitelist."); } } /// @dev Collects and distributes the primary sale value of tokens being claimed. function collectClaimPrice(ClaimCondition memory _claimCondition, uint256 _quantityToClaim) internal { if (_claimCondition.pricePerToken == 0) { return; } uint256 totalPrice = _quantityToClaim * _claimCondition.pricePerToken; uint256 fees = (totalPrice * feeBps) / MAX_BPS; if (_claimCondition.currency == NATIVE_TOKEN) { require(msg.value == totalPrice, "must send total price."); } else { validateERC20BalAndAllowance(_msgSender(), _claimCondition.currency, totalPrice); } transferCurrency(_claimCondition.currency, _msgSender(), controlCenter.getRoyaltyTreasury(address(this)), fees); transferCurrency(_claimCondition.currency, _msgSender(), defaultSaleRecipient, totalPrice - fees); } /// @dev Transfers the tokens being claimed. function transferClaimedTokens( address _to, uint256 _claimConditionIndex, uint256 _quantityBeingClaimed ) internal { // Update the supply minted under mint condition. claimConditions.claimConditionAtIndex[_claimConditionIndex].supplyClaimed += _quantityBeingClaimed; // Update the claimer's next valid timestamp to mint. If next mint timestamp overflows, cap it to max uint256. uint256 timestampIndex = _claimConditionIndex + claimConditions.timstampLimitIndex; claimConditions.timestampOfLastClaim[_msgSender()][timestampIndex] = block.timestamp; uint256 tokenIdToClaim = nextTokenIdToClaim; for (uint256 i = 0; i < _quantityBeingClaimed; i += 1) { _mint(_to, tokenIdToClaim); tokenIdToClaim += 1; } nextTokenIdToClaim = tokenIdToClaim; } /// @dev Transfers a given amount of currency. function transferCurrency( address _currency, address _from, address _to, uint256 _amount ) internal { if (_amount == 0) { return; } if (_currency == NATIVE_TOKEN) { if (_from == address(this)) { IWETH(nativeTokenWrapper).withdraw(_amount); safeTransferNativeToken(_to, _amount); } else if (_to == address(this)) { require(_amount == msg.value, "native token value does not match bid amount."); IWETH(nativeTokenWrapper).deposit{ value: _amount }(); } else { safeTransferNativeToken(_to, _amount); } } else { safeTransferERC20(_currency, _from, _to, _amount); } } /// @dev Validates that `_addrToCheck` owns and has approved contract to transfer the appropriate amount of currency function validateERC20BalAndAllowance( address _addrToCheck, address _currency, uint256 _currencyAmountToCheckAgainst ) internal view { require( IERC20(_currency).balanceOf(_addrToCheck) >= _currencyAmountToCheckAgainst && IERC20(_currency).allowance(_addrToCheck, address(this)) >= _currencyAmountToCheckAgainst, "insufficient currency balance or allowance." ); } /// @dev Transfers `amount` of native token to `to`. function safeTransferNativeToken(address to, uint256 value) internal { (bool success, ) = to.call{ value: value }(""); if (!success) { IWETH(nativeTokenWrapper).deposit{ value: value }(); safeTransferERC20(nativeTokenWrapper, address(this), to, value); } } /// @dev Transfer `amount` of ERC20 token from `from` to `to`. function safeTransferERC20( address _currency, address _from, address _to, uint256 _amount ) internal { if (_from == _to) { return; } uint256 balBefore = IERC20(_currency).balanceOf(_to); bool success = _from == address(this) ? IERC20(_currency).transfer(_to, _amount) : IERC20(_currency).transferFrom(_from, _to, _amount); uint256 balAfter = IERC20(_currency).balanceOf(_to); require(success && balAfter == balBefore + _amount, "failed to transfer currency."); } // ===== Setter functions ===== /// @dev Lets a module admin set the default recipient of all primary sales. function setDefaultSaleRecipient(address _saleRecipient) external onlyModuleAdmin { defaultSaleRecipient = _saleRecipient; emit NewSaleRecipient(_saleRecipient); } /// @dev Lets a module admin update the royalties paid on secondary token sales. function setRoyaltyBps(uint256 _royaltyBps) public onlyModuleAdmin { require(_royaltyBps <= MAX_BPS, "bps <= 10000."); royaltyBps = uint64(_royaltyBps); emit RoyaltyUpdated(_royaltyBps); } /// @dev Lets a module admin update the fees on primary sales. function setFeeBps(uint256 _feeBps) public onlyModuleAdmin { require(_feeBps <= MAX_BPS, "bps <= 10000."); feeBps = uint120(_feeBps); emit PrimarySalesFeeUpdates(_feeBps); } /// @dev Lets a module admin restrict token transfers. function setRestrictedTransfer(bool _restrictedTransfer) external onlyModuleAdmin { transfersRestricted = _restrictedTransfer; emit TransfersRestricted(_restrictedTransfer); } /// @dev Lets a module admin set a new owner for the contract. The new owner must be a module admin. function setOwner(address _newOwner) external onlyModuleAdmin { require(hasRole(DEFAULT_ADMIN_ROLE, _newOwner), "new owner not module admin."); address _prevOwner = _owner; _owner = _newOwner; emit NewOwner(_prevOwner, _newOwner); } /// @dev Lets a module admin set the URI for contract-level metadata. function setContractURI(string calldata _uri) external onlyModuleAdmin { contractURI = _uri; } // ===== Getter functions ===== /// @dev Returns the current active mint condition for a given tokenId. function getTimestampForNextValidClaim(uint256 _index, address _claimer) public view returns (uint256 nextValidTimestampForClaim) { uint256 timestampIndex = _index + claimConditions.timstampLimitIndex; uint256 timestampOfLastClaim = claimConditions.timestampOfLastClaim[_claimer][timestampIndex]; unchecked { nextValidTimestampForClaim = timestampOfLastClaim + claimConditions.claimConditionAtIndex[_index].waitTimeInSecondsBetweenClaims; if (nextValidTimestampForClaim < timestampOfLastClaim) { nextValidTimestampForClaim = type(uint256).max; } } } /// @dev Returns the mint condition for a given tokenId, at the given index. function getClaimConditionAtIndex(uint256 _index) external view returns (ClaimCondition memory mintCondition) { mintCondition = claimConditions.claimConditionAtIndex[_index]; } /// ===== ERC 721 functions ===== /// @dev Burns `tokenId`. See {ERC721-_burn}. function burn(uint256 tokenId) public virtual { //solhint-disable-next-line max-line-length require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721Burnable: caller is not owner nor approved"); _burn(tokenId); } /// @dev See {ERC721-_beforeTokenTransfer}. function _beforeTokenTransfer( address from, address to, uint256 tokenId ) internal virtual override(ERC721Enumerable) { super._beforeTokenTransfer(from, to, tokenId); // if transfer is restricted on the contract, we still want to allow burning and minting if (transfersRestricted && from != address(0) && to != address(0)) { require(hasRole(TRANSFER_ROLE, from) || hasRole(TRANSFER_ROLE, to), "restricted to TRANSFER_ROLE holders"); } } function supportsInterface(bytes4 interfaceId) public view virtual override(AccessControlEnumerable, ERC721Enumerable, IERC165) returns (bool) { return super.supportsInterface(interfaceId) || interfaceId == type(IERC2981).interfaceId; } function _msgSender() internal view virtual override(Context, ERC2771Context) returns (address sender) { return ERC2771Context._msgSender(); } function _msgData() internal view virtual override(Context, ERC2771Context) returns (bytes calldata) { return ERC2771Context._msgData(); } }