// SPDX-License-Identifier: Apache-2.0 pragma solidity ^0.8.16; // ========== External imports ========== import "@openzeppelin/contracts/access/Ownable.sol"; import "@openzeppelin/contracts/utils/Strings.sol"; import "@openzeppelin/contracts/interfaces/IERC2981.sol"; import "@openzeppelin/contracts/security/ReentrancyGuard.sol"; import "@openzeppelin/contracts/security/Pausable.sol"; import "@erc721/ERC721A/ERC721A.sol"; // ========== Internal imports ========== import "./lib/CurrencyTransferLib.sol"; // ========== Features ========== import "./extension/RoyaltySplitter.sol"; import "./extension/PrimarySale.sol"; import "./extension/DelayedReveal.sol"; import "./extension/LazyMint.sol"; import "./extension/Drop.sol"; import "./extension/WalletClaimCounter.sol"; import "./extension/Oneshot.sol"; import "./extension/ThirdPartyMint721.sol"; import "./lib/Crypto.sol"; contract KanjiDropERC721A is WalletClaimCounter, RoyaltySplitter, PrimarySale, Ownable, DelayedReveal, LazyMint, Drop, ThirdPartyMint721, ReentrancyGuard, Pausable, Oneshot, ERC721A { using Strings for uint256; /*/////////////////////////////////////////////////////////////// Errors //////////////////////////////////////////////////////////////*/ error NotEnoughMintedTokensError(uint256 quantity, uint256 nextTokenIdToClaim, uint256 nextTokenIdToMint); error ExceedMaxSupplyError(uint256 quantity, uint256 maxSupply, uint256 nextTokenIdToClaim); error ExceedClaimLimitForWalletError( uint256 quantity, uint256 walletClaimCountClaimer, address claimer, uint256 maxWalletClaimCount ); error PausedError(); error IncorrectMsgValue(uint256 expected); /*/////////////////////////////////////////////////////////////// Events //////////////////////////////////////////////////////////////*/ event MaxSupplyUpdated(uint256 maxSupply); /*/////////////////////////////////////////////////////////////// State variables //////////////////////////////////////////////////////////////*/ /// @dev Max bps in the thirdweb system. uint256 private constant MAX_BPS = 10_000; /// @dev Global max total supply of NFTs. uint256 public immutable maxSupply; /*/////////////////////////////////////////////////////////////// Constructor + initializer logic //////////////////////////////////////////////////////////////*/ /// @notice Creates a new `KanjiDropERC721A` contract. /// @param _name The name of the NFT. /// @param _symbol The symbol of the NFT. /// @param _maxSupply The maximum supply of the collection /// @param _metadata Contract-level metadata /// @param _saleRecipients Recipients of the sale /// @param _royaltyBps The ERC2981 royalty cut /// @param _royaltyRecipients The ERC2981 royalty recipients constructor( string memory _name, string memory _symbol, uint256 _maxSupply, Metadata memory _metadata, PrimarySaleRecipient[] memory _saleRecipients, uint16 _royaltyBps, RoyaltyRecipient[] memory _royaltyRecipients, IAccessControl _thirdParty ) PrimarySale(_saleRecipients) RoyaltySplitter(_royaltyRecipients, _metadata, _royaltyBps) ERC721A(_name, _symbol) ReentrancyGuard() Ownable() Pausable() ThirdPartyMint721(_thirdParty) { maxSupply = _maxSupply; } /*/////////////////////////////////////////////////////////////// ERC 165 / 721 / 2981 logic //////////////////////////////////////////////////////////////*/ /// @dev Returns the URI for a given tokenId. function tokenURI(uint256 _tokenId) public view override returns (string memory) { if (isEncrypted) { return flaskURI; } else { return string(abi.encodePacked(baseURI, _tokenId.toString(), ".json")); } } /// @dev See ERC 165 function supportsInterface(bytes4 interfaceId) public view virtual override(ERC721A, IERC165) returns (bool) { return super.supportsInterface(interfaceId) || type(IERC2981).interfaceId == interfaceId; } /*/////////////////////////////////////////////////////////////// Lazy minting + delayed-reveal logic //////////////////////////////////////////////////////////////*/ function lazyMint(uint256 _amount, bytes calldata _baseURI, string calldata _flaskURI) public onlyOwner oneShot { if (_amount == 0) revert AmountCantBeZeroError(); if (_baseURI.length == 0) { revert BaseURIRequiredError(); } baseURI = string(_baseURI); if (bytes(_flaskURI).length != 0) { isEncrypted = true; flaskURI = _flaskURI; } super.lazyMint(_amount); } /// @dev Lets an owner reveal the URI for a batch of 'delayed-reveal' NFTs. function reveal(bytes calldata _key) external onlyOwner oneShot returns (string memory revealedURI) { if (!isEncrypted) revert NothingToReveal(); revealedURI = string(Crypto.encryptDecrypt(bytes(baseURI), _key)); baseURI = revealedURI; isEncrypted = false; emit URIRevealed(revealedURI); } /// @notice owner mint a reserve of tokens to specific address /// @param receiver receiver of mint /// @param quantity quantity gonna be mint function mintOwner(address receiver, uint256 quantity) external onlyOwner { if (_nextTokenId() + quantity > maxSupply) revert ExceedMaxSupplyError(quantity, maxSupply, _nextTokenId()); super.lazyMint(quantity); _safeMint(receiver, quantity); } /// @notice owner mint a one token for every address in the list /// Be aware of the order, as the tokenID depends on it /// @param recipients receiver of mint function batchMintOwner(address[] memory recipients) external onlyOwner { if (_nextTokenId() + recipients.length > maxSupply) { revert ExceedMaxSupplyError(recipients.length, maxSupply, _nextTokenId()); } super.lazyMint(recipients.length); for (uint256 i = 0; i < recipients.length; i++) { _safeMint(recipients[i], 1); } } /*/////////////////////////////////////////////////////////////// Internal functions //////////////////////////////////////////////////////////////*/ /// @dev Runs before every `claim` function call. function _beforeClaim(address _claimer, uint256 _quantity, bytes32[] calldata) internal view override { if (_nextTokenId() + _quantity > nextTokenIdToLazyMint) { revert NotEnoughMintedTokensError({ quantity: _quantity, nextTokenIdToClaim: _nextTokenId(), nextTokenIdToMint: nextTokenIdToLazyMint }); } if (maxSupply != 0 && _nextTokenId() + _quantity > maxSupply) { revert ExceedMaxSupplyError({quantity: _quantity, maxSupply: maxSupply, nextTokenIdToClaim: _nextTokenId()}); } if (maxWalletClaimCount != 0 && walletClaimCount[_claimer] + _quantity > maxWalletClaimCount) { revert ExceedClaimLimitForWalletError({ quantity: _quantity, walletClaimCountClaimer: walletClaimCount[_claimer], claimer: _claimer, maxWalletClaimCount: maxWalletClaimCount }); } } /// @dev Collects and distributes the primary sale value of NFTs being claimed. function collectPriceOnClaim(uint256 _quantityToClaim, uint256 _pricePerToken) internal override { if (_pricePerToken == 0) { return; } uint256 totalPrice = _quantityToClaim * _pricePerToken; if (msg.value != totalPrice) { revert IncorrectMsgValue({expected: totalPrice}); } address[] memory recipients = primarySaleRecipients(); for (uint256 i = 0; i < recipients.length; i++) { uint256 payout = ((totalPrice) * primarySaleRecipientBps(recipients[i])) / MAX_BPS; CurrencyTransferLib.transferCurrency(CurrencyTransferLib.NATIVE_TOKEN, _msgSender(), recipients[i], payout); } } /// @dev Transfers the NFTs being claimed. function transferTokensOnClaim(address _to, uint256 _quantityBeingClaimed) internal override returns (uint256 startTokenId) { startTokenId = _nextTokenId(); walletClaimCount[_msgSender()] += _quantityBeingClaimed; _safeMint(_to, _quantityBeingClaimed); } function _afterMintFromThirdParty(address _receiver, uint256 _quantity) internal override { _safeMint(_receiver, _quantity); } function _beforeChangeThirdPartySetup(IAccessControl _thirdPartySetup) internal override onlyOwner {} /*/////////////////////////////////////////////////////////////// Miscellaneous //////////////////////////////////////////////////////////////*/ /** * Returns the total amount of tokens minted in the contract. */ function totalMinted() external view returns (uint256) { unchecked { return _nextTokenId() - _startTokenId(); } } /// @dev The tokenId of the next NFT that will be minted / lazy minted. function nextTokenIdToMint() external view returns (uint256) { return nextTokenIdToLazyMint; } /// @dev The next token ID of the NFT that can be claimed. function nextTokenIdToClaim() external view returns (uint256) { return _nextTokenId(); } /// @dev See {ERC721-_beforeTokenTransfer}. function _beforeTokenTransfers(address from, address to, uint256 startTokenId, uint256 quantity) internal virtual override { if (paused()) { revert PausedError(); } super._beforeTokenTransfers(from, to, startTokenId, quantity); } /// @notice Temporarily lock the contract function pause() external onlyOwner { if (paused()) _unpause(); else _pause(); } function setClaimConditions(ClaimCondition[] calldata _conditions, bool _resetClaimEligibility) public override onlyOwner { super.setClaimConditions(_conditions, _resetClaimEligibility); } function claim(address _receiver, uint256 _quantity, bytes32[] calldata _proof) public payable override nonReentrant { super.claim(_receiver, _quantity, _proof); } }