ETH Price: $3,145.83 (+2.63%)
Gas: 13 Gwei

Transaction Decoder

Block:
19616716 at Apr-09-2024 07:51:47 AM +UTC
Transaction Fee:
0.004048730706274184 ETH $12.74
Gas Used:
174,062 Gas / 23.260279132 Gwei

Account State Difference:

  Address   Before After State Difference Code
0x15C905de...5D30ef1f4
0x1cf0dF2A...b8d39e18a
(Rarible: Treasury)
1,411.12894546318301812 Eth1,411.13044546318301812 Eth0.0015
(Titan Builder)
12.742400671479751179 Eth12.742400845541751179 Eth0.000000174062
0xa94fdea8...93A63f1F2
0.081618912699820052 Eth
Nonce: 1218
0.076070181993545868 Eth
Nonce: 1219
0.005548730706274184

Execution Trace

ETH 0.0015 OpenEditionERC721FlatFee.claim( )
  • ETH 0.0015 OpenEditionERC721FlatFee.claim( )
    claim[Drop (ln:1808)]
    File 1 of 4: OpenEditionERC721FlatFee
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)
    pragma solidity ^0.8.0;
    /**
     * @dev Interface of the ERC165 standard, as defined in the
     * [EIP](https://eips.ethereum.org/EIPS/eip-165).
     *
     * Implementers can declare support of contract interfaces, which can then be
     * queried by others ({ERC165Checker}).
     *
     * For an implementation, see {ERC165}.
     */
    interface IERC165 {
        /**
         * @dev Returns true if this contract implements the interface defined by
         * `interfaceId`. See the corresponding
         * [EIP section](https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified)
         * to learn more about how these ids are created.
         *
         * This function call must use less than 30 000 gas.
         */
        function supportsInterface(bytes4 interfaceId) external view returns (bool);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /**
     * @title ERC20 interface
     * @dev see https://github.com/ethereum/EIPs/issues/20
     */
    interface IERC20 {
        function totalSupply() external view returns (uint256);
        function balanceOf(address who) external view returns (uint256);
        function allowance(address owner, address spender) external view returns (uint256);
        function transfer(address to, uint256 value) external returns (bool);
        function approve(address spender, uint256 value) external returns (bool);
        function transferFrom(address from, address to, uint256 value) external returns (bool);
        event Transfer(address indexed from, address indexed to, uint256 value);
        event Approval(address indexed owner, address indexed spender, uint256 value);
    }
    // SPDX-License-Identifier: Apache 2.0
    pragma solidity ^0.8.0;
    import "./IERC165.sol";
    /**
     * @dev Interface for the NFT Royalty Standard.
     *
     * A standardized way to retrieve royalty payment information for non-fungible tokens (NFTs) to enable universal
     * support for royalty payments across all NFT marketplaces and ecosystem participants.
     *
     * _Available since v4.5._
     */
    interface IERC2981 is IERC165 {
        /**
         * @dev Returns how much royalty is owed and to whom, based on a sale price that may be denominated in any unit of
         * exchange. The royalty amount is denominated and should be payed in that same unit of exchange.
         */
        function royaltyInfo(
            uint256 tokenId,
            uint256 salePrice
        ) external view returns (address receiver, uint256 royaltyAmount);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.11;
    import "./IERC165.sol";
    import "./IERC721.sol";
    interface IERC4906 is IERC165 {
        /// @dev This event emits when the metadata of a token is changed.
        /// So that the third-party platforms such as NFT market could
        /// timely update the images and related attributes of the NFT.
        event MetadataUpdate(uint256 _tokenId);
        /// @dev This event emits when the metadata of a range of tokens is changed.
        /// So that the third-party platforms such as NFT market could
        /// timely update the images and related attributes of the NFTs.
        event BatchMetadataUpdate(uint256 _fromTokenId, uint256 _toTokenId);
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts v4.4.1 (token/ERC721/IERC721.sol)
    pragma solidity ^0.8.0;
    /**
     * @dev Required interface of an ERC721 compliant contract.
     */
    interface IERC721 {
        /**
         * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
         */
        event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
        /**
         * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
         */
        event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
        /**
         * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
         */
        event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
        /**
         * @dev Returns the number of tokens in ``owner``'s account.
         */
        function balanceOf(address owner) external view returns (uint256);
        /**
         * @dev Returns the owner of the `tokenId` token.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         */
        function ownerOf(uint256 tokenId) external view returns (address);
        /**
         * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
         * are aware of the ERC721 protocol to prevent tokens from being forever locked.
         *
         * Requirements:
         *
         * - `from` cannot be the zero address.
         * - `to` cannot be the zero address.
         * - `tokenId` token must exist and be owned by `from`.
         * - If the caller is not `from`, it must be have been allowed to move this token by either {approve} or {setApprovalForAll}.
         * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
         *
         * Emits a {Transfer} event.
         */
        function safeTransferFrom(address from, address to, uint256 tokenId) external;
        /**
         * @dev Transfers `tokenId` token from `from` to `to`.
         *
         * WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
         *
         * Requirements:
         *
         * - `from` cannot be the zero address.
         * - `to` cannot be the zero address.
         * - `tokenId` token must be owned by `from`.
         * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
         *
         * Emits a {Transfer} event.
         */
        function transferFrom(address from, address to, uint256 tokenId) external;
        /**
         * @dev Gives permission to `to` to transfer `tokenId` token to another account.
         * The approval is cleared when the token is transferred.
         *
         * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
         *
         * Requirements:
         *
         * - The caller must own the token or be an approved operator.
         * - `tokenId` must exist.
         *
         * Emits an {Approval} event.
         */
        function approve(address to, uint256 tokenId) external;
        /**
         * @dev Returns the account approved for `tokenId` token.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         */
        function getApproved(uint256 tokenId) external view returns (address);
        /**
         * @dev Approve or remove `operator` as an operator for the caller.
         * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
         *
         * Requirements:
         *
         * - The `operator` cannot be the caller.
         *
         * Emits an {ApprovalForAll} event.
         */
        function setApprovalForAll(address operator, bool _approved) external;
        /**
         * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
         *
         * See {setApprovalForAll}
         */
        function isApprovedForAll(address owner, address operator) external view returns (bool);
        /**
         * @dev Safely transfers `tokenId` token from `from` to `to`.
         *
         * Requirements:
         *
         * - `from` cannot be the zero address.
         * - `to` cannot be the zero address.
         * - `tokenId` token must exist and be owned by `from`.
         * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
         * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
         *
         * Emits a {Transfer} event.
         */
        function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
    }
    // SPDX-License-Identifier: MIT
    // ERC721A Contracts v4.2.3
    // Creator: Chiru Labs
    pragma solidity ^0.8.4;
    import "./IERC721AQueryableUpgradeable.sol";
    import "./ERC721AUpgradeable.sol";
    import "./ERC721A__Initializable.sol";
    /**
     * @title ERC721AQueryable.
     *
     * @dev ERC721A subclass with convenience query functions.
     */
    abstract contract ERC721AQueryableUpgradeable is
        ERC721A__Initializable,
        ERC721AUpgradeable,
        IERC721AQueryableUpgradeable
    {
        function __ERC721AQueryable_init() internal onlyInitializingERC721A {
            __ERC721AQueryable_init_unchained();
        }
        function __ERC721AQueryable_init_unchained() internal onlyInitializingERC721A {}
        /**
         * @dev Returns the `TokenOwnership` struct at `tokenId` without reverting.
         *
         * If the `tokenId` is out of bounds:
         *
         * - `addr = address(0)`
         * - `startTimestamp = 0`
         * - `burned = false`
         * - `extraData = 0`
         *
         * If the `tokenId` is burned:
         *
         * - `addr = <Address of owner before token was burned>`
         * - `startTimestamp = <Timestamp when token was burned>`
         * - `burned = true`
         * - `extraData = <Extra data when token was burned>`
         *
         * Otherwise:
         *
         * - `addr = <Address of owner>`
         * - `startTimestamp = <Timestamp of start of ownership>`
         * - `burned = false`
         * - `extraData = <Extra data at start of ownership>`
         */
        function explicitOwnershipOf(
            uint256 tokenId
        ) public view virtual override returns (TokenOwnership memory ownership) {
            unchecked {
                if (tokenId >= _startTokenId()) {
                    if (tokenId < _nextTokenId()) {
                        // If the `tokenId` is within bounds,
                        // scan backwards for the initialized ownership slot.
                        while (!_ownershipIsInitialized(tokenId)) --tokenId;
                        return _ownershipAt(tokenId);
                    }
                }
            }
        }
        /**
         * @dev Returns an array of token IDs owned by `owner`,
         * in the range [`start`, `stop`)
         * (i.e. `start <= tokenId < stop`).
         *
         * This function allows for tokens to be queried if the collection
         * grows too big for a single call of {ERC721AQueryable-tokensOfOwner}.
         *
         * Requirements:
         *
         * - `start < stop`
         */
        function tokensOfOwnerIn(
            address owner,
            uint256 start,
            uint256 stop
        ) external view virtual override returns (uint256[] memory) {
            return _tokensOfOwnerIn(owner, start, stop);
        }
        /**
         * @dev Returns an array of token IDs owned by `owner`.
         *
         * This function scans the ownership mapping and is O(`totalSupply`) in complexity.
         * It is meant to be called off-chain.
         *
         * See {ERC721AQueryable-tokensOfOwnerIn} for splitting the scan into
         * multiple smaller scans if the collection is large enough to cause
         * an out-of-gas error (10K collections should be fine).
         */
        function tokensOfOwner(address owner) external view virtual override returns (uint256[] memory) {
            uint256 start = _startTokenId();
            uint256 stop = _nextTokenId();
            uint256[] memory tokenIds;
            if (start != stop) tokenIds = _tokensOfOwnerIn(owner, start, stop);
            return tokenIds;
        }
        /**
         * @dev Helper function for returning an array of token IDs owned by `owner`.
         *
         * Note that this function is optimized for smaller bytecode size over runtime gas,
         * since it is meant to be called off-chain.
         */
        function _tokensOfOwnerIn(address owner, uint256 start, uint256 stop) private view returns (uint256[] memory) {
            unchecked {
                if (start >= stop) _revert(InvalidQueryRange.selector);
                // Set `start = max(start, _startTokenId())`.
                if (start < _startTokenId()) {
                    start = _startTokenId();
                }
                uint256 stopLimit = _nextTokenId();
                // Set `stop = min(stop, stopLimit)`.
                if (stop >= stopLimit) {
                    stop = stopLimit;
                }
                uint256[] memory tokenIds;
                uint256 tokenIdsMaxLength = balanceOf(owner);
                bool startLtStop = start < stop;
                assembly {
                    // Set `tokenIdsMaxLength` to zero if `start` is less than `stop`.
                    tokenIdsMaxLength := mul(tokenIdsMaxLength, startLtStop)
                }
                if (tokenIdsMaxLength != 0) {
                    // Set `tokenIdsMaxLength = min(balanceOf(owner), stop - start)`,
                    // to cater for cases where `balanceOf(owner)` is too big.
                    if (stop - start <= tokenIdsMaxLength) {
                        tokenIdsMaxLength = stop - start;
                    }
                    assembly {
                        // Grab the free memory pointer.
                        tokenIds := mload(0x40)
                        // Allocate one word for the length, and `tokenIdsMaxLength` words
                        // for the data. `shl(5, x)` is equivalent to `mul(32, x)`.
                        mstore(0x40, add(tokenIds, shl(5, add(tokenIdsMaxLength, 1))))
                    }
                    // We need to call `explicitOwnershipOf(start)`,
                    // because the slot at `start` may not be initialized.
                    TokenOwnership memory ownership = explicitOwnershipOf(start);
                    address currOwnershipAddr;
                    // If the starting slot exists (i.e. not burned),
                    // initialize `currOwnershipAddr`.
                    // `ownership.address` will not be zero,
                    // as `start` is clamped to the valid token ID range.
                    if (!ownership.burned) {
                        currOwnershipAddr = ownership.addr;
                    }
                    uint256 tokenIdsIdx;
                    // Use a do-while, which is slightly more efficient for this case,
                    // as the array will at least contain one element.
                    do {
                        ownership = _ownershipAt(start);
                        assembly {
                            switch mload(add(ownership, 0x40))
                            // if `ownership.burned == false`.
                            case 0 {
                                // if `ownership.addr != address(0)`.
                                // The `addr` already has it's upper 96 bits clearned,
                                // since it is written to memory with regular Solidity.
                                if mload(ownership) {
                                    currOwnershipAddr := mload(ownership)
                                }
                                // if `currOwnershipAddr == owner`.
                                // The `shl(96, x)` is to make the comparison agnostic to any
                                // dirty upper 96 bits in `owner`.
                                if iszero(shl(96, xor(currOwnershipAddr, owner))) {
                                    tokenIdsIdx := add(tokenIdsIdx, 1)
                                    mstore(add(tokenIds, shl(5, tokenIdsIdx)), start)
                                }
                            }
                            // Otherwise, reset `currOwnershipAddr`.
                            // This handles the case of batch burned tokens
                            // (burned bit of first slot set, remaining slots left uninitialized).
                            default {
                                currOwnershipAddr := 0
                            }
                            start := add(start, 1)
                        }
                    } while (!(start == stop || tokenIdsIdx == tokenIdsMaxLength));
                    // Store the length of the array.
                    assembly {
                        mstore(tokenIds, tokenIdsIdx)
                    }
                }
                return tokenIds;
            }
        }
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    library ERC721AStorage {
        // Bypass for a `--via-ir` bug (https://github.com/chiru-labs/ERC721A/pull/364).
        struct TokenApprovalRef {
            address value;
        }
        struct Layout {
            // =============================================================
            //                            STORAGE
            // =============================================================
            // The next token ID to be minted.
            uint256 _currentIndex;
            // The number of tokens burned.
            uint256 _burnCounter;
            // Token name
            string _name;
            // Token symbol
            string _symbol;
            // Mapping from token ID to ownership details
            // An empty struct value does not necessarily mean the token is unowned.
            // See {_packedOwnershipOf} implementation for details.
            //
            // Bits Layout:
            // - [0..159]   `addr`
            // - [160..223] `startTimestamp`
            // - [224]      `burned`
            // - [225]      `nextInitialized`
            // - [232..255] `extraData`
            mapping(uint256 => uint256) _packedOwnerships;
            // Mapping owner address to address data.
            //
            // Bits Layout:
            // - [0..63]    `balance`
            // - [64..127]  `numberMinted`
            // - [128..191] `numberBurned`
            // - [192..255] `aux`
            mapping(address => uint256) _packedAddressData;
            // Mapping from token ID to approved address.
            mapping(uint256 => ERC721AStorage.TokenApprovalRef) _tokenApprovals;
            // Mapping from owner to operator approvals
            mapping(address => mapping(address => bool)) _operatorApprovals;
        }
        bytes32 internal constant STORAGE_SLOT = keccak256("ERC721A.contracts.storage.ERC721A");
        function layout() internal pure returns (Layout storage l) {
            bytes32 slot = STORAGE_SLOT;
            assembly {
                l.slot := slot
            }
        }
    }
    // SPDX-License-Identifier: MIT
    // ERC721A Contracts v4.2.3
    // Creator: Chiru Labs
    pragma solidity ^0.8.4;
    import "./IERC721AUpgradeable.sol";
    import { ERC721AStorage } from "./ERC721AStorage.sol";
    import "./ERC721A__Initializable.sol";
    /**
     * @dev Interface of ERC721 token receiver.
     */
    interface ERC721A__IERC721ReceiverUpgradeable {
        function onERC721Received(
            address operator,
            address from,
            uint256 tokenId,
            bytes calldata data
        ) external returns (bytes4);
    }
    /**
     * @title ERC721A
     *
     * @dev Implementation of the [ERC721](https://eips.ethereum.org/EIPS/eip-721)
     * Non-Fungible Token Standard, including the Metadata extension.
     * Optimized for lower gas during batch mints.
     *
     * Token IDs are minted in sequential order (e.g. 0, 1, 2, 3, ...)
     * starting from `_startTokenId()`.
     *
     * Assumptions:
     *
     * - An owner cannot have more than 2**64 - 1 (max value of uint64) of supply.
     * - The maximum token ID cannot exceed 2**256 - 1 (max value of uint256).
     */
    contract ERC721AUpgradeable is ERC721A__Initializable, IERC721AUpgradeable {
        using ERC721AStorage for ERC721AStorage.Layout;
        // =============================================================
        //                           CONSTANTS
        // =============================================================
        // Mask of an entry in packed address data.
        uint256 private constant _BITMASK_ADDRESS_DATA_ENTRY = (1 << 64) - 1;
        // The bit position of `numberMinted` in packed address data.
        uint256 private constant _BITPOS_NUMBER_MINTED = 64;
        // The bit position of `numberBurned` in packed address data.
        uint256 private constant _BITPOS_NUMBER_BURNED = 128;
        // The bit position of `aux` in packed address data.
        uint256 private constant _BITPOS_AUX = 192;
        // Mask of all 256 bits in packed address data except the 64 bits for `aux`.
        uint256 private constant _BITMASK_AUX_COMPLEMENT = (1 << 192) - 1;
        // The bit position of `startTimestamp` in packed ownership.
        uint256 private constant _BITPOS_START_TIMESTAMP = 160;
        // The bit mask of the `burned` bit in packed ownership.
        uint256 private constant _BITMASK_BURNED = 1 << 224;
        // The bit position of the `nextInitialized` bit in packed ownership.
        uint256 private constant _BITPOS_NEXT_INITIALIZED = 225;
        // The bit mask of the `nextInitialized` bit in packed ownership.
        uint256 private constant _BITMASK_NEXT_INITIALIZED = 1 << 225;
        // The bit position of `extraData` in packed ownership.
        uint256 private constant _BITPOS_EXTRA_DATA = 232;
        // Mask of all 256 bits in a packed ownership except the 24 bits for `extraData`.
        uint256 private constant _BITMASK_EXTRA_DATA_COMPLEMENT = (1 << 232) - 1;
        // The mask of the lower 160 bits for addresses.
        uint256 private constant _BITMASK_ADDRESS = (1 << 160) - 1;
        // The maximum `quantity` that can be minted with {_mintERC2309}.
        // This limit is to prevent overflows on the address data entries.
        // For a limit of 5000, a total of 3.689e15 calls to {_mintERC2309}
        // is required to cause an overflow, which is unrealistic.
        uint256 private constant _MAX_MINT_ERC2309_QUANTITY_LIMIT = 5000;
        // The `Transfer` event signature is given by:
        // `keccak256(bytes("Transfer(address,address,uint256)"))`.
        bytes32 private constant _TRANSFER_EVENT_SIGNATURE =
            0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef;
        // =============================================================
        //                          CONSTRUCTOR
        // =============================================================
        function __ERC721A_init(string memory name_, string memory symbol_) internal onlyInitializingERC721A {
            __ERC721A_init_unchained(name_, symbol_);
        }
        function __ERC721A_init_unchained(string memory name_, string memory symbol_) internal onlyInitializingERC721A {
            ERC721AStorage.layout()._name = name_;
            ERC721AStorage.layout()._symbol = symbol_;
            ERC721AStorage.layout()._currentIndex = _startTokenId();
        }
        // =============================================================
        //                   TOKEN COUNTING OPERATIONS
        // =============================================================
        /**
         * @dev Returns the starting token ID.
         * To change the starting token ID, please override this function.
         */
        function _startTokenId() internal view virtual returns (uint256) {
            return 0;
        }
        /**
         * @dev Returns the next token ID to be minted.
         */
        function _nextTokenId() internal view virtual returns (uint256) {
            return ERC721AStorage.layout()._currentIndex;
        }
        /**
         * @dev Returns the total number of tokens in existence.
         * Burned tokens will reduce the count.
         * To get the total number of tokens minted, please see {_totalMinted}.
         */
        function totalSupply() public view virtual override returns (uint256) {
            // Counter underflow is impossible as _burnCounter cannot be incremented
            // more than `_currentIndex - _startTokenId()` times.
            unchecked {
                return ERC721AStorage.layout()._currentIndex - ERC721AStorage.layout()._burnCounter - _startTokenId();
            }
        }
        /**
         * @dev Returns the total amount of tokens minted in the contract.
         */
        function _totalMinted() internal view virtual returns (uint256) {
            // Counter underflow is impossible as `_currentIndex` does not decrement,
            // and it is initialized to `_startTokenId()`.
            unchecked {
                return ERC721AStorage.layout()._currentIndex - _startTokenId();
            }
        }
        /**
         * @dev Returns the total number of tokens burned.
         */
        function _totalBurned() internal view virtual returns (uint256) {
            return ERC721AStorage.layout()._burnCounter;
        }
        // =============================================================
        //                    ADDRESS DATA OPERATIONS
        // =============================================================
        /**
         * @dev Returns the number of tokens in `owner`'s account.
         */
        function balanceOf(address owner) public view virtual override returns (uint256) {
            if (owner == address(0)) _revert(BalanceQueryForZeroAddress.selector);
            return ERC721AStorage.layout()._packedAddressData[owner] & _BITMASK_ADDRESS_DATA_ENTRY;
        }
        /**
         * Returns the number of tokens minted by `owner`.
         */
        function _numberMinted(address owner) internal view returns (uint256) {
            return
                (ERC721AStorage.layout()._packedAddressData[owner] >> _BITPOS_NUMBER_MINTED) & _BITMASK_ADDRESS_DATA_ENTRY;
        }
        /**
         * Returns the number of tokens burned by or on behalf of `owner`.
         */
        function _numberBurned(address owner) internal view returns (uint256) {
            return
                (ERC721AStorage.layout()._packedAddressData[owner] >> _BITPOS_NUMBER_BURNED) & _BITMASK_ADDRESS_DATA_ENTRY;
        }
        /**
         * Returns the auxiliary data for `owner`. (e.g. number of whitelist mint slots used).
         */
        function _getAux(address owner) internal view returns (uint64) {
            return uint64(ERC721AStorage.layout()._packedAddressData[owner] >> _BITPOS_AUX);
        }
        /**
         * Sets the auxiliary data for `owner`. (e.g. number of whitelist mint slots used).
         * If there are multiple variables, please pack them into a uint64.
         */
        function _setAux(address owner, uint64 aux) internal virtual {
            uint256 packed = ERC721AStorage.layout()._packedAddressData[owner];
            uint256 auxCasted;
            // Cast `aux` with assembly to avoid redundant masking.
            assembly {
                auxCasted := aux
            }
            packed = (packed & _BITMASK_AUX_COMPLEMENT) | (auxCasted << _BITPOS_AUX);
            ERC721AStorage.layout()._packedAddressData[owner] = packed;
        }
        // =============================================================
        //                            IERC165
        // =============================================================
        /**
         * @dev Returns true if this contract implements the interface defined by
         * `interfaceId`. See the corresponding
         * [EIP section](https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified)
         * to learn more about how these ids are created.
         *
         * This function call must use less than 30000 gas.
         */
        function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
            // The interface IDs are constants representing the first 4 bytes
            // of the XOR of all function selectors in the interface.
            // See: [ERC165](https://eips.ethereum.org/EIPS/eip-165)
            // (e.g. `bytes4(i.functionA.selector ^ i.functionB.selector ^ ...)`)
            return
                interfaceId == 0x01ffc9a7 || // ERC165 interface ID for ERC165.
                interfaceId == 0x80ac58cd || // ERC165 interface ID for ERC721.
                interfaceId == 0x5b5e139f; // ERC165 interface ID for ERC721Metadata.
        }
        // =============================================================
        //                        IERC721Metadata
        // =============================================================
        /**
         * @dev Returns the token collection name.
         */
        function name() public view virtual override returns (string memory) {
            return ERC721AStorage.layout()._name;
        }
        /**
         * @dev Returns the token collection symbol.
         */
        function symbol() public view virtual override returns (string memory) {
            return ERC721AStorage.layout()._symbol;
        }
        /**
         * @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
         */
        function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
            if (!_exists(tokenId)) _revert(URIQueryForNonexistentToken.selector);
            string memory baseURI = _baseURI();
            return bytes(baseURI).length != 0 ? string(abi.encodePacked(baseURI, _toString(tokenId))) : "";
        }
        /**
         * @dev Base URI for computing {tokenURI}. If set, the resulting URI for each
         * token will be the concatenation of the `baseURI` and the `tokenId`. Empty
         * by default, it can be overridden in child contracts.
         */
        function _baseURI() internal view virtual returns (string memory) {
            return "";
        }
        // =============================================================
        //                     OWNERSHIPS OPERATIONS
        // =============================================================
        /**
         * @dev Returns the owner of the `tokenId` token.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         */
        function ownerOf(uint256 tokenId) public view virtual override returns (address) {
            return address(uint160(_packedOwnershipOf(tokenId)));
        }
        /**
         * @dev Gas spent here starts off proportional to the maximum mint batch size.
         * It gradually moves to O(1) as tokens get transferred around over time.
         */
        function _ownershipOf(uint256 tokenId) internal view virtual returns (TokenOwnership memory) {
            return _unpackedOwnership(_packedOwnershipOf(tokenId));
        }
        /**
         * @dev Returns the unpacked `TokenOwnership` struct at `index`.
         */
        function _ownershipAt(uint256 index) internal view virtual returns (TokenOwnership memory) {
            return _unpackedOwnership(ERC721AStorage.layout()._packedOwnerships[index]);
        }
        /**
         * @dev Returns whether the ownership slot at `index` is initialized.
         * An uninitialized slot does not necessarily mean that the slot has no owner.
         */
        function _ownershipIsInitialized(uint256 index) internal view virtual returns (bool) {
            return ERC721AStorage.layout()._packedOwnerships[index] != 0;
        }
        /**
         * @dev Initializes the ownership slot minted at `index` for efficiency purposes.
         */
        function _initializeOwnershipAt(uint256 index) internal virtual {
            if (ERC721AStorage.layout()._packedOwnerships[index] == 0) {
                ERC721AStorage.layout()._packedOwnerships[index] = _packedOwnershipOf(index);
            }
        }
        /**
         * Returns the packed ownership data of `tokenId`.
         */
        function _packedOwnershipOf(uint256 tokenId) private view returns (uint256 packed) {
            if (_startTokenId() <= tokenId) {
                packed = ERC721AStorage.layout()._packedOwnerships[tokenId];
                // If the data at the starting slot does not exist, start the scan.
                if (packed == 0) {
                    if (tokenId >= ERC721AStorage.layout()._currentIndex) _revert(OwnerQueryForNonexistentToken.selector);
                    // Invariant:
                    // There will always be an initialized ownership slot
                    // (i.e. `ownership.addr != address(0) && ownership.burned == false`)
                    // before an unintialized ownership slot
                    // (i.e. `ownership.addr == address(0) && ownership.burned == false`)
                    // Hence, `tokenId` will not underflow.
                    //
                    // We can directly compare the packed value.
                    // If the address is zero, packed will be zero.
                    for (;;) {
                        unchecked {
                            packed = ERC721AStorage.layout()._packedOwnerships[--tokenId];
                        }
                        if (packed == 0) continue;
                        if (packed & _BITMASK_BURNED == 0) return packed;
                        // Otherwise, the token is burned, and we must revert.
                        // This handles the case of batch burned tokens, where only the burned bit
                        // of the starting slot is set, and remaining slots are left uninitialized.
                        _revert(OwnerQueryForNonexistentToken.selector);
                    }
                }
                // Otherwise, the data exists and we can skip the scan.
                // This is possible because we have already achieved the target condition.
                // This saves 2143 gas on transfers of initialized tokens.
                // If the token is not burned, return `packed`. Otherwise, revert.
                if (packed & _BITMASK_BURNED == 0) return packed;
            }
            _revert(OwnerQueryForNonexistentToken.selector);
        }
        /**
         * @dev Returns the unpacked `TokenOwnership` struct from `packed`.
         */
        function _unpackedOwnership(uint256 packed) private pure returns (TokenOwnership memory ownership) {
            ownership.addr = address(uint160(packed));
            ownership.startTimestamp = uint64(packed >> _BITPOS_START_TIMESTAMP);
            ownership.burned = packed & _BITMASK_BURNED != 0;
            ownership.extraData = uint24(packed >> _BITPOS_EXTRA_DATA);
        }
        /**
         * @dev Packs ownership data into a single uint256.
         */
        function _packOwnershipData(address owner, uint256 flags) private view returns (uint256 result) {
            assembly {
                // Mask `owner` to the lower 160 bits, in case the upper bits somehow aren't clean.
                owner := and(owner, _BITMASK_ADDRESS)
                // `owner | (block.timestamp << _BITPOS_START_TIMESTAMP) | flags`.
                result := or(owner, or(shl(_BITPOS_START_TIMESTAMP, timestamp()), flags))
            }
        }
        /**
         * @dev Returns the `nextInitialized` flag set if `quantity` equals 1.
         */
        function _nextInitializedFlag(uint256 quantity) private pure returns (uint256 result) {
            // For branchless setting of the `nextInitialized` flag.
            assembly {
                // `(quantity == 1) << _BITPOS_NEXT_INITIALIZED`.
                result := shl(_BITPOS_NEXT_INITIALIZED, eq(quantity, 1))
            }
        }
        // =============================================================
        //                      APPROVAL OPERATIONS
        // =============================================================
        /**
         * @dev Gives permission to `to` to transfer `tokenId` token to another account. See {ERC721A-_approve}.
         *
         * Requirements:
         *
         * - The caller must own the token or be an approved operator.
         */
        function approve(address to, uint256 tokenId) public payable virtual override {
            _approve(to, tokenId, true);
        }
        /**
         * @dev Returns the account approved for `tokenId` token.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         */
        function getApproved(uint256 tokenId) public view virtual override returns (address) {
            if (!_exists(tokenId)) _revert(ApprovalQueryForNonexistentToken.selector);
            return ERC721AStorage.layout()._tokenApprovals[tokenId].value;
        }
        /**
         * @dev Approve or remove `operator` as an operator for the caller.
         * Operators can call {transferFrom} or {safeTransferFrom}
         * for any token owned by the caller.
         *
         * Requirements:
         *
         * - The `operator` cannot be the caller.
         *
         * Emits an {ApprovalForAll} event.
         */
        function setApprovalForAll(address operator, bool approved) public virtual override {
            ERC721AStorage.layout()._operatorApprovals[_msgSenderERC721A()][operator] = approved;
            emit ApprovalForAll(_msgSenderERC721A(), operator, approved);
        }
        /**
         * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
         *
         * See {setApprovalForAll}.
         */
        function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
            return ERC721AStorage.layout()._operatorApprovals[owner][operator];
        }
        /**
         * @dev Returns whether `tokenId` exists.
         *
         * Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}.
         *
         * Tokens start existing when they are minted. See {_mint}.
         */
        function _exists(uint256 tokenId) internal view virtual returns (bool result) {
            if (_startTokenId() <= tokenId) {
                if (tokenId < ERC721AStorage.layout()._currentIndex) {
                    uint256 packed;
                    while ((packed = ERC721AStorage.layout()._packedOwnerships[tokenId]) == 0) --tokenId;
                    result = packed & _BITMASK_BURNED == 0;
                }
            }
        }
        /**
         * @dev Returns whether `msgSender` is equal to `approvedAddress` or `owner`.
         */
        function _isSenderApprovedOrOwner(
            address approvedAddress,
            address owner,
            address msgSender
        ) private pure returns (bool result) {
            assembly {
                // Mask `owner` to the lower 160 bits, in case the upper bits somehow aren't clean.
                owner := and(owner, _BITMASK_ADDRESS)
                // Mask `msgSender` to the lower 160 bits, in case the upper bits somehow aren't clean.
                msgSender := and(msgSender, _BITMASK_ADDRESS)
                // `msgSender == owner || msgSender == approvedAddress`.
                result := or(eq(msgSender, owner), eq(msgSender, approvedAddress))
            }
        }
        /**
         * @dev Returns the storage slot and value for the approved address of `tokenId`.
         */
        function _getApprovedSlotAndAddress(
            uint256 tokenId
        ) private view returns (uint256 approvedAddressSlot, address approvedAddress) {
            ERC721AStorage.TokenApprovalRef storage tokenApproval = ERC721AStorage.layout()._tokenApprovals[tokenId];
            // The following is equivalent to `approvedAddress = _tokenApprovals[tokenId].value`.
            assembly {
                approvedAddressSlot := tokenApproval.slot
                approvedAddress := sload(approvedAddressSlot)
            }
        }
        // =============================================================
        //                      TRANSFER OPERATIONS
        // =============================================================
        /**
         * @dev Transfers `tokenId` from `from` to `to`.
         *
         * Requirements:
         *
         * - `from` cannot be the zero address.
         * - `to` cannot be the zero address.
         * - `tokenId` token must be owned by `from`.
         * - If the caller is not `from`, it must be approved to move this token
         * by either {approve} or {setApprovalForAll}.
         *
         * Emits a {Transfer} event.
         */
        function transferFrom(address from, address to, uint256 tokenId) public payable virtual override {
            uint256 prevOwnershipPacked = _packedOwnershipOf(tokenId);
            // Mask `from` to the lower 160 bits, in case the upper bits somehow aren't clean.
            from = address(uint160(uint256(uint160(from)) & _BITMASK_ADDRESS));
            if (address(uint160(prevOwnershipPacked)) != from) _revert(TransferFromIncorrectOwner.selector);
            (uint256 approvedAddressSlot, address approvedAddress) = _getApprovedSlotAndAddress(tokenId);
            // The nested ifs save around 20+ gas over a compound boolean condition.
            if (!_isSenderApprovedOrOwner(approvedAddress, from, _msgSenderERC721A()))
                if (!isApprovedForAll(from, _msgSenderERC721A())) _revert(TransferCallerNotOwnerNorApproved.selector);
            _beforeTokenTransfers(from, to, tokenId, 1);
            // Clear approvals from the previous owner.
            assembly {
                if approvedAddress {
                    // This is equivalent to `delete _tokenApprovals[tokenId]`.
                    sstore(approvedAddressSlot, 0)
                }
            }
            // Underflow of the sender's balance is impossible because we check for
            // ownership above and the recipient's balance can't realistically overflow.
            // Counter overflow is incredibly unrealistic as `tokenId` would have to be 2**256.
            unchecked {
                // We can directly increment and decrement the balances.
                --ERC721AStorage.layout()._packedAddressData[from]; // Updates: `balance -= 1`.
                ++ERC721AStorage.layout()._packedAddressData[to]; // Updates: `balance += 1`.
                // Updates:
                // - `address` to the next owner.
                // - `startTimestamp` to the timestamp of transfering.
                // - `burned` to `false`.
                // - `nextInitialized` to `true`.
                ERC721AStorage.layout()._packedOwnerships[tokenId] = _packOwnershipData(
                    to,
                    _BITMASK_NEXT_INITIALIZED | _nextExtraData(from, to, prevOwnershipPacked)
                );
                // If the next slot may not have been initialized (i.e. `nextInitialized == false`) .
                if (prevOwnershipPacked & _BITMASK_NEXT_INITIALIZED == 0) {
                    uint256 nextTokenId = tokenId + 1;
                    // If the next slot's address is zero and not burned (i.e. packed value is zero).
                    if (ERC721AStorage.layout()._packedOwnerships[nextTokenId] == 0) {
                        // If the next slot is within bounds.
                        if (nextTokenId != ERC721AStorage.layout()._currentIndex) {
                            // Initialize the next slot to maintain correctness for `ownerOf(tokenId + 1)`.
                            ERC721AStorage.layout()._packedOwnerships[nextTokenId] = prevOwnershipPacked;
                        }
                    }
                }
            }
            // Mask `to` to the lower 160 bits, in case the upper bits somehow aren't clean.
            uint256 toMasked = uint256(uint160(to)) & _BITMASK_ADDRESS;
            assembly {
                // Emit the `Transfer` event.
                log4(
                    0, // Start of data (0, since no data).
                    0, // End of data (0, since no data).
                    _TRANSFER_EVENT_SIGNATURE, // Signature.
                    from, // `from`.
                    toMasked, // `to`.
                    tokenId // `tokenId`.
                )
            }
            if (toMasked == 0) _revert(TransferToZeroAddress.selector);
            _afterTokenTransfers(from, to, tokenId, 1);
        }
        /**
         * @dev Equivalent to `safeTransferFrom(from, to, tokenId, '')`.
         */
        function safeTransferFrom(address from, address to, uint256 tokenId) public payable virtual override {
            safeTransferFrom(from, to, tokenId, "");
        }
        /**
         * @dev Safely transfers `tokenId` token from `from` to `to`.
         *
         * Requirements:
         *
         * - `from` cannot be the zero address.
         * - `to` cannot be the zero address.
         * - `tokenId` token must exist and be owned by `from`.
         * - If the caller is not `from`, it must be approved to move this token
         * by either {approve} or {setApprovalForAll}.
         * - If `to` refers to a smart contract, it must implement
         * {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
         *
         * Emits a {Transfer} event.
         */
        function safeTransferFrom(
            address from,
            address to,
            uint256 tokenId,
            bytes memory _data
        ) public payable virtual override {
            transferFrom(from, to, tokenId);
            if (to.code.length != 0)
                if (!_checkContractOnERC721Received(from, to, tokenId, _data)) {
                    _revert(TransferToNonERC721ReceiverImplementer.selector);
                }
        }
        /**
         * @dev Hook that is called before a set of serially-ordered token IDs
         * are about to be transferred. This includes minting.
         * And also called before burning one token.
         *
         * `startTokenId` - the first token ID to be transferred.
         * `quantity` - the amount to be transferred.
         *
         * Calling conditions:
         *
         * - When `from` and `to` are both non-zero, `from`'s `tokenId` will be
         * transferred to `to`.
         * - When `from` is zero, `tokenId` will be minted for `to`.
         * - When `to` is zero, `tokenId` will be burned by `from`.
         * - `from` and `to` are never both zero.
         */
        function _beforeTokenTransfers(address from, address to, uint256 startTokenId, uint256 quantity) internal virtual {}
        /**
         * @dev Hook that is called after a set of serially-ordered token IDs
         * have been transferred. This includes minting.
         * And also called after one token has been burned.
         *
         * `startTokenId` - the first token ID to be transferred.
         * `quantity` - the amount to be transferred.
         *
         * Calling conditions:
         *
         * - When `from` and `to` are both non-zero, `from`'s `tokenId` has been
         * transferred to `to`.
         * - When `from` is zero, `tokenId` has been minted for `to`.
         * - When `to` is zero, `tokenId` has been burned by `from`.
         * - `from` and `to` are never both zero.
         */
        function _afterTokenTransfers(address from, address to, uint256 startTokenId, uint256 quantity) internal virtual {}
        /**
         * @dev Private function to invoke {IERC721Receiver-onERC721Received} on a target contract.
         *
         * `from` - Previous owner of the given token ID.
         * `to` - Target address that will receive the token.
         * `tokenId` - Token ID to be transferred.
         * `_data` - Optional data to send along with the call.
         *
         * Returns whether the call correctly returned the expected magic value.
         */
        function _checkContractOnERC721Received(
            address from,
            address to,
            uint256 tokenId,
            bytes memory _data
        ) private returns (bool) {
            try
                ERC721A__IERC721ReceiverUpgradeable(to).onERC721Received(_msgSenderERC721A(), from, tokenId, _data)
            returns (bytes4 retval) {
                return retval == ERC721A__IERC721ReceiverUpgradeable(to).onERC721Received.selector;
            } catch (bytes memory reason) {
                if (reason.length == 0) {
                    _revert(TransferToNonERC721ReceiverImplementer.selector);
                }
                assembly {
                    revert(add(32, reason), mload(reason))
                }
            }
        }
        // =============================================================
        //                        MINT OPERATIONS
        // =============================================================
        /**
         * @dev Mints `quantity` tokens and transfers them to `to`.
         *
         * Requirements:
         *
         * - `to` cannot be the zero address.
         * - `quantity` must be greater than 0.
         *
         * Emits a {Transfer} event for each mint.
         */
        function _mint(address to, uint256 quantity) internal virtual {
            uint256 startTokenId = ERC721AStorage.layout()._currentIndex;
            if (quantity == 0) _revert(MintZeroQuantity.selector);
            _beforeTokenTransfers(address(0), to, startTokenId, quantity);
            // Overflows are incredibly unrealistic.
            // `balance` and `numberMinted` have a maximum limit of 2**64.
            // `tokenId` has a maximum limit of 2**256.
            unchecked {
                // Updates:
                // - `address` to the owner.
                // - `startTimestamp` to the timestamp of minting.
                // - `burned` to `false`.
                // - `nextInitialized` to `quantity == 1`.
                ERC721AStorage.layout()._packedOwnerships[startTokenId] = _packOwnershipData(
                    to,
                    _nextInitializedFlag(quantity) | _nextExtraData(address(0), to, 0)
                );
                // Updates:
                // - `balance += quantity`.
                // - `numberMinted += quantity`.
                //
                // We can directly add to the `balance` and `numberMinted`.
                ERC721AStorage.layout()._packedAddressData[to] += quantity * ((1 << _BITPOS_NUMBER_MINTED) | 1);
                // Mask `to` to the lower 160 bits, in case the upper bits somehow aren't clean.
                uint256 toMasked = uint256(uint160(to)) & _BITMASK_ADDRESS;
                if (toMasked == 0) _revert(MintToZeroAddress.selector);
                uint256 end = startTokenId + quantity;
                uint256 tokenId = startTokenId;
                do {
                    assembly {
                        // Emit the `Transfer` event.
                        log4(
                            0, // Start of data (0, since no data).
                            0, // End of data (0, since no data).
                            _TRANSFER_EVENT_SIGNATURE, // Signature.
                            0, // `address(0)`.
                            toMasked, // `to`.
                            tokenId // `tokenId`.
                        )
                    }
                    // The `!=` check ensures that large values of `quantity`
                    // that overflows uint256 will make the loop run out of gas.
                } while (++tokenId != end);
                ERC721AStorage.layout()._currentIndex = end;
            }
            _afterTokenTransfers(address(0), to, startTokenId, quantity);
        }
        /**
         * @dev Mints `quantity` tokens and transfers them to `to`.
         *
         * This function is intended for efficient minting only during contract creation.
         *
         * It emits only one {ConsecutiveTransfer} as defined in
         * [ERC2309](https://eips.ethereum.org/EIPS/eip-2309),
         * instead of a sequence of {Transfer} event(s).
         *
         * Calling this function outside of contract creation WILL make your contract
         * non-compliant with the ERC721 standard.
         * For full ERC721 compliance, substituting ERC721 {Transfer} event(s) with the ERC2309
         * {ConsecutiveTransfer} event is only permissible during contract creation.
         *
         * Requirements:
         *
         * - `to` cannot be the zero address.
         * - `quantity` must be greater than 0.
         *
         * Emits a {ConsecutiveTransfer} event.
         */
        function _mintERC2309(address to, uint256 quantity) internal virtual {
            uint256 startTokenId = ERC721AStorage.layout()._currentIndex;
            if (to == address(0)) _revert(MintToZeroAddress.selector);
            if (quantity == 0) _revert(MintZeroQuantity.selector);
            if (quantity > _MAX_MINT_ERC2309_QUANTITY_LIMIT) _revert(MintERC2309QuantityExceedsLimit.selector);
            _beforeTokenTransfers(address(0), to, startTokenId, quantity);
            // Overflows are unrealistic due to the above check for `quantity` to be below the limit.
            unchecked {
                // Updates:
                // - `balance += quantity`.
                // - `numberMinted += quantity`.
                //
                // We can directly add to the `balance` and `numberMinted`.
                ERC721AStorage.layout()._packedAddressData[to] += quantity * ((1 << _BITPOS_NUMBER_MINTED) | 1);
                // Updates:
                // - `address` to the owner.
                // - `startTimestamp` to the timestamp of minting.
                // - `burned` to `false`.
                // - `nextInitialized` to `quantity == 1`.
                ERC721AStorage.layout()._packedOwnerships[startTokenId] = _packOwnershipData(
                    to,
                    _nextInitializedFlag(quantity) | _nextExtraData(address(0), to, 0)
                );
                emit ConsecutiveTransfer(startTokenId, startTokenId + quantity - 1, address(0), to);
                ERC721AStorage.layout()._currentIndex = startTokenId + quantity;
            }
            _afterTokenTransfers(address(0), to, startTokenId, quantity);
        }
        /**
         * @dev Safely mints `quantity` tokens and transfers them to `to`.
         *
         * Requirements:
         *
         * - If `to` refers to a smart contract, it must implement
         * {IERC721Receiver-onERC721Received}, which is called for each safe transfer.
         * - `quantity` must be greater than 0.
         *
         * See {_mint}.
         *
         * Emits a {Transfer} event for each mint.
         */
        function _safeMint(address to, uint256 quantity, bytes memory _data) internal virtual {
            _mint(to, quantity);
            unchecked {
                if (to.code.length != 0) {
                    uint256 end = ERC721AStorage.layout()._currentIndex;
                    uint256 index = end - quantity;
                    do {
                        if (!_checkContractOnERC721Received(address(0), to, index++, _data)) {
                            _revert(TransferToNonERC721ReceiverImplementer.selector);
                        }
                    } while (index < end);
                    // Reentrancy protection.
                    if (ERC721AStorage.layout()._currentIndex != end) _revert(bytes4(0));
                }
            }
        }
        /**
         * @dev Equivalent to `_safeMint(to, quantity, '')`.
         */
        function _safeMint(address to, uint256 quantity) internal virtual {
            _safeMint(to, quantity, "");
        }
        // =============================================================
        //                       APPROVAL OPERATIONS
        // =============================================================
        /**
         * @dev Equivalent to `_approve(to, tokenId, false)`.
         */
        function _approve(address to, uint256 tokenId) internal virtual {
            _approve(to, tokenId, false);
        }
        /**
         * @dev Gives permission to `to` to transfer `tokenId` token to another account.
         * The approval is cleared when the token is transferred.
         *
         * Only a single account can be approved at a time, so approving the
         * zero address clears previous approvals.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         *
         * Emits an {Approval} event.
         */
        function _approve(address to, uint256 tokenId, bool approvalCheck) internal virtual {
            address owner = ownerOf(tokenId);
            if (approvalCheck && _msgSenderERC721A() != owner)
                if (!isApprovedForAll(owner, _msgSenderERC721A())) {
                    _revert(ApprovalCallerNotOwnerNorApproved.selector);
                }
            ERC721AStorage.layout()._tokenApprovals[tokenId].value = to;
            emit Approval(owner, to, tokenId);
        }
        // =============================================================
        //                        BURN OPERATIONS
        // =============================================================
        /**
         * @dev Equivalent to `_burn(tokenId, false)`.
         */
        function _burn(uint256 tokenId) internal virtual {
            _burn(tokenId, false);
        }
        /**
         * @dev Destroys `tokenId`.
         * The approval is cleared when the token is burned.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         *
         * Emits a {Transfer} event.
         */
        function _burn(uint256 tokenId, bool approvalCheck) internal virtual {
            uint256 prevOwnershipPacked = _packedOwnershipOf(tokenId);
            address from = address(uint160(prevOwnershipPacked));
            (uint256 approvedAddressSlot, address approvedAddress) = _getApprovedSlotAndAddress(tokenId);
            if (approvalCheck) {
                // The nested ifs save around 20+ gas over a compound boolean condition.
                if (!_isSenderApprovedOrOwner(approvedAddress, from, _msgSenderERC721A()))
                    if (!isApprovedForAll(from, _msgSenderERC721A())) _revert(TransferCallerNotOwnerNorApproved.selector);
            }
            _beforeTokenTransfers(from, address(0), tokenId, 1);
            // Clear approvals from the previous owner.
            assembly {
                if approvedAddress {
                    // This is equivalent to `delete _tokenApprovals[tokenId]`.
                    sstore(approvedAddressSlot, 0)
                }
            }
            // Underflow of the sender's balance is impossible because we check for
            // ownership above and the recipient's balance can't realistically overflow.
            // Counter overflow is incredibly unrealistic as `tokenId` would have to be 2**256.
            unchecked {
                // Updates:
                // - `balance -= 1`.
                // - `numberBurned += 1`.
                //
                // We can directly decrement the balance, and increment the number burned.
                // This is equivalent to `packed -= 1; packed += 1 << _BITPOS_NUMBER_BURNED;`.
                ERC721AStorage.layout()._packedAddressData[from] += (1 << _BITPOS_NUMBER_BURNED) - 1;
                // Updates:
                // - `address` to the last owner.
                // - `startTimestamp` to the timestamp of burning.
                // - `burned` to `true`.
                // - `nextInitialized` to `true`.
                ERC721AStorage.layout()._packedOwnerships[tokenId] = _packOwnershipData(
                    from,
                    (_BITMASK_BURNED | _BITMASK_NEXT_INITIALIZED) | _nextExtraData(from, address(0), prevOwnershipPacked)
                );
                // If the next slot may not have been initialized (i.e. `nextInitialized == false`) .
                if (prevOwnershipPacked & _BITMASK_NEXT_INITIALIZED == 0) {
                    uint256 nextTokenId = tokenId + 1;
                    // If the next slot's address is zero and not burned (i.e. packed value is zero).
                    if (ERC721AStorage.layout()._packedOwnerships[nextTokenId] == 0) {
                        // If the next slot is within bounds.
                        if (nextTokenId != ERC721AStorage.layout()._currentIndex) {
                            // Initialize the next slot to maintain correctness for `ownerOf(tokenId + 1)`.
                            ERC721AStorage.layout()._packedOwnerships[nextTokenId] = prevOwnershipPacked;
                        }
                    }
                }
            }
            emit Transfer(from, address(0), tokenId);
            _afterTokenTransfers(from, address(0), tokenId, 1);
            // Overflow not possible, as _burnCounter cannot be exceed _currentIndex times.
            unchecked {
                ERC721AStorage.layout()._burnCounter++;
            }
        }
        // =============================================================
        //                     EXTRA DATA OPERATIONS
        // =============================================================
        /**
         * @dev Directly sets the extra data for the ownership data `index`.
         */
        function _setExtraDataAt(uint256 index, uint24 extraData) internal virtual {
            uint256 packed = ERC721AStorage.layout()._packedOwnerships[index];
            if (packed == 0) _revert(OwnershipNotInitializedForExtraData.selector);
            uint256 extraDataCasted;
            // Cast `extraData` with assembly to avoid redundant masking.
            assembly {
                extraDataCasted := extraData
            }
            packed = (packed & _BITMASK_EXTRA_DATA_COMPLEMENT) | (extraDataCasted << _BITPOS_EXTRA_DATA);
            ERC721AStorage.layout()._packedOwnerships[index] = packed;
        }
        /**
         * @dev Called during each token transfer to set the 24bit `extraData` field.
         * Intended to be overridden by the cosumer contract.
         *
         * `previousExtraData` - the value of `extraData` before transfer.
         *
         * Calling conditions:
         *
         * - When `from` and `to` are both non-zero, `from`'s `tokenId` will be
         * transferred to `to`.
         * - When `from` is zero, `tokenId` will be minted for `to`.
         * - When `to` is zero, `tokenId` will be burned by `from`.
         * - `from` and `to` are never both zero.
         */
        function _extraData(address from, address to, uint24 previousExtraData) internal view virtual returns (uint24) {}
        /**
         * @dev Returns the next extra data for the packed ownership data.
         * The returned result is shifted into position.
         */
        function _nextExtraData(address from, address to, uint256 prevOwnershipPacked) private view returns (uint256) {
            uint24 extraData = uint24(prevOwnershipPacked >> _BITPOS_EXTRA_DATA);
            return uint256(_extraData(from, to, extraData)) << _BITPOS_EXTRA_DATA;
        }
        // =============================================================
        //                       OTHER OPERATIONS
        // =============================================================
        /**
         * @dev Returns the message sender (defaults to `msg.sender`).
         *
         * If you are writing GSN compatible contracts, you need to override this function.
         */
        function _msgSenderERC721A() internal view virtual returns (address) {
            return msg.sender;
        }
        /**
         * @dev Converts a uint256 to its ASCII string decimal representation.
         */
        function _toString(uint256 value) internal pure virtual returns (string memory str) {
            assembly {
                // The maximum value of a uint256 contains 78 digits (1 byte per digit), but
                // we allocate 0xa0 bytes to keep the free memory pointer 32-byte word aligned.
                // We will need 1 word for the trailing zeros padding, 1 word for the length,
                // and 3 words for a maximum of 78 digits. Total: 5 * 0x20 = 0xa0.
                let m := add(mload(0x40), 0xa0)
                // Update the free memory pointer to allocate.
                mstore(0x40, m)
                // Assign the `str` to the end.
                str := sub(m, 0x20)
                // Zeroize the slot after the string.
                mstore(str, 0)
                // Cache the end of the memory to calculate the length later.
                let end := str
                // We write the string from rightmost digit to leftmost digit.
                // The following is essentially a do-while loop that also handles the zero case.
                // prettier-ignore
                for { let temp := value } 1 {} {
                    str := sub(str, 1)
                    // Write the character to the pointer.
                    // The ASCII index of the '0' character is 48.
                    mstore8(str, add(48, mod(temp, 10)))
                    // Keep dividing `temp` until zero.
                    temp := div(temp, 10)
                    // prettier-ignore
                    if iszero(temp) { break }
                }
                let length := sub(end, str)
                // Move the pointer 32 bytes leftwards to make room for the length.
                str := sub(str, 0x20)
                // Store the length.
                mstore(str, length)
            }
        }
        /**
         * @dev For more efficient reverts.
         */
        function _revert(bytes4 errorSelector) internal pure {
            assembly {
                mstore(0x00, errorSelector)
                revert(0x00, 0x04)
            }
        }
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    /**
     * @dev This is a base contract to aid in writing upgradeable diamond facet contracts, or any kind of contract that will be deployed
     * behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
     * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
     * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
     *
     * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
     * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
     *
     * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
     * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
     */
    import { ERC721A__InitializableStorage } from "./ERC721A__InitializableStorage.sol";
    abstract contract ERC721A__Initializable {
        using ERC721A__InitializableStorage for ERC721A__InitializableStorage.Layout;
        /**
         * @dev Modifier to protect an initializer function from being invoked twice.
         */
        modifier initializerERC721A() {
            // If the contract is initializing we ignore whether _initialized is set in order to support multiple
            // inheritance patterns, but we only do this in the context of a constructor, because in other contexts the
            // contract may have been reentered.
            require(
                ERC721A__InitializableStorage.layout()._initializing
                    ? _isConstructor()
                    : !ERC721A__InitializableStorage.layout()._initialized,
                "ERC721A__Initializable: contract is already initialized"
            );
            bool isTopLevelCall = !ERC721A__InitializableStorage.layout()._initializing;
            if (isTopLevelCall) {
                ERC721A__InitializableStorage.layout()._initializing = true;
                ERC721A__InitializableStorage.layout()._initialized = true;
            }
            _;
            if (isTopLevelCall) {
                ERC721A__InitializableStorage.layout()._initializing = false;
            }
        }
        /**
         * @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
         * {initializer} modifier, directly or indirectly.
         */
        modifier onlyInitializingERC721A() {
            require(
                ERC721A__InitializableStorage.layout()._initializing,
                "ERC721A__Initializable: contract is not initializing"
            );
            _;
        }
        /// @dev Returns true if and only if the function is running in the constructor
        function _isConstructor() private view returns (bool) {
            // extcodesize checks the size of the code stored in an address, and
            // address returns the current address. Since the code is still not
            // deployed when running a constructor, any checks on its code size will
            // yield zero, making it an effective way to detect if a contract is
            // under construction or not.
            address self = address(this);
            uint256 cs;
            assembly {
                cs := extcodesize(self)
            }
            return cs == 0;
        }
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    /**
     * @dev This is a base storage for the  initialization function for upgradeable diamond facet contracts
     **/
    library ERC721A__InitializableStorage {
        struct Layout {
            /*
             * Indicates that the contract has been initialized.
             */
            bool _initialized;
            /*
             * Indicates that the contract is in the process of being initialized.
             */
            bool _initializing;
        }
        bytes32 internal constant STORAGE_SLOT = keccak256("ERC721A.contracts.storage.initializable.facet");
        function layout() internal pure returns (Layout storage l) {
            bytes32 slot = STORAGE_SLOT;
            assembly {
                l.slot := slot
            }
        }
    }
    // SPDX-License-Identifier: MIT
    // ERC721A Contracts v4.2.3
    // Creator: Chiru Labs
    pragma solidity ^0.8.4;
    import "./IERC721AUpgradeable.sol";
    /**
     * @dev Interface of ERC721AQueryable.
     */
    interface IERC721AQueryableUpgradeable is IERC721AUpgradeable {
        /**
         * Invalid query range (`start` >= `stop`).
         */
        error InvalidQueryRange();
        /**
         * @dev Returns the `TokenOwnership` struct at `tokenId` without reverting.
         *
         * If the `tokenId` is out of bounds:
         *
         * - `addr = address(0)`
         * - `startTimestamp = 0`
         * - `burned = false`
         * - `extraData = 0`
         *
         * If the `tokenId` is burned:
         *
         * - `addr = <Address of owner before token was burned>`
         * - `startTimestamp = <Timestamp when token was burned>`
         * - `burned = true`
         * - `extraData = <Extra data when token was burned>`
         *
         * Otherwise:
         *
         * - `addr = <Address of owner>`
         * - `startTimestamp = <Timestamp of start of ownership>`
         * - `burned = false`
         * - `extraData = <Extra data at start of ownership>`
         */
        function explicitOwnershipOf(uint256 tokenId) external view returns (TokenOwnership memory);
        /**
         * @dev Returns an array of token IDs owned by `owner`,
         * in the range [`start`, `stop`)
         * (i.e. `start <= tokenId < stop`).
         *
         * This function allows for tokens to be queried if the collection
         * grows too big for a single call of {ERC721AQueryable-tokensOfOwner}.
         *
         * Requirements:
         *
         * - `start < stop`
         */
        function tokensOfOwnerIn(address owner, uint256 start, uint256 stop) external view returns (uint256[] memory);
        /**
         * @dev Returns an array of token IDs owned by `owner`.
         *
         * This function scans the ownership mapping and is O(`totalSupply`) in complexity.
         * It is meant to be called off-chain.
         *
         * See {ERC721AQueryable-tokensOfOwnerIn} for splitting the scan into
         * multiple smaller scans if the collection is large enough to cause
         * an out-of-gas error (10K collections should be fine).
         */
        function tokensOfOwner(address owner) external view returns (uint256[] memory);
    }
    // SPDX-License-Identifier: MIT
    // ERC721A Contracts v4.2.3
    // Creator: Chiru Labs
    pragma solidity ^0.8.4;
    /**
     * @dev Interface of ERC721A.
     */
    interface IERC721AUpgradeable {
        /**
         * The caller must own the token or be an approved operator.
         */
        error ApprovalCallerNotOwnerNorApproved();
        /**
         * The token does not exist.
         */
        error ApprovalQueryForNonexistentToken();
        /**
         * Cannot query the balance for the zero address.
         */
        error BalanceQueryForZeroAddress();
        /**
         * Cannot mint to the zero address.
         */
        error MintToZeroAddress();
        /**
         * The quantity of tokens minted must be more than zero.
         */
        error MintZeroQuantity();
        /**
         * The token does not exist.
         */
        error OwnerQueryForNonexistentToken();
        /**
         * The caller must own the token or be an approved operator.
         */
        error TransferCallerNotOwnerNorApproved();
        /**
         * The token must be owned by `from`.
         */
        error TransferFromIncorrectOwner();
        /**
         * Cannot safely transfer to a contract that does not implement the
         * ERC721Receiver interface.
         */
        error TransferToNonERC721ReceiverImplementer();
        /**
         * Cannot transfer to the zero address.
         */
        error TransferToZeroAddress();
        /**
         * The token does not exist.
         */
        error URIQueryForNonexistentToken();
        /**
         * The `quantity` minted with ERC2309 exceeds the safety limit.
         */
        error MintERC2309QuantityExceedsLimit();
        /**
         * The `extraData` cannot be set on an unintialized ownership slot.
         */
        error OwnershipNotInitializedForExtraData();
        // =============================================================
        //                            STRUCTS
        // =============================================================
        struct TokenOwnership {
            // The address of the owner.
            address addr;
            // Stores the start time of ownership with minimal overhead for tokenomics.
            uint64 startTimestamp;
            // Whether the token has been burned.
            bool burned;
            // Arbitrary data similar to `startTimestamp` that can be set via {_extraData}.
            uint24 extraData;
        }
        // =============================================================
        //                         TOKEN COUNTERS
        // =============================================================
        /**
         * @dev Returns the total number of tokens in existence.
         * Burned tokens will reduce the count.
         * To get the total number of tokens minted, please see {_totalMinted}.
         */
        function totalSupply() external view returns (uint256);
        // =============================================================
        //                            IERC165
        // =============================================================
        /**
         * @dev Returns true if this contract implements the interface defined by
         * `interfaceId`. See the corresponding
         * [EIP section](https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified)
         * to learn more about how these ids are created.
         *
         * This function call must use less than 30000 gas.
         */
        function supportsInterface(bytes4 interfaceId) external view returns (bool);
        // =============================================================
        //                            IERC721
        // =============================================================
        /**
         * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
         */
        event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
        /**
         * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
         */
        event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
        /**
         * @dev Emitted when `owner` enables or disables
         * (`approved`) `operator` to manage all of its assets.
         */
        event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
        /**
         * @dev Returns the number of tokens in `owner`'s account.
         */
        function balanceOf(address owner) external view returns (uint256 balance);
        /**
         * @dev Returns the owner of the `tokenId` token.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         */
        function ownerOf(uint256 tokenId) external view returns (address owner);
        /**
         * @dev Safely transfers `tokenId` token from `from` to `to`,
         * checking first that contract recipients are aware of the ERC721 protocol
         * to prevent tokens from being forever locked.
         *
         * Requirements:
         *
         * - `from` cannot be the zero address.
         * - `to` cannot be the zero address.
         * - `tokenId` token must exist and be owned by `from`.
         * - If the caller is not `from`, it must be have been allowed to move
         * this token by either {approve} or {setApprovalForAll}.
         * - If `to` refers to a smart contract, it must implement
         * {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
         *
         * Emits a {Transfer} event.
         */
        function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external payable;
        /**
         * @dev Equivalent to `safeTransferFrom(from, to, tokenId, '')`.
         */
        function safeTransferFrom(address from, address to, uint256 tokenId) external payable;
        /**
         * @dev Transfers `tokenId` from `from` to `to`.
         *
         * WARNING: Usage of this method is discouraged, use {safeTransferFrom}
         * whenever possible.
         *
         * Requirements:
         *
         * - `from` cannot be the zero address.
         * - `to` cannot be the zero address.
         * - `tokenId` token must be owned by `from`.
         * - If the caller is not `from`, it must be approved to move this token
         * by either {approve} or {setApprovalForAll}.
         *
         * Emits a {Transfer} event.
         */
        function transferFrom(address from, address to, uint256 tokenId) external payable;
        /**
         * @dev Gives permission to `to` to transfer `tokenId` token to another account.
         * The approval is cleared when the token is transferred.
         *
         * Only a single account can be approved at a time, so approving the
         * zero address clears previous approvals.
         *
         * Requirements:
         *
         * - The caller must own the token or be an approved operator.
         * - `tokenId` must exist.
         *
         * Emits an {Approval} event.
         */
        function approve(address to, uint256 tokenId) external payable;
        /**
         * @dev Approve or remove `operator` as an operator for the caller.
         * Operators can call {transferFrom} or {safeTransferFrom}
         * for any token owned by the caller.
         *
         * Requirements:
         *
         * - The `operator` cannot be the caller.
         *
         * Emits an {ApprovalForAll} event.
         */
        function setApprovalForAll(address operator, bool _approved) external;
        /**
         * @dev Returns the account approved for `tokenId` token.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         */
        function getApproved(uint256 tokenId) external view returns (address operator);
        /**
         * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
         *
         * See {setApprovalForAll}.
         */
        function isApprovedForAll(address owner, address operator) external view returns (bool);
        // =============================================================
        //                        IERC721Metadata
        // =============================================================
        /**
         * @dev Returns the token collection name.
         */
        function name() external view returns (string memory);
        /**
         * @dev Returns the token collection symbol.
         */
        function symbol() external view returns (string memory);
        /**
         * @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
         */
        function tokenURI(uint256 tokenId) external view returns (string memory);
        // =============================================================
        //                           IERC2309
        // =============================================================
        /**
         * @dev Emitted when tokens in `fromTokenId` to `toTokenId`
         * (inclusive) is transferred from `from` to `to`, as defined in the
         * [ERC2309](https://eips.ethereum.org/EIPS/eip-2309) standard.
         *
         * See {_mintERC2309} for more details.
         */
        event ConsecutiveTransfer(uint256 indexed fromTokenId, uint256 toTokenId, address indexed from, address indexed to);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IContractMetadata.sol";
    /**
     *  @title   Contract Metadata
     *  @notice  Thirdweb's `ContractMetadata` is a contract extension for any base contracts. It lets you set a metadata URI
     *           for you contract.
     *           Additionally, `ContractMetadata` is necessary for NFT contracts that want royalties to get distributed on OpenSea.
     */
    abstract contract ContractMetadata is IContractMetadata {
        /// @notice Returns the contract metadata URI.
        string public override contractURI;
        /**
         *  @notice         Lets a contract admin set the URI for contract-level metadata.
         *  @dev            Caller should be authorized to setup contractURI, e.g. contract admin.
         *                  See {_canSetContractURI}.
         *                  Emits {ContractURIUpdated Event}.
         *
         *  @param _uri     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         */
        function setContractURI(string memory _uri) external override {
            if (!_canSetContractURI()) {
                revert("Not authorized");
            }
            _setupContractURI(_uri);
        }
        /// @dev Lets a contract admin set the URI for contract-level metadata.
        function _setupContractURI(string memory _uri) internal {
            string memory prevURI = contractURI;
            contractURI = _uri;
            emit ContractURIUpdated(prevURI, _uri);
        }
        /// @dev Returns whether contract metadata can be set in the given execution context.
        function _canSetContractURI() internal view virtual returns (bool);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IDrop.sol";
    import "../lib/MerkleProof.sol";
    abstract contract Drop is IDrop {
        /*///////////////////////////////////////////////////////////////
                                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,
            address _currency,
            uint256 _pricePerToken,
            AllowlistProof calldata _allowlistProof,
            bytes memory _data
        ) public payable virtual override {
            _beforeClaim(_receiver, _quantity, _currency, _pricePerToken, _allowlistProof, _data);
            uint256 activeConditionId = getActiveClaimConditionId();
            verifyClaim(activeConditionId, _dropMsgSender(), _quantity, _currency, _pricePerToken, _allowlistProof);
            // Update contract state.
            claimCondition.conditions[activeConditionId].supplyClaimed += _quantity;
            claimCondition.supplyClaimedByWallet[activeConditionId][_dropMsgSender()] += _quantity;
            // If there's a price, collect price.
            _collectPriceOnClaim(address(0), _quantity, _currency, _pricePerToken);
            // Mint the relevant tokens to claimer.
            uint256 startTokenId = _transferTokensOnClaim(_receiver, _quantity);
            emit TokensClaimed(activeConditionId, _dropMsgSender(), _receiver, startTokenId, _quantity);
            _afterClaim(_receiver, _quantity, _currency, _pricePerToken, _allowlistProof, _data);
        }
        /// @dev Lets a contract admin set claim conditions.
        function setClaimConditions(
            ClaimCondition[] calldata _conditions,
            bool _resetClaimEligibility
        ) external virtual override {
            if (!_canSetClaimConditions()) {
                revert("Not authorized");
            }
            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 lastConditionStartTimestamp;
            for (uint256 i = 0; i < _conditions.length; i++) {
                require(i == 0 || lastConditionStartTimestamp < _conditions[i].startTimestamp, "ST");
                uint256 supplyClaimedAlready = claimCondition.conditions[newStartIndex + i].supplyClaimed;
                if (supplyClaimedAlready > _conditions[i].maxClaimableSupply) {
                    revert("max supply claimed");
                }
                claimCondition.conditions[newStartIndex + i] = _conditions[i];
                claimCondition.conditions[newStartIndex + i].supplyClaimed = supplyClaimedAlready;
                lastConditionStartTimestamp = _conditions[i].startTimestamp;
            }
            /**
             *  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];
                }
            } else {
                if (existingPhaseCount > _conditions.length) {
                    for (uint256 i = _conditions.length; i < existingPhaseCount; i++) {
                        delete claimCondition.conditions[newStartIndex + i];
                    }
                }
            }
            emit ClaimConditionsUpdated(_conditions, _resetClaimEligibility);
        }
        /// @dev Checks a request to claim NFTs against the active claim condition's criteria.
        function verifyClaim(
            uint256 _conditionId,
            address _claimer,
            uint256 _quantity,
            address _currency,
            uint256 _pricePerToken,
            AllowlistProof calldata _allowlistProof
        ) public view virtual returns (bool isOverride) {
            ClaimCondition memory currentClaimPhase = claimCondition.conditions[_conditionId];
            uint256 claimLimit = currentClaimPhase.quantityLimitPerWallet;
            uint256 claimPrice = currentClaimPhase.pricePerToken;
            address claimCurrency = currentClaimPhase.currency;
            /*
             * Here `isOverride` implies that if the merkle proof verification fails,
             * the claimer would claim through open claim limit instead of allowlisted limit.
             */
            if (currentClaimPhase.merkleRoot != bytes32(0)) {
                (isOverride, ) = MerkleProof.verify(
                    _allowlistProof.proof,
                    currentClaimPhase.merkleRoot,
                    keccak256(
                        abi.encodePacked(
                            _claimer,
                            _allowlistProof.quantityLimitPerWallet,
                            _allowlistProof.pricePerToken,
                            _allowlistProof.currency
                        )
                    )
                );
            }
            if (isOverride) {
                claimLimit = _allowlistProof.quantityLimitPerWallet != 0
                    ? _allowlistProof.quantityLimitPerWallet
                    : claimLimit;
                claimPrice = _allowlistProof.pricePerToken != type(uint256).max
                    ? _allowlistProof.pricePerToken
                    : claimPrice;
                claimCurrency = _allowlistProof.pricePerToken != type(uint256).max && _allowlistProof.currency != address(0)
                    ? _allowlistProof.currency
                    : claimCurrency;
            }
            uint256 supplyClaimedByWallet = claimCondition.supplyClaimedByWallet[_conditionId][_claimer];
            if (_currency != claimCurrency || _pricePerToken != claimPrice) {
                revert("!PriceOrCurrency");
            }
            if (_quantity == 0 || (_quantity + supplyClaimedByWallet > claimLimit)) {
                revert("!Qty");
            }
            if (currentClaimPhase.supplyClaimed + _quantity > currentClaimPhase.maxClaimableSupply) {
                revert("!MaxSupply");
            }
            if (currentClaimPhase.startTimestamp > block.timestamp) {
                revert("cant claim yet");
            }
        }
        /// @dev At any given moment, returns the uid for the active claim condition.
        function getActiveClaimConditionId() public view returns (uint256) {
            for (uint256 i = claimCondition.currentStartId + claimCondition.count; i > claimCondition.currentStartId; i--) {
                if (block.timestamp >= claimCondition.conditions[i - 1].startTimestamp) {
                    return i - 1;
                }
            }
            revert("!CONDITION.");
        }
        /// @dev Returns the claim condition at the given uid.
        function getClaimConditionById(uint256 _conditionId) external view returns (ClaimCondition memory condition) {
            condition = claimCondition.conditions[_conditionId];
        }
        /// @dev Returns the supply claimed by claimer for a given conditionId.
        function getSupplyClaimedByWallet(
            uint256 _conditionId,
            address _claimer
        ) public view returns (uint256 supplyClaimedByWallet) {
            supplyClaimedByWallet = claimCondition.supplyClaimedByWallet[_conditionId][_claimer];
        }
        /*////////////////////////////////////////////////////////////////////
            Optional hooks that can be implemented in the derived contract
        ///////////////////////////////////////////////////////////////////*/
        /// @dev Exposes the ability to override the msg sender.
        function _dropMsgSender() internal virtual returns (address) {
            return msg.sender;
        }
        /// @dev Runs before every `claim` function call.
        function _beforeClaim(
            address _receiver,
            uint256 _quantity,
            address _currency,
            uint256 _pricePerToken,
            AllowlistProof calldata _allowlistProof,
            bytes memory _data
        ) internal virtual {}
        /// @dev Runs after every `claim` function call.
        function _afterClaim(
            address _receiver,
            uint256 _quantity,
            address _currency,
            uint256 _pricePerToken,
            AllowlistProof calldata _allowlistProof,
            bytes memory _data
        ) internal virtual {}
        /*///////////////////////////////////////////////////////////////
            Virtual functions: to be implemented in derived contract
        //////////////////////////////////////////////////////////////*/
        /// @dev Collects and distributes the primary sale value of NFTs being claimed.
        function _collectPriceOnClaim(
            address _primarySaleRecipient,
            uint256 _quantityToClaim,
            address _currency,
            uint256 _pricePerToken
        ) internal virtual;
        /// @dev Transfers the NFTs being claimed.
        function _transferTokensOnClaim(
            address _to,
            uint256 _quantityBeingClaimed
        ) internal virtual returns (uint256 startTokenId);
        /// @dev Determine what wallet can update claim conditions
        function _canSetClaimConditions() internal view virtual returns (bool);
    }
    // SPDX-License-Identifier: Apache 2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "../lib/Address.sol";
    import "./interface/IMulticall.sol";
    /**
     * @dev Provides a function to batch together multiple calls in a single external call.
     *
     * _Available since v4.1._
     */
    contract Multicall is IMulticall {
        /**
         *  @notice Receives and executes a batch of function calls on this contract.
         *  @dev Receives and executes a batch of function calls on this contract.
         *
         *  @param data The bytes data that makes up the batch of function calls to execute.
         *  @return results The bytes data that makes up the result of the batch of function calls executed.
         */
        function multicall(bytes[] calldata data) external returns (bytes[] memory results) {
            results = new bytes[](data.length);
            address sender = _msgSender();
            bool isForwarder = msg.sender != sender;
            for (uint256 i = 0; i < data.length; i++) {
                if (isForwarder) {
                    results[i] = Address.functionDelegateCall(address(this), abi.encodePacked(data[i], sender));
                } else {
                    results[i] = Address.functionDelegateCall(address(this), data[i]);
                }
            }
            return results;
        }
        /// @notice Returns the sender in the given execution context.
        function _msgSender() internal view virtual returns (address) {
            return msg.sender;
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IOwnable.sol";
    /**
     *  @title   Ownable
     *  @notice  Thirdweb's `Ownable` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *           who the 'owner' of the inheriting smart contract is, and lets the inheriting contract perform conditional logic that uses
     *           information about who the contract's owner is.
     */
    abstract contract Ownable is IOwnable {
        /// @dev Owner of the contract (purpose: OpenSea compatibility)
        address private _owner;
        /// @dev Reverts if caller is not the owner.
        modifier onlyOwner() {
            if (msg.sender != _owner) {
                revert("Not authorized");
            }
            _;
        }
        /**
         *  @notice Returns the owner of the contract.
         */
        function owner() public view override returns (address) {
            return _owner;
        }
        /**
         *  @notice Lets an authorized wallet set a new owner for the contract.
         *  @param _newOwner The address to set as the new owner of the contract.
         */
        function setOwner(address _newOwner) external override {
            if (!_canSetOwner()) {
                revert("Not authorized");
            }
            _setupOwner(_newOwner);
        }
        /// @dev Lets a contract admin set a new owner for the contract. The new owner must be a contract admin.
        function _setupOwner(address _newOwner) internal {
            address _prevOwner = _owner;
            _owner = _newOwner;
            emit OwnerUpdated(_prevOwner, _newOwner);
        }
        /// @dev Returns whether owner can be set in the given execution context.
        function _canSetOwner() internal view virtual returns (bool);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IPermissions.sol";
    import "../lib/Strings.sol";
    /**
     *  @title   Permissions
     *  @dev     This contracts provides extending-contracts with role-based access control mechanisms
     */
    contract Permissions is IPermissions {
        /// @dev Map from keccak256 hash of a role => a map from address => whether address has role.
        mapping(bytes32 => mapping(address => bool)) private _hasRole;
        /// @dev Map from keccak256 hash of a role to role admin. See {getRoleAdmin}.
        mapping(bytes32 => bytes32) private _getRoleAdmin;
        /// @dev Default admin role for all roles. Only accounts with this role can grant/revoke other roles.
        bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
        /// @dev Modifier that checks if an account has the specified role; reverts otherwise.
        modifier onlyRole(bytes32 role) {
            _checkRole(role, msg.sender);
            _;
        }
        /**
         *  @notice         Checks whether an account has a particular role.
         *  @dev            Returns `true` if `account` has been granted `role`.
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         *  @param account  Address of the account for which the role is being checked.
         */
        function hasRole(bytes32 role, address account) public view override returns (bool) {
            return _hasRole[role][account];
        }
        /**
         *  @notice         Checks whether an account has a particular role;
         *                  role restrictions can be swtiched on and off.
         *
         *  @dev            Returns `true` if `account` has been granted `role`.
         *                  Role restrictions can be swtiched on and off:
         *                      - If address(0) has ROLE, then the ROLE restrictions
         *                        don't apply.
         *                      - If address(0) does not have ROLE, then the ROLE
         *                        restrictions will apply.
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         *  @param account  Address of the account for which the role is being checked.
         */
        function hasRoleWithSwitch(bytes32 role, address account) public view returns (bool) {
            if (!_hasRole[role][address(0)]) {
                return _hasRole[role][account];
            }
            return true;
        }
        /**
         *  @notice         Returns the admin role that controls the specified role.
         *  @dev            See {grantRole} and {revokeRole}.
         *                  To change a role's admin, use {_setRoleAdmin}.
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         */
        function getRoleAdmin(bytes32 role) external view override returns (bytes32) {
            return _getRoleAdmin[role];
        }
        /**
         *  @notice         Grants a role to an account, if not previously granted.
         *  @dev            Caller must have admin role for the `role`.
         *                  Emits {RoleGranted Event}.
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         *  @param account  Address of the account to which the role is being granted.
         */
        function grantRole(bytes32 role, address account) public virtual override {
            _checkRole(_getRoleAdmin[role], msg.sender);
            if (_hasRole[role][account]) {
                revert("Can only grant to non holders");
            }
            _setupRole(role, account);
        }
        /**
         *  @notice         Revokes role from an account.
         *  @dev            Caller must have admin role for the `role`.
         *                  Emits {RoleRevoked Event}.
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         *  @param account  Address of the account from which the role is being revoked.
         */
        function revokeRole(bytes32 role, address account) public virtual override {
            _checkRole(_getRoleAdmin[role], msg.sender);
            _revokeRole(role, account);
        }
        /**
         *  @notice         Revokes role from the account.
         *  @dev            Caller must have the `role`, with caller being the same as `account`.
         *                  Emits {RoleRevoked Event}.
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         *  @param account  Address of the account from which the role is being revoked.
         */
        function renounceRole(bytes32 role, address account) public virtual override {
            if (msg.sender != account) {
                revert("Can only renounce for self");
            }
            _revokeRole(role, account);
        }
        /// @dev Sets `adminRole` as `role`'s admin role.
        function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
            bytes32 previousAdminRole = _getRoleAdmin[role];
            _getRoleAdmin[role] = adminRole;
            emit RoleAdminChanged(role, previousAdminRole, adminRole);
        }
        /// @dev Sets up `role` for `account`
        function _setupRole(bytes32 role, address account) internal virtual {
            _hasRole[role][account] = true;
            emit RoleGranted(role, account, msg.sender);
        }
        /// @dev Revokes `role` from `account`
        function _revokeRole(bytes32 role, address account) internal virtual {
            _checkRole(role, account);
            delete _hasRole[role][account];
            emit RoleRevoked(role, account, msg.sender);
        }
        /// @dev Checks `role` for `account`. Reverts with a message including the required role.
        function _checkRole(bytes32 role, address account) internal view virtual {
            if (!_hasRole[role][account]) {
                revert(
                    string(
                        abi.encodePacked(
                            "Permissions: account ",
                            Strings.toHexString(uint160(account), 20),
                            " is missing role ",
                            Strings.toHexString(uint256(role), 32)
                        )
                    )
                );
            }
        }
        /// @dev Checks `role` for `account`. Reverts with a message including the required role.
        function _checkRoleWithSwitch(bytes32 role, address account) internal view virtual {
            if (!hasRoleWithSwitch(role, account)) {
                revert(
                    string(
                        abi.encodePacked(
                            "Permissions: account ",
                            Strings.toHexString(uint160(account), 20),
                            " is missing role ",
                            Strings.toHexString(uint256(role), 32)
                        )
                    )
                );
            }
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IPermissionsEnumerable.sol";
    import "./Permissions.sol";
    /**
     *  @title   PermissionsEnumerable
     *  @dev     This contracts provides extending-contracts with role-based access control mechanisms.
     *           Also provides interfaces to view all members with a given role, and total count of members.
     */
    contract PermissionsEnumerable is IPermissionsEnumerable, Permissions {
        /**
         *  @notice A data structure to store data of members for a given role.
         *
         *  @param index    Current index in the list of accounts that have a role.
         *  @param members  map from index => address of account that has a role
         *  @param indexOf  map from address => index which the account has.
         */
        struct RoleMembers {
            uint256 index;
            mapping(uint256 => address) members;
            mapping(address => uint256) indexOf;
        }
        /// @dev map from keccak256 hash of a role to its members' data. See {RoleMembers}.
        mapping(bytes32 => RoleMembers) private roleMembers;
        /**
         *  @notice         Returns the role-member from a list of members for a role,
         *                  at a given index.
         *  @dev            Returns `member` who has `role`, at `index` of role-members list.
         *                  See struct {RoleMembers}, and mapping {roleMembers}
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         *  @param index    Index in list of current members for the role.
         *
         *  @return member  Address of account that has `role`
         */
        function getRoleMember(bytes32 role, uint256 index) external view override returns (address member) {
            uint256 currentIndex = roleMembers[role].index;
            uint256 check;
            for (uint256 i = 0; i < currentIndex; i += 1) {
                if (roleMembers[role].members[i] != address(0)) {
                    if (check == index) {
                        member = roleMembers[role].members[i];
                        return member;
                    }
                    check += 1;
                } else if (hasRole(role, address(0)) && i == roleMembers[role].indexOf[address(0)]) {
                    check += 1;
                }
            }
        }
        /**
         *  @notice         Returns total number of accounts that have a role.
         *  @dev            Returns `count` of accounts that have `role`.
         *                  See struct {RoleMembers}, and mapping {roleMembers}
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         *
         *  @return count   Total number of accounts that have `role`
         */
        function getRoleMemberCount(bytes32 role) external view override returns (uint256 count) {
            uint256 currentIndex = roleMembers[role].index;
            for (uint256 i = 0; i < currentIndex; i += 1) {
                if (roleMembers[role].members[i] != address(0)) {
                    count += 1;
                }
            }
            if (hasRole(role, address(0))) {
                count += 1;
            }
        }
        /// @dev Revokes `role` from `account`, and removes `account` from {roleMembers}
        ///      See {_removeMember}
        function _revokeRole(bytes32 role, address account) internal override {
            super._revokeRole(role, account);
            _removeMember(role, account);
        }
        /// @dev Grants `role` to `account`, and adds `account` to {roleMembers}
        ///      See {_addMember}
        function _setupRole(bytes32 role, address account) internal override {
            super._setupRole(role, account);
            _addMember(role, account);
        }
        /// @dev adds `account` to {roleMembers}, for `role`
        function _addMember(bytes32 role, address account) internal {
            uint256 idx = roleMembers[role].index;
            roleMembers[role].index += 1;
            roleMembers[role].members[idx] = account;
            roleMembers[role].indexOf[account] = idx;
        }
        /// @dev removes `account` from {roleMembers}, for `role`
        function _removeMember(bytes32 role, address account) internal {
            uint256 idx = roleMembers[role].indexOf[account];
            delete roleMembers[role].members[idx];
            delete roleMembers[role].indexOf[account];
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IPlatformFee.sol";
    /**
     *  @title   Platform Fee
     *  @notice  Thirdweb's `PlatformFee` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *           the recipient of platform fee and the platform fee basis points, and lets the inheriting contract perform conditional logic
     *           that uses information about platform fees, if desired.
     */
    abstract contract PlatformFee is IPlatformFee {
        /// @dev The address that receives all platform fees from all sales.
        address private platformFeeRecipient;
        /// @dev The % of primary sales collected as platform fees.
        uint16 private platformFeeBps;
        /// @dev Fee type variants: percentage fee and flat fee
        PlatformFeeType private platformFeeType;
        /// @dev The flat amount collected by the contract as fees on primary sales.
        uint256 private flatPlatformFee;
        /// @dev Returns the platform fee recipient and bps.
        function getPlatformFeeInfo() public view override returns (address, uint16) {
            return (platformFeeRecipient, uint16(platformFeeBps));
        }
        /// @dev Returns the platform fee bps and recipient.
        function getFlatPlatformFeeInfo() public view returns (address, uint256) {
            return (platformFeeRecipient, flatPlatformFee);
        }
        /// @dev Returns the platform fee type.
        function getPlatformFeeType() public view returns (PlatformFeeType) {
            return platformFeeType;
        }
        /**
         *  @notice         Updates the platform fee recipient and bps.
         *  @dev            Caller should be authorized to set platform fee info.
         *                  See {_canSetPlatformFeeInfo}.
         *                  Emits {PlatformFeeInfoUpdated Event}; See {_setupPlatformFeeInfo}.
         *
         *  @param _platformFeeRecipient   Address to be set as new platformFeeRecipient.
         *  @param _platformFeeBps         Updated platformFeeBps.
         */
        function setPlatformFeeInfo(address _platformFeeRecipient, uint256 _platformFeeBps) external override {
            if (!_canSetPlatformFeeInfo()) {
                revert("Not authorized");
            }
            _setupPlatformFeeInfo(_platformFeeRecipient, _platformFeeBps);
        }
        /// @dev Sets the platform fee recipient and bps
        function _setupPlatformFeeInfo(address _platformFeeRecipient, uint256 _platformFeeBps) internal {
            if (_platformFeeBps > 10_000) {
                revert("Exceeds max bps");
            }
            if (_platformFeeRecipient == address(0)) {
                revert("Invalid recipient");
            }
            platformFeeBps = uint16(_platformFeeBps);
            platformFeeRecipient = _platformFeeRecipient;
            emit PlatformFeeInfoUpdated(_platformFeeRecipient, _platformFeeBps);
        }
        /// @notice Lets a module admin set a flat fee on primary sales.
        function setFlatPlatformFeeInfo(address _platformFeeRecipient, uint256 _flatFee) external {
            if (!_canSetPlatformFeeInfo()) {
                revert("Not authorized");
            }
            _setupFlatPlatformFeeInfo(_platformFeeRecipient, _flatFee);
        }
        /// @dev Sets a flat fee on primary sales.
        function _setupFlatPlatformFeeInfo(address _platformFeeRecipient, uint256 _flatFee) internal {
            flatPlatformFee = _flatFee;
            platformFeeRecipient = _platformFeeRecipient;
            emit FlatPlatformFeeUpdated(_platformFeeRecipient, _flatFee);
        }
        /// @notice Lets a module admin set platform fee type.
        function setPlatformFeeType(PlatformFeeType _feeType) external {
            if (!_canSetPlatformFeeInfo()) {
                revert("Not authorized");
            }
            _setupPlatformFeeType(_feeType);
        }
        /// @dev Sets platform fee type.
        function _setupPlatformFeeType(PlatformFeeType _feeType) internal {
            platformFeeType = _feeType;
            emit PlatformFeeTypeUpdated(_feeType);
        }
        /// @dev Returns whether platform fee info can be set in the given execution context.
        function _canSetPlatformFeeInfo() internal view virtual returns (bool);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IPrimarySale.sol";
    /**
     *  @title   Primary Sale
     *  @notice  Thirdweb's `PrimarySale` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *           the recipient of primary sales, and lets the inheriting contract perform conditional logic that uses information about
     *           primary sales, if desired.
     */
    abstract contract PrimarySale is IPrimarySale {
        /// @dev The address that receives all primary sales value.
        address private recipient;
        /// @dev Returns primary sale recipient address.
        function primarySaleRecipient() public view override returns (address) {
            return recipient;
        }
        /**
         *  @notice         Updates primary sale recipient.
         *  @dev            Caller should be authorized to set primary sales info.
         *                  See {_canSetPrimarySaleRecipient}.
         *                  Emits {PrimarySaleRecipientUpdated Event}; See {_setupPrimarySaleRecipient}.
         *
         *  @param _saleRecipient   Address to be set as new recipient of primary sales.
         */
        function setPrimarySaleRecipient(address _saleRecipient) external override {
            if (!_canSetPrimarySaleRecipient()) {
                revert("Not authorized");
            }
            _setupPrimarySaleRecipient(_saleRecipient);
        }
        /// @dev Lets a contract admin set the recipient for all primary sales.
        function _setupPrimarySaleRecipient(address _saleRecipient) internal {
            if (_saleRecipient == address(0)) {
                revert("Invalid recipient");
            }
            recipient = _saleRecipient;
            emit PrimarySaleRecipientUpdated(_saleRecipient);
        }
        /// @dev Returns whether primary sale recipient can be set in the given execution context.
        function _canSetPrimarySaleRecipient() internal view virtual returns (bool);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IRoyalty.sol";
    /**
     *  @title   Royalty
     *  @notice  Thirdweb's `Royalty` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *           the recipient of royalty fee and the royalty fee basis points, and lets the inheriting contract perform conditional logic
     *           that uses information about royalty fees, if desired.
     *
     *  @dev     The `Royalty` contract is ERC2981 compliant.
     */
    abstract contract Royalty is IRoyalty {
        /// @dev The (default) address that receives all royalty value.
        address private royaltyRecipient;
        /// @dev The (default) % of a sale to take as royalty (in basis points).
        uint16 private royaltyBps;
        /// @dev Token ID => royalty recipient and bps for token
        mapping(uint256 => RoyaltyInfo) private royaltyInfoForToken;
        /**
         *  @notice   View royalty info for a given token and sale price.
         *  @dev      Returns royalty amount and recipient for `tokenId` and `salePrice`.
         *  @param tokenId          The tokenID of the NFT for which to query royalty info.
         *  @param salePrice        Sale price of the token.
         *
         *  @return receiver        Address of royalty recipient account.
         *  @return royaltyAmount   Royalty amount calculated at current royaltyBps value.
         */
        function royaltyInfo(
            uint256 tokenId,
            uint256 salePrice
        ) external view virtual override returns (address receiver, uint256 royaltyAmount) {
            (address recipient, uint256 bps) = getRoyaltyInfoForToken(tokenId);
            receiver = recipient;
            royaltyAmount = (salePrice * bps) / 10_000;
        }
        /**
         *  @notice          View royalty info for a given token.
         *  @dev             Returns royalty recipient and bps for `_tokenId`.
         *  @param _tokenId  The tokenID of the NFT for which to query royalty info.
         */
        function getRoyaltyInfoForToken(uint256 _tokenId) public view override returns (address, uint16) {
            RoyaltyInfo memory royaltyForToken = royaltyInfoForToken[_tokenId];
            return
                royaltyForToken.recipient == address(0)
                    ? (royaltyRecipient, uint16(royaltyBps))
                    : (royaltyForToken.recipient, uint16(royaltyForToken.bps));
        }
        /**
         *  @notice Returns the defualt royalty recipient and BPS for this contract's NFTs.
         */
        function getDefaultRoyaltyInfo() external view override returns (address, uint16) {
            return (royaltyRecipient, uint16(royaltyBps));
        }
        /**
         *  @notice         Updates default royalty recipient and bps.
         *  @dev            Caller should be authorized to set royalty info.
         *                  See {_canSetRoyaltyInfo}.
         *                  Emits {DefaultRoyalty Event}; See {_setupDefaultRoyaltyInfo}.
         *
         *  @param _royaltyRecipient   Address to be set as default royalty recipient.
         *  @param _royaltyBps         Updated royalty bps.
         */
        function setDefaultRoyaltyInfo(address _royaltyRecipient, uint256 _royaltyBps) external override {
            if (!_canSetRoyaltyInfo()) {
                revert("Not authorized");
            }
            _setupDefaultRoyaltyInfo(_royaltyRecipient, _royaltyBps);
        }
        /// @dev Lets a contract admin update the default royalty recipient and bps.
        function _setupDefaultRoyaltyInfo(address _royaltyRecipient, uint256 _royaltyBps) internal {
            if (_royaltyBps > 10_000) {
                revert("Exceeds max bps");
            }
            royaltyRecipient = _royaltyRecipient;
            royaltyBps = uint16(_royaltyBps);
            emit DefaultRoyalty(_royaltyRecipient, _royaltyBps);
        }
        /**
         *  @notice         Updates default royalty recipient and bps for a particular token.
         *  @dev            Sets royalty info for `_tokenId`. Caller should be authorized to set royalty info.
         *                  See {_canSetRoyaltyInfo}.
         *                  Emits {RoyaltyForToken Event}; See {_setupRoyaltyInfoForToken}.
         *
         *  @param _recipient   Address to be set as royalty recipient for given token Id.
         *  @param _bps         Updated royalty bps for the token Id.
         */
        function setRoyaltyInfoForToken(uint256 _tokenId, address _recipient, uint256 _bps) external override {
            if (!_canSetRoyaltyInfo()) {
                revert("Not authorized");
            }
            _setupRoyaltyInfoForToken(_tokenId, _recipient, _bps);
        }
        /// @dev Lets a contract admin set the royalty recipient and bps for a particular token Id.
        function _setupRoyaltyInfoForToken(uint256 _tokenId, address _recipient, uint256 _bps) internal {
            if (_bps > 10_000) {
                revert("Exceeds max bps");
            }
            royaltyInfoForToken[_tokenId] = RoyaltyInfo({ recipient: _recipient, bps: _bps });
            emit RoyaltyForToken(_tokenId, _recipient, _bps);
        }
        /// @dev Returns whether royalty info can be set in the given execution context.
        function _canSetRoyaltyInfo() internal view virtual returns (bool);
    }
    // SPDX-License-Identifier: Apache 2.0
    pragma solidity ^0.8.10;
    /// @author thirdweb
    import "../lib/NFTMetadataRenderer.sol";
    import "./interface/ISharedMetadata.sol";
    import "../eip/interface/IERC4906.sol";
    abstract contract SharedMetadata is ISharedMetadata, IERC4906 {
        /// @notice Token metadata information
        SharedMetadataInfo public sharedMetadata;
        /// @notice Set shared metadata for NFTs
        function setSharedMetadata(SharedMetadataInfo calldata _metadata) external virtual {
            if (!_canSetSharedMetadata()) {
                revert("Not authorized");
            }
            _setSharedMetadata(_metadata);
        }
        /**
         *  @dev Sets shared metadata for NFTs.
         *  @param _metadata common metadata for all tokens
         */
        function _setSharedMetadata(SharedMetadataInfo calldata _metadata) internal {
            sharedMetadata = SharedMetadataInfo({
                name: _metadata.name,
                description: _metadata.description,
                imageURI: _metadata.imageURI,
                animationURI: _metadata.animationURI
            });
            emit BatchMetadataUpdate(0, type(uint256).max);
            emit SharedMetadataUpdated({
                name: _metadata.name,
                description: _metadata.description,
                imageURI: _metadata.imageURI,
                animationURI: _metadata.animationURI
            });
        }
        /**
         *  @dev Token URI information getter
         *  @param tokenId Token ID to get URI for
         */
        function _getURIFromSharedMetadata(uint256 tokenId) internal view returns (string memory) {
            SharedMetadataInfo memory info = sharedMetadata;
            return
                NFTMetadataRenderer.createMetadataEdition({
                    name: info.name,
                    description: info.description,
                    imageURI: info.imageURI,
                    animationURI: info.animationURI,
                    tokenOfEdition: tokenId
                });
        }
        /// @dev Returns whether shared metadata can be set in the given execution context.
        function _canSetSharedMetadata() internal view virtual returns (bool);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     *  The interface `IClaimCondition` is written for thirdweb's 'Drop' contracts, which are distribution mechanisms for tokens.
     *
     *  A claim condition defines criteria under which accounts can mint tokens. Claim conditions can be overwritten
     *  or added to by the contract admin. At any moment, there is only one active claim condition.
     */
    interface IClaimCondition {
        /**
         *  @notice The criteria that make up a claim condition.
         *
         *  @param startTimestamp                 The unix timestamp after which the claim condition applies.
         *                                        The same claim condition applies until the `startTimestamp`
         *                                        of the next claim condition.
         *
         *  @param maxClaimableSupply             The maximum total number of tokens that can be claimed under
         *                                        the claim condition.
         *
         *  @param supplyClaimed                  At any given point, the number of tokens that have been claimed
         *                                        under the claim condition.
         *
         *  @param quantityLimitPerWallet         The maximum number of tokens that can be claimed by a wallet.
         *
         *  @param merkleRoot                     The allowlist of addresses that can claim tokens under the claim
         *                                        condition.
         *
         *  @param pricePerToken                  The price required to pay per token claimed.
         *
         *  @param currency                       The currency in which the `pricePerToken` must be paid.
         *
         *  @param metadata                       Claim condition metadata.
         */
        struct ClaimCondition {
            uint256 startTimestamp;
            uint256 maxClaimableSupply;
            uint256 supplyClaimed;
            uint256 quantityLimitPerWallet;
            bytes32 merkleRoot;
            uint256 pricePerToken;
            address currency;
            string metadata;
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./IClaimCondition.sol";
    /**
     *  The interface `IClaimConditionMultiPhase` is written for thirdweb's 'Drop' contracts, which are distribution mechanisms for tokens.
     *
     *  An authorized wallet can set a series of claim conditions, ordered by their respective `startTimestamp`.
     *  A claim condition defines criteria under which accounts can mint tokens. Claim conditions can be overwritten
     *  or added to by the contract admin. At any moment, there is only one active claim condition.
     */
    interface IClaimConditionMultiPhase is IClaimCondition {
        /**
         *  @notice The set of all claim conditions, at any given moment.
         *  Claim Phase ID = [currentStartId, currentStartId + length - 1];
         *
         *  @param currentStartId           The uid for the first claim condition amongst the current set of
         *                                  claim conditions. The uid for each next claim condition is one
         *                                  more than the previous claim condition's uid.
         *
         *  @param count                    The total number of phases / claim conditions in the list
         *                                  of claim conditions.
         *
         *  @param conditions                   The claim conditions at a given uid. Claim conditions
         *                                  are ordered in an ascending order by their `startTimestamp`.
         *
         *  @param supplyClaimedByWallet    Map from a claim condition uid and account to supply claimed by account.
         */
        struct ClaimConditionList {
            uint256 currentStartId;
            uint256 count;
            mapping(uint256 => ClaimCondition) conditions;
            mapping(uint256 => mapping(address => uint256)) supplyClaimedByWallet;
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     *  Thirdweb's `ContractMetadata` is a contract extension for any base contracts. It lets you set a metadata URI
     *  for you contract.
     *
     *  Additionally, `ContractMetadata` is necessary for NFT contracts that want royalties to get distributed on OpenSea.
     */
    interface IContractMetadata {
        /// @dev Returns the metadata URI of the contract.
        function contractURI() external view returns (string memory);
        /**
         *  @dev Sets contract URI for the storefront-level metadata of the contract.
         *       Only module admin can call this function.
         */
        function setContractURI(string calldata _uri) external;
        /// @dev Emitted when the contract URI is updated.
        event ContractURIUpdated(string prevURI, string newURI);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./IClaimConditionMultiPhase.sol";
    /**
     *  The interface `IDrop` is written for thirdweb's 'Drop' contracts, which are distribution mechanisms for tokens.
     *
     *  An authorized wallet can set a series of claim conditions, ordered by their respective `startTimestamp`.
     *  A claim condition defines criteria under which accounts can mint tokens. Claim conditions can be overwritten
     *  or added to by the contract admin. At any moment, there is only one active claim condition.
     */
    interface IDrop is IClaimConditionMultiPhase {
        /**
         *  @param proof Proof of concerned wallet's inclusion in an allowlist.
         *  @param quantityLimitPerWallet The total quantity of tokens the allowlisted wallet is eligible to claim over time.
         *  @param pricePerToken The price per token the allowlisted wallet must pay to claim tokens.
         *  @param currency The currency in which the allowlisted wallet must pay the price for claiming tokens.
         */
        struct AllowlistProof {
            bytes32[] proof;
            uint256 quantityLimitPerWallet;
            uint256 pricePerToken;
            address currency;
        }
        /// @notice Emitted when tokens are claimed via `claim`.
        event TokensClaimed(
            uint256 indexed claimConditionIndex,
            address indexed claimer,
            address indexed receiver,
            uint256 startTokenId,
            uint256 quantityClaimed
        );
        /// @notice Emitted when the contract's claim conditions are updated.
        event ClaimConditionsUpdated(ClaimCondition[] claimConditions, bool resetEligibility);
        /**
         *  @notice Lets an account claim a given quantity of NFTs.
         *
         *  @param receiver                       The receiver of the NFTs to claim.
         *  @param quantity                       The quantity of NFTs to claim.
         *  @param currency                       The currency in which to pay for the claim.
         *  @param pricePerToken                  The price per token to pay for the claim.
         *  @param allowlistProof                 The proof of the claimer's inclusion in the merkle root allowlist
         *                                        of the claim conditions that apply.
         *  @param data                           Arbitrary bytes data that can be leveraged in the implementation of this interface.
         */
        function claim(
            address receiver,
            uint256 quantity,
            address currency,
            uint256 pricePerToken,
            AllowlistProof calldata allowlistProof,
            bytes memory data
        ) external payable;
        /**
         *  @notice Lets a contract admin (account with `DEFAULT_ADMIN_ROLE`) set claim conditions.
         *
         *  @param phases                   Claim conditions in ascending order by `startTimestamp`.
         *
         *  @param resetClaimEligibility    Whether to honor the restrictions applied to wallets who have claimed tokens in the current conditions,
         *                                  in the new claim conditions being set.
         *
         */
        function setClaimConditions(ClaimCondition[] calldata phases, bool resetClaimEligibility) external;
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     * @dev Provides a function to batch together multiple calls in a single external call.
     *
     * _Available since v4.1._
     */
    interface IMulticall {
        /**
         * @dev Receives and executes a batch of function calls on this contract.
         */
        function multicall(bytes[] calldata data) external returns (bytes[] memory results);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     *  Thirdweb's `Ownable` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *  who the 'owner' of the inheriting smart contract is, and lets the inheriting contract perform conditional logic that uses
     *  information about who the contract's owner is.
     */
    interface IOwnable {
        /// @dev Returns the owner of the contract.
        function owner() external view returns (address);
        /// @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;
        /// @dev Emitted when a new Owner is set.
        event OwnerUpdated(address indexed prevOwner, address indexed newOwner);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     * @dev External interface of AccessControl declared to support ERC165 detection.
     */
    interface IPermissions {
        /**
         * @dev Emitted when `newAdminRole` is set as ``role``'s admin role, replacing `previousAdminRole`
         *
         * `DEFAULT_ADMIN_ROLE` is the starting admin for all roles, despite
         * {RoleAdminChanged} not being emitted signaling this.
         *
         * _Available since v3.1._
         */
        event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole);
        /**
         * @dev Emitted when `account` is granted `role`.
         *
         * `sender` is the account that originated the contract call, an admin role
         * bearer except when using {AccessControl-_setupRole}.
         */
        event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender);
        /**
         * @dev Emitted when `account` is revoked `role`.
         *
         * `sender` is the account that originated the contract call:
         *   - if using `revokeRole`, it is the admin role bearer
         *   - if using `renounceRole`, it is the role bearer (i.e. `account`)
         */
        event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender);
        /**
         * @dev Returns `true` if `account` has been granted `role`.
         */
        function hasRole(bytes32 role, address account) external view returns (bool);
        /**
         * @dev Returns the admin role that controls `role`. See {grantRole} and
         * {revokeRole}.
         *
         * To change a role's admin, use {AccessControl-_setRoleAdmin}.
         */
        function getRoleAdmin(bytes32 role) external view returns (bytes32);
        /**
         * @dev Grants `role` to `account`.
         *
         * If `account` had not been already granted `role`, emits a {RoleGranted}
         * event.
         *
         * Requirements:
         *
         * - the caller must have ``role``'s admin role.
         */
        function grantRole(bytes32 role, address account) external;
        /**
         * @dev Revokes `role` from `account`.
         *
         * If `account` had been granted `role`, emits a {RoleRevoked} event.
         *
         * Requirements:
         *
         * - the caller must have ``role``'s admin role.
         */
        function revokeRole(bytes32 role, address account) external;
        /**
         * @dev Revokes `role` from the calling account.
         *
         * Roles are often managed via {grantRole} and {revokeRole}: this function's
         * purpose is to provide a mechanism for accounts to lose their privileges
         * if they are compromised (such as when a trusted device is misplaced).
         *
         * If the calling account had been granted `role`, emits a {RoleRevoked}
         * event.
         *
         * Requirements:
         *
         * - the caller must be `account`.
         */
        function renounceRole(bytes32 role, address account) external;
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./IPermissions.sol";
    /**
     * @dev External interface of AccessControlEnumerable declared to support ERC165 detection.
     */
    interface IPermissionsEnumerable is IPermissions {
        /**
         * @dev Returns one of the accounts that have `role`. `index` must be a
         * value between 0 and {getRoleMemberCount}, non-inclusive.
         *
         * Role bearers are not sorted in any particular way, and their ordering may
         * change at any point.
         *
         * WARNING: When using {getRoleMember} and {getRoleMemberCount}, make sure
         * you perform all queries on the same block. See the following
         * [forum post](https://forum.openzeppelin.com/t/iterating-over-elements-on-enumerableset-in-openzeppelin-contracts/2296)
         * for more information.
         */
        function getRoleMember(bytes32 role, uint256 index) external view returns (address);
        /**
         * @dev Returns the number of accounts that have `role`. Can be used
         * together with {getRoleMember} to enumerate all bearers of a role.
         */
        function getRoleMemberCount(bytes32 role) external view returns (uint256);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     *  Thirdweb's `PlatformFee` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *  the recipient of platform fee and the platform fee basis points, and lets the inheriting contract perform conditional logic
     *  that uses information about platform fees, if desired.
     */
    interface IPlatformFee {
        /// @dev Fee type variants: percentage fee and flat fee
        enum PlatformFeeType {
            Bps,
            Flat
        }
        /// @dev Returns the platform fee bps and recipient.
        function getPlatformFeeInfo() external view returns (address, uint16);
        /// @dev Lets a module admin update the fees on primary sales.
        function setPlatformFeeInfo(address _platformFeeRecipient, uint256 _platformFeeBps) external;
        /// @dev Emitted when fee on primary sales is updated.
        event PlatformFeeInfoUpdated(address indexed platformFeeRecipient, uint256 platformFeeBps);
        /// @dev Emitted when the flat platform fee is updated.
        event FlatPlatformFeeUpdated(address platformFeeRecipient, uint256 flatFee);
        /// @dev Emitted when the platform fee type is updated.
        event PlatformFeeTypeUpdated(PlatformFeeType feeType);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     *  Thirdweb's `Primary` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *  the recipient of primary sales, and lets the inheriting contract perform conditional logic that uses information about
     *  primary sales, if desired.
     */
    interface IPrimarySale {
        /// @dev The adress that receives all primary sales value.
        function primarySaleRecipient() external view returns (address);
        /// @dev Lets a module admin set the default recipient of all primary sales.
        function setPrimarySaleRecipient(address _saleRecipient) external;
        /// @dev Emitted when a new sale recipient is set.
        event PrimarySaleRecipientUpdated(address indexed recipient);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "../../eip/interface/IERC2981.sol";
    /**
     *  Thirdweb's `Royalty` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *  the recipient of royalty fee and the royalty fee basis points, and lets the inheriting contract perform conditional logic
     *  that uses information about royalty fees, if desired.
     *
     *  The `Royalty` contract is ERC2981 compliant.
     */
    interface IRoyalty is IERC2981 {
        struct RoyaltyInfo {
            address recipient;
            uint256 bps;
        }
        /// @dev Returns the royalty recipient and fee bps.
        function getDefaultRoyaltyInfo() external view returns (address, uint16);
        /// @dev Lets a module admin update the royalty bps and recipient.
        function setDefaultRoyaltyInfo(address _royaltyRecipient, uint256 _royaltyBps) external;
        /// @dev Lets a module admin set the royalty recipient for a particular token Id.
        function setRoyaltyInfoForToken(uint256 tokenId, address recipient, uint256 bps) external;
        /// @dev Returns the royalty recipient for a particular token Id.
        function getRoyaltyInfoForToken(uint256 tokenId) external view returns (address, uint16);
        /// @dev Emitted when royalty info is updated.
        event DefaultRoyalty(address indexed newRoyaltyRecipient, uint256 newRoyaltyBps);
        /// @dev Emitted when royalty recipient for tokenId is set
        event RoyaltyForToken(uint256 indexed tokenId, address indexed royaltyRecipient, uint256 royaltyBps);
    }
    // SPDX-License-Identifier: Apache 2.0
    pragma solidity ^0.8.10;
    /// @author thirdweb
    interface ISharedMetadata {
        /// @notice Emitted when shared metadata is lazy minted.
        event SharedMetadataUpdated(string name, string description, string imageURI, string animationURI);
        /**
         *  @notice Structure for metadata shared across all tokens
         *
         *  @param name Shared name of NFT in metadata
         *  @param description Shared description of NFT in metadata
         *  @param imageURI Shared URI of image to render for NFTs
         *  @param animationURI Shared URI of animation to render for NFTs
         */
        struct SharedMetadataInfo {
            string name;
            string description;
            string imageURI;
            string animationURI;
        }
        /**
         *  @notice Set shared metadata for NFTs
         *  @param _metadata common metadata for all tokens
         */
        function setSharedMetadata(SharedMetadataInfo calldata _metadata) external;
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts v4.4.0 (metatx/ERC2771Context.sol)
    pragma solidity ^0.8.11;
    import "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol";
    import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
    /**
     * @dev Context variant with ERC2771 support.
     */
    abstract contract ERC2771ContextUpgradeable is Initializable, ContextUpgradeable {
        mapping(address => bool) private _trustedForwarder;
        function __ERC2771Context_init(address[] memory trustedForwarder) internal onlyInitializing {
            __Context_init_unchained();
            __ERC2771Context_init_unchained(trustedForwarder);
        }
        function __ERC2771Context_init_unchained(address[] memory trustedForwarder) internal onlyInitializing {
            for (uint256 i = 0; i < trustedForwarder.length; i++) {
                _trustedForwarder[trustedForwarder[i]] = true;
            }
        }
        function isTrustedForwarder(address forwarder) public view virtual returns (bool) {
            return _trustedForwarder[forwarder];
        }
        function _msgSender() internal view virtual override returns (address sender) {
            if (isTrustedForwarder(msg.sender)) {
                // The assembly code is more direct than the Solidity version using `abi.decode`.
                assembly {
                    sender := shr(96, calldataload(sub(calldatasize(), 20)))
                }
            } else {
                return super._msgSender();
            }
        }
        function _msgData() internal view virtual override returns (bytes calldata) {
            if (isTrustedForwarder(msg.sender)) {
                return msg.data[:msg.data.length - 20];
            } else {
                return super._msgData();
            }
        }
        uint256[49] private __gap;
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts v4.4.1 (token/ERC20/utils/SafeERC20.sol)
    pragma solidity ^0.8.0;
    import "../../../../../eip/interface/IERC20.sol";
    import { Address } from "@openzeppelin/contracts/utils/Address.sol";
    /**
     * @title SafeERC20
     * @dev Wrappers around ERC20 operations that throw on failure (when the token
     * contract returns false). Tokens that return no value (and instead revert or
     * throw on failure) are also supported, non-reverting calls are assumed to be
     * successful.
     * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
     * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
     */
    library SafeERC20 {
        using Address for address;
        function safeTransfer(IERC20 token, address to, uint256 value) internal {
            _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
        }
        function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
            _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
        }
        /**
         * @dev Deprecated. This function has issues similar to the ones found in
         * {IERC20-approve}, and its usage is discouraged.
         *
         * Whenever possible, use {safeIncreaseAllowance} and
         * {safeDecreaseAllowance} instead.
         */
        function safeApprove(IERC20 token, address spender, uint256 value) internal {
            // safeApprove should only be called when setting an initial allowance,
            // or when resetting it to zero. To increase and decrease it, use
            // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
            require(
                (value == 0) || (token.allowance(address(this), spender) == 0),
                "SafeERC20: approve from non-zero to non-zero allowance"
            );
            _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
        }
        function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
            uint256 newAllowance = token.allowance(address(this), spender) + value;
            _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
        }
        function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
            unchecked {
                uint256 oldAllowance = token.allowance(address(this), spender);
                require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
                uint256 newAllowance = oldAllowance - value;
                _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
            }
        }
        /**
         * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
         * on the return value: the return value is optional (but if data is returned, it must not be false).
         * @param token The token targeted by the call.
         * @param data The call data (encoded using abi.encode or one of its variants).
         */
        function _callOptionalReturn(IERC20 token, bytes memory data) private {
            // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
            // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
            // the target address contains contract code and also asserts for success in the low-level call.
            bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
            if (returndata.length > 0) {
                // Return data is optional
                require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
            }
        }
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.7.0) (utils/Base64.sol)
    pragma solidity ^0.8.0;
    /**
     * @dev Provides a set of functions to operate with Base64 strings.
     *
     * _Available since v4.5._
     */
    library Base64 {
        /**
         * @dev Base64 Encoding/Decoding Table
         */
        string internal constant _TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        /**
         * @dev Converts a `bytes` to its Bytes64 `string` representation.
         */
        function encode(bytes memory data) internal pure returns (string memory) {
            /**
             * Inspired by Brecht Devos (Brechtpd) implementation - MIT licence
             * https://github.com/Brechtpd/base64/blob/e78d9fd951e7b0977ddca77d92dc85183770daf4/base64.sol
             */
            if (data.length == 0) return "";
            // Loads the table into memory
            string memory table = _TABLE;
            // Encoding takes 3 bytes chunks of binary data from `bytes` data parameter
            // and split into 4 numbers of 6 bits.
            // The final Base64 length should be `bytes` data length multiplied by 4/3 rounded up
            // - `data.length + 2`  -> Round up
            // - `/ 3`              -> Number of 3-bytes chunks
            // - `4 *`              -> 4 characters for each chunk
            string memory result = new string(4 * ((data.length + 2) / 3));
            /// @solidity memory-safe-assembly
            assembly {
                // Prepare the lookup table (skip the first "length" byte)
                let tablePtr := add(table, 1)
                // Prepare result pointer, jump over length
                let resultPtr := add(result, 32)
                // Run over the input, 3 bytes at a time
                for {
                    let dataPtr := data
                    let endPtr := add(data, mload(data))
                } lt(dataPtr, endPtr) {
                } {
                    // Advance 3 bytes
                    dataPtr := add(dataPtr, 3)
                    let input := mload(dataPtr)
                    // To write each character, shift the 3 bytes (18 bits) chunk
                    // 4 times in blocks of 6 bits for each character (18, 12, 6, 0)
                    // and apply logical AND with 0x3F which is the number of
                    // the previous character in the ASCII table prior to the Base64 Table
                    // The result is then added to the table to get the character to write,
                    // and finally write it in the result pointer but with a left shift
                    // of 256 (1 byte) - 8 (1 ASCII char) = 248 bits
                    mstore8(resultPtr, mload(add(tablePtr, and(shr(18, input), 0x3F))))
                    resultPtr := add(resultPtr, 1) // Advance
                    mstore8(resultPtr, mload(add(tablePtr, and(shr(12, input), 0x3F))))
                    resultPtr := add(resultPtr, 1) // Advance
                    mstore8(resultPtr, mload(add(tablePtr, and(shr(6, input), 0x3F))))
                    resultPtr := add(resultPtr, 1) // Advance
                    mstore8(resultPtr, mload(add(tablePtr, and(input, 0x3F))))
                    resultPtr := add(resultPtr, 1) // Advance
                }
                // When data `bytes` is not exactly 3 bytes long
                // it is padded with `=` characters at the end
                switch mod(mload(data), 3)
                case 1 {
                    mstore8(sub(resultPtr, 1), 0x3d)
                    mstore8(sub(resultPtr, 2), 0x3d)
                }
                case 2 {
                    mstore8(sub(resultPtr, 1), 0x3d)
                }
            }
            return result;
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    interface IWETH {
        function deposit() external payable;
        function withdraw(uint256 amount) external;
        function transfer(address to, uint256 value) external returns (bool);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.1;
    /// @author thirdweb, OpenZeppelin Contracts (v4.9.0)
    /**
     * @dev Collection of functions related to the address type
     */
    library Address {
        /**
         * @dev Returns true if `account` is a contract.
         *
         * [IMPORTANT]
         * ====
         * It is unsafe to assume that an address for which this function returns
         * false is an externally-owned account (EOA) and not a contract.
         *
         * Among others, `isContract` will return false for the following
         * types of addresses:
         *
         *  - an externally-owned account
         *  - a contract in construction
         *  - an address where a contract will be created
         *  - an address where a contract lived, but was destroyed
         *
         * Furthermore, `isContract` will also return true if the target contract within
         * the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
         * which only has an effect at the end of a transaction.
         * ====
         *
         * [IMPORTANT]
         * ====
         * You shouldn't rely on `isContract` to protect against flash loan attacks!
         *
         * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
         * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
         * constructor.
         * ====
         */
        function isContract(address account) internal view returns (bool) {
            // This method relies on extcodesize/address.code.length, which returns 0
            // for contracts in construction, since the code is only stored at the end
            // of the constructor execution.
            return account.code.length > 0;
        }
        /**
         * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
         * `recipient`, forwarding all available gas and reverting on errors.
         *
         * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
         * of certain opcodes, possibly making contracts go over the 2300 gas limit
         * imposed by `transfer`, making them unable to receive funds via
         * `transfer`. {sendValue} removes this limitation.
         *
         * https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
         *
         * IMPORTANT: because control is transferred to `recipient`, care must be
         * taken to not create reentrancy vulnerabilities. Consider using
         * {ReentrancyGuard} or the
         * https://solidity.readthedocs.io/en/v0.8.0/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
         */
        function sendValue(address payable recipient, uint256 amount) internal {
            require(address(this).balance >= amount, "Address: insufficient balance");
            (bool success, ) = recipient.call{ value: amount }("");
            require(success, "Address: unable to send value, recipient may have reverted");
        }
        /**
         * @dev Performs a Solidity function call using a low level `call`. A
         * plain `call` is an unsafe replacement for a function call: use this
         * function instead.
         *
         * If `target` reverts with a revert reason, it is bubbled up by this
         * function (like regular Solidity function calls).
         *
         * Returns the raw returned data. To convert to the expected return value,
         * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
         *
         * Requirements:
         *
         * - `target` must be a contract.
         * - calling `target` with `data` must not revert.
         *
         * _Available since v3.1._
         */
        function functionCall(address target, bytes memory data) internal returns (bytes memory) {
            return functionCallWithValue(target, data, 0, "Address: low-level call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
         * `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal returns (bytes memory) {
            return functionCallWithValue(target, data, 0, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but also transferring `value` wei to `target`.
         *
         * Requirements:
         *
         * - the calling contract must have an ETH balance of at least `value`.
         * - the called Solidity function must be `payable`.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
            return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
        }
        /**
         * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
         * with `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(
            address target,
            bytes memory data,
            uint256 value,
            string memory errorMessage
        ) internal returns (bytes memory) {
            require(address(this).balance >= value, "Address: insufficient balance for call");
            (bool success, bytes memory returndata) = target.call{ value: value }(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
            return functionStaticCall(target, data, "Address: low-level static call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal view returns (bytes memory) {
            (bool success, bytes memory returndata) = target.staticcall(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but performing a delegate call.
         *
         * _Available since v3.4._
         */
        function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
            return functionDelegateCall(target, data, "Address: low-level delegate call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
         * but performing a delegate call.
         *
         * _Available since v3.4._
         */
        function functionDelegateCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal returns (bytes memory) {
            (bool success, bytes memory returndata) = target.delegatecall(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
         * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
         *
         * _Available since v4.8._
         */
        function verifyCallResultFromTarget(
            address target,
            bool success,
            bytes memory returndata,
            string memory errorMessage
        ) internal view returns (bytes memory) {
            if (success) {
                if (returndata.length == 0) {
                    // only check isContract if the call was successful and the return data is empty
                    // otherwise we already know that it was a contract
                    require(isContract(target), "Address: call to non-contract");
                }
                return returndata;
            } else {
                _revert(returndata, errorMessage);
            }
        }
        /**
         * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
         * revert reason or using the provided one.
         *
         * _Available since v4.3._
         */
        function verifyCallResult(
            bool success,
            bytes memory returndata,
            string memory errorMessage
        ) internal pure returns (bytes memory) {
            if (success) {
                return returndata;
            } else {
                _revert(returndata, errorMessage);
            }
        }
        function _revert(bytes memory returndata, string memory errorMessage) private pure {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly
                /// @solidity memory-safe-assembly
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    // Helper interfaces
    import { IWETH } from "../infra/interface/IWETH.sol";
    import { SafeERC20, IERC20 } from "../external-deps/openzeppelin/token/ERC20/utils/SafeERC20.sol";
    library CurrencyTransferLib {
        using SafeERC20 for IERC20;
        /// @dev The address interpreted as native token of the chain.
        address public constant NATIVE_TOKEN = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
        /// @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) {
                safeTransferNativeToken(_to, _amount);
            } else {
                safeTransferERC20(_currency, _from, _to, _amount);
            }
        }
        /// @dev Transfers a given amount of currency. (With native token wrapping)
        function transferCurrencyWithWrapper(
            address _currency,
            address _from,
            address _to,
            uint256 _amount,
            address _nativeTokenWrapper
        ) internal {
            if (_amount == 0) {
                return;
            }
            if (_currency == NATIVE_TOKEN) {
                if (_from == address(this)) {
                    // withdraw from weth then transfer withdrawn native token to recipient
                    IWETH(_nativeTokenWrapper).withdraw(_amount);
                    safeTransferNativeTokenWithWrapper(_to, _amount, _nativeTokenWrapper);
                } else if (_to == address(this)) {
                    // store native currency in weth
                    require(_amount == msg.value, "msg.value != amount");
                    IWETH(_nativeTokenWrapper).deposit{ value: _amount }();
                } else {
                    safeTransferNativeTokenWithWrapper(_to, _amount, _nativeTokenWrapper);
                }
            } else {
                safeTransferERC20(_currency, _from, _to, _amount);
            }
        }
        /// @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;
            }
            if (_from == address(this)) {
                IERC20(_currency).safeTransfer(_to, _amount);
            } else {
                IERC20(_currency).safeTransferFrom(_from, _to, _amount);
            }
        }
        /// @dev Transfers `amount` of native token to `to`.
        function safeTransferNativeToken(address to, uint256 value) internal {
            // solhint-disable avoid-low-level-calls
            // slither-disable-next-line low-level-calls
            (bool success, ) = to.call{ value: value }("");
            require(success, "native token transfer failed");
        }
        /// @dev Transfers `amount` of native token to `to`. (With native token wrapping)
        function safeTransferNativeTokenWithWrapper(address to, uint256 value, address _nativeTokenWrapper) internal {
            // solhint-disable avoid-low-level-calls
            // slither-disable-next-line low-level-calls
            (bool success, ) = to.call{ value: value }("");
            if (!success) {
                IWETH(_nativeTokenWrapper).deposit{ value: value }();
                IERC20(_nativeTokenWrapper).safeTransfer(to, value);
            }
        }
    }
    // SPDX-License-Identifier: Apache 2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    library MerkleProof {
        function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool, uint256) {
            bytes32 computedHash = leaf;
            uint256 index = 0;
            for (uint256 i = 0; i < proof.length; i++) {
                index *= 2;
                bytes32 proofElement = proof[i];
                if (computedHash <= proofElement) {
                    // Hash(current computed hash + current element of the proof)
                    computedHash = keccak256(abi.encodePacked(computedHash, proofElement));
                } else {
                    // Hash(current element of the proof + current computed hash)
                    computedHash = keccak256(abi.encodePacked(proofElement, computedHash));
                    index += 1;
                }
            }
            // Check if the computed hash (root) is equal to the provided root
            return (computedHash == root, index);
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /* solhint-disable quotes */
    /// @author thirdweb
    /// credits: Zora
    import "./Strings.sol";
    import "../external-deps/openzeppelin/utils/Base64.sol";
    /// NFT metadata library for rendering metadata associated with editions
    library NFTMetadataRenderer {
        /**
         *  @notice Generate edition metadata from storage information as base64-json blob
         *  @dev Combines the media data and metadata
         * @param name Name of NFT in metadata
         * @param description Description of NFT in metadata
         * @param imageURI URI of image to render for edition
         * @param animationURI URI of animation to render for edition
         * @param tokenOfEdition Token ID for specific token
         */
        function createMetadataEdition(
            string memory name,
            string memory description,
            string memory imageURI,
            string memory animationURI,
            uint256 tokenOfEdition
        ) internal pure returns (string memory) {
            string memory _tokenMediaData = tokenMediaData(imageURI, animationURI);
            bytes memory json = createMetadataJSON(name, description, _tokenMediaData, tokenOfEdition);
            return encodeMetadataJSON(json);
        }
        /**
         * @param name Name of NFT in metadata
         * @param description Description of NFT in metadata
         * @param mediaData Data for media to include in json object
         * @param tokenOfEdition Token ID for specific token
         */
        function createMetadataJSON(
            string memory name,
            string memory description,
            string memory mediaData,
            uint256 tokenOfEdition
        ) internal pure returns (bytes memory) {
            return
                abi.encodePacked(
                    '{"name": "',
                    name,
                    " ",
                    Strings.toString(tokenOfEdition),
                    '", "',
                    'description": "',
                    description,
                    '", "',
                    mediaData,
                    'properties": {"number": ',
                    Strings.toString(tokenOfEdition),
                    ', "name": "',
                    name,
                    '"}}'
                );
        }
        /// Encodes the argument json bytes into base64-data uri format
        /// @param json Raw json to base64 and turn into a data-uri
        function encodeMetadataJSON(bytes memory json) internal pure returns (string memory) {
            return string(abi.encodePacked("data:application/json;base64,", Base64.encode(json)));
        }
        /// Generates edition metadata from storage information as base64-json blob
        /// Combines the media data and metadata
        /// @param imageUrl URL of image to render for edition
        /// @param animationUrl URL of animation to render for edition
        function tokenMediaData(string memory imageUrl, string memory animationUrl) internal pure returns (string memory) {
            bool hasImage = bytes(imageUrl).length > 0;
            bool hasAnimation = bytes(animationUrl).length > 0;
            if (hasImage && hasAnimation) {
                return string(abi.encodePacked('image": "', imageUrl, '", "animation_url": "', animationUrl, '", "'));
            }
            if (hasImage) {
                return string(abi.encodePacked('image": "', imageUrl, '", "'));
            }
            if (hasAnimation) {
                return string(abi.encodePacked('animation_url": "', animationUrl, '", "'));
            }
            return "";
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     * @dev String operations.
     */
    library Strings {
        bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";
        /**
         * @dev Converts a `uint256` to its ASCII `string` decimal representation.
         */
        function toString(uint256 value) internal pure returns (string memory) {
            // Inspired by OraclizeAPI's implementation - MIT licence
            // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
            if (value == 0) {
                return "0";
            }
            uint256 temp = value;
            uint256 digits;
            while (temp != 0) {
                digits++;
                temp /= 10;
            }
            bytes memory buffer = new bytes(digits);
            while (value != 0) {
                digits -= 1;
                buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
                value /= 10;
            }
            return string(buffer);
        }
        /**
         * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
         */
        function toHexString(uint256 value) internal pure returns (string memory) {
            if (value == 0) {
                return "0x00";
            }
            uint256 temp = value;
            uint256 length = 0;
            while (temp != 0) {
                length++;
                temp >>= 8;
            }
            return toHexString(value, length);
        }
        /**
         * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
         */
        function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
            bytes memory buffer = new bytes(2 * length + 2);
            buffer[0] = "0";
            buffer[1] = "x";
            for (uint256 i = 2 * length + 1; i > 1; --i) {
                buffer[i] = _HEX_SYMBOLS[value & 0xf];
                value >>= 4;
            }
            require(value == 0, "Strings: hex length insufficient");
            return string(buffer);
        }
        /// @dev Returns the hexadecimal representation of `value`.
        /// The output is prefixed with "0x", encoded using 2 hexadecimal digits per byte,
        /// and the alphabets are capitalized conditionally according to
        /// https://eips.ethereum.org/EIPS/eip-55
        function toHexStringChecksummed(address value) internal pure returns (string memory str) {
            str = toHexString(value);
            /// @solidity memory-safe-assembly
            assembly {
                let mask := shl(6, div(not(0), 255)) // `0b010000000100000000 ...`
                let o := add(str, 0x22)
                let hashed := and(keccak256(o, 40), mul(34, mask)) // `0b10001000 ... `
                let t := shl(240, 136) // `0b10001000 << 240`
                for {
                    let i := 0
                } 1 {
                } {
                    mstore(add(i, i), mul(t, byte(i, hashed)))
                    i := add(i, 1)
                    if eq(i, 20) {
                        break
                    }
                }
                mstore(o, xor(mload(o), shr(1, and(mload(0x00), and(mload(o), mask)))))
                o := add(o, 0x20)
                mstore(o, xor(mload(o), shr(1, and(mload(0x20), and(mload(o), mask)))))
            }
        }
        /// @dev Returns the hexadecimal representation of `value`.
        /// The output is prefixed with "0x" and encoded using 2 hexadecimal digits per byte.
        function toHexString(address value) internal pure returns (string memory str) {
            str = toHexStringNoPrefix(value);
            /// @solidity memory-safe-assembly
            assembly {
                let strLength := add(mload(str), 2) // Compute the length.
                mstore(str, 0x3078) // Write the "0x" prefix.
                str := sub(str, 2) // Move the pointer.
                mstore(str, strLength) // Write the length.
            }
        }
        /// @dev Returns the hexadecimal representation of `value`.
        /// The output is encoded using 2 hexadecimal digits per byte.
        function toHexStringNoPrefix(address value) internal pure returns (string memory str) {
            /// @solidity memory-safe-assembly
            assembly {
                str := mload(0x40)
                // Allocate the memory.
                // We need 0x20 bytes for the trailing zeros padding, 0x20 bytes for the length,
                // 0x02 bytes for the prefix, and 0x28 bytes for the digits.
                // The next multiple of 0x20 above (0x20 + 0x20 + 0x02 + 0x28) is 0x80.
                mstore(0x40, add(str, 0x80))
                // Store "0123456789abcdef" in scratch space.
                mstore(0x0f, 0x30313233343536373839616263646566)
                str := add(str, 2)
                mstore(str, 40)
                let o := add(str, 0x20)
                mstore(add(o, 40), 0)
                value := shl(96, value)
                // We write the string from rightmost digit to leftmost digit.
                // The following is essentially a do-while loop that also handles the zero case.
                for {
                    let i := 0
                } 1 {
                } {
                    let p := add(o, add(i, i))
                    let temp := byte(i, value)
                    mstore8(add(p, 1), mload(and(temp, 15)))
                    mstore8(p, mload(shr(4, temp)))
                    i := add(i, 1)
                    if eq(i, 20) {
                        break
                    }
                }
            }
        }
        /// @dev Returns the hex encoded string from the raw bytes.
        /// The output is encoded using 2 hexadecimal digits per byte.
        function toHexString(bytes memory raw) internal pure returns (string memory str) {
            str = toHexStringNoPrefix(raw);
            /// @solidity memory-safe-assembly
            assembly {
                let strLength := add(mload(str), 2) // Compute the length.
                mstore(str, 0x3078) // Write the "0x" prefix.
                str := sub(str, 2) // Move the pointer.
                mstore(str, strLength) // Write the length.
            }
        }
        /// @dev Returns the hex encoded string from the raw bytes.
        /// The output is encoded using 2 hexadecimal digits per byte.
        function toHexStringNoPrefix(bytes memory raw) internal pure returns (string memory str) {
            /// @solidity memory-safe-assembly
            assembly {
                let length := mload(raw)
                str := add(mload(0x40), 2) // Skip 2 bytes for the optional prefix.
                mstore(str, add(length, length)) // Store the length of the output.
                // Store "0123456789abcdef" in scratch space.
                mstore(0x0f, 0x30313233343536373839616263646566)
                let o := add(str, 0x20)
                let end := add(raw, length)
                for {
                } iszero(eq(raw, end)) {
                } {
                    raw := add(raw, 1)
                    mstore8(add(o, 1), mload(and(mload(raw), 15)))
                    mstore8(o, mload(and(shr(4, mload(raw)), 15)))
                    o := add(o, 2)
                }
                mstore(o, 0) // Zeroize the slot after the string.
                mstore(0x40, add(o, 0x20)) // Allocate the memory.
            }
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.11;
    /// @author thirdweb
    //   $$\\     $$\\       $$\\                 $$\\                         $$\\
    //   $$ |    $$ |      \\__|                $$ |                        $$ |
    // $$$$$$\\   $$$$$$$\\  $$\\  $$$$$$\\   $$$$$$$ |$$\\  $$\\  $$\\  $$$$$$\\  $$$$$$$\\
    // \\_$$  _|  $$  __$$\\ $$ |$$  __$$\\ $$  __$$ |$$ | $$ | $$ |$$  __$$\\ $$  __$$\\
    //   $$ |    $$ |  $$ |$$ |$$ |  \\__|$$ /  $$ |$$ | $$ | $$ |$$$$$$$$ |$$ |  $$ |
    //   $$ |$$\\ $$ |  $$ |$$ |$$ |      $$ |  $$ |$$ | $$ | $$ |$$   ____|$$ |  $$ |
    //   \\$$$$  |$$ |  $$ |$$ |$$ |      \\$$$$$$$ |\\$$$$$\\$$$$  |\\$$$$$$$\\ $$$$$$$  |
    //    \\____/ \\__|  \\__|\\__|\\__|       \\_______| \\_____\\____/  \\_______|\\_______/
    //  ==========  External imports    ==========
    import "@openzeppelin/contracts-upgradeable/utils/StringsUpgradeable.sol";
    import "@openzeppelin/contracts-upgradeable/interfaces/IERC2981Upgradeable.sol";
    import "../../eip/queryable/ERC721AQueryableUpgradeable.sol";
    //  ==========  Internal imports    ==========
    import "../../external-deps/openzeppelin/metatx/ERC2771ContextUpgradeable.sol";
    import "../../lib/CurrencyTransferLib.sol";
    //  ==========  Features    ==========
    import "../../extension/Multicall.sol";
    import "../../extension/ContractMetadata.sol";
    import "../../extension/Royalty.sol";
    import "../../extension/PrimarySale.sol";
    import "../../extension/Ownable.sol";
    import "../../extension/SharedMetadata.sol";
    import "../../extension/PermissionsEnumerable.sol";
    import "../../extension/Drop.sol";
    import "../../extension/PlatformFee.sol";
    contract OpenEditionERC721FlatFee is
        Initializable,
        ContractMetadata,
        PlatformFee,
        Royalty,
        PrimarySale,
        Ownable,
        SharedMetadata,
        PermissionsEnumerable,
        Drop,
        ERC2771ContextUpgradeable,
        Multicall,
        ERC721AQueryableUpgradeable
    {
        using StringsUpgradeable for uint256;
        /*///////////////////////////////////////////////////////////////
                                State variables
        //////////////////////////////////////////////////////////////*/
        /// @dev Only transfers to or from TRANSFER_ROLE holders are valid, when transfers are restricted.
        bytes32 private transferRole;
        /// @dev Only MINTER_ROLE holders can update the shared metadata of tokens.
        bytes32 private minterRole;
        /// @dev Max bps in the thirdweb system.
        uint256 private constant MAX_BPS = 10_000;
        /*///////////////////////////////////////////////////////////////
                        Constructor + initializer logic
        //////////////////////////////////////////////////////////////*/
        constructor() initializer {}
        /// @dev Initializes the contract, like a constructor.
        function initialize(
            address _defaultAdmin,
            string memory _name,
            string memory _symbol,
            string memory _contractURI,
            address[] memory _trustedForwarders,
            address _saleRecipient,
            address _royaltyRecipient,
            uint128 _royaltyBps,
            uint128 _platformFeeBps,
            address _platformFeeRecipient
        ) external initializerERC721A initializer {
            bytes32 _transferRole = keccak256("TRANSFER_ROLE");
            bytes32 _minterRole = keccak256("MINTER_ROLE");
            // Initialize inherited contracts, most base-like -> most derived.
            __ERC2771Context_init(_trustedForwarders);
            __ERC721A_init(_name, _symbol);
            _setupContractURI(_contractURI);
            _setupOwner(_defaultAdmin);
            _setupRole(DEFAULT_ADMIN_ROLE, _defaultAdmin);
            _setupRole(_minterRole, _defaultAdmin);
            _setupRole(_transferRole, _defaultAdmin);
            _setupRole(_transferRole, address(0));
            _setupPlatformFeeInfo(_platformFeeRecipient, _platformFeeBps);
            _setupDefaultRoyaltyInfo(_royaltyRecipient, _royaltyBps);
            _setupPrimarySaleRecipient(_saleRecipient);
            transferRole = _transferRole;
            minterRole = _minterRole;
        }
        /*///////////////////////////////////////////////////////////////
                            ERC 165 / 721 / 2981 logic
        //////////////////////////////////////////////////////////////*/
        /// @dev Returns the URI for a given tokenId.
        function tokenURI(
            uint256 _tokenId
        ) public view virtual override(ERC721AUpgradeable, IERC721AUpgradeable) returns (string memory) {
            if (!_exists(_tokenId)) {
                revert("!ID");
            }
            return _getURIFromSharedMetadata(_tokenId);
        }
        /// @dev See ERC 165
        function supportsInterface(
            bytes4 interfaceId
        ) public view virtual override(ERC721AUpgradeable, IERC165, IERC721AUpgradeable) returns (bool) {
            return super.supportsInterface(interfaceId) || type(IERC2981Upgradeable).interfaceId == interfaceId;
        }
        /// @dev The start token ID for the contract.
        function _startTokenId() internal pure override returns (uint256) {
            return 1;
        }
        function startTokenId() public pure returns (uint256) {
            return _startTokenId();
        }
        /*///////////////////////////////////////////////////////////////
                            Internal functions
        //////////////////////////////////////////////////////////////*/
        /// @dev Collects and distributes the primary sale value of NFTs being claimed.
        function _collectPriceOnClaim(
            address _primarySaleRecipient,
            uint256 _quantityToClaim,
            address _currency,
            uint256 _pricePerToken
        ) internal override {
            if (_pricePerToken == 0) {
                require(msg.value == 0, "!Value");
                return;
            }
            uint256 totalPrice = _quantityToClaim * _pricePerToken;
            uint256 platformFees;
            address platformFeeRecipient;
            if (getPlatformFeeType() == IPlatformFee.PlatformFeeType.Flat) {
                (platformFeeRecipient, platformFees) = getFlatPlatformFeeInfo();
            } else {
                (address recipient, uint16 platformFeeBps) = getPlatformFeeInfo();
                platformFeeRecipient = recipient;
                platformFees = ((totalPrice * platformFeeBps) / MAX_BPS);
            }
            require(totalPrice >= platformFees, "price less than platform fee");
            bool validMsgValue;
            if (_currency == CurrencyTransferLib.NATIVE_TOKEN) {
                validMsgValue = msg.value == totalPrice;
            } else {
                validMsgValue = msg.value == 0;
            }
            require(validMsgValue, "!V");
            address saleRecipient = _primarySaleRecipient == address(0) ? primarySaleRecipient() : _primarySaleRecipient;
            CurrencyTransferLib.transferCurrency(_currency, _msgSender(), platformFeeRecipient, platformFees);
            CurrencyTransferLib.transferCurrency(_currency, _msgSender(), saleRecipient, totalPrice - platformFees);
        }
        /// @dev Transfers the NFTs being claimed.
        function _transferTokensOnClaim(
            address _to,
            uint256 _quantityBeingClaimed
        ) internal override returns (uint256 startTokenId_) {
            startTokenId_ = _nextTokenId();
            _safeMint(_to, _quantityBeingClaimed);
        }
        /// @dev Checks whether primary sale recipient can be set in the given execution context.
        function _canSetPrimarySaleRecipient() internal view override returns (bool) {
            return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
        }
        /// @dev Checks whether owner can be set in the given execution context.
        function _canSetOwner() internal view override returns (bool) {
            return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
        }
        /// @dev Checks whether royalty info can be set in the given execution context.
        function _canSetRoyaltyInfo() internal view override returns (bool) {
            return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
        }
        /// @dev Checks whether contract metadata can be set in the given execution context.
        function _canSetContractURI() internal view override returns (bool) {
            return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
        }
        /// @dev Checks whether platform fee info can be set in the given execution context.
        function _canSetClaimConditions() internal view override returns (bool) {
            return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
        }
        /// @dev Returns whether the shared metadata of tokens can be set in the given execution context.
        function _canSetSharedMetadata() internal view virtual override returns (bool) {
            return hasRole(minterRole, _msgSender());
        }
        /// @dev Checks whether platform fee info can be set in the given execution context.
        function _canSetPlatformFeeInfo() internal view override returns (bool) {
            return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
        }
        /*///////////////////////////////////////////////////////////////
                            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 _nextTokenId();
        }
        /// @dev The next token ID of the NFT that can be claimed.
        function nextTokenIdToClaim() external view returns (uint256) {
            return _nextTokenId();
        }
        /// @dev Burns `tokenId`. See {ERC721-_burn}.
        function burn(uint256 tokenId) external virtual {
            // note: ERC721AUpgradeable's `_burn(uint256,bool)` internally checks for token approvals.
            _burn(tokenId, true);
        }
        /// @dev See {ERC721-_beforeTokenTransfer}.
        function _beforeTokenTransfers(
            address from,
            address to,
            uint256 startTokenId_,
            uint256 quantity
        ) internal virtual override {
            super._beforeTokenTransfers(from, to, startTokenId_, quantity);
            // if transfer is restricted on the contract, we still want to allow burning and minting
            if (!hasRole(transferRole, address(0)) && from != address(0) && to != address(0)) {
                if (!hasRole(transferRole, from) && !hasRole(transferRole, to)) {
                    revert("!T");
                }
            }
        }
        function _dropMsgSender() internal view virtual override returns (address) {
            return _msgSender();
        }
        function _msgSenderERC721A() internal view virtual override returns (address) {
            return _msgSender();
        }
        function _msgSender()
            internal
            view
            virtual
            override(ERC2771ContextUpgradeable, Multicall)
            returns (address sender)
        {
            return ERC2771ContextUpgradeable._msgSender();
        }
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.9.0) (interfaces/IERC2981.sol)
    pragma solidity ^0.8.0;
    import "../utils/introspection/IERC165Upgradeable.sol";
    /**
     * @dev Interface for the NFT Royalty Standard.
     *
     * A standardized way to retrieve royalty payment information for non-fungible tokens (NFTs) to enable universal
     * support for royalty payments across all NFT marketplaces and ecosystem participants.
     *
     * _Available since v4.5._
     */
    interface IERC2981Upgradeable is IERC165Upgradeable {
        /**
         * @dev Returns how much royalty is owed and to whom, based on a sale price that may be denominated in any unit of
         * exchange. The royalty amount is denominated and should be paid in that same unit of exchange.
         */
        function royaltyInfo(
            uint256 tokenId,
            uint256 salePrice
        ) external view returns (address receiver, uint256 royaltyAmount);
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.9.0) (proxy/utils/Initializable.sol)
    pragma solidity ^0.8.2;
    import "../../utils/AddressUpgradeable.sol";
    /**
     * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
     * behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
     * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
     * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
     *
     * The initialization functions use a version number. Once a version number is used, it is consumed and cannot be
     * reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in
     * case an upgrade adds a module that needs to be initialized.
     *
     * For example:
     *
     * [.hljs-theme-light.nopadding]
     * ```solidity
     * contract MyToken is ERC20Upgradeable {
     *     function initialize() initializer public {
     *         __ERC20_init("MyToken", "MTK");
     *     }
     * }
     *
     * contract MyTokenV2 is MyToken, ERC20PermitUpgradeable {
     *     function initializeV2() reinitializer(2) public {
     *         __ERC20Permit_init("MyToken");
     *     }
     * }
     * ```
     *
     * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
     * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
     *
     * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
     * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
     *
     * [CAUTION]
     * ====
     * Avoid leaving a contract uninitialized.
     *
     * An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
     * contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke
     * the {_disableInitializers} function in the constructor to automatically lock it when it is deployed:
     *
     * [.hljs-theme-light.nopadding]
     * ```
     * /// @custom:oz-upgrades-unsafe-allow constructor
     * constructor() {
     *     _disableInitializers();
     * }
     * ```
     * ====
     */
    abstract contract Initializable {
        /**
         * @dev Indicates that the contract has been initialized.
         * @custom:oz-retyped-from bool
         */
        uint8 private _initialized;
        /**
         * @dev Indicates that the contract is in the process of being initialized.
         */
        bool private _initializing;
        /**
         * @dev Triggered when the contract has been initialized or reinitialized.
         */
        event Initialized(uint8 version);
        /**
         * @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope,
         * `onlyInitializing` functions can be used to initialize parent contracts.
         *
         * Similar to `reinitializer(1)`, except that functions marked with `initializer` can be nested in the context of a
         * constructor.
         *
         * Emits an {Initialized} event.
         */
        modifier initializer() {
            bool isTopLevelCall = !_initializing;
            require(
                (isTopLevelCall && _initialized < 1) || (!AddressUpgradeable.isContract(address(this)) && _initialized == 1),
                "Initializable: contract is already initialized"
            );
            _initialized = 1;
            if (isTopLevelCall) {
                _initializing = true;
            }
            _;
            if (isTopLevelCall) {
                _initializing = false;
                emit Initialized(1);
            }
        }
        /**
         * @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the
         * contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be
         * used to initialize parent contracts.
         *
         * A reinitializer may be used after the original initialization step. This is essential to configure modules that
         * are added through upgrades and that require initialization.
         *
         * When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer`
         * cannot be nested. If one is invoked in the context of another, execution will revert.
         *
         * Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in
         * a contract, executing them in the right order is up to the developer or operator.
         *
         * WARNING: setting the version to 255 will prevent any future reinitialization.
         *
         * Emits an {Initialized} event.
         */
        modifier reinitializer(uint8 version) {
            require(!_initializing && _initialized < version, "Initializable: contract is already initialized");
            _initialized = version;
            _initializing = true;
            _;
            _initializing = false;
            emit Initialized(version);
        }
        /**
         * @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
         * {initializer} and {reinitializer} modifiers, directly or indirectly.
         */
        modifier onlyInitializing() {
            require(_initializing, "Initializable: contract is not initializing");
            _;
        }
        /**
         * @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call.
         * Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized
         * to any version. It is recommended to use this to lock implementation contracts that are designed to be called
         * through proxies.
         *
         * Emits an {Initialized} event the first time it is successfully executed.
         */
        function _disableInitializers() internal virtual {
            require(!_initializing, "Initializable: contract is initializing");
            if (_initialized != type(uint8).max) {
                _initialized = type(uint8).max;
                emit Initialized(type(uint8).max);
            }
        }
        /**
         * @dev Returns the highest version that has been initialized. See {reinitializer}.
         */
        function _getInitializedVersion() internal view returns (uint8) {
            return _initialized;
        }
        /**
         * @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}.
         */
        function _isInitializing() internal view returns (bool) {
            return _initializing;
        }
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.9.0) (utils/Address.sol)
    pragma solidity ^0.8.1;
    /**
     * @dev Collection of functions related to the address type
     */
    library AddressUpgradeable {
        /**
         * @dev Returns true if `account` is a contract.
         *
         * [IMPORTANT]
         * ====
         * It is unsafe to assume that an address for which this function returns
         * false is an externally-owned account (EOA) and not a contract.
         *
         * Among others, `isContract` will return false for the following
         * types of addresses:
         *
         *  - an externally-owned account
         *  - a contract in construction
         *  - an address where a contract will be created
         *  - an address where a contract lived, but was destroyed
         *
         * Furthermore, `isContract` will also return true if the target contract within
         * the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
         * which only has an effect at the end of a transaction.
         * ====
         *
         * [IMPORTANT]
         * ====
         * You shouldn't rely on `isContract` to protect against flash loan attacks!
         *
         * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
         * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
         * constructor.
         * ====
         */
        function isContract(address account) internal view returns (bool) {
            // This method relies on extcodesize/address.code.length, which returns 0
            // for contracts in construction, since the code is only stored at the end
            // of the constructor execution.
            return account.code.length > 0;
        }
        /**
         * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
         * `recipient`, forwarding all available gas and reverting on errors.
         *
         * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
         * of certain opcodes, possibly making contracts go over the 2300 gas limit
         * imposed by `transfer`, making them unable to receive funds via
         * `transfer`. {sendValue} removes this limitation.
         *
         * https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
         *
         * IMPORTANT: because control is transferred to `recipient`, care must be
         * taken to not create reentrancy vulnerabilities. Consider using
         * {ReentrancyGuard} or the
         * https://solidity.readthedocs.io/en/v0.8.0/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
         */
        function sendValue(address payable recipient, uint256 amount) internal {
            require(address(this).balance >= amount, "Address: insufficient balance");
            (bool success, ) = recipient.call{value: amount}("");
            require(success, "Address: unable to send value, recipient may have reverted");
        }
        /**
         * @dev Performs a Solidity function call using a low level `call`. A
         * plain `call` is an unsafe replacement for a function call: use this
         * function instead.
         *
         * If `target` reverts with a revert reason, it is bubbled up by this
         * function (like regular Solidity function calls).
         *
         * Returns the raw returned data. To convert to the expected return value,
         * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
         *
         * Requirements:
         *
         * - `target` must be a contract.
         * - calling `target` with `data` must not revert.
         *
         * _Available since v3.1._
         */
        function functionCall(address target, bytes memory data) internal returns (bytes memory) {
            return functionCallWithValue(target, data, 0, "Address: low-level call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
         * `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal returns (bytes memory) {
            return functionCallWithValue(target, data, 0, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but also transferring `value` wei to `target`.
         *
         * Requirements:
         *
         * - the calling contract must have an ETH balance of at least `value`.
         * - the called Solidity function must be `payable`.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
            return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
        }
        /**
         * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
         * with `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(
            address target,
            bytes memory data,
            uint256 value,
            string memory errorMessage
        ) internal returns (bytes memory) {
            require(address(this).balance >= value, "Address: insufficient balance for call");
            (bool success, bytes memory returndata) = target.call{value: value}(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
            return functionStaticCall(target, data, "Address: low-level static call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal view returns (bytes memory) {
            (bool success, bytes memory returndata) = target.staticcall(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but performing a delegate call.
         *
         * _Available since v3.4._
         */
        function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
            return functionDelegateCall(target, data, "Address: low-level delegate call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
         * but performing a delegate call.
         *
         * _Available since v3.4._
         */
        function functionDelegateCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal returns (bytes memory) {
            (bool success, bytes memory returndata) = target.delegatecall(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
         * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
         *
         * _Available since v4.8._
         */
        function verifyCallResultFromTarget(
            address target,
            bool success,
            bytes memory returndata,
            string memory errorMessage
        ) internal view returns (bytes memory) {
            if (success) {
                if (returndata.length == 0) {
                    // only check isContract if the call was successful and the return data is empty
                    // otherwise we already know that it was a contract
                    require(isContract(target), "Address: call to non-contract");
                }
                return returndata;
            } else {
                _revert(returndata, errorMessage);
            }
        }
        /**
         * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
         * revert reason or using the provided one.
         *
         * _Available since v4.3._
         */
        function verifyCallResult(
            bool success,
            bytes memory returndata,
            string memory errorMessage
        ) internal pure returns (bytes memory) {
            if (success) {
                return returndata;
            } else {
                _revert(returndata, errorMessage);
            }
        }
        function _revert(bytes memory returndata, string memory errorMessage) private pure {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly
                /// @solidity memory-safe-assembly
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts v4.4.1 (utils/Context.sol)
    pragma solidity ^0.8.0;
    import "../proxy/utils/Initializable.sol";
    /**
     * @dev Provides information about the current execution context, including the
     * sender of the transaction and its data. While these are generally available
     * via msg.sender and msg.data, they should not be accessed in such a direct
     * manner, since when dealing with meta-transactions the account sending and
     * paying for execution may not be the actual sender (as far as an application
     * is concerned).
     *
     * This contract is only required for intermediate, library-like contracts.
     */
    abstract contract ContextUpgradeable is Initializable {
        function __Context_init() internal onlyInitializing {
        }
        function __Context_init_unchained() internal onlyInitializing {
        }
        function _msgSender() internal view virtual returns (address) {
            return msg.sender;
        }
        function _msgData() internal view virtual returns (bytes calldata) {
            return msg.data;
        }
        /**
         * @dev This empty reserved space is put in place to allow future versions to add new
         * variables without shifting down storage in the inheritance chain.
         * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
         */
        uint256[50] private __gap;
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.9.0) (utils/Strings.sol)
    pragma solidity ^0.8.0;
    import "./math/MathUpgradeable.sol";
    import "./math/SignedMathUpgradeable.sol";
    /**
     * @dev String operations.
     */
    library StringsUpgradeable {
        bytes16 private constant _SYMBOLS = "0123456789abcdef";
        uint8 private constant _ADDRESS_LENGTH = 20;
        /**
         * @dev Converts a `uint256` to its ASCII `string` decimal representation.
         */
        function toString(uint256 value) internal pure returns (string memory) {
            unchecked {
                uint256 length = MathUpgradeable.log10(value) + 1;
                string memory buffer = new string(length);
                uint256 ptr;
                /// @solidity memory-safe-assembly
                assembly {
                    ptr := add(buffer, add(32, length))
                }
                while (true) {
                    ptr--;
                    /// @solidity memory-safe-assembly
                    assembly {
                        mstore8(ptr, byte(mod(value, 10), _SYMBOLS))
                    }
                    value /= 10;
                    if (value == 0) break;
                }
                return buffer;
            }
        }
        /**
         * @dev Converts a `int256` to its ASCII `string` decimal representation.
         */
        function toString(int256 value) internal pure returns (string memory) {
            return string(abi.encodePacked(value < 0 ? "-" : "", toString(SignedMathUpgradeable.abs(value))));
        }
        /**
         * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
         */
        function toHexString(uint256 value) internal pure returns (string memory) {
            unchecked {
                return toHexString(value, MathUpgradeable.log256(value) + 1);
            }
        }
        /**
         * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
         */
        function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
            bytes memory buffer = new bytes(2 * length + 2);
            buffer[0] = "0";
            buffer[1] = "x";
            for (uint256 i = 2 * length + 1; i > 1; --i) {
                buffer[i] = _SYMBOLS[value & 0xf];
                value >>= 4;
            }
            require(value == 0, "Strings: hex length insufficient");
            return string(buffer);
        }
        /**
         * @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal representation.
         */
        function toHexString(address addr) internal pure returns (string memory) {
            return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
        }
        /**
         * @dev Returns true if the two strings are equal.
         */
        function equal(string memory a, string memory b) internal pure returns (bool) {
            return keccak256(bytes(a)) == keccak256(bytes(b));
        }
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)
    pragma solidity ^0.8.0;
    /**
     * @dev Interface of the ERC165 standard, as defined in the
     * https://eips.ethereum.org/EIPS/eip-165[EIP].
     *
     * Implementers can declare support of contract interfaces, which can then be
     * queried by others ({ERC165Checker}).
     *
     * For an implementation, see {ERC165}.
     */
    interface IERC165Upgradeable {
        /**
         * @dev Returns true if this contract implements the interface defined by
         * `interfaceId`. See the corresponding
         * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
         * to learn more about how these ids are created.
         *
         * This function call must use less than 30 000 gas.
         */
        function supportsInterface(bytes4 interfaceId) external view returns (bool);
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.9.0) (utils/math/Math.sol)
    pragma solidity ^0.8.0;
    /**
     * @dev Standard math utilities missing in the Solidity language.
     */
    library MathUpgradeable {
        enum Rounding {
            Down, // Toward negative infinity
            Up, // Toward infinity
            Zero // Toward zero
        }
        /**
         * @dev Returns the largest of two numbers.
         */
        function max(uint256 a, uint256 b) internal pure returns (uint256) {
            return a > b ? a : b;
        }
        /**
         * @dev Returns the smallest of two numbers.
         */
        function min(uint256 a, uint256 b) internal pure returns (uint256) {
            return a < b ? a : b;
        }
        /**
         * @dev Returns the average of two numbers. The result is rounded towards
         * zero.
         */
        function average(uint256 a, uint256 b) internal pure returns (uint256) {
            // (a + b) / 2 can overflow.
            return (a & b) + (a ^ b) / 2;
        }
        /**
         * @dev Returns the ceiling of the division of two numbers.
         *
         * This differs from standard division with `/` in that it rounds up instead
         * of rounding down.
         */
        function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
            // (a + b - 1) / b can overflow on addition, so we distribute.
            return a == 0 ? 0 : (a - 1) / b + 1;
        }
        /**
         * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
         * @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
         * with further edits by Uniswap Labs also under MIT license.
         */
        function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
            unchecked {
                // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
                // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
                // variables such that product = prod1 * 2^256 + prod0.
                uint256 prod0; // Least significant 256 bits of the product
                uint256 prod1; // Most significant 256 bits of the product
                assembly {
                    let mm := mulmod(x, y, not(0))
                    prod0 := mul(x, y)
                    prod1 := sub(sub(mm, prod0), lt(mm, prod0))
                }
                // Handle non-overflow cases, 256 by 256 division.
                if (prod1 == 0) {
                    // Solidity will revert if denominator == 0, unlike the div opcode on its own.
                    // The surrounding unchecked block does not change this fact.
                    // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
                    return prod0 / denominator;
                }
                // Make sure the result is less than 2^256. Also prevents denominator == 0.
                require(denominator > prod1, "Math: mulDiv overflow");
                ///////////////////////////////////////////////
                // 512 by 256 division.
                ///////////////////////////////////////////////
                // Make division exact by subtracting the remainder from [prod1 prod0].
                uint256 remainder;
                assembly {
                    // Compute remainder using mulmod.
                    remainder := mulmod(x, y, denominator)
                    // Subtract 256 bit number from 512 bit number.
                    prod1 := sub(prod1, gt(remainder, prod0))
                    prod0 := sub(prod0, remainder)
                }
                // Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
                // See https://cs.stackexchange.com/q/138556/92363.
                // Does not overflow because the denominator cannot be zero at this stage in the function.
                uint256 twos = denominator & (~denominator + 1);
                assembly {
                    // Divide denominator by twos.
                    denominator := div(denominator, twos)
                    // Divide [prod1 prod0] by twos.
                    prod0 := div(prod0, twos)
                    // Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
                    twos := add(div(sub(0, twos), twos), 1)
                }
                // Shift in bits from prod1 into prod0.
                prod0 |= prod1 * twos;
                // Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
                // that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
                // four bits. That is, denominator * inv = 1 mod 2^4.
                uint256 inverse = (3 * denominator) ^ 2;
                // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
                // in modular arithmetic, doubling the correct bits in each step.
                inverse *= 2 - denominator * inverse; // inverse mod 2^8
                inverse *= 2 - denominator * inverse; // inverse mod 2^16
                inverse *= 2 - denominator * inverse; // inverse mod 2^32
                inverse *= 2 - denominator * inverse; // inverse mod 2^64
                inverse *= 2 - denominator * inverse; // inverse mod 2^128
                inverse *= 2 - denominator * inverse; // inverse mod 2^256
                // Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
                // This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
                // less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
                // is no longer required.
                result = prod0 * inverse;
                return result;
            }
        }
        /**
         * @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
         */
        function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
            uint256 result = mulDiv(x, y, denominator);
            if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
                result += 1;
            }
            return result;
        }
        /**
         * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
         *
         * Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
         */
        function sqrt(uint256 a) internal pure returns (uint256) {
            if (a == 0) {
                return 0;
            }
            // For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
            //
            // We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
            // `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
            //
            // This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
            // → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
            // → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
            //
            // Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
            uint256 result = 1 << (log2(a) >> 1);
            // At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
            // since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
            // every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
            // into the expected uint128 result.
            unchecked {
                result = (result + a / result) >> 1;
                result = (result + a / result) >> 1;
                result = (result + a / result) >> 1;
                result = (result + a / result) >> 1;
                result = (result + a / result) >> 1;
                result = (result + a / result) >> 1;
                result = (result + a / result) >> 1;
                return min(result, a / result);
            }
        }
        /**
         * @notice Calculates sqrt(a), following the selected rounding direction.
         */
        function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
            unchecked {
                uint256 result = sqrt(a);
                return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
            }
        }
        /**
         * @dev Return the log in base 2, rounded down, of a positive value.
         * Returns 0 if given 0.
         */
        function log2(uint256 value) internal pure returns (uint256) {
            uint256 result = 0;
            unchecked {
                if (value >> 128 > 0) {
                    value >>= 128;
                    result += 128;
                }
                if (value >> 64 > 0) {
                    value >>= 64;
                    result += 64;
                }
                if (value >> 32 > 0) {
                    value >>= 32;
                    result += 32;
                }
                if (value >> 16 > 0) {
                    value >>= 16;
                    result += 16;
                }
                if (value >> 8 > 0) {
                    value >>= 8;
                    result += 8;
                }
                if (value >> 4 > 0) {
                    value >>= 4;
                    result += 4;
                }
                if (value >> 2 > 0) {
                    value >>= 2;
                    result += 2;
                }
                if (value >> 1 > 0) {
                    result += 1;
                }
            }
            return result;
        }
        /**
         * @dev Return the log in base 2, following the selected rounding direction, of a positive value.
         * Returns 0 if given 0.
         */
        function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
            unchecked {
                uint256 result = log2(value);
                return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
            }
        }
        /**
         * @dev Return the log in base 10, rounded down, of a positive value.
         * Returns 0 if given 0.
         */
        function log10(uint256 value) internal pure returns (uint256) {
            uint256 result = 0;
            unchecked {
                if (value >= 10 ** 64) {
                    value /= 10 ** 64;
                    result += 64;
                }
                if (value >= 10 ** 32) {
                    value /= 10 ** 32;
                    result += 32;
                }
                if (value >= 10 ** 16) {
                    value /= 10 ** 16;
                    result += 16;
                }
                if (value >= 10 ** 8) {
                    value /= 10 ** 8;
                    result += 8;
                }
                if (value >= 10 ** 4) {
                    value /= 10 ** 4;
                    result += 4;
                }
                if (value >= 10 ** 2) {
                    value /= 10 ** 2;
                    result += 2;
                }
                if (value >= 10 ** 1) {
                    result += 1;
                }
            }
            return result;
        }
        /**
         * @dev Return the log in base 10, following the selected rounding direction, of a positive value.
         * Returns 0 if given 0.
         */
        function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
            unchecked {
                uint256 result = log10(value);
                return result + (rounding == Rounding.Up && 10 ** result < value ? 1 : 0);
            }
        }
        /**
         * @dev Return the log in base 256, rounded down, of a positive value.
         * Returns 0 if given 0.
         *
         * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
         */
        function log256(uint256 value) internal pure returns (uint256) {
            uint256 result = 0;
            unchecked {
                if (value >> 128 > 0) {
                    value >>= 128;
                    result += 16;
                }
                if (value >> 64 > 0) {
                    value >>= 64;
                    result += 8;
                }
                if (value >> 32 > 0) {
                    value >>= 32;
                    result += 4;
                }
                if (value >> 16 > 0) {
                    value >>= 16;
                    result += 2;
                }
                if (value >> 8 > 0) {
                    result += 1;
                }
            }
            return result;
        }
        /**
         * @dev Return the log in base 256, following the selected rounding direction, of a positive value.
         * Returns 0 if given 0.
         */
        function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
            unchecked {
                uint256 result = log256(value);
                return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0);
            }
        }
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.8.0) (utils/math/SignedMath.sol)
    pragma solidity ^0.8.0;
    /**
     * @dev Standard signed math utilities missing in the Solidity language.
     */
    library SignedMathUpgradeable {
        /**
         * @dev Returns the largest of two signed numbers.
         */
        function max(int256 a, int256 b) internal pure returns (int256) {
            return a > b ? a : b;
        }
        /**
         * @dev Returns the smallest of two signed numbers.
         */
        function min(int256 a, int256 b) internal pure returns (int256) {
            return a < b ? a : b;
        }
        /**
         * @dev Returns the average of two signed numbers without overflow.
         * The result is rounded towards zero.
         */
        function average(int256 a, int256 b) internal pure returns (int256) {
            // Formula from the book "Hacker's Delight"
            int256 x = (a & b) + ((a ^ b) >> 1);
            return x + (int256(uint256(x) >> 255) & (a ^ b));
        }
        /**
         * @dev Returns the absolute unsigned value of a signed value.
         */
        function abs(int256 n) internal pure returns (uint256) {
            unchecked {
                // must be unchecked in order to support `n = type(int256).min`
                return uint256(n >= 0 ? n : -n);
            }
        }
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.9.0) (utils/Address.sol)
    pragma solidity ^0.8.1;
    /**
     * @dev Collection of functions related to the address type
     */
    library Address {
        /**
         * @dev Returns true if `account` is a contract.
         *
         * [IMPORTANT]
         * ====
         * It is unsafe to assume that an address for which this function returns
         * false is an externally-owned account (EOA) and not a contract.
         *
         * Among others, `isContract` will return false for the following
         * types of addresses:
         *
         *  - an externally-owned account
         *  - a contract in construction
         *  - an address where a contract will be created
         *  - an address where a contract lived, but was destroyed
         *
         * Furthermore, `isContract` will also return true if the target contract within
         * the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
         * which only has an effect at the end of a transaction.
         * ====
         *
         * [IMPORTANT]
         * ====
         * You shouldn't rely on `isContract` to protect against flash loan attacks!
         *
         * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
         * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
         * constructor.
         * ====
         */
        function isContract(address account) internal view returns (bool) {
            // This method relies on extcodesize/address.code.length, which returns 0
            // for contracts in construction, since the code is only stored at the end
            // of the constructor execution.
            return account.code.length > 0;
        }
        /**
         * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
         * `recipient`, forwarding all available gas and reverting on errors.
         *
         * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
         * of certain opcodes, possibly making contracts go over the 2300 gas limit
         * imposed by `transfer`, making them unable to receive funds via
         * `transfer`. {sendValue} removes this limitation.
         *
         * https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
         *
         * IMPORTANT: because control is transferred to `recipient`, care must be
         * taken to not create reentrancy vulnerabilities. Consider using
         * {ReentrancyGuard} or the
         * https://solidity.readthedocs.io/en/v0.8.0/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
         */
        function sendValue(address payable recipient, uint256 amount) internal {
            require(address(this).balance >= amount, "Address: insufficient balance");
            (bool success, ) = recipient.call{value: amount}("");
            require(success, "Address: unable to send value, recipient may have reverted");
        }
        /**
         * @dev Performs a Solidity function call using a low level `call`. A
         * plain `call` is an unsafe replacement for a function call: use this
         * function instead.
         *
         * If `target` reverts with a revert reason, it is bubbled up by this
         * function (like regular Solidity function calls).
         *
         * Returns the raw returned data. To convert to the expected return value,
         * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
         *
         * Requirements:
         *
         * - `target` must be a contract.
         * - calling `target` with `data` must not revert.
         *
         * _Available since v3.1._
         */
        function functionCall(address target, bytes memory data) internal returns (bytes memory) {
            return functionCallWithValue(target, data, 0, "Address: low-level call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
         * `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal returns (bytes memory) {
            return functionCallWithValue(target, data, 0, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but also transferring `value` wei to `target`.
         *
         * Requirements:
         *
         * - the calling contract must have an ETH balance of at least `value`.
         * - the called Solidity function must be `payable`.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
            return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
        }
        /**
         * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
         * with `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(
            address target,
            bytes memory data,
            uint256 value,
            string memory errorMessage
        ) internal returns (bytes memory) {
            require(address(this).balance >= value, "Address: insufficient balance for call");
            (bool success, bytes memory returndata) = target.call{value: value}(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
            return functionStaticCall(target, data, "Address: low-level static call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal view returns (bytes memory) {
            (bool success, bytes memory returndata) = target.staticcall(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but performing a delegate call.
         *
         * _Available since v3.4._
         */
        function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
            return functionDelegateCall(target, data, "Address: low-level delegate call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
         * but performing a delegate call.
         *
         * _Available since v3.4._
         */
        function functionDelegateCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal returns (bytes memory) {
            (bool success, bytes memory returndata) = target.delegatecall(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
         * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
         *
         * _Available since v4.8._
         */
        function verifyCallResultFromTarget(
            address target,
            bool success,
            bytes memory returndata,
            string memory errorMessage
        ) internal view returns (bytes memory) {
            if (success) {
                if (returndata.length == 0) {
                    // only check isContract if the call was successful and the return data is empty
                    // otherwise we already know that it was a contract
                    require(isContract(target), "Address: call to non-contract");
                }
                return returndata;
            } else {
                _revert(returndata, errorMessage);
            }
        }
        /**
         * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
         * revert reason or using the provided one.
         *
         * _Available since v4.3._
         */
        function verifyCallResult(
            bool success,
            bytes memory returndata,
            string memory errorMessage
        ) internal pure returns (bytes memory) {
            if (success) {
                return returndata;
            } else {
                _revert(returndata, errorMessage);
            }
        }
        function _revert(bytes memory returndata, string memory errorMessage) private pure {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly
                /// @solidity memory-safe-assembly
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
    

    File 2 of 4: OpenEditionERC721FlatFee
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)
    pragma solidity ^0.8.0;
    /**
     * @dev Interface of the ERC165 standard, as defined in the
     * [EIP](https://eips.ethereum.org/EIPS/eip-165).
     *
     * Implementers can declare support of contract interfaces, which can then be
     * queried by others ({ERC165Checker}).
     *
     * For an implementation, see {ERC165}.
     */
    interface IERC165 {
        /**
         * @dev Returns true if this contract implements the interface defined by
         * `interfaceId`. See the corresponding
         * [EIP section](https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified)
         * to learn more about how these ids are created.
         *
         * This function call must use less than 30 000 gas.
         */
        function supportsInterface(bytes4 interfaceId) external view returns (bool);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /**
     * @title ERC20 interface
     * @dev see https://github.com/ethereum/EIPs/issues/20
     */
    interface IERC20 {
        function totalSupply() external view returns (uint256);
        function balanceOf(address who) external view returns (uint256);
        function allowance(address owner, address spender) external view returns (uint256);
        function transfer(address to, uint256 value) external returns (bool);
        function approve(address spender, uint256 value) external returns (bool);
        function transferFrom(address from, address to, uint256 value) external returns (bool);
        event Transfer(address indexed from, address indexed to, uint256 value);
        event Approval(address indexed owner, address indexed spender, uint256 value);
    }
    // SPDX-License-Identifier: Apache 2.0
    pragma solidity ^0.8.0;
    import "./IERC165.sol";
    /**
     * @dev Interface for the NFT Royalty Standard.
     *
     * A standardized way to retrieve royalty payment information for non-fungible tokens (NFTs) to enable universal
     * support for royalty payments across all NFT marketplaces and ecosystem participants.
     *
     * _Available since v4.5._
     */
    interface IERC2981 is IERC165 {
        /**
         * @dev Returns how much royalty is owed and to whom, based on a sale price that may be denominated in any unit of
         * exchange. The royalty amount is denominated and should be payed in that same unit of exchange.
         */
        function royaltyInfo(
            uint256 tokenId,
            uint256 salePrice
        ) external view returns (address receiver, uint256 royaltyAmount);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.11;
    import "./IERC165.sol";
    import "./IERC721.sol";
    interface IERC4906 is IERC165 {
        /// @dev This event emits when the metadata of a token is changed.
        /// So that the third-party platforms such as NFT market could
        /// timely update the images and related attributes of the NFT.
        event MetadataUpdate(uint256 _tokenId);
        /// @dev This event emits when the metadata of a range of tokens is changed.
        /// So that the third-party platforms such as NFT market could
        /// timely update the images and related attributes of the NFTs.
        event BatchMetadataUpdate(uint256 _fromTokenId, uint256 _toTokenId);
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts v4.4.1 (token/ERC721/IERC721.sol)
    pragma solidity ^0.8.0;
    /**
     * @dev Required interface of an ERC721 compliant contract.
     */
    interface IERC721 {
        /**
         * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
         */
        event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
        /**
         * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
         */
        event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
        /**
         * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
         */
        event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
        /**
         * @dev Returns the number of tokens in ``owner``'s account.
         */
        function balanceOf(address owner) external view returns (uint256);
        /**
         * @dev Returns the owner of the `tokenId` token.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         */
        function ownerOf(uint256 tokenId) external view returns (address);
        /**
         * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
         * are aware of the ERC721 protocol to prevent tokens from being forever locked.
         *
         * Requirements:
         *
         * - `from` cannot be the zero address.
         * - `to` cannot be the zero address.
         * - `tokenId` token must exist and be owned by `from`.
         * - If the caller is not `from`, it must be have been allowed to move this token by either {approve} or {setApprovalForAll}.
         * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
         *
         * Emits a {Transfer} event.
         */
        function safeTransferFrom(address from, address to, uint256 tokenId) external;
        /**
         * @dev Transfers `tokenId` token from `from` to `to`.
         *
         * WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
         *
         * Requirements:
         *
         * - `from` cannot be the zero address.
         * - `to` cannot be the zero address.
         * - `tokenId` token must be owned by `from`.
         * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
         *
         * Emits a {Transfer} event.
         */
        function transferFrom(address from, address to, uint256 tokenId) external;
        /**
         * @dev Gives permission to `to` to transfer `tokenId` token to another account.
         * The approval is cleared when the token is transferred.
         *
         * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
         *
         * Requirements:
         *
         * - The caller must own the token or be an approved operator.
         * - `tokenId` must exist.
         *
         * Emits an {Approval} event.
         */
        function approve(address to, uint256 tokenId) external;
        /**
         * @dev Returns the account approved for `tokenId` token.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         */
        function getApproved(uint256 tokenId) external view returns (address);
        /**
         * @dev Approve or remove `operator` as an operator for the caller.
         * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
         *
         * Requirements:
         *
         * - The `operator` cannot be the caller.
         *
         * Emits an {ApprovalForAll} event.
         */
        function setApprovalForAll(address operator, bool _approved) external;
        /**
         * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
         *
         * See {setApprovalForAll}
         */
        function isApprovedForAll(address owner, address operator) external view returns (bool);
        /**
         * @dev Safely transfers `tokenId` token from `from` to `to`.
         *
         * Requirements:
         *
         * - `from` cannot be the zero address.
         * - `to` cannot be the zero address.
         * - `tokenId` token must exist and be owned by `from`.
         * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
         * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
         *
         * Emits a {Transfer} event.
         */
        function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
    }
    // SPDX-License-Identifier: MIT
    // ERC721A Contracts v4.2.3
    // Creator: Chiru Labs
    pragma solidity ^0.8.4;
    import "./IERC721AQueryableUpgradeable.sol";
    import "./ERC721AUpgradeable.sol";
    import "./ERC721A__Initializable.sol";
    /**
     * @title ERC721AQueryable.
     *
     * @dev ERC721A subclass with convenience query functions.
     */
    abstract contract ERC721AQueryableUpgradeable is
        ERC721A__Initializable,
        ERC721AUpgradeable,
        IERC721AQueryableUpgradeable
    {
        function __ERC721AQueryable_init() internal onlyInitializingERC721A {
            __ERC721AQueryable_init_unchained();
        }
        function __ERC721AQueryable_init_unchained() internal onlyInitializingERC721A {}
        /**
         * @dev Returns the `TokenOwnership` struct at `tokenId` without reverting.
         *
         * If the `tokenId` is out of bounds:
         *
         * - `addr = address(0)`
         * - `startTimestamp = 0`
         * - `burned = false`
         * - `extraData = 0`
         *
         * If the `tokenId` is burned:
         *
         * - `addr = <Address of owner before token was burned>`
         * - `startTimestamp = <Timestamp when token was burned>`
         * - `burned = true`
         * - `extraData = <Extra data when token was burned>`
         *
         * Otherwise:
         *
         * - `addr = <Address of owner>`
         * - `startTimestamp = <Timestamp of start of ownership>`
         * - `burned = false`
         * - `extraData = <Extra data at start of ownership>`
         */
        function explicitOwnershipOf(
            uint256 tokenId
        ) public view virtual override returns (TokenOwnership memory ownership) {
            unchecked {
                if (tokenId >= _startTokenId()) {
                    if (tokenId < _nextTokenId()) {
                        // If the `tokenId` is within bounds,
                        // scan backwards for the initialized ownership slot.
                        while (!_ownershipIsInitialized(tokenId)) --tokenId;
                        return _ownershipAt(tokenId);
                    }
                }
            }
        }
        /**
         * @dev Returns an array of token IDs owned by `owner`,
         * in the range [`start`, `stop`)
         * (i.e. `start <= tokenId < stop`).
         *
         * This function allows for tokens to be queried if the collection
         * grows too big for a single call of {ERC721AQueryable-tokensOfOwner}.
         *
         * Requirements:
         *
         * - `start < stop`
         */
        function tokensOfOwnerIn(
            address owner,
            uint256 start,
            uint256 stop
        ) external view virtual override returns (uint256[] memory) {
            return _tokensOfOwnerIn(owner, start, stop);
        }
        /**
         * @dev Returns an array of token IDs owned by `owner`.
         *
         * This function scans the ownership mapping and is O(`totalSupply`) in complexity.
         * It is meant to be called off-chain.
         *
         * See {ERC721AQueryable-tokensOfOwnerIn} for splitting the scan into
         * multiple smaller scans if the collection is large enough to cause
         * an out-of-gas error (10K collections should be fine).
         */
        function tokensOfOwner(address owner) external view virtual override returns (uint256[] memory) {
            uint256 start = _startTokenId();
            uint256 stop = _nextTokenId();
            uint256[] memory tokenIds;
            if (start != stop) tokenIds = _tokensOfOwnerIn(owner, start, stop);
            return tokenIds;
        }
        /**
         * @dev Helper function for returning an array of token IDs owned by `owner`.
         *
         * Note that this function is optimized for smaller bytecode size over runtime gas,
         * since it is meant to be called off-chain.
         */
        function _tokensOfOwnerIn(address owner, uint256 start, uint256 stop) private view returns (uint256[] memory) {
            unchecked {
                if (start >= stop) _revert(InvalidQueryRange.selector);
                // Set `start = max(start, _startTokenId())`.
                if (start < _startTokenId()) {
                    start = _startTokenId();
                }
                uint256 stopLimit = _nextTokenId();
                // Set `stop = min(stop, stopLimit)`.
                if (stop >= stopLimit) {
                    stop = stopLimit;
                }
                uint256[] memory tokenIds;
                uint256 tokenIdsMaxLength = balanceOf(owner);
                bool startLtStop = start < stop;
                assembly {
                    // Set `tokenIdsMaxLength` to zero if `start` is less than `stop`.
                    tokenIdsMaxLength := mul(tokenIdsMaxLength, startLtStop)
                }
                if (tokenIdsMaxLength != 0) {
                    // Set `tokenIdsMaxLength = min(balanceOf(owner), stop - start)`,
                    // to cater for cases where `balanceOf(owner)` is too big.
                    if (stop - start <= tokenIdsMaxLength) {
                        tokenIdsMaxLength = stop - start;
                    }
                    assembly {
                        // Grab the free memory pointer.
                        tokenIds := mload(0x40)
                        // Allocate one word for the length, and `tokenIdsMaxLength` words
                        // for the data. `shl(5, x)` is equivalent to `mul(32, x)`.
                        mstore(0x40, add(tokenIds, shl(5, add(tokenIdsMaxLength, 1))))
                    }
                    // We need to call `explicitOwnershipOf(start)`,
                    // because the slot at `start` may not be initialized.
                    TokenOwnership memory ownership = explicitOwnershipOf(start);
                    address currOwnershipAddr;
                    // If the starting slot exists (i.e. not burned),
                    // initialize `currOwnershipAddr`.
                    // `ownership.address` will not be zero,
                    // as `start` is clamped to the valid token ID range.
                    if (!ownership.burned) {
                        currOwnershipAddr = ownership.addr;
                    }
                    uint256 tokenIdsIdx;
                    // Use a do-while, which is slightly more efficient for this case,
                    // as the array will at least contain one element.
                    do {
                        ownership = _ownershipAt(start);
                        assembly {
                            switch mload(add(ownership, 0x40))
                            // if `ownership.burned == false`.
                            case 0 {
                                // if `ownership.addr != address(0)`.
                                // The `addr` already has it's upper 96 bits clearned,
                                // since it is written to memory with regular Solidity.
                                if mload(ownership) {
                                    currOwnershipAddr := mload(ownership)
                                }
                                // if `currOwnershipAddr == owner`.
                                // The `shl(96, x)` is to make the comparison agnostic to any
                                // dirty upper 96 bits in `owner`.
                                if iszero(shl(96, xor(currOwnershipAddr, owner))) {
                                    tokenIdsIdx := add(tokenIdsIdx, 1)
                                    mstore(add(tokenIds, shl(5, tokenIdsIdx)), start)
                                }
                            }
                            // Otherwise, reset `currOwnershipAddr`.
                            // This handles the case of batch burned tokens
                            // (burned bit of first slot set, remaining slots left uninitialized).
                            default {
                                currOwnershipAddr := 0
                            }
                            start := add(start, 1)
                        }
                    } while (!(start == stop || tokenIdsIdx == tokenIdsMaxLength));
                    // Store the length of the array.
                    assembly {
                        mstore(tokenIds, tokenIdsIdx)
                    }
                }
                return tokenIds;
            }
        }
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    library ERC721AStorage {
        // Bypass for a `--via-ir` bug (https://github.com/chiru-labs/ERC721A/pull/364).
        struct TokenApprovalRef {
            address value;
        }
        struct Layout {
            // =============================================================
            //                            STORAGE
            // =============================================================
            // The next token ID to be minted.
            uint256 _currentIndex;
            // The number of tokens burned.
            uint256 _burnCounter;
            // Token name
            string _name;
            // Token symbol
            string _symbol;
            // Mapping from token ID to ownership details
            // An empty struct value does not necessarily mean the token is unowned.
            // See {_packedOwnershipOf} implementation for details.
            //
            // Bits Layout:
            // - [0..159]   `addr`
            // - [160..223] `startTimestamp`
            // - [224]      `burned`
            // - [225]      `nextInitialized`
            // - [232..255] `extraData`
            mapping(uint256 => uint256) _packedOwnerships;
            // Mapping owner address to address data.
            //
            // Bits Layout:
            // - [0..63]    `balance`
            // - [64..127]  `numberMinted`
            // - [128..191] `numberBurned`
            // - [192..255] `aux`
            mapping(address => uint256) _packedAddressData;
            // Mapping from token ID to approved address.
            mapping(uint256 => ERC721AStorage.TokenApprovalRef) _tokenApprovals;
            // Mapping from owner to operator approvals
            mapping(address => mapping(address => bool)) _operatorApprovals;
        }
        bytes32 internal constant STORAGE_SLOT = keccak256("ERC721A.contracts.storage.ERC721A");
        function layout() internal pure returns (Layout storage l) {
            bytes32 slot = STORAGE_SLOT;
            assembly {
                l.slot := slot
            }
        }
    }
    // SPDX-License-Identifier: MIT
    // ERC721A Contracts v4.2.3
    // Creator: Chiru Labs
    pragma solidity ^0.8.4;
    import "./IERC721AUpgradeable.sol";
    import { ERC721AStorage } from "./ERC721AStorage.sol";
    import "./ERC721A__Initializable.sol";
    /**
     * @dev Interface of ERC721 token receiver.
     */
    interface ERC721A__IERC721ReceiverUpgradeable {
        function onERC721Received(
            address operator,
            address from,
            uint256 tokenId,
            bytes calldata data
        ) external returns (bytes4);
    }
    /**
     * @title ERC721A
     *
     * @dev Implementation of the [ERC721](https://eips.ethereum.org/EIPS/eip-721)
     * Non-Fungible Token Standard, including the Metadata extension.
     * Optimized for lower gas during batch mints.
     *
     * Token IDs are minted in sequential order (e.g. 0, 1, 2, 3, ...)
     * starting from `_startTokenId()`.
     *
     * Assumptions:
     *
     * - An owner cannot have more than 2**64 - 1 (max value of uint64) of supply.
     * - The maximum token ID cannot exceed 2**256 - 1 (max value of uint256).
     */
    contract ERC721AUpgradeable is ERC721A__Initializable, IERC721AUpgradeable {
        using ERC721AStorage for ERC721AStorage.Layout;
        // =============================================================
        //                           CONSTANTS
        // =============================================================
        // Mask of an entry in packed address data.
        uint256 private constant _BITMASK_ADDRESS_DATA_ENTRY = (1 << 64) - 1;
        // The bit position of `numberMinted` in packed address data.
        uint256 private constant _BITPOS_NUMBER_MINTED = 64;
        // The bit position of `numberBurned` in packed address data.
        uint256 private constant _BITPOS_NUMBER_BURNED = 128;
        // The bit position of `aux` in packed address data.
        uint256 private constant _BITPOS_AUX = 192;
        // Mask of all 256 bits in packed address data except the 64 bits for `aux`.
        uint256 private constant _BITMASK_AUX_COMPLEMENT = (1 << 192) - 1;
        // The bit position of `startTimestamp` in packed ownership.
        uint256 private constant _BITPOS_START_TIMESTAMP = 160;
        // The bit mask of the `burned` bit in packed ownership.
        uint256 private constant _BITMASK_BURNED = 1 << 224;
        // The bit position of the `nextInitialized` bit in packed ownership.
        uint256 private constant _BITPOS_NEXT_INITIALIZED = 225;
        // The bit mask of the `nextInitialized` bit in packed ownership.
        uint256 private constant _BITMASK_NEXT_INITIALIZED = 1 << 225;
        // The bit position of `extraData` in packed ownership.
        uint256 private constant _BITPOS_EXTRA_DATA = 232;
        // Mask of all 256 bits in a packed ownership except the 24 bits for `extraData`.
        uint256 private constant _BITMASK_EXTRA_DATA_COMPLEMENT = (1 << 232) - 1;
        // The mask of the lower 160 bits for addresses.
        uint256 private constant _BITMASK_ADDRESS = (1 << 160) - 1;
        // The maximum `quantity` that can be minted with {_mintERC2309}.
        // This limit is to prevent overflows on the address data entries.
        // For a limit of 5000, a total of 3.689e15 calls to {_mintERC2309}
        // is required to cause an overflow, which is unrealistic.
        uint256 private constant _MAX_MINT_ERC2309_QUANTITY_LIMIT = 5000;
        // The `Transfer` event signature is given by:
        // `keccak256(bytes("Transfer(address,address,uint256)"))`.
        bytes32 private constant _TRANSFER_EVENT_SIGNATURE =
            0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef;
        // =============================================================
        //                          CONSTRUCTOR
        // =============================================================
        function __ERC721A_init(string memory name_, string memory symbol_) internal onlyInitializingERC721A {
            __ERC721A_init_unchained(name_, symbol_);
        }
        function __ERC721A_init_unchained(string memory name_, string memory symbol_) internal onlyInitializingERC721A {
            ERC721AStorage.layout()._name = name_;
            ERC721AStorage.layout()._symbol = symbol_;
            ERC721AStorage.layout()._currentIndex = _startTokenId();
        }
        // =============================================================
        //                   TOKEN COUNTING OPERATIONS
        // =============================================================
        /**
         * @dev Returns the starting token ID.
         * To change the starting token ID, please override this function.
         */
        function _startTokenId() internal view virtual returns (uint256) {
            return 0;
        }
        /**
         * @dev Returns the next token ID to be minted.
         */
        function _nextTokenId() internal view virtual returns (uint256) {
            return ERC721AStorage.layout()._currentIndex;
        }
        /**
         * @dev Returns the total number of tokens in existence.
         * Burned tokens will reduce the count.
         * To get the total number of tokens minted, please see {_totalMinted}.
         */
        function totalSupply() public view virtual override returns (uint256) {
            // Counter underflow is impossible as _burnCounter cannot be incremented
            // more than `_currentIndex - _startTokenId()` times.
            unchecked {
                return ERC721AStorage.layout()._currentIndex - ERC721AStorage.layout()._burnCounter - _startTokenId();
            }
        }
        /**
         * @dev Returns the total amount of tokens minted in the contract.
         */
        function _totalMinted() internal view virtual returns (uint256) {
            // Counter underflow is impossible as `_currentIndex` does not decrement,
            // and it is initialized to `_startTokenId()`.
            unchecked {
                return ERC721AStorage.layout()._currentIndex - _startTokenId();
            }
        }
        /**
         * @dev Returns the total number of tokens burned.
         */
        function _totalBurned() internal view virtual returns (uint256) {
            return ERC721AStorage.layout()._burnCounter;
        }
        // =============================================================
        //                    ADDRESS DATA OPERATIONS
        // =============================================================
        /**
         * @dev Returns the number of tokens in `owner`'s account.
         */
        function balanceOf(address owner) public view virtual override returns (uint256) {
            if (owner == address(0)) _revert(BalanceQueryForZeroAddress.selector);
            return ERC721AStorage.layout()._packedAddressData[owner] & _BITMASK_ADDRESS_DATA_ENTRY;
        }
        /**
         * Returns the number of tokens minted by `owner`.
         */
        function _numberMinted(address owner) internal view returns (uint256) {
            return
                (ERC721AStorage.layout()._packedAddressData[owner] >> _BITPOS_NUMBER_MINTED) & _BITMASK_ADDRESS_DATA_ENTRY;
        }
        /**
         * Returns the number of tokens burned by or on behalf of `owner`.
         */
        function _numberBurned(address owner) internal view returns (uint256) {
            return
                (ERC721AStorage.layout()._packedAddressData[owner] >> _BITPOS_NUMBER_BURNED) & _BITMASK_ADDRESS_DATA_ENTRY;
        }
        /**
         * Returns the auxiliary data for `owner`. (e.g. number of whitelist mint slots used).
         */
        function _getAux(address owner) internal view returns (uint64) {
            return uint64(ERC721AStorage.layout()._packedAddressData[owner] >> _BITPOS_AUX);
        }
        /**
         * Sets the auxiliary data for `owner`. (e.g. number of whitelist mint slots used).
         * If there are multiple variables, please pack them into a uint64.
         */
        function _setAux(address owner, uint64 aux) internal virtual {
            uint256 packed = ERC721AStorage.layout()._packedAddressData[owner];
            uint256 auxCasted;
            // Cast `aux` with assembly to avoid redundant masking.
            assembly {
                auxCasted := aux
            }
            packed = (packed & _BITMASK_AUX_COMPLEMENT) | (auxCasted << _BITPOS_AUX);
            ERC721AStorage.layout()._packedAddressData[owner] = packed;
        }
        // =============================================================
        //                            IERC165
        // =============================================================
        /**
         * @dev Returns true if this contract implements the interface defined by
         * `interfaceId`. See the corresponding
         * [EIP section](https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified)
         * to learn more about how these ids are created.
         *
         * This function call must use less than 30000 gas.
         */
        function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
            // The interface IDs are constants representing the first 4 bytes
            // of the XOR of all function selectors in the interface.
            // See: [ERC165](https://eips.ethereum.org/EIPS/eip-165)
            // (e.g. `bytes4(i.functionA.selector ^ i.functionB.selector ^ ...)`)
            return
                interfaceId == 0x01ffc9a7 || // ERC165 interface ID for ERC165.
                interfaceId == 0x80ac58cd || // ERC165 interface ID for ERC721.
                interfaceId == 0x5b5e139f; // ERC165 interface ID for ERC721Metadata.
        }
        // =============================================================
        //                        IERC721Metadata
        // =============================================================
        /**
         * @dev Returns the token collection name.
         */
        function name() public view virtual override returns (string memory) {
            return ERC721AStorage.layout()._name;
        }
        /**
         * @dev Returns the token collection symbol.
         */
        function symbol() public view virtual override returns (string memory) {
            return ERC721AStorage.layout()._symbol;
        }
        /**
         * @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
         */
        function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
            if (!_exists(tokenId)) _revert(URIQueryForNonexistentToken.selector);
            string memory baseURI = _baseURI();
            return bytes(baseURI).length != 0 ? string(abi.encodePacked(baseURI, _toString(tokenId))) : "";
        }
        /**
         * @dev Base URI for computing {tokenURI}. If set, the resulting URI for each
         * token will be the concatenation of the `baseURI` and the `tokenId`. Empty
         * by default, it can be overridden in child contracts.
         */
        function _baseURI() internal view virtual returns (string memory) {
            return "";
        }
        // =============================================================
        //                     OWNERSHIPS OPERATIONS
        // =============================================================
        /**
         * @dev Returns the owner of the `tokenId` token.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         */
        function ownerOf(uint256 tokenId) public view virtual override returns (address) {
            return address(uint160(_packedOwnershipOf(tokenId)));
        }
        /**
         * @dev Gas spent here starts off proportional to the maximum mint batch size.
         * It gradually moves to O(1) as tokens get transferred around over time.
         */
        function _ownershipOf(uint256 tokenId) internal view virtual returns (TokenOwnership memory) {
            return _unpackedOwnership(_packedOwnershipOf(tokenId));
        }
        /**
         * @dev Returns the unpacked `TokenOwnership` struct at `index`.
         */
        function _ownershipAt(uint256 index) internal view virtual returns (TokenOwnership memory) {
            return _unpackedOwnership(ERC721AStorage.layout()._packedOwnerships[index]);
        }
        /**
         * @dev Returns whether the ownership slot at `index` is initialized.
         * An uninitialized slot does not necessarily mean that the slot has no owner.
         */
        function _ownershipIsInitialized(uint256 index) internal view virtual returns (bool) {
            return ERC721AStorage.layout()._packedOwnerships[index] != 0;
        }
        /**
         * @dev Initializes the ownership slot minted at `index` for efficiency purposes.
         */
        function _initializeOwnershipAt(uint256 index) internal virtual {
            if (ERC721AStorage.layout()._packedOwnerships[index] == 0) {
                ERC721AStorage.layout()._packedOwnerships[index] = _packedOwnershipOf(index);
            }
        }
        /**
         * Returns the packed ownership data of `tokenId`.
         */
        function _packedOwnershipOf(uint256 tokenId) private view returns (uint256 packed) {
            if (_startTokenId() <= tokenId) {
                packed = ERC721AStorage.layout()._packedOwnerships[tokenId];
                // If the data at the starting slot does not exist, start the scan.
                if (packed == 0) {
                    if (tokenId >= ERC721AStorage.layout()._currentIndex) _revert(OwnerQueryForNonexistentToken.selector);
                    // Invariant:
                    // There will always be an initialized ownership slot
                    // (i.e. `ownership.addr != address(0) && ownership.burned == false`)
                    // before an unintialized ownership slot
                    // (i.e. `ownership.addr == address(0) && ownership.burned == false`)
                    // Hence, `tokenId` will not underflow.
                    //
                    // We can directly compare the packed value.
                    // If the address is zero, packed will be zero.
                    for (;;) {
                        unchecked {
                            packed = ERC721AStorage.layout()._packedOwnerships[--tokenId];
                        }
                        if (packed == 0) continue;
                        if (packed & _BITMASK_BURNED == 0) return packed;
                        // Otherwise, the token is burned, and we must revert.
                        // This handles the case of batch burned tokens, where only the burned bit
                        // of the starting slot is set, and remaining slots are left uninitialized.
                        _revert(OwnerQueryForNonexistentToken.selector);
                    }
                }
                // Otherwise, the data exists and we can skip the scan.
                // This is possible because we have already achieved the target condition.
                // This saves 2143 gas on transfers of initialized tokens.
                // If the token is not burned, return `packed`. Otherwise, revert.
                if (packed & _BITMASK_BURNED == 0) return packed;
            }
            _revert(OwnerQueryForNonexistentToken.selector);
        }
        /**
         * @dev Returns the unpacked `TokenOwnership` struct from `packed`.
         */
        function _unpackedOwnership(uint256 packed) private pure returns (TokenOwnership memory ownership) {
            ownership.addr = address(uint160(packed));
            ownership.startTimestamp = uint64(packed >> _BITPOS_START_TIMESTAMP);
            ownership.burned = packed & _BITMASK_BURNED != 0;
            ownership.extraData = uint24(packed >> _BITPOS_EXTRA_DATA);
        }
        /**
         * @dev Packs ownership data into a single uint256.
         */
        function _packOwnershipData(address owner, uint256 flags) private view returns (uint256 result) {
            assembly {
                // Mask `owner` to the lower 160 bits, in case the upper bits somehow aren't clean.
                owner := and(owner, _BITMASK_ADDRESS)
                // `owner | (block.timestamp << _BITPOS_START_TIMESTAMP) | flags`.
                result := or(owner, or(shl(_BITPOS_START_TIMESTAMP, timestamp()), flags))
            }
        }
        /**
         * @dev Returns the `nextInitialized` flag set if `quantity` equals 1.
         */
        function _nextInitializedFlag(uint256 quantity) private pure returns (uint256 result) {
            // For branchless setting of the `nextInitialized` flag.
            assembly {
                // `(quantity == 1) << _BITPOS_NEXT_INITIALIZED`.
                result := shl(_BITPOS_NEXT_INITIALIZED, eq(quantity, 1))
            }
        }
        // =============================================================
        //                      APPROVAL OPERATIONS
        // =============================================================
        /**
         * @dev Gives permission to `to` to transfer `tokenId` token to another account. See {ERC721A-_approve}.
         *
         * Requirements:
         *
         * - The caller must own the token or be an approved operator.
         */
        function approve(address to, uint256 tokenId) public payable virtual override {
            _approve(to, tokenId, true);
        }
        /**
         * @dev Returns the account approved for `tokenId` token.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         */
        function getApproved(uint256 tokenId) public view virtual override returns (address) {
            if (!_exists(tokenId)) _revert(ApprovalQueryForNonexistentToken.selector);
            return ERC721AStorage.layout()._tokenApprovals[tokenId].value;
        }
        /**
         * @dev Approve or remove `operator` as an operator for the caller.
         * Operators can call {transferFrom} or {safeTransferFrom}
         * for any token owned by the caller.
         *
         * Requirements:
         *
         * - The `operator` cannot be the caller.
         *
         * Emits an {ApprovalForAll} event.
         */
        function setApprovalForAll(address operator, bool approved) public virtual override {
            ERC721AStorage.layout()._operatorApprovals[_msgSenderERC721A()][operator] = approved;
            emit ApprovalForAll(_msgSenderERC721A(), operator, approved);
        }
        /**
         * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
         *
         * See {setApprovalForAll}.
         */
        function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
            return ERC721AStorage.layout()._operatorApprovals[owner][operator];
        }
        /**
         * @dev Returns whether `tokenId` exists.
         *
         * Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}.
         *
         * Tokens start existing when they are minted. See {_mint}.
         */
        function _exists(uint256 tokenId) internal view virtual returns (bool result) {
            if (_startTokenId() <= tokenId) {
                if (tokenId < ERC721AStorage.layout()._currentIndex) {
                    uint256 packed;
                    while ((packed = ERC721AStorage.layout()._packedOwnerships[tokenId]) == 0) --tokenId;
                    result = packed & _BITMASK_BURNED == 0;
                }
            }
        }
        /**
         * @dev Returns whether `msgSender` is equal to `approvedAddress` or `owner`.
         */
        function _isSenderApprovedOrOwner(
            address approvedAddress,
            address owner,
            address msgSender
        ) private pure returns (bool result) {
            assembly {
                // Mask `owner` to the lower 160 bits, in case the upper bits somehow aren't clean.
                owner := and(owner, _BITMASK_ADDRESS)
                // Mask `msgSender` to the lower 160 bits, in case the upper bits somehow aren't clean.
                msgSender := and(msgSender, _BITMASK_ADDRESS)
                // `msgSender == owner || msgSender == approvedAddress`.
                result := or(eq(msgSender, owner), eq(msgSender, approvedAddress))
            }
        }
        /**
         * @dev Returns the storage slot and value for the approved address of `tokenId`.
         */
        function _getApprovedSlotAndAddress(
            uint256 tokenId
        ) private view returns (uint256 approvedAddressSlot, address approvedAddress) {
            ERC721AStorage.TokenApprovalRef storage tokenApproval = ERC721AStorage.layout()._tokenApprovals[tokenId];
            // The following is equivalent to `approvedAddress = _tokenApprovals[tokenId].value`.
            assembly {
                approvedAddressSlot := tokenApproval.slot
                approvedAddress := sload(approvedAddressSlot)
            }
        }
        // =============================================================
        //                      TRANSFER OPERATIONS
        // =============================================================
        /**
         * @dev Transfers `tokenId` from `from` to `to`.
         *
         * Requirements:
         *
         * - `from` cannot be the zero address.
         * - `to` cannot be the zero address.
         * - `tokenId` token must be owned by `from`.
         * - If the caller is not `from`, it must be approved to move this token
         * by either {approve} or {setApprovalForAll}.
         *
         * Emits a {Transfer} event.
         */
        function transferFrom(address from, address to, uint256 tokenId) public payable virtual override {
            uint256 prevOwnershipPacked = _packedOwnershipOf(tokenId);
            // Mask `from` to the lower 160 bits, in case the upper bits somehow aren't clean.
            from = address(uint160(uint256(uint160(from)) & _BITMASK_ADDRESS));
            if (address(uint160(prevOwnershipPacked)) != from) _revert(TransferFromIncorrectOwner.selector);
            (uint256 approvedAddressSlot, address approvedAddress) = _getApprovedSlotAndAddress(tokenId);
            // The nested ifs save around 20+ gas over a compound boolean condition.
            if (!_isSenderApprovedOrOwner(approvedAddress, from, _msgSenderERC721A()))
                if (!isApprovedForAll(from, _msgSenderERC721A())) _revert(TransferCallerNotOwnerNorApproved.selector);
            _beforeTokenTransfers(from, to, tokenId, 1);
            // Clear approvals from the previous owner.
            assembly {
                if approvedAddress {
                    // This is equivalent to `delete _tokenApprovals[tokenId]`.
                    sstore(approvedAddressSlot, 0)
                }
            }
            // Underflow of the sender's balance is impossible because we check for
            // ownership above and the recipient's balance can't realistically overflow.
            // Counter overflow is incredibly unrealistic as `tokenId` would have to be 2**256.
            unchecked {
                // We can directly increment and decrement the balances.
                --ERC721AStorage.layout()._packedAddressData[from]; // Updates: `balance -= 1`.
                ++ERC721AStorage.layout()._packedAddressData[to]; // Updates: `balance += 1`.
                // Updates:
                // - `address` to the next owner.
                // - `startTimestamp` to the timestamp of transfering.
                // - `burned` to `false`.
                // - `nextInitialized` to `true`.
                ERC721AStorage.layout()._packedOwnerships[tokenId] = _packOwnershipData(
                    to,
                    _BITMASK_NEXT_INITIALIZED | _nextExtraData(from, to, prevOwnershipPacked)
                );
                // If the next slot may not have been initialized (i.e. `nextInitialized == false`) .
                if (prevOwnershipPacked & _BITMASK_NEXT_INITIALIZED == 0) {
                    uint256 nextTokenId = tokenId + 1;
                    // If the next slot's address is zero and not burned (i.e. packed value is zero).
                    if (ERC721AStorage.layout()._packedOwnerships[nextTokenId] == 0) {
                        // If the next slot is within bounds.
                        if (nextTokenId != ERC721AStorage.layout()._currentIndex) {
                            // Initialize the next slot to maintain correctness for `ownerOf(tokenId + 1)`.
                            ERC721AStorage.layout()._packedOwnerships[nextTokenId] = prevOwnershipPacked;
                        }
                    }
                }
            }
            // Mask `to` to the lower 160 bits, in case the upper bits somehow aren't clean.
            uint256 toMasked = uint256(uint160(to)) & _BITMASK_ADDRESS;
            assembly {
                // Emit the `Transfer` event.
                log4(
                    0, // Start of data (0, since no data).
                    0, // End of data (0, since no data).
                    _TRANSFER_EVENT_SIGNATURE, // Signature.
                    from, // `from`.
                    toMasked, // `to`.
                    tokenId // `tokenId`.
                )
            }
            if (toMasked == 0) _revert(TransferToZeroAddress.selector);
            _afterTokenTransfers(from, to, tokenId, 1);
        }
        /**
         * @dev Equivalent to `safeTransferFrom(from, to, tokenId, '')`.
         */
        function safeTransferFrom(address from, address to, uint256 tokenId) public payable virtual override {
            safeTransferFrom(from, to, tokenId, "");
        }
        /**
         * @dev Safely transfers `tokenId` token from `from` to `to`.
         *
         * Requirements:
         *
         * - `from` cannot be the zero address.
         * - `to` cannot be the zero address.
         * - `tokenId` token must exist and be owned by `from`.
         * - If the caller is not `from`, it must be approved to move this token
         * by either {approve} or {setApprovalForAll}.
         * - If `to` refers to a smart contract, it must implement
         * {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
         *
         * Emits a {Transfer} event.
         */
        function safeTransferFrom(
            address from,
            address to,
            uint256 tokenId,
            bytes memory _data
        ) public payable virtual override {
            transferFrom(from, to, tokenId);
            if (to.code.length != 0)
                if (!_checkContractOnERC721Received(from, to, tokenId, _data)) {
                    _revert(TransferToNonERC721ReceiverImplementer.selector);
                }
        }
        /**
         * @dev Hook that is called before a set of serially-ordered token IDs
         * are about to be transferred. This includes minting.
         * And also called before burning one token.
         *
         * `startTokenId` - the first token ID to be transferred.
         * `quantity` - the amount to be transferred.
         *
         * Calling conditions:
         *
         * - When `from` and `to` are both non-zero, `from`'s `tokenId` will be
         * transferred to `to`.
         * - When `from` is zero, `tokenId` will be minted for `to`.
         * - When `to` is zero, `tokenId` will be burned by `from`.
         * - `from` and `to` are never both zero.
         */
        function _beforeTokenTransfers(address from, address to, uint256 startTokenId, uint256 quantity) internal virtual {}
        /**
         * @dev Hook that is called after a set of serially-ordered token IDs
         * have been transferred. This includes minting.
         * And also called after one token has been burned.
         *
         * `startTokenId` - the first token ID to be transferred.
         * `quantity` - the amount to be transferred.
         *
         * Calling conditions:
         *
         * - When `from` and `to` are both non-zero, `from`'s `tokenId` has been
         * transferred to `to`.
         * - When `from` is zero, `tokenId` has been minted for `to`.
         * - When `to` is zero, `tokenId` has been burned by `from`.
         * - `from` and `to` are never both zero.
         */
        function _afterTokenTransfers(address from, address to, uint256 startTokenId, uint256 quantity) internal virtual {}
        /**
         * @dev Private function to invoke {IERC721Receiver-onERC721Received} on a target contract.
         *
         * `from` - Previous owner of the given token ID.
         * `to` - Target address that will receive the token.
         * `tokenId` - Token ID to be transferred.
         * `_data` - Optional data to send along with the call.
         *
         * Returns whether the call correctly returned the expected magic value.
         */
        function _checkContractOnERC721Received(
            address from,
            address to,
            uint256 tokenId,
            bytes memory _data
        ) private returns (bool) {
            try
                ERC721A__IERC721ReceiverUpgradeable(to).onERC721Received(_msgSenderERC721A(), from, tokenId, _data)
            returns (bytes4 retval) {
                return retval == ERC721A__IERC721ReceiverUpgradeable(to).onERC721Received.selector;
            } catch (bytes memory reason) {
                if (reason.length == 0) {
                    _revert(TransferToNonERC721ReceiverImplementer.selector);
                }
                assembly {
                    revert(add(32, reason), mload(reason))
                }
            }
        }
        // =============================================================
        //                        MINT OPERATIONS
        // =============================================================
        /**
         * @dev Mints `quantity` tokens and transfers them to `to`.
         *
         * Requirements:
         *
         * - `to` cannot be the zero address.
         * - `quantity` must be greater than 0.
         *
         * Emits a {Transfer} event for each mint.
         */
        function _mint(address to, uint256 quantity) internal virtual {
            uint256 startTokenId = ERC721AStorage.layout()._currentIndex;
            if (quantity == 0) _revert(MintZeroQuantity.selector);
            _beforeTokenTransfers(address(0), to, startTokenId, quantity);
            // Overflows are incredibly unrealistic.
            // `balance` and `numberMinted` have a maximum limit of 2**64.
            // `tokenId` has a maximum limit of 2**256.
            unchecked {
                // Updates:
                // - `address` to the owner.
                // - `startTimestamp` to the timestamp of minting.
                // - `burned` to `false`.
                // - `nextInitialized` to `quantity == 1`.
                ERC721AStorage.layout()._packedOwnerships[startTokenId] = _packOwnershipData(
                    to,
                    _nextInitializedFlag(quantity) | _nextExtraData(address(0), to, 0)
                );
                // Updates:
                // - `balance += quantity`.
                // - `numberMinted += quantity`.
                //
                // We can directly add to the `balance` and `numberMinted`.
                ERC721AStorage.layout()._packedAddressData[to] += quantity * ((1 << _BITPOS_NUMBER_MINTED) | 1);
                // Mask `to` to the lower 160 bits, in case the upper bits somehow aren't clean.
                uint256 toMasked = uint256(uint160(to)) & _BITMASK_ADDRESS;
                if (toMasked == 0) _revert(MintToZeroAddress.selector);
                uint256 end = startTokenId + quantity;
                uint256 tokenId = startTokenId;
                do {
                    assembly {
                        // Emit the `Transfer` event.
                        log4(
                            0, // Start of data (0, since no data).
                            0, // End of data (0, since no data).
                            _TRANSFER_EVENT_SIGNATURE, // Signature.
                            0, // `address(0)`.
                            toMasked, // `to`.
                            tokenId // `tokenId`.
                        )
                    }
                    // The `!=` check ensures that large values of `quantity`
                    // that overflows uint256 will make the loop run out of gas.
                } while (++tokenId != end);
                ERC721AStorage.layout()._currentIndex = end;
            }
            _afterTokenTransfers(address(0), to, startTokenId, quantity);
        }
        /**
         * @dev Mints `quantity` tokens and transfers them to `to`.
         *
         * This function is intended for efficient minting only during contract creation.
         *
         * It emits only one {ConsecutiveTransfer} as defined in
         * [ERC2309](https://eips.ethereum.org/EIPS/eip-2309),
         * instead of a sequence of {Transfer} event(s).
         *
         * Calling this function outside of contract creation WILL make your contract
         * non-compliant with the ERC721 standard.
         * For full ERC721 compliance, substituting ERC721 {Transfer} event(s) with the ERC2309
         * {ConsecutiveTransfer} event is only permissible during contract creation.
         *
         * Requirements:
         *
         * - `to` cannot be the zero address.
         * - `quantity` must be greater than 0.
         *
         * Emits a {ConsecutiveTransfer} event.
         */
        function _mintERC2309(address to, uint256 quantity) internal virtual {
            uint256 startTokenId = ERC721AStorage.layout()._currentIndex;
            if (to == address(0)) _revert(MintToZeroAddress.selector);
            if (quantity == 0) _revert(MintZeroQuantity.selector);
            if (quantity > _MAX_MINT_ERC2309_QUANTITY_LIMIT) _revert(MintERC2309QuantityExceedsLimit.selector);
            _beforeTokenTransfers(address(0), to, startTokenId, quantity);
            // Overflows are unrealistic due to the above check for `quantity` to be below the limit.
            unchecked {
                // Updates:
                // - `balance += quantity`.
                // - `numberMinted += quantity`.
                //
                // We can directly add to the `balance` and `numberMinted`.
                ERC721AStorage.layout()._packedAddressData[to] += quantity * ((1 << _BITPOS_NUMBER_MINTED) | 1);
                // Updates:
                // - `address` to the owner.
                // - `startTimestamp` to the timestamp of minting.
                // - `burned` to `false`.
                // - `nextInitialized` to `quantity == 1`.
                ERC721AStorage.layout()._packedOwnerships[startTokenId] = _packOwnershipData(
                    to,
                    _nextInitializedFlag(quantity) | _nextExtraData(address(0), to, 0)
                );
                emit ConsecutiveTransfer(startTokenId, startTokenId + quantity - 1, address(0), to);
                ERC721AStorage.layout()._currentIndex = startTokenId + quantity;
            }
            _afterTokenTransfers(address(0), to, startTokenId, quantity);
        }
        /**
         * @dev Safely mints `quantity` tokens and transfers them to `to`.
         *
         * Requirements:
         *
         * - If `to` refers to a smart contract, it must implement
         * {IERC721Receiver-onERC721Received}, which is called for each safe transfer.
         * - `quantity` must be greater than 0.
         *
         * See {_mint}.
         *
         * Emits a {Transfer} event for each mint.
         */
        function _safeMint(address to, uint256 quantity, bytes memory _data) internal virtual {
            _mint(to, quantity);
            unchecked {
                if (to.code.length != 0) {
                    uint256 end = ERC721AStorage.layout()._currentIndex;
                    uint256 index = end - quantity;
                    do {
                        if (!_checkContractOnERC721Received(address(0), to, index++, _data)) {
                            _revert(TransferToNonERC721ReceiverImplementer.selector);
                        }
                    } while (index < end);
                    // Reentrancy protection.
                    if (ERC721AStorage.layout()._currentIndex != end) _revert(bytes4(0));
                }
            }
        }
        /**
         * @dev Equivalent to `_safeMint(to, quantity, '')`.
         */
        function _safeMint(address to, uint256 quantity) internal virtual {
            _safeMint(to, quantity, "");
        }
        // =============================================================
        //                       APPROVAL OPERATIONS
        // =============================================================
        /**
         * @dev Equivalent to `_approve(to, tokenId, false)`.
         */
        function _approve(address to, uint256 tokenId) internal virtual {
            _approve(to, tokenId, false);
        }
        /**
         * @dev Gives permission to `to` to transfer `tokenId` token to another account.
         * The approval is cleared when the token is transferred.
         *
         * Only a single account can be approved at a time, so approving the
         * zero address clears previous approvals.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         *
         * Emits an {Approval} event.
         */
        function _approve(address to, uint256 tokenId, bool approvalCheck) internal virtual {
            address owner = ownerOf(tokenId);
            if (approvalCheck && _msgSenderERC721A() != owner)
                if (!isApprovedForAll(owner, _msgSenderERC721A())) {
                    _revert(ApprovalCallerNotOwnerNorApproved.selector);
                }
            ERC721AStorage.layout()._tokenApprovals[tokenId].value = to;
            emit Approval(owner, to, tokenId);
        }
        // =============================================================
        //                        BURN OPERATIONS
        // =============================================================
        /**
         * @dev Equivalent to `_burn(tokenId, false)`.
         */
        function _burn(uint256 tokenId) internal virtual {
            _burn(tokenId, false);
        }
        /**
         * @dev Destroys `tokenId`.
         * The approval is cleared when the token is burned.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         *
         * Emits a {Transfer} event.
         */
        function _burn(uint256 tokenId, bool approvalCheck) internal virtual {
            uint256 prevOwnershipPacked = _packedOwnershipOf(tokenId);
            address from = address(uint160(prevOwnershipPacked));
            (uint256 approvedAddressSlot, address approvedAddress) = _getApprovedSlotAndAddress(tokenId);
            if (approvalCheck) {
                // The nested ifs save around 20+ gas over a compound boolean condition.
                if (!_isSenderApprovedOrOwner(approvedAddress, from, _msgSenderERC721A()))
                    if (!isApprovedForAll(from, _msgSenderERC721A())) _revert(TransferCallerNotOwnerNorApproved.selector);
            }
            _beforeTokenTransfers(from, address(0), tokenId, 1);
            // Clear approvals from the previous owner.
            assembly {
                if approvedAddress {
                    // This is equivalent to `delete _tokenApprovals[tokenId]`.
                    sstore(approvedAddressSlot, 0)
                }
            }
            // Underflow of the sender's balance is impossible because we check for
            // ownership above and the recipient's balance can't realistically overflow.
            // Counter overflow is incredibly unrealistic as `tokenId` would have to be 2**256.
            unchecked {
                // Updates:
                // - `balance -= 1`.
                // - `numberBurned += 1`.
                //
                // We can directly decrement the balance, and increment the number burned.
                // This is equivalent to `packed -= 1; packed += 1 << _BITPOS_NUMBER_BURNED;`.
                ERC721AStorage.layout()._packedAddressData[from] += (1 << _BITPOS_NUMBER_BURNED) - 1;
                // Updates:
                // - `address` to the last owner.
                // - `startTimestamp` to the timestamp of burning.
                // - `burned` to `true`.
                // - `nextInitialized` to `true`.
                ERC721AStorage.layout()._packedOwnerships[tokenId] = _packOwnershipData(
                    from,
                    (_BITMASK_BURNED | _BITMASK_NEXT_INITIALIZED) | _nextExtraData(from, address(0), prevOwnershipPacked)
                );
                // If the next slot may not have been initialized (i.e. `nextInitialized == false`) .
                if (prevOwnershipPacked & _BITMASK_NEXT_INITIALIZED == 0) {
                    uint256 nextTokenId = tokenId + 1;
                    // If the next slot's address is zero and not burned (i.e. packed value is zero).
                    if (ERC721AStorage.layout()._packedOwnerships[nextTokenId] == 0) {
                        // If the next slot is within bounds.
                        if (nextTokenId != ERC721AStorage.layout()._currentIndex) {
                            // Initialize the next slot to maintain correctness for `ownerOf(tokenId + 1)`.
                            ERC721AStorage.layout()._packedOwnerships[nextTokenId] = prevOwnershipPacked;
                        }
                    }
                }
            }
            emit Transfer(from, address(0), tokenId);
            _afterTokenTransfers(from, address(0), tokenId, 1);
            // Overflow not possible, as _burnCounter cannot be exceed _currentIndex times.
            unchecked {
                ERC721AStorage.layout()._burnCounter++;
            }
        }
        // =============================================================
        //                     EXTRA DATA OPERATIONS
        // =============================================================
        /**
         * @dev Directly sets the extra data for the ownership data `index`.
         */
        function _setExtraDataAt(uint256 index, uint24 extraData) internal virtual {
            uint256 packed = ERC721AStorage.layout()._packedOwnerships[index];
            if (packed == 0) _revert(OwnershipNotInitializedForExtraData.selector);
            uint256 extraDataCasted;
            // Cast `extraData` with assembly to avoid redundant masking.
            assembly {
                extraDataCasted := extraData
            }
            packed = (packed & _BITMASK_EXTRA_DATA_COMPLEMENT) | (extraDataCasted << _BITPOS_EXTRA_DATA);
            ERC721AStorage.layout()._packedOwnerships[index] = packed;
        }
        /**
         * @dev Called during each token transfer to set the 24bit `extraData` field.
         * Intended to be overridden by the cosumer contract.
         *
         * `previousExtraData` - the value of `extraData` before transfer.
         *
         * Calling conditions:
         *
         * - When `from` and `to` are both non-zero, `from`'s `tokenId` will be
         * transferred to `to`.
         * - When `from` is zero, `tokenId` will be minted for `to`.
         * - When `to` is zero, `tokenId` will be burned by `from`.
         * - `from` and `to` are never both zero.
         */
        function _extraData(address from, address to, uint24 previousExtraData) internal view virtual returns (uint24) {}
        /**
         * @dev Returns the next extra data for the packed ownership data.
         * The returned result is shifted into position.
         */
        function _nextExtraData(address from, address to, uint256 prevOwnershipPacked) private view returns (uint256) {
            uint24 extraData = uint24(prevOwnershipPacked >> _BITPOS_EXTRA_DATA);
            return uint256(_extraData(from, to, extraData)) << _BITPOS_EXTRA_DATA;
        }
        // =============================================================
        //                       OTHER OPERATIONS
        // =============================================================
        /**
         * @dev Returns the message sender (defaults to `msg.sender`).
         *
         * If you are writing GSN compatible contracts, you need to override this function.
         */
        function _msgSenderERC721A() internal view virtual returns (address) {
            return msg.sender;
        }
        /**
         * @dev Converts a uint256 to its ASCII string decimal representation.
         */
        function _toString(uint256 value) internal pure virtual returns (string memory str) {
            assembly {
                // The maximum value of a uint256 contains 78 digits (1 byte per digit), but
                // we allocate 0xa0 bytes to keep the free memory pointer 32-byte word aligned.
                // We will need 1 word for the trailing zeros padding, 1 word for the length,
                // and 3 words for a maximum of 78 digits. Total: 5 * 0x20 = 0xa0.
                let m := add(mload(0x40), 0xa0)
                // Update the free memory pointer to allocate.
                mstore(0x40, m)
                // Assign the `str` to the end.
                str := sub(m, 0x20)
                // Zeroize the slot after the string.
                mstore(str, 0)
                // Cache the end of the memory to calculate the length later.
                let end := str
                // We write the string from rightmost digit to leftmost digit.
                // The following is essentially a do-while loop that also handles the zero case.
                // prettier-ignore
                for { let temp := value } 1 {} {
                    str := sub(str, 1)
                    // Write the character to the pointer.
                    // The ASCII index of the '0' character is 48.
                    mstore8(str, add(48, mod(temp, 10)))
                    // Keep dividing `temp` until zero.
                    temp := div(temp, 10)
                    // prettier-ignore
                    if iszero(temp) { break }
                }
                let length := sub(end, str)
                // Move the pointer 32 bytes leftwards to make room for the length.
                str := sub(str, 0x20)
                // Store the length.
                mstore(str, length)
            }
        }
        /**
         * @dev For more efficient reverts.
         */
        function _revert(bytes4 errorSelector) internal pure {
            assembly {
                mstore(0x00, errorSelector)
                revert(0x00, 0x04)
            }
        }
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    /**
     * @dev This is a base contract to aid in writing upgradeable diamond facet contracts, or any kind of contract that will be deployed
     * behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
     * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
     * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
     *
     * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
     * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
     *
     * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
     * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
     */
    import { ERC721A__InitializableStorage } from "./ERC721A__InitializableStorage.sol";
    abstract contract ERC721A__Initializable {
        using ERC721A__InitializableStorage for ERC721A__InitializableStorage.Layout;
        /**
         * @dev Modifier to protect an initializer function from being invoked twice.
         */
        modifier initializerERC721A() {
            // If the contract is initializing we ignore whether _initialized is set in order to support multiple
            // inheritance patterns, but we only do this in the context of a constructor, because in other contexts the
            // contract may have been reentered.
            require(
                ERC721A__InitializableStorage.layout()._initializing
                    ? _isConstructor()
                    : !ERC721A__InitializableStorage.layout()._initialized,
                "ERC721A__Initializable: contract is already initialized"
            );
            bool isTopLevelCall = !ERC721A__InitializableStorage.layout()._initializing;
            if (isTopLevelCall) {
                ERC721A__InitializableStorage.layout()._initializing = true;
                ERC721A__InitializableStorage.layout()._initialized = true;
            }
            _;
            if (isTopLevelCall) {
                ERC721A__InitializableStorage.layout()._initializing = false;
            }
        }
        /**
         * @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
         * {initializer} modifier, directly or indirectly.
         */
        modifier onlyInitializingERC721A() {
            require(
                ERC721A__InitializableStorage.layout()._initializing,
                "ERC721A__Initializable: contract is not initializing"
            );
            _;
        }
        /// @dev Returns true if and only if the function is running in the constructor
        function _isConstructor() private view returns (bool) {
            // extcodesize checks the size of the code stored in an address, and
            // address returns the current address. Since the code is still not
            // deployed when running a constructor, any checks on its code size will
            // yield zero, making it an effective way to detect if a contract is
            // under construction or not.
            address self = address(this);
            uint256 cs;
            assembly {
                cs := extcodesize(self)
            }
            return cs == 0;
        }
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    /**
     * @dev This is a base storage for the  initialization function for upgradeable diamond facet contracts
     **/
    library ERC721A__InitializableStorage {
        struct Layout {
            /*
             * Indicates that the contract has been initialized.
             */
            bool _initialized;
            /*
             * Indicates that the contract is in the process of being initialized.
             */
            bool _initializing;
        }
        bytes32 internal constant STORAGE_SLOT = keccak256("ERC721A.contracts.storage.initializable.facet");
        function layout() internal pure returns (Layout storage l) {
            bytes32 slot = STORAGE_SLOT;
            assembly {
                l.slot := slot
            }
        }
    }
    // SPDX-License-Identifier: MIT
    // ERC721A Contracts v4.2.3
    // Creator: Chiru Labs
    pragma solidity ^0.8.4;
    import "./IERC721AUpgradeable.sol";
    /**
     * @dev Interface of ERC721AQueryable.
     */
    interface IERC721AQueryableUpgradeable is IERC721AUpgradeable {
        /**
         * Invalid query range (`start` >= `stop`).
         */
        error InvalidQueryRange();
        /**
         * @dev Returns the `TokenOwnership` struct at `tokenId` without reverting.
         *
         * If the `tokenId` is out of bounds:
         *
         * - `addr = address(0)`
         * - `startTimestamp = 0`
         * - `burned = false`
         * - `extraData = 0`
         *
         * If the `tokenId` is burned:
         *
         * - `addr = <Address of owner before token was burned>`
         * - `startTimestamp = <Timestamp when token was burned>`
         * - `burned = true`
         * - `extraData = <Extra data when token was burned>`
         *
         * Otherwise:
         *
         * - `addr = <Address of owner>`
         * - `startTimestamp = <Timestamp of start of ownership>`
         * - `burned = false`
         * - `extraData = <Extra data at start of ownership>`
         */
        function explicitOwnershipOf(uint256 tokenId) external view returns (TokenOwnership memory);
        /**
         * @dev Returns an array of token IDs owned by `owner`,
         * in the range [`start`, `stop`)
         * (i.e. `start <= tokenId < stop`).
         *
         * This function allows for tokens to be queried if the collection
         * grows too big for a single call of {ERC721AQueryable-tokensOfOwner}.
         *
         * Requirements:
         *
         * - `start < stop`
         */
        function tokensOfOwnerIn(address owner, uint256 start, uint256 stop) external view returns (uint256[] memory);
        /**
         * @dev Returns an array of token IDs owned by `owner`.
         *
         * This function scans the ownership mapping and is O(`totalSupply`) in complexity.
         * It is meant to be called off-chain.
         *
         * See {ERC721AQueryable-tokensOfOwnerIn} for splitting the scan into
         * multiple smaller scans if the collection is large enough to cause
         * an out-of-gas error (10K collections should be fine).
         */
        function tokensOfOwner(address owner) external view returns (uint256[] memory);
    }
    // SPDX-License-Identifier: MIT
    // ERC721A Contracts v4.2.3
    // Creator: Chiru Labs
    pragma solidity ^0.8.4;
    /**
     * @dev Interface of ERC721A.
     */
    interface IERC721AUpgradeable {
        /**
         * The caller must own the token or be an approved operator.
         */
        error ApprovalCallerNotOwnerNorApproved();
        /**
         * The token does not exist.
         */
        error ApprovalQueryForNonexistentToken();
        /**
         * Cannot query the balance for the zero address.
         */
        error BalanceQueryForZeroAddress();
        /**
         * Cannot mint to the zero address.
         */
        error MintToZeroAddress();
        /**
         * The quantity of tokens minted must be more than zero.
         */
        error MintZeroQuantity();
        /**
         * The token does not exist.
         */
        error OwnerQueryForNonexistentToken();
        /**
         * The caller must own the token or be an approved operator.
         */
        error TransferCallerNotOwnerNorApproved();
        /**
         * The token must be owned by `from`.
         */
        error TransferFromIncorrectOwner();
        /**
         * Cannot safely transfer to a contract that does not implement the
         * ERC721Receiver interface.
         */
        error TransferToNonERC721ReceiverImplementer();
        /**
         * Cannot transfer to the zero address.
         */
        error TransferToZeroAddress();
        /**
         * The token does not exist.
         */
        error URIQueryForNonexistentToken();
        /**
         * The `quantity` minted with ERC2309 exceeds the safety limit.
         */
        error MintERC2309QuantityExceedsLimit();
        /**
         * The `extraData` cannot be set on an unintialized ownership slot.
         */
        error OwnershipNotInitializedForExtraData();
        // =============================================================
        //                            STRUCTS
        // =============================================================
        struct TokenOwnership {
            // The address of the owner.
            address addr;
            // Stores the start time of ownership with minimal overhead for tokenomics.
            uint64 startTimestamp;
            // Whether the token has been burned.
            bool burned;
            // Arbitrary data similar to `startTimestamp` that can be set via {_extraData}.
            uint24 extraData;
        }
        // =============================================================
        //                         TOKEN COUNTERS
        // =============================================================
        /**
         * @dev Returns the total number of tokens in existence.
         * Burned tokens will reduce the count.
         * To get the total number of tokens minted, please see {_totalMinted}.
         */
        function totalSupply() external view returns (uint256);
        // =============================================================
        //                            IERC165
        // =============================================================
        /**
         * @dev Returns true if this contract implements the interface defined by
         * `interfaceId`. See the corresponding
         * [EIP section](https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified)
         * to learn more about how these ids are created.
         *
         * This function call must use less than 30000 gas.
         */
        function supportsInterface(bytes4 interfaceId) external view returns (bool);
        // =============================================================
        //                            IERC721
        // =============================================================
        /**
         * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
         */
        event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
        /**
         * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
         */
        event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
        /**
         * @dev Emitted when `owner` enables or disables
         * (`approved`) `operator` to manage all of its assets.
         */
        event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
        /**
         * @dev Returns the number of tokens in `owner`'s account.
         */
        function balanceOf(address owner) external view returns (uint256 balance);
        /**
         * @dev Returns the owner of the `tokenId` token.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         */
        function ownerOf(uint256 tokenId) external view returns (address owner);
        /**
         * @dev Safely transfers `tokenId` token from `from` to `to`,
         * checking first that contract recipients are aware of the ERC721 protocol
         * to prevent tokens from being forever locked.
         *
         * Requirements:
         *
         * - `from` cannot be the zero address.
         * - `to` cannot be the zero address.
         * - `tokenId` token must exist and be owned by `from`.
         * - If the caller is not `from`, it must be have been allowed to move
         * this token by either {approve} or {setApprovalForAll}.
         * - If `to` refers to a smart contract, it must implement
         * {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
         *
         * Emits a {Transfer} event.
         */
        function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external payable;
        /**
         * @dev Equivalent to `safeTransferFrom(from, to, tokenId, '')`.
         */
        function safeTransferFrom(address from, address to, uint256 tokenId) external payable;
        /**
         * @dev Transfers `tokenId` from `from` to `to`.
         *
         * WARNING: Usage of this method is discouraged, use {safeTransferFrom}
         * whenever possible.
         *
         * Requirements:
         *
         * - `from` cannot be the zero address.
         * - `to` cannot be the zero address.
         * - `tokenId` token must be owned by `from`.
         * - If the caller is not `from`, it must be approved to move this token
         * by either {approve} or {setApprovalForAll}.
         *
         * Emits a {Transfer} event.
         */
        function transferFrom(address from, address to, uint256 tokenId) external payable;
        /**
         * @dev Gives permission to `to` to transfer `tokenId` token to another account.
         * The approval is cleared when the token is transferred.
         *
         * Only a single account can be approved at a time, so approving the
         * zero address clears previous approvals.
         *
         * Requirements:
         *
         * - The caller must own the token or be an approved operator.
         * - `tokenId` must exist.
         *
         * Emits an {Approval} event.
         */
        function approve(address to, uint256 tokenId) external payable;
        /**
         * @dev Approve or remove `operator` as an operator for the caller.
         * Operators can call {transferFrom} or {safeTransferFrom}
         * for any token owned by the caller.
         *
         * Requirements:
         *
         * - The `operator` cannot be the caller.
         *
         * Emits an {ApprovalForAll} event.
         */
        function setApprovalForAll(address operator, bool _approved) external;
        /**
         * @dev Returns the account approved for `tokenId` token.
         *
         * Requirements:
         *
         * - `tokenId` must exist.
         */
        function getApproved(uint256 tokenId) external view returns (address operator);
        /**
         * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
         *
         * See {setApprovalForAll}.
         */
        function isApprovedForAll(address owner, address operator) external view returns (bool);
        // =============================================================
        //                        IERC721Metadata
        // =============================================================
        /**
         * @dev Returns the token collection name.
         */
        function name() external view returns (string memory);
        /**
         * @dev Returns the token collection symbol.
         */
        function symbol() external view returns (string memory);
        /**
         * @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
         */
        function tokenURI(uint256 tokenId) external view returns (string memory);
        // =============================================================
        //                           IERC2309
        // =============================================================
        /**
         * @dev Emitted when tokens in `fromTokenId` to `toTokenId`
         * (inclusive) is transferred from `from` to `to`, as defined in the
         * [ERC2309](https://eips.ethereum.org/EIPS/eip-2309) standard.
         *
         * See {_mintERC2309} for more details.
         */
        event ConsecutiveTransfer(uint256 indexed fromTokenId, uint256 toTokenId, address indexed from, address indexed to);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IContractMetadata.sol";
    /**
     *  @title   Contract Metadata
     *  @notice  Thirdweb's `ContractMetadata` is a contract extension for any base contracts. It lets you set a metadata URI
     *           for you contract.
     *           Additionally, `ContractMetadata` is necessary for NFT contracts that want royalties to get distributed on OpenSea.
     */
    abstract contract ContractMetadata is IContractMetadata {
        /// @notice Returns the contract metadata URI.
        string public override contractURI;
        /**
         *  @notice         Lets a contract admin set the URI for contract-level metadata.
         *  @dev            Caller should be authorized to setup contractURI, e.g. contract admin.
         *                  See {_canSetContractURI}.
         *                  Emits {ContractURIUpdated Event}.
         *
         *  @param _uri     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         */
        function setContractURI(string memory _uri) external override {
            if (!_canSetContractURI()) {
                revert("Not authorized");
            }
            _setupContractURI(_uri);
        }
        /// @dev Lets a contract admin set the URI for contract-level metadata.
        function _setupContractURI(string memory _uri) internal {
            string memory prevURI = contractURI;
            contractURI = _uri;
            emit ContractURIUpdated(prevURI, _uri);
        }
        /// @dev Returns whether contract metadata can be set in the given execution context.
        function _canSetContractURI() internal view virtual returns (bool);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IDrop.sol";
    import "../lib/MerkleProof.sol";
    abstract contract Drop is IDrop {
        /*///////////////////////////////////////////////////////////////
                                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,
            address _currency,
            uint256 _pricePerToken,
            AllowlistProof calldata _allowlistProof,
            bytes memory _data
        ) public payable virtual override {
            _beforeClaim(_receiver, _quantity, _currency, _pricePerToken, _allowlistProof, _data);
            uint256 activeConditionId = getActiveClaimConditionId();
            verifyClaim(activeConditionId, _dropMsgSender(), _quantity, _currency, _pricePerToken, _allowlistProof);
            // Update contract state.
            claimCondition.conditions[activeConditionId].supplyClaimed += _quantity;
            claimCondition.supplyClaimedByWallet[activeConditionId][_dropMsgSender()] += _quantity;
            // If there's a price, collect price.
            _collectPriceOnClaim(address(0), _quantity, _currency, _pricePerToken);
            // Mint the relevant tokens to claimer.
            uint256 startTokenId = _transferTokensOnClaim(_receiver, _quantity);
            emit TokensClaimed(activeConditionId, _dropMsgSender(), _receiver, startTokenId, _quantity);
            _afterClaim(_receiver, _quantity, _currency, _pricePerToken, _allowlistProof, _data);
        }
        /// @dev Lets a contract admin set claim conditions.
        function setClaimConditions(
            ClaimCondition[] calldata _conditions,
            bool _resetClaimEligibility
        ) external virtual override {
            if (!_canSetClaimConditions()) {
                revert("Not authorized");
            }
            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 lastConditionStartTimestamp;
            for (uint256 i = 0; i < _conditions.length; i++) {
                require(i == 0 || lastConditionStartTimestamp < _conditions[i].startTimestamp, "ST");
                uint256 supplyClaimedAlready = claimCondition.conditions[newStartIndex + i].supplyClaimed;
                if (supplyClaimedAlready > _conditions[i].maxClaimableSupply) {
                    revert("max supply claimed");
                }
                claimCondition.conditions[newStartIndex + i] = _conditions[i];
                claimCondition.conditions[newStartIndex + i].supplyClaimed = supplyClaimedAlready;
                lastConditionStartTimestamp = _conditions[i].startTimestamp;
            }
            /**
             *  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];
                }
            } else {
                if (existingPhaseCount > _conditions.length) {
                    for (uint256 i = _conditions.length; i < existingPhaseCount; i++) {
                        delete claimCondition.conditions[newStartIndex + i];
                    }
                }
            }
            emit ClaimConditionsUpdated(_conditions, _resetClaimEligibility);
        }
        /// @dev Checks a request to claim NFTs against the active claim condition's criteria.
        function verifyClaim(
            uint256 _conditionId,
            address _claimer,
            uint256 _quantity,
            address _currency,
            uint256 _pricePerToken,
            AllowlistProof calldata _allowlistProof
        ) public view virtual returns (bool isOverride) {
            ClaimCondition memory currentClaimPhase = claimCondition.conditions[_conditionId];
            uint256 claimLimit = currentClaimPhase.quantityLimitPerWallet;
            uint256 claimPrice = currentClaimPhase.pricePerToken;
            address claimCurrency = currentClaimPhase.currency;
            /*
             * Here `isOverride` implies that if the merkle proof verification fails,
             * the claimer would claim through open claim limit instead of allowlisted limit.
             */
            if (currentClaimPhase.merkleRoot != bytes32(0)) {
                (isOverride, ) = MerkleProof.verify(
                    _allowlistProof.proof,
                    currentClaimPhase.merkleRoot,
                    keccak256(
                        abi.encodePacked(
                            _claimer,
                            _allowlistProof.quantityLimitPerWallet,
                            _allowlistProof.pricePerToken,
                            _allowlistProof.currency
                        )
                    )
                );
            }
            if (isOverride) {
                claimLimit = _allowlistProof.quantityLimitPerWallet != 0
                    ? _allowlistProof.quantityLimitPerWallet
                    : claimLimit;
                claimPrice = _allowlistProof.pricePerToken != type(uint256).max
                    ? _allowlistProof.pricePerToken
                    : claimPrice;
                claimCurrency = _allowlistProof.pricePerToken != type(uint256).max && _allowlistProof.currency != address(0)
                    ? _allowlistProof.currency
                    : claimCurrency;
            }
            uint256 supplyClaimedByWallet = claimCondition.supplyClaimedByWallet[_conditionId][_claimer];
            if (_currency != claimCurrency || _pricePerToken != claimPrice) {
                revert("!PriceOrCurrency");
            }
            if (_quantity == 0 || (_quantity + supplyClaimedByWallet > claimLimit)) {
                revert("!Qty");
            }
            if (currentClaimPhase.supplyClaimed + _quantity > currentClaimPhase.maxClaimableSupply) {
                revert("!MaxSupply");
            }
            if (currentClaimPhase.startTimestamp > block.timestamp) {
                revert("cant claim yet");
            }
        }
        /// @dev At any given moment, returns the uid for the active claim condition.
        function getActiveClaimConditionId() public view returns (uint256) {
            for (uint256 i = claimCondition.currentStartId + claimCondition.count; i > claimCondition.currentStartId; i--) {
                if (block.timestamp >= claimCondition.conditions[i - 1].startTimestamp) {
                    return i - 1;
                }
            }
            revert("!CONDITION.");
        }
        /// @dev Returns the claim condition at the given uid.
        function getClaimConditionById(uint256 _conditionId) external view returns (ClaimCondition memory condition) {
            condition = claimCondition.conditions[_conditionId];
        }
        /// @dev Returns the supply claimed by claimer for a given conditionId.
        function getSupplyClaimedByWallet(
            uint256 _conditionId,
            address _claimer
        ) public view returns (uint256 supplyClaimedByWallet) {
            supplyClaimedByWallet = claimCondition.supplyClaimedByWallet[_conditionId][_claimer];
        }
        /*////////////////////////////////////////////////////////////////////
            Optional hooks that can be implemented in the derived contract
        ///////////////////////////////////////////////////////////////////*/
        /// @dev Exposes the ability to override the msg sender.
        function _dropMsgSender() internal virtual returns (address) {
            return msg.sender;
        }
        /// @dev Runs before every `claim` function call.
        function _beforeClaim(
            address _receiver,
            uint256 _quantity,
            address _currency,
            uint256 _pricePerToken,
            AllowlistProof calldata _allowlistProof,
            bytes memory _data
        ) internal virtual {}
        /// @dev Runs after every `claim` function call.
        function _afterClaim(
            address _receiver,
            uint256 _quantity,
            address _currency,
            uint256 _pricePerToken,
            AllowlistProof calldata _allowlistProof,
            bytes memory _data
        ) internal virtual {}
        /*///////////////////////////////////////////////////////////////
            Virtual functions: to be implemented in derived contract
        //////////////////////////////////////////////////////////////*/
        /// @dev Collects and distributes the primary sale value of NFTs being claimed.
        function _collectPriceOnClaim(
            address _primarySaleRecipient,
            uint256 _quantityToClaim,
            address _currency,
            uint256 _pricePerToken
        ) internal virtual;
        /// @dev Transfers the NFTs being claimed.
        function _transferTokensOnClaim(
            address _to,
            uint256 _quantityBeingClaimed
        ) internal virtual returns (uint256 startTokenId);
        /// @dev Determine what wallet can update claim conditions
        function _canSetClaimConditions() internal view virtual returns (bool);
    }
    // SPDX-License-Identifier: Apache 2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "../lib/Address.sol";
    import "./interface/IMulticall.sol";
    /**
     * @dev Provides a function to batch together multiple calls in a single external call.
     *
     * _Available since v4.1._
     */
    contract Multicall is IMulticall {
        /**
         *  @notice Receives and executes a batch of function calls on this contract.
         *  @dev Receives and executes a batch of function calls on this contract.
         *
         *  @param data The bytes data that makes up the batch of function calls to execute.
         *  @return results The bytes data that makes up the result of the batch of function calls executed.
         */
        function multicall(bytes[] calldata data) external returns (bytes[] memory results) {
            results = new bytes[](data.length);
            address sender = _msgSender();
            bool isForwarder = msg.sender != sender;
            for (uint256 i = 0; i < data.length; i++) {
                if (isForwarder) {
                    results[i] = Address.functionDelegateCall(address(this), abi.encodePacked(data[i], sender));
                } else {
                    results[i] = Address.functionDelegateCall(address(this), data[i]);
                }
            }
            return results;
        }
        /// @notice Returns the sender in the given execution context.
        function _msgSender() internal view virtual returns (address) {
            return msg.sender;
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IOwnable.sol";
    /**
     *  @title   Ownable
     *  @notice  Thirdweb's `Ownable` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *           who the 'owner' of the inheriting smart contract is, and lets the inheriting contract perform conditional logic that uses
     *           information about who the contract's owner is.
     */
    abstract contract Ownable is IOwnable {
        /// @dev Owner of the contract (purpose: OpenSea compatibility)
        address private _owner;
        /// @dev Reverts if caller is not the owner.
        modifier onlyOwner() {
            if (msg.sender != _owner) {
                revert("Not authorized");
            }
            _;
        }
        /**
         *  @notice Returns the owner of the contract.
         */
        function owner() public view override returns (address) {
            return _owner;
        }
        /**
         *  @notice Lets an authorized wallet set a new owner for the contract.
         *  @param _newOwner The address to set as the new owner of the contract.
         */
        function setOwner(address _newOwner) external override {
            if (!_canSetOwner()) {
                revert("Not authorized");
            }
            _setupOwner(_newOwner);
        }
        /// @dev Lets a contract admin set a new owner for the contract. The new owner must be a contract admin.
        function _setupOwner(address _newOwner) internal {
            address _prevOwner = _owner;
            _owner = _newOwner;
            emit OwnerUpdated(_prevOwner, _newOwner);
        }
        /// @dev Returns whether owner can be set in the given execution context.
        function _canSetOwner() internal view virtual returns (bool);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IPermissions.sol";
    import "../lib/Strings.sol";
    /**
     *  @title   Permissions
     *  @dev     This contracts provides extending-contracts with role-based access control mechanisms
     */
    contract Permissions is IPermissions {
        /// @dev Map from keccak256 hash of a role => a map from address => whether address has role.
        mapping(bytes32 => mapping(address => bool)) private _hasRole;
        /// @dev Map from keccak256 hash of a role to role admin. See {getRoleAdmin}.
        mapping(bytes32 => bytes32) private _getRoleAdmin;
        /// @dev Default admin role for all roles. Only accounts with this role can grant/revoke other roles.
        bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
        /// @dev Modifier that checks if an account has the specified role; reverts otherwise.
        modifier onlyRole(bytes32 role) {
            _checkRole(role, msg.sender);
            _;
        }
        /**
         *  @notice         Checks whether an account has a particular role.
         *  @dev            Returns `true` if `account` has been granted `role`.
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         *  @param account  Address of the account for which the role is being checked.
         */
        function hasRole(bytes32 role, address account) public view override returns (bool) {
            return _hasRole[role][account];
        }
        /**
         *  @notice         Checks whether an account has a particular role;
         *                  role restrictions can be swtiched on and off.
         *
         *  @dev            Returns `true` if `account` has been granted `role`.
         *                  Role restrictions can be swtiched on and off:
         *                      - If address(0) has ROLE, then the ROLE restrictions
         *                        don't apply.
         *                      - If address(0) does not have ROLE, then the ROLE
         *                        restrictions will apply.
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         *  @param account  Address of the account for which the role is being checked.
         */
        function hasRoleWithSwitch(bytes32 role, address account) public view returns (bool) {
            if (!_hasRole[role][address(0)]) {
                return _hasRole[role][account];
            }
            return true;
        }
        /**
         *  @notice         Returns the admin role that controls the specified role.
         *  @dev            See {grantRole} and {revokeRole}.
         *                  To change a role's admin, use {_setRoleAdmin}.
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         */
        function getRoleAdmin(bytes32 role) external view override returns (bytes32) {
            return _getRoleAdmin[role];
        }
        /**
         *  @notice         Grants a role to an account, if not previously granted.
         *  @dev            Caller must have admin role for the `role`.
         *                  Emits {RoleGranted Event}.
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         *  @param account  Address of the account to which the role is being granted.
         */
        function grantRole(bytes32 role, address account) public virtual override {
            _checkRole(_getRoleAdmin[role], msg.sender);
            if (_hasRole[role][account]) {
                revert("Can only grant to non holders");
            }
            _setupRole(role, account);
        }
        /**
         *  @notice         Revokes role from an account.
         *  @dev            Caller must have admin role for the `role`.
         *                  Emits {RoleRevoked Event}.
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         *  @param account  Address of the account from which the role is being revoked.
         */
        function revokeRole(bytes32 role, address account) public virtual override {
            _checkRole(_getRoleAdmin[role], msg.sender);
            _revokeRole(role, account);
        }
        /**
         *  @notice         Revokes role from the account.
         *  @dev            Caller must have the `role`, with caller being the same as `account`.
         *                  Emits {RoleRevoked Event}.
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         *  @param account  Address of the account from which the role is being revoked.
         */
        function renounceRole(bytes32 role, address account) public virtual override {
            if (msg.sender != account) {
                revert("Can only renounce for self");
            }
            _revokeRole(role, account);
        }
        /// @dev Sets `adminRole` as `role`'s admin role.
        function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
            bytes32 previousAdminRole = _getRoleAdmin[role];
            _getRoleAdmin[role] = adminRole;
            emit RoleAdminChanged(role, previousAdminRole, adminRole);
        }
        /// @dev Sets up `role` for `account`
        function _setupRole(bytes32 role, address account) internal virtual {
            _hasRole[role][account] = true;
            emit RoleGranted(role, account, msg.sender);
        }
        /// @dev Revokes `role` from `account`
        function _revokeRole(bytes32 role, address account) internal virtual {
            _checkRole(role, account);
            delete _hasRole[role][account];
            emit RoleRevoked(role, account, msg.sender);
        }
        /// @dev Checks `role` for `account`. Reverts with a message including the required role.
        function _checkRole(bytes32 role, address account) internal view virtual {
            if (!_hasRole[role][account]) {
                revert(
                    string(
                        abi.encodePacked(
                            "Permissions: account ",
                            Strings.toHexString(uint160(account), 20),
                            " is missing role ",
                            Strings.toHexString(uint256(role), 32)
                        )
                    )
                );
            }
        }
        /// @dev Checks `role` for `account`. Reverts with a message including the required role.
        function _checkRoleWithSwitch(bytes32 role, address account) internal view virtual {
            if (!hasRoleWithSwitch(role, account)) {
                revert(
                    string(
                        abi.encodePacked(
                            "Permissions: account ",
                            Strings.toHexString(uint160(account), 20),
                            " is missing role ",
                            Strings.toHexString(uint256(role), 32)
                        )
                    )
                );
            }
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IPermissionsEnumerable.sol";
    import "./Permissions.sol";
    /**
     *  @title   PermissionsEnumerable
     *  @dev     This contracts provides extending-contracts with role-based access control mechanisms.
     *           Also provides interfaces to view all members with a given role, and total count of members.
     */
    contract PermissionsEnumerable is IPermissionsEnumerable, Permissions {
        /**
         *  @notice A data structure to store data of members for a given role.
         *
         *  @param index    Current index in the list of accounts that have a role.
         *  @param members  map from index => address of account that has a role
         *  @param indexOf  map from address => index which the account has.
         */
        struct RoleMembers {
            uint256 index;
            mapping(uint256 => address) members;
            mapping(address => uint256) indexOf;
        }
        /// @dev map from keccak256 hash of a role to its members' data. See {RoleMembers}.
        mapping(bytes32 => RoleMembers) private roleMembers;
        /**
         *  @notice         Returns the role-member from a list of members for a role,
         *                  at a given index.
         *  @dev            Returns `member` who has `role`, at `index` of role-members list.
         *                  See struct {RoleMembers}, and mapping {roleMembers}
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         *  @param index    Index in list of current members for the role.
         *
         *  @return member  Address of account that has `role`
         */
        function getRoleMember(bytes32 role, uint256 index) external view override returns (address member) {
            uint256 currentIndex = roleMembers[role].index;
            uint256 check;
            for (uint256 i = 0; i < currentIndex; i += 1) {
                if (roleMembers[role].members[i] != address(0)) {
                    if (check == index) {
                        member = roleMembers[role].members[i];
                        return member;
                    }
                    check += 1;
                } else if (hasRole(role, address(0)) && i == roleMembers[role].indexOf[address(0)]) {
                    check += 1;
                }
            }
        }
        /**
         *  @notice         Returns total number of accounts that have a role.
         *  @dev            Returns `count` of accounts that have `role`.
         *                  See struct {RoleMembers}, and mapping {roleMembers}
         *
         *  @param role     keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE")
         *
         *  @return count   Total number of accounts that have `role`
         */
        function getRoleMemberCount(bytes32 role) external view override returns (uint256 count) {
            uint256 currentIndex = roleMembers[role].index;
            for (uint256 i = 0; i < currentIndex; i += 1) {
                if (roleMembers[role].members[i] != address(0)) {
                    count += 1;
                }
            }
            if (hasRole(role, address(0))) {
                count += 1;
            }
        }
        /// @dev Revokes `role` from `account`, and removes `account` from {roleMembers}
        ///      See {_removeMember}
        function _revokeRole(bytes32 role, address account) internal override {
            super._revokeRole(role, account);
            _removeMember(role, account);
        }
        /// @dev Grants `role` to `account`, and adds `account` to {roleMembers}
        ///      See {_addMember}
        function _setupRole(bytes32 role, address account) internal override {
            super._setupRole(role, account);
            _addMember(role, account);
        }
        /// @dev adds `account` to {roleMembers}, for `role`
        function _addMember(bytes32 role, address account) internal {
            uint256 idx = roleMembers[role].index;
            roleMembers[role].index += 1;
            roleMembers[role].members[idx] = account;
            roleMembers[role].indexOf[account] = idx;
        }
        /// @dev removes `account` from {roleMembers}, for `role`
        function _removeMember(bytes32 role, address account) internal {
            uint256 idx = roleMembers[role].indexOf[account];
            delete roleMembers[role].members[idx];
            delete roleMembers[role].indexOf[account];
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IPlatformFee.sol";
    /**
     *  @title   Platform Fee
     *  @notice  Thirdweb's `PlatformFee` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *           the recipient of platform fee and the platform fee basis points, and lets the inheriting contract perform conditional logic
     *           that uses information about platform fees, if desired.
     */
    abstract contract PlatformFee is IPlatformFee {
        /// @dev The address that receives all platform fees from all sales.
        address private platformFeeRecipient;
        /// @dev The % of primary sales collected as platform fees.
        uint16 private platformFeeBps;
        /// @dev Fee type variants: percentage fee and flat fee
        PlatformFeeType private platformFeeType;
        /// @dev The flat amount collected by the contract as fees on primary sales.
        uint256 private flatPlatformFee;
        /// @dev Returns the platform fee recipient and bps.
        function getPlatformFeeInfo() public view override returns (address, uint16) {
            return (platformFeeRecipient, uint16(platformFeeBps));
        }
        /// @dev Returns the platform fee bps and recipient.
        function getFlatPlatformFeeInfo() public view returns (address, uint256) {
            return (platformFeeRecipient, flatPlatformFee);
        }
        /// @dev Returns the platform fee type.
        function getPlatformFeeType() public view returns (PlatformFeeType) {
            return platformFeeType;
        }
        /**
         *  @notice         Updates the platform fee recipient and bps.
         *  @dev            Caller should be authorized to set platform fee info.
         *                  See {_canSetPlatformFeeInfo}.
         *                  Emits {PlatformFeeInfoUpdated Event}; See {_setupPlatformFeeInfo}.
         *
         *  @param _platformFeeRecipient   Address to be set as new platformFeeRecipient.
         *  @param _platformFeeBps         Updated platformFeeBps.
         */
        function setPlatformFeeInfo(address _platformFeeRecipient, uint256 _platformFeeBps) external override {
            if (!_canSetPlatformFeeInfo()) {
                revert("Not authorized");
            }
            _setupPlatformFeeInfo(_platformFeeRecipient, _platformFeeBps);
        }
        /// @dev Sets the platform fee recipient and bps
        function _setupPlatformFeeInfo(address _platformFeeRecipient, uint256 _platformFeeBps) internal {
            if (_platformFeeBps > 10_000) {
                revert("Exceeds max bps");
            }
            if (_platformFeeRecipient == address(0)) {
                revert("Invalid recipient");
            }
            platformFeeBps = uint16(_platformFeeBps);
            platformFeeRecipient = _platformFeeRecipient;
            emit PlatformFeeInfoUpdated(_platformFeeRecipient, _platformFeeBps);
        }
        /// @notice Lets a module admin set a flat fee on primary sales.
        function setFlatPlatformFeeInfo(address _platformFeeRecipient, uint256 _flatFee) external {
            if (!_canSetPlatformFeeInfo()) {
                revert("Not authorized");
            }
            _setupFlatPlatformFeeInfo(_platformFeeRecipient, _flatFee);
        }
        /// @dev Sets a flat fee on primary sales.
        function _setupFlatPlatformFeeInfo(address _platformFeeRecipient, uint256 _flatFee) internal {
            flatPlatformFee = _flatFee;
            platformFeeRecipient = _platformFeeRecipient;
            emit FlatPlatformFeeUpdated(_platformFeeRecipient, _flatFee);
        }
        /// @notice Lets a module admin set platform fee type.
        function setPlatformFeeType(PlatformFeeType _feeType) external {
            if (!_canSetPlatformFeeInfo()) {
                revert("Not authorized");
            }
            _setupPlatformFeeType(_feeType);
        }
        /// @dev Sets platform fee type.
        function _setupPlatformFeeType(PlatformFeeType _feeType) internal {
            platformFeeType = _feeType;
            emit PlatformFeeTypeUpdated(_feeType);
        }
        /// @dev Returns whether platform fee info can be set in the given execution context.
        function _canSetPlatformFeeInfo() internal view virtual returns (bool);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IPrimarySale.sol";
    /**
     *  @title   Primary Sale
     *  @notice  Thirdweb's `PrimarySale` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *           the recipient of primary sales, and lets the inheriting contract perform conditional logic that uses information about
     *           primary sales, if desired.
     */
    abstract contract PrimarySale is IPrimarySale {
        /// @dev The address that receives all primary sales value.
        address private recipient;
        /// @dev Returns primary sale recipient address.
        function primarySaleRecipient() public view override returns (address) {
            return recipient;
        }
        /**
         *  @notice         Updates primary sale recipient.
         *  @dev            Caller should be authorized to set primary sales info.
         *                  See {_canSetPrimarySaleRecipient}.
         *                  Emits {PrimarySaleRecipientUpdated Event}; See {_setupPrimarySaleRecipient}.
         *
         *  @param _saleRecipient   Address to be set as new recipient of primary sales.
         */
        function setPrimarySaleRecipient(address _saleRecipient) external override {
            if (!_canSetPrimarySaleRecipient()) {
                revert("Not authorized");
            }
            _setupPrimarySaleRecipient(_saleRecipient);
        }
        /// @dev Lets a contract admin set the recipient for all primary sales.
        function _setupPrimarySaleRecipient(address _saleRecipient) internal {
            if (_saleRecipient == address(0)) {
                revert("Invalid recipient");
            }
            recipient = _saleRecipient;
            emit PrimarySaleRecipientUpdated(_saleRecipient);
        }
        /// @dev Returns whether primary sale recipient can be set in the given execution context.
        function _canSetPrimarySaleRecipient() internal view virtual returns (bool);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./interface/IRoyalty.sol";
    /**
     *  @title   Royalty
     *  @notice  Thirdweb's `Royalty` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *           the recipient of royalty fee and the royalty fee basis points, and lets the inheriting contract perform conditional logic
     *           that uses information about royalty fees, if desired.
     *
     *  @dev     The `Royalty` contract is ERC2981 compliant.
     */
    abstract contract Royalty is IRoyalty {
        /// @dev The (default) address that receives all royalty value.
        address private royaltyRecipient;
        /// @dev The (default) % of a sale to take as royalty (in basis points).
        uint16 private royaltyBps;
        /// @dev Token ID => royalty recipient and bps for token
        mapping(uint256 => RoyaltyInfo) private royaltyInfoForToken;
        /**
         *  @notice   View royalty info for a given token and sale price.
         *  @dev      Returns royalty amount and recipient for `tokenId` and `salePrice`.
         *  @param tokenId          The tokenID of the NFT for which to query royalty info.
         *  @param salePrice        Sale price of the token.
         *
         *  @return receiver        Address of royalty recipient account.
         *  @return royaltyAmount   Royalty amount calculated at current royaltyBps value.
         */
        function royaltyInfo(
            uint256 tokenId,
            uint256 salePrice
        ) external view virtual override returns (address receiver, uint256 royaltyAmount) {
            (address recipient, uint256 bps) = getRoyaltyInfoForToken(tokenId);
            receiver = recipient;
            royaltyAmount = (salePrice * bps) / 10_000;
        }
        /**
         *  @notice          View royalty info for a given token.
         *  @dev             Returns royalty recipient and bps for `_tokenId`.
         *  @param _tokenId  The tokenID of the NFT for which to query royalty info.
         */
        function getRoyaltyInfoForToken(uint256 _tokenId) public view override returns (address, uint16) {
            RoyaltyInfo memory royaltyForToken = royaltyInfoForToken[_tokenId];
            return
                royaltyForToken.recipient == address(0)
                    ? (royaltyRecipient, uint16(royaltyBps))
                    : (royaltyForToken.recipient, uint16(royaltyForToken.bps));
        }
        /**
         *  @notice Returns the defualt royalty recipient and BPS for this contract's NFTs.
         */
        function getDefaultRoyaltyInfo() external view override returns (address, uint16) {
            return (royaltyRecipient, uint16(royaltyBps));
        }
        /**
         *  @notice         Updates default royalty recipient and bps.
         *  @dev            Caller should be authorized to set royalty info.
         *                  See {_canSetRoyaltyInfo}.
         *                  Emits {DefaultRoyalty Event}; See {_setupDefaultRoyaltyInfo}.
         *
         *  @param _royaltyRecipient   Address to be set as default royalty recipient.
         *  @param _royaltyBps         Updated royalty bps.
         */
        function setDefaultRoyaltyInfo(address _royaltyRecipient, uint256 _royaltyBps) external override {
            if (!_canSetRoyaltyInfo()) {
                revert("Not authorized");
            }
            _setupDefaultRoyaltyInfo(_royaltyRecipient, _royaltyBps);
        }
        /// @dev Lets a contract admin update the default royalty recipient and bps.
        function _setupDefaultRoyaltyInfo(address _royaltyRecipient, uint256 _royaltyBps) internal {
            if (_royaltyBps > 10_000) {
                revert("Exceeds max bps");
            }
            royaltyRecipient = _royaltyRecipient;
            royaltyBps = uint16(_royaltyBps);
            emit DefaultRoyalty(_royaltyRecipient, _royaltyBps);
        }
        /**
         *  @notice         Updates default royalty recipient and bps for a particular token.
         *  @dev            Sets royalty info for `_tokenId`. Caller should be authorized to set royalty info.
         *                  See {_canSetRoyaltyInfo}.
         *                  Emits {RoyaltyForToken Event}; See {_setupRoyaltyInfoForToken}.
         *
         *  @param _recipient   Address to be set as royalty recipient for given token Id.
         *  @param _bps         Updated royalty bps for the token Id.
         */
        function setRoyaltyInfoForToken(uint256 _tokenId, address _recipient, uint256 _bps) external override {
            if (!_canSetRoyaltyInfo()) {
                revert("Not authorized");
            }
            _setupRoyaltyInfoForToken(_tokenId, _recipient, _bps);
        }
        /// @dev Lets a contract admin set the royalty recipient and bps for a particular token Id.
        function _setupRoyaltyInfoForToken(uint256 _tokenId, address _recipient, uint256 _bps) internal {
            if (_bps > 10_000) {
                revert("Exceeds max bps");
            }
            royaltyInfoForToken[_tokenId] = RoyaltyInfo({ recipient: _recipient, bps: _bps });
            emit RoyaltyForToken(_tokenId, _recipient, _bps);
        }
        /// @dev Returns whether royalty info can be set in the given execution context.
        function _canSetRoyaltyInfo() internal view virtual returns (bool);
    }
    // SPDX-License-Identifier: Apache 2.0
    pragma solidity ^0.8.10;
    /// @author thirdweb
    import "../lib/NFTMetadataRenderer.sol";
    import "./interface/ISharedMetadata.sol";
    import "../eip/interface/IERC4906.sol";
    abstract contract SharedMetadata is ISharedMetadata, IERC4906 {
        /// @notice Token metadata information
        SharedMetadataInfo public sharedMetadata;
        /// @notice Set shared metadata for NFTs
        function setSharedMetadata(SharedMetadataInfo calldata _metadata) external virtual {
            if (!_canSetSharedMetadata()) {
                revert("Not authorized");
            }
            _setSharedMetadata(_metadata);
        }
        /**
         *  @dev Sets shared metadata for NFTs.
         *  @param _metadata common metadata for all tokens
         */
        function _setSharedMetadata(SharedMetadataInfo calldata _metadata) internal {
            sharedMetadata = SharedMetadataInfo({
                name: _metadata.name,
                description: _metadata.description,
                imageURI: _metadata.imageURI,
                animationURI: _metadata.animationURI
            });
            emit BatchMetadataUpdate(0, type(uint256).max);
            emit SharedMetadataUpdated({
                name: _metadata.name,
                description: _metadata.description,
                imageURI: _metadata.imageURI,
                animationURI: _metadata.animationURI
            });
        }
        /**
         *  @dev Token URI information getter
         *  @param tokenId Token ID to get URI for
         */
        function _getURIFromSharedMetadata(uint256 tokenId) internal view returns (string memory) {
            SharedMetadataInfo memory info = sharedMetadata;
            return
                NFTMetadataRenderer.createMetadataEdition({
                    name: info.name,
                    description: info.description,
                    imageURI: info.imageURI,
                    animationURI: info.animationURI,
                    tokenOfEdition: tokenId
                });
        }
        /// @dev Returns whether shared metadata can be set in the given execution context.
        function _canSetSharedMetadata() internal view virtual returns (bool);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     *  The interface `IClaimCondition` is written for thirdweb's 'Drop' contracts, which are distribution mechanisms for tokens.
     *
     *  A claim condition defines criteria under which accounts can mint tokens. Claim conditions can be overwritten
     *  or added to by the contract admin. At any moment, there is only one active claim condition.
     */
    interface IClaimCondition {
        /**
         *  @notice The criteria that make up a claim condition.
         *
         *  @param startTimestamp                 The unix timestamp after which the claim condition applies.
         *                                        The same claim condition applies until the `startTimestamp`
         *                                        of the next claim condition.
         *
         *  @param maxClaimableSupply             The maximum total number of tokens that can be claimed under
         *                                        the claim condition.
         *
         *  @param supplyClaimed                  At any given point, the number of tokens that have been claimed
         *                                        under the claim condition.
         *
         *  @param quantityLimitPerWallet         The maximum number of tokens that can be claimed by a wallet.
         *
         *  @param merkleRoot                     The allowlist of addresses that can claim tokens under the claim
         *                                        condition.
         *
         *  @param pricePerToken                  The price required to pay per token claimed.
         *
         *  @param currency                       The currency in which the `pricePerToken` must be paid.
         *
         *  @param metadata                       Claim condition metadata.
         */
        struct ClaimCondition {
            uint256 startTimestamp;
            uint256 maxClaimableSupply;
            uint256 supplyClaimed;
            uint256 quantityLimitPerWallet;
            bytes32 merkleRoot;
            uint256 pricePerToken;
            address currency;
            string metadata;
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./IClaimCondition.sol";
    /**
     *  The interface `IClaimConditionMultiPhase` is written for thirdweb's 'Drop' contracts, which are distribution mechanisms for tokens.
     *
     *  An authorized wallet can set a series of claim conditions, ordered by their respective `startTimestamp`.
     *  A claim condition defines criteria under which accounts can mint tokens. Claim conditions can be overwritten
     *  or added to by the contract admin. At any moment, there is only one active claim condition.
     */
    interface IClaimConditionMultiPhase is IClaimCondition {
        /**
         *  @notice The set of all claim conditions, at any given moment.
         *  Claim Phase ID = [currentStartId, currentStartId + length - 1];
         *
         *  @param currentStartId           The uid for the first claim condition amongst the current set of
         *                                  claim conditions. The uid for each next claim condition is one
         *                                  more than the previous claim condition's uid.
         *
         *  @param count                    The total number of phases / claim conditions in the list
         *                                  of claim conditions.
         *
         *  @param conditions                   The claim conditions at a given uid. Claim conditions
         *                                  are ordered in an ascending order by their `startTimestamp`.
         *
         *  @param supplyClaimedByWallet    Map from a claim condition uid and account to supply claimed by account.
         */
        struct ClaimConditionList {
            uint256 currentStartId;
            uint256 count;
            mapping(uint256 => ClaimCondition) conditions;
            mapping(uint256 => mapping(address => uint256)) supplyClaimedByWallet;
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     *  Thirdweb's `ContractMetadata` is a contract extension for any base contracts. It lets you set a metadata URI
     *  for you contract.
     *
     *  Additionally, `ContractMetadata` is necessary for NFT contracts that want royalties to get distributed on OpenSea.
     */
    interface IContractMetadata {
        /// @dev Returns the metadata URI of the contract.
        function contractURI() external view returns (string memory);
        /**
         *  @dev Sets contract URI for the storefront-level metadata of the contract.
         *       Only module admin can call this function.
         */
        function setContractURI(string calldata _uri) external;
        /// @dev Emitted when the contract URI is updated.
        event ContractURIUpdated(string prevURI, string newURI);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./IClaimConditionMultiPhase.sol";
    /**
     *  The interface `IDrop` is written for thirdweb's 'Drop' contracts, which are distribution mechanisms for tokens.
     *
     *  An authorized wallet can set a series of claim conditions, ordered by their respective `startTimestamp`.
     *  A claim condition defines criteria under which accounts can mint tokens. Claim conditions can be overwritten
     *  or added to by the contract admin. At any moment, there is only one active claim condition.
     */
    interface IDrop is IClaimConditionMultiPhase {
        /**
         *  @param proof Proof of concerned wallet's inclusion in an allowlist.
         *  @param quantityLimitPerWallet The total quantity of tokens the allowlisted wallet is eligible to claim over time.
         *  @param pricePerToken The price per token the allowlisted wallet must pay to claim tokens.
         *  @param currency The currency in which the allowlisted wallet must pay the price for claiming tokens.
         */
        struct AllowlistProof {
            bytes32[] proof;
            uint256 quantityLimitPerWallet;
            uint256 pricePerToken;
            address currency;
        }
        /// @notice Emitted when tokens are claimed via `claim`.
        event TokensClaimed(
            uint256 indexed claimConditionIndex,
            address indexed claimer,
            address indexed receiver,
            uint256 startTokenId,
            uint256 quantityClaimed
        );
        /// @notice Emitted when the contract's claim conditions are updated.
        event ClaimConditionsUpdated(ClaimCondition[] claimConditions, bool resetEligibility);
        /**
         *  @notice Lets an account claim a given quantity of NFTs.
         *
         *  @param receiver                       The receiver of the NFTs to claim.
         *  @param quantity                       The quantity of NFTs to claim.
         *  @param currency                       The currency in which to pay for the claim.
         *  @param pricePerToken                  The price per token to pay for the claim.
         *  @param allowlistProof                 The proof of the claimer's inclusion in the merkle root allowlist
         *                                        of the claim conditions that apply.
         *  @param data                           Arbitrary bytes data that can be leveraged in the implementation of this interface.
         */
        function claim(
            address receiver,
            uint256 quantity,
            address currency,
            uint256 pricePerToken,
            AllowlistProof calldata allowlistProof,
            bytes memory data
        ) external payable;
        /**
         *  @notice Lets a contract admin (account with `DEFAULT_ADMIN_ROLE`) set claim conditions.
         *
         *  @param phases                   Claim conditions in ascending order by `startTimestamp`.
         *
         *  @param resetClaimEligibility    Whether to honor the restrictions applied to wallets who have claimed tokens in the current conditions,
         *                                  in the new claim conditions being set.
         *
         */
        function setClaimConditions(ClaimCondition[] calldata phases, bool resetClaimEligibility) external;
    }
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     * @dev Provides a function to batch together multiple calls in a single external call.
     *
     * _Available since v4.1._
     */
    interface IMulticall {
        /**
         * @dev Receives and executes a batch of function calls on this contract.
         */
        function multicall(bytes[] calldata data) external returns (bytes[] memory results);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     *  Thirdweb's `Ownable` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *  who the 'owner' of the inheriting smart contract is, and lets the inheriting contract perform conditional logic that uses
     *  information about who the contract's owner is.
     */
    interface IOwnable {
        /// @dev Returns the owner of the contract.
        function owner() external view returns (address);
        /// @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;
        /// @dev Emitted when a new Owner is set.
        event OwnerUpdated(address indexed prevOwner, address indexed newOwner);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     * @dev External interface of AccessControl declared to support ERC165 detection.
     */
    interface IPermissions {
        /**
         * @dev Emitted when `newAdminRole` is set as ``role``'s admin role, replacing `previousAdminRole`
         *
         * `DEFAULT_ADMIN_ROLE` is the starting admin for all roles, despite
         * {RoleAdminChanged} not being emitted signaling this.
         *
         * _Available since v3.1._
         */
        event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole);
        /**
         * @dev Emitted when `account` is granted `role`.
         *
         * `sender` is the account that originated the contract call, an admin role
         * bearer except when using {AccessControl-_setupRole}.
         */
        event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender);
        /**
         * @dev Emitted when `account` is revoked `role`.
         *
         * `sender` is the account that originated the contract call:
         *   - if using `revokeRole`, it is the admin role bearer
         *   - if using `renounceRole`, it is the role bearer (i.e. `account`)
         */
        event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender);
        /**
         * @dev Returns `true` if `account` has been granted `role`.
         */
        function hasRole(bytes32 role, address account) external view returns (bool);
        /**
         * @dev Returns the admin role that controls `role`. See {grantRole} and
         * {revokeRole}.
         *
         * To change a role's admin, use {AccessControl-_setRoleAdmin}.
         */
        function getRoleAdmin(bytes32 role) external view returns (bytes32);
        /**
         * @dev Grants `role` to `account`.
         *
         * If `account` had not been already granted `role`, emits a {RoleGranted}
         * event.
         *
         * Requirements:
         *
         * - the caller must have ``role``'s admin role.
         */
        function grantRole(bytes32 role, address account) external;
        /**
         * @dev Revokes `role` from `account`.
         *
         * If `account` had been granted `role`, emits a {RoleRevoked} event.
         *
         * Requirements:
         *
         * - the caller must have ``role``'s admin role.
         */
        function revokeRole(bytes32 role, address account) external;
        /**
         * @dev Revokes `role` from the calling account.
         *
         * Roles are often managed via {grantRole} and {revokeRole}: this function's
         * purpose is to provide a mechanism for accounts to lose their privileges
         * if they are compromised (such as when a trusted device is misplaced).
         *
         * If the calling account had been granted `role`, emits a {RoleRevoked}
         * event.
         *
         * Requirements:
         *
         * - the caller must be `account`.
         */
        function renounceRole(bytes32 role, address account) external;
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "./IPermissions.sol";
    /**
     * @dev External interface of AccessControlEnumerable declared to support ERC165 detection.
     */
    interface IPermissionsEnumerable is IPermissions {
        /**
         * @dev Returns one of the accounts that have `role`. `index` must be a
         * value between 0 and {getRoleMemberCount}, non-inclusive.
         *
         * Role bearers are not sorted in any particular way, and their ordering may
         * change at any point.
         *
         * WARNING: When using {getRoleMember} and {getRoleMemberCount}, make sure
         * you perform all queries on the same block. See the following
         * [forum post](https://forum.openzeppelin.com/t/iterating-over-elements-on-enumerableset-in-openzeppelin-contracts/2296)
         * for more information.
         */
        function getRoleMember(bytes32 role, uint256 index) external view returns (address);
        /**
         * @dev Returns the number of accounts that have `role`. Can be used
         * together with {getRoleMember} to enumerate all bearers of a role.
         */
        function getRoleMemberCount(bytes32 role) external view returns (uint256);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     *  Thirdweb's `PlatformFee` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *  the recipient of platform fee and the platform fee basis points, and lets the inheriting contract perform conditional logic
     *  that uses information about platform fees, if desired.
     */
    interface IPlatformFee {
        /// @dev Fee type variants: percentage fee and flat fee
        enum PlatformFeeType {
            Bps,
            Flat
        }
        /// @dev Returns the platform fee bps and recipient.
        function getPlatformFeeInfo() external view returns (address, uint16);
        /// @dev Lets a module admin update the fees on primary sales.
        function setPlatformFeeInfo(address _platformFeeRecipient, uint256 _platformFeeBps) external;
        /// @dev Emitted when fee on primary sales is updated.
        event PlatformFeeInfoUpdated(address indexed platformFeeRecipient, uint256 platformFeeBps);
        /// @dev Emitted when the flat platform fee is updated.
        event FlatPlatformFeeUpdated(address platformFeeRecipient, uint256 flatFee);
        /// @dev Emitted when the platform fee type is updated.
        event PlatformFeeTypeUpdated(PlatformFeeType feeType);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     *  Thirdweb's `Primary` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *  the recipient of primary sales, and lets the inheriting contract perform conditional logic that uses information about
     *  primary sales, if desired.
     */
    interface IPrimarySale {
        /// @dev The adress that receives all primary sales value.
        function primarySaleRecipient() external view returns (address);
        /// @dev Lets a module admin set the default recipient of all primary sales.
        function setPrimarySaleRecipient(address _saleRecipient) external;
        /// @dev Emitted when a new sale recipient is set.
        event PrimarySaleRecipientUpdated(address indexed recipient);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    import "../../eip/interface/IERC2981.sol";
    /**
     *  Thirdweb's `Royalty` is a contract extension to be used with any base contract. It exposes functions for setting and reading
     *  the recipient of royalty fee and the royalty fee basis points, and lets the inheriting contract perform conditional logic
     *  that uses information about royalty fees, if desired.
     *
     *  The `Royalty` contract is ERC2981 compliant.
     */
    interface IRoyalty is IERC2981 {
        struct RoyaltyInfo {
            address recipient;
            uint256 bps;
        }
        /// @dev Returns the royalty recipient and fee bps.
        function getDefaultRoyaltyInfo() external view returns (address, uint16);
        /// @dev Lets a module admin update the royalty bps and recipient.
        function setDefaultRoyaltyInfo(address _royaltyRecipient, uint256 _royaltyBps) external;
        /// @dev Lets a module admin set the royalty recipient for a particular token Id.
        function setRoyaltyInfoForToken(uint256 tokenId, address recipient, uint256 bps) external;
        /// @dev Returns the royalty recipient for a particular token Id.
        function getRoyaltyInfoForToken(uint256 tokenId) external view returns (address, uint16);
        /// @dev Emitted when royalty info is updated.
        event DefaultRoyalty(address indexed newRoyaltyRecipient, uint256 newRoyaltyBps);
        /// @dev Emitted when royalty recipient for tokenId is set
        event RoyaltyForToken(uint256 indexed tokenId, address indexed royaltyRecipient, uint256 royaltyBps);
    }
    // SPDX-License-Identifier: Apache 2.0
    pragma solidity ^0.8.10;
    /// @author thirdweb
    interface ISharedMetadata {
        /// @notice Emitted when shared metadata is lazy minted.
        event SharedMetadataUpdated(string name, string description, string imageURI, string animationURI);
        /**
         *  @notice Structure for metadata shared across all tokens
         *
         *  @param name Shared name of NFT in metadata
         *  @param description Shared description of NFT in metadata
         *  @param imageURI Shared URI of image to render for NFTs
         *  @param animationURI Shared URI of animation to render for NFTs
         */
        struct SharedMetadataInfo {
            string name;
            string description;
            string imageURI;
            string animationURI;
        }
        /**
         *  @notice Set shared metadata for NFTs
         *  @param _metadata common metadata for all tokens
         */
        function setSharedMetadata(SharedMetadataInfo calldata _metadata) external;
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts v4.4.0 (metatx/ERC2771Context.sol)
    pragma solidity ^0.8.11;
    import "@openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol";
    import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
    /**
     * @dev Context variant with ERC2771 support.
     */
    abstract contract ERC2771ContextUpgradeable is Initializable, ContextUpgradeable {
        mapping(address => bool) private _trustedForwarder;
        function __ERC2771Context_init(address[] memory trustedForwarder) internal onlyInitializing {
            __Context_init_unchained();
            __ERC2771Context_init_unchained(trustedForwarder);
        }
        function __ERC2771Context_init_unchained(address[] memory trustedForwarder) internal onlyInitializing {
            for (uint256 i = 0; i < trustedForwarder.length; i++) {
                _trustedForwarder[trustedForwarder[i]] = true;
            }
        }
        function isTrustedForwarder(address forwarder) public view virtual returns (bool) {
            return _trustedForwarder[forwarder];
        }
        function _msgSender() internal view virtual override returns (address sender) {
            if (isTrustedForwarder(msg.sender)) {
                // The assembly code is more direct than the Solidity version using `abi.decode`.
                assembly {
                    sender := shr(96, calldataload(sub(calldatasize(), 20)))
                }
            } else {
                return super._msgSender();
            }
        }
        function _msgData() internal view virtual override returns (bytes calldata) {
            if (isTrustedForwarder(msg.sender)) {
                return msg.data[:msg.data.length - 20];
            } else {
                return super._msgData();
            }
        }
        uint256[49] private __gap;
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts v4.4.1 (token/ERC20/utils/SafeERC20.sol)
    pragma solidity ^0.8.0;
    import "../../../../../eip/interface/IERC20.sol";
    import { Address } from "@openzeppelin/contracts/utils/Address.sol";
    /**
     * @title SafeERC20
     * @dev Wrappers around ERC20 operations that throw on failure (when the token
     * contract returns false). Tokens that return no value (and instead revert or
     * throw on failure) are also supported, non-reverting calls are assumed to be
     * successful.
     * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
     * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
     */
    library SafeERC20 {
        using Address for address;
        function safeTransfer(IERC20 token, address to, uint256 value) internal {
            _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
        }
        function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
            _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
        }
        /**
         * @dev Deprecated. This function has issues similar to the ones found in
         * {IERC20-approve}, and its usage is discouraged.
         *
         * Whenever possible, use {safeIncreaseAllowance} and
         * {safeDecreaseAllowance} instead.
         */
        function safeApprove(IERC20 token, address spender, uint256 value) internal {
            // safeApprove should only be called when setting an initial allowance,
            // or when resetting it to zero. To increase and decrease it, use
            // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
            require(
                (value == 0) || (token.allowance(address(this), spender) == 0),
                "SafeERC20: approve from non-zero to non-zero allowance"
            );
            _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
        }
        function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
            uint256 newAllowance = token.allowance(address(this), spender) + value;
            _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
        }
        function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
            unchecked {
                uint256 oldAllowance = token.allowance(address(this), spender);
                require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
                uint256 newAllowance = oldAllowance - value;
                _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
            }
        }
        /**
         * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
         * on the return value: the return value is optional (but if data is returned, it must not be false).
         * @param token The token targeted by the call.
         * @param data The call data (encoded using abi.encode or one of its variants).
         */
        function _callOptionalReturn(IERC20 token, bytes memory data) private {
            // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
            // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
            // the target address contains contract code and also asserts for success in the low-level call.
            bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
            if (returndata.length > 0) {
                // Return data is optional
                require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
            }
        }
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.7.0) (utils/Base64.sol)
    pragma solidity ^0.8.0;
    /**
     * @dev Provides a set of functions to operate with Base64 strings.
     *
     * _Available since v4.5._
     */
    library Base64 {
        /**
         * @dev Base64 Encoding/Decoding Table
         */
        string internal constant _TABLE = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
        /**
         * @dev Converts a `bytes` to its Bytes64 `string` representation.
         */
        function encode(bytes memory data) internal pure returns (string memory) {
            /**
             * Inspired by Brecht Devos (Brechtpd) implementation - MIT licence
             * https://github.com/Brechtpd/base64/blob/e78d9fd951e7b0977ddca77d92dc85183770daf4/base64.sol
             */
            if (data.length == 0) return "";
            // Loads the table into memory
            string memory table = _TABLE;
            // Encoding takes 3 bytes chunks of binary data from `bytes` data parameter
            // and split into 4 numbers of 6 bits.
            // The final Base64 length should be `bytes` data length multiplied by 4/3 rounded up
            // - `data.length + 2`  -> Round up
            // - `/ 3`              -> Number of 3-bytes chunks
            // - `4 *`              -> 4 characters for each chunk
            string memory result = new string(4 * ((data.length + 2) / 3));
            /// @solidity memory-safe-assembly
            assembly {
                // Prepare the lookup table (skip the first "length" byte)
                let tablePtr := add(table, 1)
                // Prepare result pointer, jump over length
                let resultPtr := add(result, 32)
                // Run over the input, 3 bytes at a time
                for {
                    let dataPtr := data
                    let endPtr := add(data, mload(data))
                } lt(dataPtr, endPtr) {
                } {
                    // Advance 3 bytes
                    dataPtr := add(dataPtr, 3)
                    let input := mload(dataPtr)
                    // To write each character, shift the 3 bytes (18 bits) chunk
                    // 4 times in blocks of 6 bits for each character (18, 12, 6, 0)
                    // and apply logical AND with 0x3F which is the number of
                    // the previous character in the ASCII table prior to the Base64 Table
                    // The result is then added to the table to get the character to write,
                    // and finally write it in the result pointer but with a left shift
                    // of 256 (1 byte) - 8 (1 ASCII char) = 248 bits
                    mstore8(resultPtr, mload(add(tablePtr, and(shr(18, input), 0x3F))))
                    resultPtr := add(resultPtr, 1) // Advance
                    mstore8(resultPtr, mload(add(tablePtr, and(shr(12, input), 0x3F))))
                    resultPtr := add(resultPtr, 1) // Advance
                    mstore8(resultPtr, mload(add(tablePtr, and(shr(6, input), 0x3F))))
                    resultPtr := add(resultPtr, 1) // Advance
                    mstore8(resultPtr, mload(add(tablePtr, and(input, 0x3F))))
                    resultPtr := add(resultPtr, 1) // Advance
                }
                // When data `bytes` is not exactly 3 bytes long
                // it is padded with `=` characters at the end
                switch mod(mload(data), 3)
                case 1 {
                    mstore8(sub(resultPtr, 1), 0x3d)
                    mstore8(sub(resultPtr, 2), 0x3d)
                }
                case 2 {
                    mstore8(sub(resultPtr, 1), 0x3d)
                }
            }
            return result;
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    interface IWETH {
        function deposit() external payable;
        function withdraw(uint256 amount) external;
        function transfer(address to, uint256 value) external returns (bool);
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.1;
    /// @author thirdweb, OpenZeppelin Contracts (v4.9.0)
    /**
     * @dev Collection of functions related to the address type
     */
    library Address {
        /**
         * @dev Returns true if `account` is a contract.
         *
         * [IMPORTANT]
         * ====
         * It is unsafe to assume that an address for which this function returns
         * false is an externally-owned account (EOA) and not a contract.
         *
         * Among others, `isContract` will return false for the following
         * types of addresses:
         *
         *  - an externally-owned account
         *  - a contract in construction
         *  - an address where a contract will be created
         *  - an address where a contract lived, but was destroyed
         *
         * Furthermore, `isContract` will also return true if the target contract within
         * the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
         * which only has an effect at the end of a transaction.
         * ====
         *
         * [IMPORTANT]
         * ====
         * You shouldn't rely on `isContract` to protect against flash loan attacks!
         *
         * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
         * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
         * constructor.
         * ====
         */
        function isContract(address account) internal view returns (bool) {
            // This method relies on extcodesize/address.code.length, which returns 0
            // for contracts in construction, since the code is only stored at the end
            // of the constructor execution.
            return account.code.length > 0;
        }
        /**
         * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
         * `recipient`, forwarding all available gas and reverting on errors.
         *
         * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
         * of certain opcodes, possibly making contracts go over the 2300 gas limit
         * imposed by `transfer`, making them unable to receive funds via
         * `transfer`. {sendValue} removes this limitation.
         *
         * https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
         *
         * IMPORTANT: because control is transferred to `recipient`, care must be
         * taken to not create reentrancy vulnerabilities. Consider using
         * {ReentrancyGuard} or the
         * https://solidity.readthedocs.io/en/v0.8.0/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
         */
        function sendValue(address payable recipient, uint256 amount) internal {
            require(address(this).balance >= amount, "Address: insufficient balance");
            (bool success, ) = recipient.call{ value: amount }("");
            require(success, "Address: unable to send value, recipient may have reverted");
        }
        /**
         * @dev Performs a Solidity function call using a low level `call`. A
         * plain `call` is an unsafe replacement for a function call: use this
         * function instead.
         *
         * If `target` reverts with a revert reason, it is bubbled up by this
         * function (like regular Solidity function calls).
         *
         * Returns the raw returned data. To convert to the expected return value,
         * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
         *
         * Requirements:
         *
         * - `target` must be a contract.
         * - calling `target` with `data` must not revert.
         *
         * _Available since v3.1._
         */
        function functionCall(address target, bytes memory data) internal returns (bytes memory) {
            return functionCallWithValue(target, data, 0, "Address: low-level call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
         * `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal returns (bytes memory) {
            return functionCallWithValue(target, data, 0, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but also transferring `value` wei to `target`.
         *
         * Requirements:
         *
         * - the calling contract must have an ETH balance of at least `value`.
         * - the called Solidity function must be `payable`.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
            return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
        }
        /**
         * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
         * with `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(
            address target,
            bytes memory data,
            uint256 value,
            string memory errorMessage
        ) internal returns (bytes memory) {
            require(address(this).balance >= value, "Address: insufficient balance for call");
            (bool success, bytes memory returndata) = target.call{ value: value }(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
            return functionStaticCall(target, data, "Address: low-level static call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal view returns (bytes memory) {
            (bool success, bytes memory returndata) = target.staticcall(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but performing a delegate call.
         *
         * _Available since v3.4._
         */
        function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
            return functionDelegateCall(target, data, "Address: low-level delegate call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
         * but performing a delegate call.
         *
         * _Available since v3.4._
         */
        function functionDelegateCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal returns (bytes memory) {
            (bool success, bytes memory returndata) = target.delegatecall(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
         * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
         *
         * _Available since v4.8._
         */
        function verifyCallResultFromTarget(
            address target,
            bool success,
            bytes memory returndata,
            string memory errorMessage
        ) internal view returns (bytes memory) {
            if (success) {
                if (returndata.length == 0) {
                    // only check isContract if the call was successful and the return data is empty
                    // otherwise we already know that it was a contract
                    require(isContract(target), "Address: call to non-contract");
                }
                return returndata;
            } else {
                _revert(returndata, errorMessage);
            }
        }
        /**
         * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
         * revert reason or using the provided one.
         *
         * _Available since v4.3._
         */
        function verifyCallResult(
            bool success,
            bytes memory returndata,
            string memory errorMessage
        ) internal pure returns (bytes memory) {
            if (success) {
                return returndata;
            } else {
                _revert(returndata, errorMessage);
            }
        }
        function _revert(bytes memory returndata, string memory errorMessage) private pure {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly
                /// @solidity memory-safe-assembly
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    // Helper interfaces
    import { IWETH } from "../infra/interface/IWETH.sol";
    import { SafeERC20, IERC20 } from "../external-deps/openzeppelin/token/ERC20/utils/SafeERC20.sol";
    library CurrencyTransferLib {
        using SafeERC20 for IERC20;
        /// @dev The address interpreted as native token of the chain.
        address public constant NATIVE_TOKEN = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
        /// @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) {
                safeTransferNativeToken(_to, _amount);
            } else {
                safeTransferERC20(_currency, _from, _to, _amount);
            }
        }
        /// @dev Transfers a given amount of currency. (With native token wrapping)
        function transferCurrencyWithWrapper(
            address _currency,
            address _from,
            address _to,
            uint256 _amount,
            address _nativeTokenWrapper
        ) internal {
            if (_amount == 0) {
                return;
            }
            if (_currency == NATIVE_TOKEN) {
                if (_from == address(this)) {
                    // withdraw from weth then transfer withdrawn native token to recipient
                    IWETH(_nativeTokenWrapper).withdraw(_amount);
                    safeTransferNativeTokenWithWrapper(_to, _amount, _nativeTokenWrapper);
                } else if (_to == address(this)) {
                    // store native currency in weth
                    require(_amount == msg.value, "msg.value != amount");
                    IWETH(_nativeTokenWrapper).deposit{ value: _amount }();
                } else {
                    safeTransferNativeTokenWithWrapper(_to, _amount, _nativeTokenWrapper);
                }
            } else {
                safeTransferERC20(_currency, _from, _to, _amount);
            }
        }
        /// @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;
            }
            if (_from == address(this)) {
                IERC20(_currency).safeTransfer(_to, _amount);
            } else {
                IERC20(_currency).safeTransferFrom(_from, _to, _amount);
            }
        }
        /// @dev Transfers `amount` of native token to `to`.
        function safeTransferNativeToken(address to, uint256 value) internal {
            // solhint-disable avoid-low-level-calls
            // slither-disable-next-line low-level-calls
            (bool success, ) = to.call{ value: value }("");
            require(success, "native token transfer failed");
        }
        /// @dev Transfers `amount` of native token to `to`. (With native token wrapping)
        function safeTransferNativeTokenWithWrapper(address to, uint256 value, address _nativeTokenWrapper) internal {
            // solhint-disable avoid-low-level-calls
            // slither-disable-next-line low-level-calls
            (bool success, ) = to.call{ value: value }("");
            if (!success) {
                IWETH(_nativeTokenWrapper).deposit{ value: value }();
                IERC20(_nativeTokenWrapper).safeTransfer(to, value);
            }
        }
    }
    // SPDX-License-Identifier: Apache 2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    library MerkleProof {
        function verify(bytes32[] memory proof, bytes32 root, bytes32 leaf) internal pure returns (bool, uint256) {
            bytes32 computedHash = leaf;
            uint256 index = 0;
            for (uint256 i = 0; i < proof.length; i++) {
                index *= 2;
                bytes32 proofElement = proof[i];
                if (computedHash <= proofElement) {
                    // Hash(current computed hash + current element of the proof)
                    computedHash = keccak256(abi.encodePacked(computedHash, proofElement));
                } else {
                    // Hash(current element of the proof + current computed hash)
                    computedHash = keccak256(abi.encodePacked(proofElement, computedHash));
                    index += 1;
                }
            }
            // Check if the computed hash (root) is equal to the provided root
            return (computedHash == root, index);
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /* solhint-disable quotes */
    /// @author thirdweb
    /// credits: Zora
    import "./Strings.sol";
    import "../external-deps/openzeppelin/utils/Base64.sol";
    /// NFT metadata library for rendering metadata associated with editions
    library NFTMetadataRenderer {
        /**
         *  @notice Generate edition metadata from storage information as base64-json blob
         *  @dev Combines the media data and metadata
         * @param name Name of NFT in metadata
         * @param description Description of NFT in metadata
         * @param imageURI URI of image to render for edition
         * @param animationURI URI of animation to render for edition
         * @param tokenOfEdition Token ID for specific token
         */
        function createMetadataEdition(
            string memory name,
            string memory description,
            string memory imageURI,
            string memory animationURI,
            uint256 tokenOfEdition
        ) internal pure returns (string memory) {
            string memory _tokenMediaData = tokenMediaData(imageURI, animationURI);
            bytes memory json = createMetadataJSON(name, description, _tokenMediaData, tokenOfEdition);
            return encodeMetadataJSON(json);
        }
        /**
         * @param name Name of NFT in metadata
         * @param description Description of NFT in metadata
         * @param mediaData Data for media to include in json object
         * @param tokenOfEdition Token ID for specific token
         */
        function createMetadataJSON(
            string memory name,
            string memory description,
            string memory mediaData,
            uint256 tokenOfEdition
        ) internal pure returns (bytes memory) {
            return
                abi.encodePacked(
                    '{"name": "',
                    name,
                    " ",
                    Strings.toString(tokenOfEdition),
                    '", "',
                    'description": "',
                    description,
                    '", "',
                    mediaData,
                    'properties": {"number": ',
                    Strings.toString(tokenOfEdition),
                    ', "name": "',
                    name,
                    '"}}'
                );
        }
        /// Encodes the argument json bytes into base64-data uri format
        /// @param json Raw json to base64 and turn into a data-uri
        function encodeMetadataJSON(bytes memory json) internal pure returns (string memory) {
            return string(abi.encodePacked("data:application/json;base64,", Base64.encode(json)));
        }
        /// Generates edition metadata from storage information as base64-json blob
        /// Combines the media data and metadata
        /// @param imageUrl URL of image to render for edition
        /// @param animationUrl URL of animation to render for edition
        function tokenMediaData(string memory imageUrl, string memory animationUrl) internal pure returns (string memory) {
            bool hasImage = bytes(imageUrl).length > 0;
            bool hasAnimation = bytes(animationUrl).length > 0;
            if (hasImage && hasAnimation) {
                return string(abi.encodePacked('image": "', imageUrl, '", "animation_url": "', animationUrl, '", "'));
            }
            if (hasImage) {
                return string(abi.encodePacked('image": "', imageUrl, '", "'));
            }
            if (hasAnimation) {
                return string(abi.encodePacked('animation_url": "', animationUrl, '", "'));
            }
            return "";
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.0;
    /// @author thirdweb
    /**
     * @dev String operations.
     */
    library Strings {
        bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";
        /**
         * @dev Converts a `uint256` to its ASCII `string` decimal representation.
         */
        function toString(uint256 value) internal pure returns (string memory) {
            // Inspired by OraclizeAPI's implementation - MIT licence
            // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
            if (value == 0) {
                return "0";
            }
            uint256 temp = value;
            uint256 digits;
            while (temp != 0) {
                digits++;
                temp /= 10;
            }
            bytes memory buffer = new bytes(digits);
            while (value != 0) {
                digits -= 1;
                buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
                value /= 10;
            }
            return string(buffer);
        }
        /**
         * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
         */
        function toHexString(uint256 value) internal pure returns (string memory) {
            if (value == 0) {
                return "0x00";
            }
            uint256 temp = value;
            uint256 length = 0;
            while (temp != 0) {
                length++;
                temp >>= 8;
            }
            return toHexString(value, length);
        }
        /**
         * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
         */
        function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
            bytes memory buffer = new bytes(2 * length + 2);
            buffer[0] = "0";
            buffer[1] = "x";
            for (uint256 i = 2 * length + 1; i > 1; --i) {
                buffer[i] = _HEX_SYMBOLS[value & 0xf];
                value >>= 4;
            }
            require(value == 0, "Strings: hex length insufficient");
            return string(buffer);
        }
        /// @dev Returns the hexadecimal representation of `value`.
        /// The output is prefixed with "0x", encoded using 2 hexadecimal digits per byte,
        /// and the alphabets are capitalized conditionally according to
        /// https://eips.ethereum.org/EIPS/eip-55
        function toHexStringChecksummed(address value) internal pure returns (string memory str) {
            str = toHexString(value);
            /// @solidity memory-safe-assembly
            assembly {
                let mask := shl(6, div(not(0), 255)) // `0b010000000100000000 ...`
                let o := add(str, 0x22)
                let hashed := and(keccak256(o, 40), mul(34, mask)) // `0b10001000 ... `
                let t := shl(240, 136) // `0b10001000 << 240`
                for {
                    let i := 0
                } 1 {
                } {
                    mstore(add(i, i), mul(t, byte(i, hashed)))
                    i := add(i, 1)
                    if eq(i, 20) {
                        break
                    }
                }
                mstore(o, xor(mload(o), shr(1, and(mload(0x00), and(mload(o), mask)))))
                o := add(o, 0x20)
                mstore(o, xor(mload(o), shr(1, and(mload(0x20), and(mload(o), mask)))))
            }
        }
        /// @dev Returns the hexadecimal representation of `value`.
        /// The output is prefixed with "0x" and encoded using 2 hexadecimal digits per byte.
        function toHexString(address value) internal pure returns (string memory str) {
            str = toHexStringNoPrefix(value);
            /// @solidity memory-safe-assembly
            assembly {
                let strLength := add(mload(str), 2) // Compute the length.
                mstore(str, 0x3078) // Write the "0x" prefix.
                str := sub(str, 2) // Move the pointer.
                mstore(str, strLength) // Write the length.
            }
        }
        /// @dev Returns the hexadecimal representation of `value`.
        /// The output is encoded using 2 hexadecimal digits per byte.
        function toHexStringNoPrefix(address value) internal pure returns (string memory str) {
            /// @solidity memory-safe-assembly
            assembly {
                str := mload(0x40)
                // Allocate the memory.
                // We need 0x20 bytes for the trailing zeros padding, 0x20 bytes for the length,
                // 0x02 bytes for the prefix, and 0x28 bytes for the digits.
                // The next multiple of 0x20 above (0x20 + 0x20 + 0x02 + 0x28) is 0x80.
                mstore(0x40, add(str, 0x80))
                // Store "0123456789abcdef" in scratch space.
                mstore(0x0f, 0x30313233343536373839616263646566)
                str := add(str, 2)
                mstore(str, 40)
                let o := add(str, 0x20)
                mstore(add(o, 40), 0)
                value := shl(96, value)
                // We write the string from rightmost digit to leftmost digit.
                // The following is essentially a do-while loop that also handles the zero case.
                for {
                    let i := 0
                } 1 {
                } {
                    let p := add(o, add(i, i))
                    let temp := byte(i, value)
                    mstore8(add(p, 1), mload(and(temp, 15)))
                    mstore8(p, mload(shr(4, temp)))
                    i := add(i, 1)
                    if eq(i, 20) {
                        break
                    }
                }
            }
        }
        /// @dev Returns the hex encoded string from the raw bytes.
        /// The output is encoded using 2 hexadecimal digits per byte.
        function toHexString(bytes memory raw) internal pure returns (string memory str) {
            str = toHexStringNoPrefix(raw);
            /// @solidity memory-safe-assembly
            assembly {
                let strLength := add(mload(str), 2) // Compute the length.
                mstore(str, 0x3078) // Write the "0x" prefix.
                str := sub(str, 2) // Move the pointer.
                mstore(str, strLength) // Write the length.
            }
        }
        /// @dev Returns the hex encoded string from the raw bytes.
        /// The output is encoded using 2 hexadecimal digits per byte.
        function toHexStringNoPrefix(bytes memory raw) internal pure returns (string memory str) {
            /// @solidity memory-safe-assembly
            assembly {
                let length := mload(raw)
                str := add(mload(0x40), 2) // Skip 2 bytes for the optional prefix.
                mstore(str, add(length, length)) // Store the length of the output.
                // Store "0123456789abcdef" in scratch space.
                mstore(0x0f, 0x30313233343536373839616263646566)
                let o := add(str, 0x20)
                let end := add(raw, length)
                for {
                } iszero(eq(raw, end)) {
                } {
                    raw := add(raw, 1)
                    mstore8(add(o, 1), mload(and(mload(raw), 15)))
                    mstore8(o, mload(and(shr(4, mload(raw)), 15)))
                    o := add(o, 2)
                }
                mstore(o, 0) // Zeroize the slot after the string.
                mstore(0x40, add(o, 0x20)) // Allocate the memory.
            }
        }
    }
    // SPDX-License-Identifier: Apache-2.0
    pragma solidity ^0.8.11;
    /// @author thirdweb
    //   $$\\     $$\\       $$\\                 $$\\                         $$\\
    //   $$ |    $$ |      \\__|                $$ |                        $$ |
    // $$$$$$\\   $$$$$$$\\  $$\\  $$$$$$\\   $$$$$$$ |$$\\  $$\\  $$\\  $$$$$$\\  $$$$$$$\\
    // \\_$$  _|  $$  __$$\\ $$ |$$  __$$\\ $$  __$$ |$$ | $$ | $$ |$$  __$$\\ $$  __$$\\
    //   $$ |    $$ |  $$ |$$ |$$ |  \\__|$$ /  $$ |$$ | $$ | $$ |$$$$$$$$ |$$ |  $$ |
    //   $$ |$$\\ $$ |  $$ |$$ |$$ |      $$ |  $$ |$$ | $$ | $$ |$$   ____|$$ |  $$ |
    //   \\$$$$  |$$ |  $$ |$$ |$$ |      \\$$$$$$$ |\\$$$$$\\$$$$  |\\$$$$$$$\\ $$$$$$$  |
    //    \\____/ \\__|  \\__|\\__|\\__|       \\_______| \\_____\\____/  \\_______|\\_______/
    //  ==========  External imports    ==========
    import "@openzeppelin/contracts-upgradeable/utils/StringsUpgradeable.sol";
    import "@openzeppelin/contracts-upgradeable/interfaces/IERC2981Upgradeable.sol";
    import "../../eip/queryable/ERC721AQueryableUpgradeable.sol";
    //  ==========  Internal imports    ==========
    import "../../external-deps/openzeppelin/metatx/ERC2771ContextUpgradeable.sol";
    import "../../lib/CurrencyTransferLib.sol";
    //  ==========  Features    ==========
    import "../../extension/Multicall.sol";
    import "../../extension/ContractMetadata.sol";
    import "../../extension/Royalty.sol";
    import "../../extension/PrimarySale.sol";
    import "../../extension/Ownable.sol";
    import "../../extension/SharedMetadata.sol";
    import "../../extension/PermissionsEnumerable.sol";
    import "../../extension/Drop.sol";
    import "../../extension/PlatformFee.sol";
    contract OpenEditionERC721FlatFee is
        Initializable,
        ContractMetadata,
        PlatformFee,
        Royalty,
        PrimarySale,
        Ownable,
        SharedMetadata,
        PermissionsEnumerable,
        Drop,
        ERC2771ContextUpgradeable,
        Multicall,
        ERC721AQueryableUpgradeable
    {
        using StringsUpgradeable for uint256;
        /*///////////////////////////////////////////////////////////////
                                State variables
        //////////////////////////////////////////////////////////////*/
        /// @dev Only transfers to or from TRANSFER_ROLE holders are valid, when transfers are restricted.
        bytes32 private transferRole;
        /// @dev Only MINTER_ROLE holders can update the shared metadata of tokens.
        bytes32 private minterRole;
        /// @dev Max bps in the thirdweb system.
        uint256 private constant MAX_BPS = 10_000;
        /*///////////////////////////////////////////////////////////////
                        Constructor + initializer logic
        //////////////////////////////////////////////////////////////*/
        constructor() initializer {}
        /// @dev Initializes the contract, like a constructor.
        function initialize(
            address _defaultAdmin,
            string memory _name,
            string memory _symbol,
            string memory _contractURI,
            address[] memory _trustedForwarders,
            address _saleRecipient,
            address _royaltyRecipient,
            uint128 _royaltyBps,
            uint128 _platformFeeBps,
            address _platformFeeRecipient
        ) external initializerERC721A initializer {
            bytes32 _transferRole = keccak256("TRANSFER_ROLE");
            bytes32 _minterRole = keccak256("MINTER_ROLE");
            // Initialize inherited contracts, most base-like -> most derived.
            __ERC2771Context_init(_trustedForwarders);
            __ERC721A_init(_name, _symbol);
            _setupContractURI(_contractURI);
            _setupOwner(_defaultAdmin);
            _setupRole(DEFAULT_ADMIN_ROLE, _defaultAdmin);
            _setupRole(_minterRole, _defaultAdmin);
            _setupRole(_transferRole, _defaultAdmin);
            _setupRole(_transferRole, address(0));
            _setupPlatformFeeInfo(_platformFeeRecipient, _platformFeeBps);
            _setupDefaultRoyaltyInfo(_royaltyRecipient, _royaltyBps);
            _setupPrimarySaleRecipient(_saleRecipient);
            transferRole = _transferRole;
            minterRole = _minterRole;
        }
        /*///////////////////////////////////////////////////////////////
                            ERC 165 / 721 / 2981 logic
        //////////////////////////////////////////////////////////////*/
        /// @dev Returns the URI for a given tokenId.
        function tokenURI(
            uint256 _tokenId
        ) public view virtual override(ERC721AUpgradeable, IERC721AUpgradeable) returns (string memory) {
            if (!_exists(_tokenId)) {
                revert("!ID");
            }
            return _getURIFromSharedMetadata(_tokenId);
        }
        /// @dev See ERC 165
        function supportsInterface(
            bytes4 interfaceId
        ) public view virtual override(ERC721AUpgradeable, IERC165, IERC721AUpgradeable) returns (bool) {
            return super.supportsInterface(interfaceId) || type(IERC2981Upgradeable).interfaceId == interfaceId;
        }
        /// @dev The start token ID for the contract.
        function _startTokenId() internal pure override returns (uint256) {
            return 1;
        }
        function startTokenId() public pure returns (uint256) {
            return _startTokenId();
        }
        /*///////////////////////////////////////////////////////////////
                            Internal functions
        //////////////////////////////////////////////////////////////*/
        /// @dev Collects and distributes the primary sale value of NFTs being claimed.
        function _collectPriceOnClaim(
            address _primarySaleRecipient,
            uint256 _quantityToClaim,
            address _currency,
            uint256 _pricePerToken
        ) internal override {
            if (_pricePerToken == 0) {
                require(msg.value == 0, "!Value");
                return;
            }
            uint256 totalPrice = _quantityToClaim * _pricePerToken;
            uint256 platformFees;
            address platformFeeRecipient;
            if (getPlatformFeeType() == IPlatformFee.PlatformFeeType.Flat) {
                (platformFeeRecipient, platformFees) = getFlatPlatformFeeInfo();
            } else {
                (address recipient, uint16 platformFeeBps) = getPlatformFeeInfo();
                platformFeeRecipient = recipient;
                platformFees = ((totalPrice * platformFeeBps) / MAX_BPS);
            }
            require(totalPrice >= platformFees, "price less than platform fee");
            bool validMsgValue;
            if (_currency == CurrencyTransferLib.NATIVE_TOKEN) {
                validMsgValue = msg.value == totalPrice;
            } else {
                validMsgValue = msg.value == 0;
            }
            require(validMsgValue, "!V");
            address saleRecipient = _primarySaleRecipient == address(0) ? primarySaleRecipient() : _primarySaleRecipient;
            CurrencyTransferLib.transferCurrency(_currency, _msgSender(), platformFeeRecipient, platformFees);
            CurrencyTransferLib.transferCurrency(_currency, _msgSender(), saleRecipient, totalPrice - platformFees);
        }
        /// @dev Transfers the NFTs being claimed.
        function _transferTokensOnClaim(
            address _to,
            uint256 _quantityBeingClaimed
        ) internal override returns (uint256 startTokenId_) {
            startTokenId_ = _nextTokenId();
            _safeMint(_to, _quantityBeingClaimed);
        }
        /// @dev Checks whether primary sale recipient can be set in the given execution context.
        function _canSetPrimarySaleRecipient() internal view override returns (bool) {
            return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
        }
        /// @dev Checks whether owner can be set in the given execution context.
        function _canSetOwner() internal view override returns (bool) {
            return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
        }
        /// @dev Checks whether royalty info can be set in the given execution context.
        function _canSetRoyaltyInfo() internal view override returns (bool) {
            return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
        }
        /// @dev Checks whether contract metadata can be set in the given execution context.
        function _canSetContractURI() internal view override returns (bool) {
            return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
        }
        /// @dev Checks whether platform fee info can be set in the given execution context.
        function _canSetClaimConditions() internal view override returns (bool) {
            return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
        }
        /// @dev Returns whether the shared metadata of tokens can be set in the given execution context.
        function _canSetSharedMetadata() internal view virtual override returns (bool) {
            return hasRole(minterRole, _msgSender());
        }
        /// @dev Checks whether platform fee info can be set in the given execution context.
        function _canSetPlatformFeeInfo() internal view override returns (bool) {
            return hasRole(DEFAULT_ADMIN_ROLE, _msgSender());
        }
        /*///////////////////////////////////////////////////////////////
                            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 _nextTokenId();
        }
        /// @dev The next token ID of the NFT that can be claimed.
        function nextTokenIdToClaim() external view returns (uint256) {
            return _nextTokenId();
        }
        /// @dev Burns `tokenId`. See {ERC721-_burn}.
        function burn(uint256 tokenId) external virtual {
            // note: ERC721AUpgradeable's `_burn(uint256,bool)` internally checks for token approvals.
            _burn(tokenId, true);
        }
        /// @dev See {ERC721-_beforeTokenTransfer}.
        function _beforeTokenTransfers(
            address from,
            address to,
            uint256 startTokenId_,
            uint256 quantity
        ) internal virtual override {
            super._beforeTokenTransfers(from, to, startTokenId_, quantity);
            // if transfer is restricted on the contract, we still want to allow burning and minting
            if (!hasRole(transferRole, address(0)) && from != address(0) && to != address(0)) {
                if (!hasRole(transferRole, from) && !hasRole(transferRole, to)) {
                    revert("!T");
                }
            }
        }
        function _dropMsgSender() internal view virtual override returns (address) {
            return _msgSender();
        }
        function _msgSenderERC721A() internal view virtual override returns (address) {
            return _msgSender();
        }
        function _msgSender()
            internal
            view
            virtual
            override(ERC2771ContextUpgradeable, Multicall)
            returns (address sender)
        {
            return ERC2771ContextUpgradeable._msgSender();
        }
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.9.0) (interfaces/IERC2981.sol)
    pragma solidity ^0.8.0;
    import "../utils/introspection/IERC165Upgradeable.sol";
    /**
     * @dev Interface for the NFT Royalty Standard.
     *
     * A standardized way to retrieve royalty payment information for non-fungible tokens (NFTs) to enable universal
     * support for royalty payments across all NFT marketplaces and ecosystem participants.
     *
     * _Available since v4.5._
     */
    interface IERC2981Upgradeable is IERC165Upgradeable {
        /**
         * @dev Returns how much royalty is owed and to whom, based on a sale price that may be denominated in any unit of
         * exchange. The royalty amount is denominated and should be paid in that same unit of exchange.
         */
        function royaltyInfo(
            uint256 tokenId,
            uint256 salePrice
        ) external view returns (address receiver, uint256 royaltyAmount);
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.9.0) (proxy/utils/Initializable.sol)
    pragma solidity ^0.8.2;
    import "../../utils/AddressUpgradeable.sol";
    /**
     * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
     * behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
     * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
     * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
     *
     * The initialization functions use a version number. Once a version number is used, it is consumed and cannot be
     * reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in
     * case an upgrade adds a module that needs to be initialized.
     *
     * For example:
     *
     * [.hljs-theme-light.nopadding]
     * ```solidity
     * contract MyToken is ERC20Upgradeable {
     *     function initialize() initializer public {
     *         __ERC20_init("MyToken", "MTK");
     *     }
     * }
     *
     * contract MyTokenV2 is MyToken, ERC20PermitUpgradeable {
     *     function initializeV2() reinitializer(2) public {
     *         __ERC20Permit_init("MyToken");
     *     }
     * }
     * ```
     *
     * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
     * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
     *
     * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
     * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
     *
     * [CAUTION]
     * ====
     * Avoid leaving a contract uninitialized.
     *
     * An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
     * contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke
     * the {_disableInitializers} function in the constructor to automatically lock it when it is deployed:
     *
     * [.hljs-theme-light.nopadding]
     * ```
     * /// @custom:oz-upgrades-unsafe-allow constructor
     * constructor() {
     *     _disableInitializers();
     * }
     * ```
     * ====
     */
    abstract contract Initializable {
        /**
         * @dev Indicates that the contract has been initialized.
         * @custom:oz-retyped-from bool
         */
        uint8 private _initialized;
        /**
         * @dev Indicates that the contract is in the process of being initialized.
         */
        bool private _initializing;
        /**
         * @dev Triggered when the contract has been initialized or reinitialized.
         */
        event Initialized(uint8 version);
        /**
         * @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope,
         * `onlyInitializing` functions can be used to initialize parent contracts.
         *
         * Similar to `reinitializer(1)`, except that functions marked with `initializer` can be nested in the context of a
         * constructor.
         *
         * Emits an {Initialized} event.
         */
        modifier initializer() {
            bool isTopLevelCall = !_initializing;
            require(
                (isTopLevelCall && _initialized < 1) || (!AddressUpgradeable.isContract(address(this)) && _initialized == 1),
                "Initializable: contract is already initialized"
            );
            _initialized = 1;
            if (isTopLevelCall) {
                _initializing = true;
            }
            _;
            if (isTopLevelCall) {
                _initializing = false;
                emit Initialized(1);
            }
        }
        /**
         * @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the
         * contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be
         * used to initialize parent contracts.
         *
         * A reinitializer may be used after the original initialization step. This is essential to configure modules that
         * are added through upgrades and that require initialization.
         *
         * When `version` is 1, this modifier is similar to `initializer`, except that functions marked with `reinitializer`
         * cannot be nested. If one is invoked in the context of another, execution will revert.
         *
         * Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in
         * a contract, executing them in the right order is up to the developer or operator.
         *
         * WARNING: setting the version to 255 will prevent any future reinitialization.
         *
         * Emits an {Initialized} event.
         */
        modifier reinitializer(uint8 version) {
            require(!_initializing && _initialized < version, "Initializable: contract is already initialized");
            _initialized = version;
            _initializing = true;
            _;
            _initializing = false;
            emit Initialized(version);
        }
        /**
         * @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
         * {initializer} and {reinitializer} modifiers, directly or indirectly.
         */
        modifier onlyInitializing() {
            require(_initializing, "Initializable: contract is not initializing");
            _;
        }
        /**
         * @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call.
         * Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized
         * to any version. It is recommended to use this to lock implementation contracts that are designed to be called
         * through proxies.
         *
         * Emits an {Initialized} event the first time it is successfully executed.
         */
        function _disableInitializers() internal virtual {
            require(!_initializing, "Initializable: contract is initializing");
            if (_initialized != type(uint8).max) {
                _initialized = type(uint8).max;
                emit Initialized(type(uint8).max);
            }
        }
        /**
         * @dev Returns the highest version that has been initialized. See {reinitializer}.
         */
        function _getInitializedVersion() internal view returns (uint8) {
            return _initialized;
        }
        /**
         * @dev Returns `true` if the contract is currently initializing. See {onlyInitializing}.
         */
        function _isInitializing() internal view returns (bool) {
            return _initializing;
        }
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.9.0) (utils/Address.sol)
    pragma solidity ^0.8.1;
    /**
     * @dev Collection of functions related to the address type
     */
    library AddressUpgradeable {
        /**
         * @dev Returns true if `account` is a contract.
         *
         * [IMPORTANT]
         * ====
         * It is unsafe to assume that an address for which this function returns
         * false is an externally-owned account (EOA) and not a contract.
         *
         * Among others, `isContract` will return false for the following
         * types of addresses:
         *
         *  - an externally-owned account
         *  - a contract in construction
         *  - an address where a contract will be created
         *  - an address where a contract lived, but was destroyed
         *
         * Furthermore, `isContract` will also return true if the target contract within
         * the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
         * which only has an effect at the end of a transaction.
         * ====
         *
         * [IMPORTANT]
         * ====
         * You shouldn't rely on `isContract` to protect against flash loan attacks!
         *
         * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
         * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
         * constructor.
         * ====
         */
        function isContract(address account) internal view returns (bool) {
            // This method relies on extcodesize/address.code.length, which returns 0
            // for contracts in construction, since the code is only stored at the end
            // of the constructor execution.
            return account.code.length > 0;
        }
        /**
         * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
         * `recipient`, forwarding all available gas and reverting on errors.
         *
         * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
         * of certain opcodes, possibly making contracts go over the 2300 gas limit
         * imposed by `transfer`, making them unable to receive funds via
         * `transfer`. {sendValue} removes this limitation.
         *
         * https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
         *
         * IMPORTANT: because control is transferred to `recipient`, care must be
         * taken to not create reentrancy vulnerabilities. Consider using
         * {ReentrancyGuard} or the
         * https://solidity.readthedocs.io/en/v0.8.0/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
         */
        function sendValue(address payable recipient, uint256 amount) internal {
            require(address(this).balance >= amount, "Address: insufficient balance");
            (bool success, ) = recipient.call{value: amount}("");
            require(success, "Address: unable to send value, recipient may have reverted");
        }
        /**
         * @dev Performs a Solidity function call using a low level `call`. A
         * plain `call` is an unsafe replacement for a function call: use this
         * function instead.
         *
         * If `target` reverts with a revert reason, it is bubbled up by this
         * function (like regular Solidity function calls).
         *
         * Returns the raw returned data. To convert to the expected return value,
         * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
         *
         * Requirements:
         *
         * - `target` must be a contract.
         * - calling `target` with `data` must not revert.
         *
         * _Available since v3.1._
         */
        function functionCall(address target, bytes memory data) internal returns (bytes memory) {
            return functionCallWithValue(target, data, 0, "Address: low-level call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
         * `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal returns (bytes memory) {
            return functionCallWithValue(target, data, 0, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but also transferring `value` wei to `target`.
         *
         * Requirements:
         *
         * - the calling contract must have an ETH balance of at least `value`.
         * - the called Solidity function must be `payable`.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
            return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
        }
        /**
         * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
         * with `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(
            address target,
            bytes memory data,
            uint256 value,
            string memory errorMessage
        ) internal returns (bytes memory) {
            require(address(this).balance >= value, "Address: insufficient balance for call");
            (bool success, bytes memory returndata) = target.call{value: value}(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
            return functionStaticCall(target, data, "Address: low-level static call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal view returns (bytes memory) {
            (bool success, bytes memory returndata) = target.staticcall(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but performing a delegate call.
         *
         * _Available since v3.4._
         */
        function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
            return functionDelegateCall(target, data, "Address: low-level delegate call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
         * but performing a delegate call.
         *
         * _Available since v3.4._
         */
        function functionDelegateCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal returns (bytes memory) {
            (bool success, bytes memory returndata) = target.delegatecall(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
         * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
         *
         * _Available since v4.8._
         */
        function verifyCallResultFromTarget(
            address target,
            bool success,
            bytes memory returndata,
            string memory errorMessage
        ) internal view returns (bytes memory) {
            if (success) {
                if (returndata.length == 0) {
                    // only check isContract if the call was successful and the return data is empty
                    // otherwise we already know that it was a contract
                    require(isContract(target), "Address: call to non-contract");
                }
                return returndata;
            } else {
                _revert(returndata, errorMessage);
            }
        }
        /**
         * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
         * revert reason or using the provided one.
         *
         * _Available since v4.3._
         */
        function verifyCallResult(
            bool success,
            bytes memory returndata,
            string memory errorMessage
        ) internal pure returns (bytes memory) {
            if (success) {
                return returndata;
            } else {
                _revert(returndata, errorMessage);
            }
        }
        function _revert(bytes memory returndata, string memory errorMessage) private pure {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly
                /// @solidity memory-safe-assembly
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts v4.4.1 (utils/Context.sol)
    pragma solidity ^0.8.0;
    import "../proxy/utils/Initializable.sol";
    /**
     * @dev Provides information about the current execution context, including the
     * sender of the transaction and its data. While these are generally available
     * via msg.sender and msg.data, they should not be accessed in such a direct
     * manner, since when dealing with meta-transactions the account sending and
     * paying for execution may not be the actual sender (as far as an application
     * is concerned).
     *
     * This contract is only required for intermediate, library-like contracts.
     */
    abstract contract ContextUpgradeable is Initializable {
        function __Context_init() internal onlyInitializing {
        }
        function __Context_init_unchained() internal onlyInitializing {
        }
        function _msgSender() internal view virtual returns (address) {
            return msg.sender;
        }
        function _msgData() internal view virtual returns (bytes calldata) {
            return msg.data;
        }
        /**
         * @dev This empty reserved space is put in place to allow future versions to add new
         * variables without shifting down storage in the inheritance chain.
         * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
         */
        uint256[50] private __gap;
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.9.0) (utils/Strings.sol)
    pragma solidity ^0.8.0;
    import "./math/MathUpgradeable.sol";
    import "./math/SignedMathUpgradeable.sol";
    /**
     * @dev String operations.
     */
    library StringsUpgradeable {
        bytes16 private constant _SYMBOLS = "0123456789abcdef";
        uint8 private constant _ADDRESS_LENGTH = 20;
        /**
         * @dev Converts a `uint256` to its ASCII `string` decimal representation.
         */
        function toString(uint256 value) internal pure returns (string memory) {
            unchecked {
                uint256 length = MathUpgradeable.log10(value) + 1;
                string memory buffer = new string(length);
                uint256 ptr;
                /// @solidity memory-safe-assembly
                assembly {
                    ptr := add(buffer, add(32, length))
                }
                while (true) {
                    ptr--;
                    /// @solidity memory-safe-assembly
                    assembly {
                        mstore8(ptr, byte(mod(value, 10), _SYMBOLS))
                    }
                    value /= 10;
                    if (value == 0) break;
                }
                return buffer;
            }
        }
        /**
         * @dev Converts a `int256` to its ASCII `string` decimal representation.
         */
        function toString(int256 value) internal pure returns (string memory) {
            return string(abi.encodePacked(value < 0 ? "-" : "", toString(SignedMathUpgradeable.abs(value))));
        }
        /**
         * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
         */
        function toHexString(uint256 value) internal pure returns (string memory) {
            unchecked {
                return toHexString(value, MathUpgradeable.log256(value) + 1);
            }
        }
        /**
         * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
         */
        function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
            bytes memory buffer = new bytes(2 * length + 2);
            buffer[0] = "0";
            buffer[1] = "x";
            for (uint256 i = 2 * length + 1; i > 1; --i) {
                buffer[i] = _SYMBOLS[value & 0xf];
                value >>= 4;
            }
            require(value == 0, "Strings: hex length insufficient");
            return string(buffer);
        }
        /**
         * @dev Converts an `address` with fixed length of 20 bytes to its not checksummed ASCII `string` hexadecimal representation.
         */
        function toHexString(address addr) internal pure returns (string memory) {
            return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
        }
        /**
         * @dev Returns true if the two strings are equal.
         */
        function equal(string memory a, string memory b) internal pure returns (bool) {
            return keccak256(bytes(a)) == keccak256(bytes(b));
        }
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)
    pragma solidity ^0.8.0;
    /**
     * @dev Interface of the ERC165 standard, as defined in the
     * https://eips.ethereum.org/EIPS/eip-165[EIP].
     *
     * Implementers can declare support of contract interfaces, which can then be
     * queried by others ({ERC165Checker}).
     *
     * For an implementation, see {ERC165}.
     */
    interface IERC165Upgradeable {
        /**
         * @dev Returns true if this contract implements the interface defined by
         * `interfaceId`. See the corresponding
         * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
         * to learn more about how these ids are created.
         *
         * This function call must use less than 30 000 gas.
         */
        function supportsInterface(bytes4 interfaceId) external view returns (bool);
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.9.0) (utils/math/Math.sol)
    pragma solidity ^0.8.0;
    /**
     * @dev Standard math utilities missing in the Solidity language.
     */
    library MathUpgradeable {
        enum Rounding {
            Down, // Toward negative infinity
            Up, // Toward infinity
            Zero // Toward zero
        }
        /**
         * @dev Returns the largest of two numbers.
         */
        function max(uint256 a, uint256 b) internal pure returns (uint256) {
            return a > b ? a : b;
        }
        /**
         * @dev Returns the smallest of two numbers.
         */
        function min(uint256 a, uint256 b) internal pure returns (uint256) {
            return a < b ? a : b;
        }
        /**
         * @dev Returns the average of two numbers. The result is rounded towards
         * zero.
         */
        function average(uint256 a, uint256 b) internal pure returns (uint256) {
            // (a + b) / 2 can overflow.
            return (a & b) + (a ^ b) / 2;
        }
        /**
         * @dev Returns the ceiling of the division of two numbers.
         *
         * This differs from standard division with `/` in that it rounds up instead
         * of rounding down.
         */
        function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
            // (a + b - 1) / b can overflow on addition, so we distribute.
            return a == 0 ? 0 : (a - 1) / b + 1;
        }
        /**
         * @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
         * @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
         * with further edits by Uniswap Labs also under MIT license.
         */
        function mulDiv(uint256 x, uint256 y, uint256 denominator) internal pure returns (uint256 result) {
            unchecked {
                // 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
                // use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
                // variables such that product = prod1 * 2^256 + prod0.
                uint256 prod0; // Least significant 256 bits of the product
                uint256 prod1; // Most significant 256 bits of the product
                assembly {
                    let mm := mulmod(x, y, not(0))
                    prod0 := mul(x, y)
                    prod1 := sub(sub(mm, prod0), lt(mm, prod0))
                }
                // Handle non-overflow cases, 256 by 256 division.
                if (prod1 == 0) {
                    // Solidity will revert if denominator == 0, unlike the div opcode on its own.
                    // The surrounding unchecked block does not change this fact.
                    // See https://docs.soliditylang.org/en/latest/control-structures.html#checked-or-unchecked-arithmetic.
                    return prod0 / denominator;
                }
                // Make sure the result is less than 2^256. Also prevents denominator == 0.
                require(denominator > prod1, "Math: mulDiv overflow");
                ///////////////////////////////////////////////
                // 512 by 256 division.
                ///////////////////////////////////////////////
                // Make division exact by subtracting the remainder from [prod1 prod0].
                uint256 remainder;
                assembly {
                    // Compute remainder using mulmod.
                    remainder := mulmod(x, y, denominator)
                    // Subtract 256 bit number from 512 bit number.
                    prod1 := sub(prod1, gt(remainder, prod0))
                    prod0 := sub(prod0, remainder)
                }
                // Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
                // See https://cs.stackexchange.com/q/138556/92363.
                // Does not overflow because the denominator cannot be zero at this stage in the function.
                uint256 twos = denominator & (~denominator + 1);
                assembly {
                    // Divide denominator by twos.
                    denominator := div(denominator, twos)
                    // Divide [prod1 prod0] by twos.
                    prod0 := div(prod0, twos)
                    // Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
                    twos := add(div(sub(0, twos), twos), 1)
                }
                // Shift in bits from prod1 into prod0.
                prod0 |= prod1 * twos;
                // Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
                // that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
                // four bits. That is, denominator * inv = 1 mod 2^4.
                uint256 inverse = (3 * denominator) ^ 2;
                // Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
                // in modular arithmetic, doubling the correct bits in each step.
                inverse *= 2 - denominator * inverse; // inverse mod 2^8
                inverse *= 2 - denominator * inverse; // inverse mod 2^16
                inverse *= 2 - denominator * inverse; // inverse mod 2^32
                inverse *= 2 - denominator * inverse; // inverse mod 2^64
                inverse *= 2 - denominator * inverse; // inverse mod 2^128
                inverse *= 2 - denominator * inverse; // inverse mod 2^256
                // Because the division is now exact we can divide by multiplying with the modular inverse of denominator.
                // This will give us the correct result modulo 2^256. Since the preconditions guarantee that the outcome is
                // less than 2^256, this is the final result. We don't need to compute the high bits of the result and prod1
                // is no longer required.
                result = prod0 * inverse;
                return result;
            }
        }
        /**
         * @notice Calculates x * y / denominator with full precision, following the selected rounding direction.
         */
        function mulDiv(uint256 x, uint256 y, uint256 denominator, Rounding rounding) internal pure returns (uint256) {
            uint256 result = mulDiv(x, y, denominator);
            if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
                result += 1;
            }
            return result;
        }
        /**
         * @dev Returns the square root of a number. If the number is not a perfect square, the value is rounded down.
         *
         * Inspired by Henry S. Warren, Jr.'s "Hacker's Delight" (Chapter 11).
         */
        function sqrt(uint256 a) internal pure returns (uint256) {
            if (a == 0) {
                return 0;
            }
            // For our first guess, we get the biggest power of 2 which is smaller than the square root of the target.
            //
            // We know that the "msb" (most significant bit) of our target number `a` is a power of 2 such that we have
            // `msb(a) <= a < 2*msb(a)`. This value can be written `msb(a)=2**k` with `k=log2(a)`.
            //
            // This can be rewritten `2**log2(a) <= a < 2**(log2(a) + 1)`
            // → `sqrt(2**k) <= sqrt(a) < sqrt(2**(k+1))`
            // → `2**(k/2) <= sqrt(a) < 2**((k+1)/2) <= 2**(k/2 + 1)`
            //
            // Consequently, `2**(log2(a) / 2)` is a good first approximation of `sqrt(a)` with at least 1 correct bit.
            uint256 result = 1 << (log2(a) >> 1);
            // At this point `result` is an estimation with one bit of precision. We know the true value is a uint128,
            // since it is the square root of a uint256. Newton's method converges quadratically (precision doubles at
            // every iteration). We thus need at most 7 iteration to turn our partial result with one bit of precision
            // into the expected uint128 result.
            unchecked {
                result = (result + a / result) >> 1;
                result = (result + a / result) >> 1;
                result = (result + a / result) >> 1;
                result = (result + a / result) >> 1;
                result = (result + a / result) >> 1;
                result = (result + a / result) >> 1;
                result = (result + a / result) >> 1;
                return min(result, a / result);
            }
        }
        /**
         * @notice Calculates sqrt(a), following the selected rounding direction.
         */
        function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
            unchecked {
                uint256 result = sqrt(a);
                return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
            }
        }
        /**
         * @dev Return the log in base 2, rounded down, of a positive value.
         * Returns 0 if given 0.
         */
        function log2(uint256 value) internal pure returns (uint256) {
            uint256 result = 0;
            unchecked {
                if (value >> 128 > 0) {
                    value >>= 128;
                    result += 128;
                }
                if (value >> 64 > 0) {
                    value >>= 64;
                    result += 64;
                }
                if (value >> 32 > 0) {
                    value >>= 32;
                    result += 32;
                }
                if (value >> 16 > 0) {
                    value >>= 16;
                    result += 16;
                }
                if (value >> 8 > 0) {
                    value >>= 8;
                    result += 8;
                }
                if (value >> 4 > 0) {
                    value >>= 4;
                    result += 4;
                }
                if (value >> 2 > 0) {
                    value >>= 2;
                    result += 2;
                }
                if (value >> 1 > 0) {
                    result += 1;
                }
            }
            return result;
        }
        /**
         * @dev Return the log in base 2, following the selected rounding direction, of a positive value.
         * Returns 0 if given 0.
         */
        function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
            unchecked {
                uint256 result = log2(value);
                return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
            }
        }
        /**
         * @dev Return the log in base 10, rounded down, of a positive value.
         * Returns 0 if given 0.
         */
        function log10(uint256 value) internal pure returns (uint256) {
            uint256 result = 0;
            unchecked {
                if (value >= 10 ** 64) {
                    value /= 10 ** 64;
                    result += 64;
                }
                if (value >= 10 ** 32) {
                    value /= 10 ** 32;
                    result += 32;
                }
                if (value >= 10 ** 16) {
                    value /= 10 ** 16;
                    result += 16;
                }
                if (value >= 10 ** 8) {
                    value /= 10 ** 8;
                    result += 8;
                }
                if (value >= 10 ** 4) {
                    value /= 10 ** 4;
                    result += 4;
                }
                if (value >= 10 ** 2) {
                    value /= 10 ** 2;
                    result += 2;
                }
                if (value >= 10 ** 1) {
                    result += 1;
                }
            }
            return result;
        }
        /**
         * @dev Return the log in base 10, following the selected rounding direction, of a positive value.
         * Returns 0 if given 0.
         */
        function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
            unchecked {
                uint256 result = log10(value);
                return result + (rounding == Rounding.Up && 10 ** result < value ? 1 : 0);
            }
        }
        /**
         * @dev Return the log in base 256, rounded down, of a positive value.
         * Returns 0 if given 0.
         *
         * Adding one to the result gives the number of pairs of hex symbols needed to represent `value` as a hex string.
         */
        function log256(uint256 value) internal pure returns (uint256) {
            uint256 result = 0;
            unchecked {
                if (value >> 128 > 0) {
                    value >>= 128;
                    result += 16;
                }
                if (value >> 64 > 0) {
                    value >>= 64;
                    result += 8;
                }
                if (value >> 32 > 0) {
                    value >>= 32;
                    result += 4;
                }
                if (value >> 16 > 0) {
                    value >>= 16;
                    result += 2;
                }
                if (value >> 8 > 0) {
                    result += 1;
                }
            }
            return result;
        }
        /**
         * @dev Return the log in base 256, following the selected rounding direction, of a positive value.
         * Returns 0 if given 0.
         */
        function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
            unchecked {
                uint256 result = log256(value);
                return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0);
            }
        }
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.8.0) (utils/math/SignedMath.sol)
    pragma solidity ^0.8.0;
    /**
     * @dev Standard signed math utilities missing in the Solidity language.
     */
    library SignedMathUpgradeable {
        /**
         * @dev Returns the largest of two signed numbers.
         */
        function max(int256 a, int256 b) internal pure returns (int256) {
            return a > b ? a : b;
        }
        /**
         * @dev Returns the smallest of two signed numbers.
         */
        function min(int256 a, int256 b) internal pure returns (int256) {
            return a < b ? a : b;
        }
        /**
         * @dev Returns the average of two signed numbers without overflow.
         * The result is rounded towards zero.
         */
        function average(int256 a, int256 b) internal pure returns (int256) {
            // Formula from the book "Hacker's Delight"
            int256 x = (a & b) + ((a ^ b) >> 1);
            return x + (int256(uint256(x) >> 255) & (a ^ b));
        }
        /**
         * @dev Returns the absolute unsigned value of a signed value.
         */
        function abs(int256 n) internal pure returns (uint256) {
            unchecked {
                // must be unchecked in order to support `n = type(int256).min`
                return uint256(n >= 0 ? n : -n);
            }
        }
    }
    // SPDX-License-Identifier: MIT
    // OpenZeppelin Contracts (last updated v4.9.0) (utils/Address.sol)
    pragma solidity ^0.8.1;
    /**
     * @dev Collection of functions related to the address type
     */
    library Address {
        /**
         * @dev Returns true if `account` is a contract.
         *
         * [IMPORTANT]
         * ====
         * It is unsafe to assume that an address for which this function returns
         * false is an externally-owned account (EOA) and not a contract.
         *
         * Among others, `isContract` will return false for the following
         * types of addresses:
         *
         *  - an externally-owned account
         *  - a contract in construction
         *  - an address where a contract will be created
         *  - an address where a contract lived, but was destroyed
         *
         * Furthermore, `isContract` will also return true if the target contract within
         * the same transaction is already scheduled for destruction by `SELFDESTRUCT`,
         * which only has an effect at the end of a transaction.
         * ====
         *
         * [IMPORTANT]
         * ====
         * You shouldn't rely on `isContract` to protect against flash loan attacks!
         *
         * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
         * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
         * constructor.
         * ====
         */
        function isContract(address account) internal view returns (bool) {
            // This method relies on extcodesize/address.code.length, which returns 0
            // for contracts in construction, since the code is only stored at the end
            // of the constructor execution.
            return account.code.length > 0;
        }
        /**
         * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
         * `recipient`, forwarding all available gas and reverting on errors.
         *
         * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
         * of certain opcodes, possibly making contracts go over the 2300 gas limit
         * imposed by `transfer`, making them unable to receive funds via
         * `transfer`. {sendValue} removes this limitation.
         *
         * https://consensys.net/diligence/blog/2019/09/stop-using-soliditys-transfer-now/[Learn more].
         *
         * IMPORTANT: because control is transferred to `recipient`, care must be
         * taken to not create reentrancy vulnerabilities. Consider using
         * {ReentrancyGuard} or the
         * https://solidity.readthedocs.io/en/v0.8.0/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
         */
        function sendValue(address payable recipient, uint256 amount) internal {
            require(address(this).balance >= amount, "Address: insufficient balance");
            (bool success, ) = recipient.call{value: amount}("");
            require(success, "Address: unable to send value, recipient may have reverted");
        }
        /**
         * @dev Performs a Solidity function call using a low level `call`. A
         * plain `call` is an unsafe replacement for a function call: use this
         * function instead.
         *
         * If `target` reverts with a revert reason, it is bubbled up by this
         * function (like regular Solidity function calls).
         *
         * Returns the raw returned data. To convert to the expected return value,
         * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
         *
         * Requirements:
         *
         * - `target` must be a contract.
         * - calling `target` with `data` must not revert.
         *
         * _Available since v3.1._
         */
        function functionCall(address target, bytes memory data) internal returns (bytes memory) {
            return functionCallWithValue(target, data, 0, "Address: low-level call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
         * `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal returns (bytes memory) {
            return functionCallWithValue(target, data, 0, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but also transferring `value` wei to `target`.
         *
         * Requirements:
         *
         * - the calling contract must have an ETH balance of at least `value`.
         * - the called Solidity function must be `payable`.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
            return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
        }
        /**
         * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
         * with `errorMessage` as a fallback revert reason when `target` reverts.
         *
         * _Available since v3.1._
         */
        function functionCallWithValue(
            address target,
            bytes memory data,
            uint256 value,
            string memory errorMessage
        ) internal returns (bytes memory) {
            require(address(this).balance >= value, "Address: insufficient balance for call");
            (bool success, bytes memory returndata) = target.call{value: value}(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
            return functionStaticCall(target, data, "Address: low-level static call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
         * but performing a static call.
         *
         * _Available since v3.3._
         */
        function functionStaticCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal view returns (bytes memory) {
            (bool success, bytes memory returndata) = target.staticcall(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
         * but performing a delegate call.
         *
         * _Available since v3.4._
         */
        function functionDelegateCall(address target, bytes memory data) internal returns (bytes memory) {
            return functionDelegateCall(target, data, "Address: low-level delegate call failed");
        }
        /**
         * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
         * but performing a delegate call.
         *
         * _Available since v3.4._
         */
        function functionDelegateCall(
            address target,
            bytes memory data,
            string memory errorMessage
        ) internal returns (bytes memory) {
            (bool success, bytes memory returndata) = target.delegatecall(data);
            return verifyCallResultFromTarget(target, success, returndata, errorMessage);
        }
        /**
         * @dev Tool to verify that a low level call to smart-contract was successful, and revert (either by bubbling
         * the revert reason or using the provided one) in case of unsuccessful call or if target was not a contract.
         *
         * _Available since v4.8._
         */
        function verifyCallResultFromTarget(
            address target,
            bool success,
            bytes memory returndata,
            string memory errorMessage
        ) internal view returns (bytes memory) {
            if (success) {
                if (returndata.length == 0) {
                    // only check isContract if the call was successful and the return data is empty
                    // otherwise we already know that it was a contract
                    require(isContract(target), "Address: call to non-contract");
                }
                return returndata;
            } else {
                _revert(returndata, errorMessage);
            }
        }
        /**
         * @dev Tool to verify that a low level call was successful, and revert if it wasn't, either by bubbling the
         * revert reason or using the provided one.
         *
         * _Available since v4.3._
         */
        function verifyCallResult(
            bool success,
            bytes memory returndata,
            string memory errorMessage
        ) internal pure returns (bytes memory) {
            if (success) {
                return returndata;
            } else {
                _revert(returndata, errorMessage);
            }
        }
        function _revert(bytes memory returndata, string memory errorMessage) private pure {
            // Look for revert reason and bubble it up if present
            if (returndata.length > 0) {
                // The easiest way to bubble the revert reason is using memory via assembly
                /// @solidity memory-safe-assembly
                assembly {
                    let returndata_size := mload(returndata)
                    revert(add(32, returndata), returndata_size)
                }
            } else {
                revert(errorMessage);
            }
        }
    }
    

    File 3 of 4: Proxy
    pragma solidity ^0.5.3;
    
    /// @title Proxy - Generic proxy contract allows to execute all transactions applying the code of a master contract.
    /// @author Stefan George - <[email protected]>
    /// @author Richard Meissner - <[email protected]>
    contract Proxy {
    
        // masterCopy always needs to be first declared variable, to ensure that it is at the same location in the contracts to which calls are delegated.
        // To reduce deployment costs this variable is internal and needs to be retrieved via `getStorageAt`
        address internal masterCopy;
    
        /// @dev Constructor function sets address of master copy contract.
        /// @param _masterCopy Master copy address.
        constructor(address _masterCopy)
            public
        {
            require(_masterCopy != address(0), "Invalid master copy address provided");
            masterCopy = _masterCopy;
        }
    
        /// @dev Fallback function forwards all transactions and returns all received return data.
        function ()
            external
            payable
        {
            // solium-disable-next-line security/no-inline-assembly
            assembly {
                let masterCopy := and(sload(0), 0xffffffffffffffffffffffffffffffffffffffff)
                // 0xa619486e == keccak("masterCopy()"). The value is right padded to 32-bytes with 0s
                if eq(calldataload(0), 0xa619486e00000000000000000000000000000000000000000000000000000000) {
                    mstore(0, masterCopy)
                    return(0, 0x20)
                }
                calldatacopy(0, 0, calldatasize())
                let success := delegatecall(gas, masterCopy, 0, calldatasize(), 0, 0)
                returndatacopy(0, 0, returndatasize())
                if eq(success, 0) { revert(0, returndatasize()) }
                return(0, returndatasize())
            }
        }
    }

    File 4 of 4: GnosisSafe
    pragma solidity >=0.5.0 <0.7.0;
    
    /// @title SelfAuthorized - authorizes current contract to perform actions
    /// @author Richard Meissner - <[email protected]>
    contract SelfAuthorized {
        modifier authorized() {
            require(msg.sender == address(this), "Method can only be called from this contract");
            _;
        }
    }
    
    
    
    /// @title MasterCopy - Base for master copy contracts (should always be first super contract)
    ///         This contract is tightly coupled to our proxy contract (see `proxies/Proxy.sol`)
    /// @author Richard Meissner - <[email protected]>
    contract MasterCopy is SelfAuthorized {
    
        event ChangedMasterCopy(address masterCopy);
    
        // masterCopy always needs to be first declared variable, to ensure that it is at the same location as in the Proxy contract.
        // It should also always be ensured that the address is stored alone (uses a full word)
        address private masterCopy;
    
        /// @dev Allows to upgrade the contract. This can only be done via a Safe transaction.
        /// @param _masterCopy New contract address.
        function changeMasterCopy(address _masterCopy)
            public
            authorized
        {
            // Master copy address cannot be null.
            require(_masterCopy != address(0), "Invalid master copy address provided");
            masterCopy = _masterCopy;
            emit ChangedMasterCopy(_masterCopy);
        }
    }
    
    
    /// @title Module - Base class for modules.
    /// @author Stefan George - <[email protected]>
    /// @author Richard Meissner - <[email protected]>
    contract Module is MasterCopy {
    
        ModuleManager public manager;
    
        modifier authorized() {
            require(msg.sender == address(manager), "Method can only be called from manager");
            _;
        }
    
        function setManager()
            internal
        {
            // manager can only be 0 at initalization of contract.
            // Check ensures that setup function can only be called once.
            require(address(manager) == address(0), "Manager has already been set");
            manager = ModuleManager(msg.sender);
        }
    }
    
    
    
    
    
    /// @title Enum - Collection of enums
    /// @author Richard Meissner - <[email protected]>
    contract Enum {
        enum Operation {
            Call,
            DelegateCall
        }
    }
    
    
    
    
    
    /// @title Executor - A contract that can execute transactions
    /// @author Richard Meissner - <[email protected]>
    contract Executor {
    
        function execute(address to, uint256 value, bytes memory data, Enum.Operation operation, uint256 txGas)
            internal
            returns (bool success)
        {
            if (operation == Enum.Operation.Call)
                success = executeCall(to, value, data, txGas);
            else if (operation == Enum.Operation.DelegateCall)
                success = executeDelegateCall(to, data, txGas);
            else
                success = false;
        }
    
        function executeCall(address to, uint256 value, bytes memory data, uint256 txGas)
            internal
            returns (bool success)
        {
            // solium-disable-next-line security/no-inline-assembly
            assembly {
                success := call(txGas, to, value, add(data, 0x20), mload(data), 0, 0)
            }
        }
    
        function executeDelegateCall(address to, bytes memory data, uint256 txGas)
            internal
            returns (bool success)
        {
            // solium-disable-next-line security/no-inline-assembly
            assembly {
                success := delegatecall(txGas, to, add(data, 0x20), mload(data), 0, 0)
            }
        }
    }
    
    
    
    /// @title SecuredTokenTransfer - Secure token transfer
    /// @author Richard Meissner - <[email protected]>
    contract SecuredTokenTransfer {
    
        /// @dev Transfers a token and returns if it was a success
        /// @param token Token that should be transferred
        /// @param receiver Receiver to whom the token should be transferred
        /// @param amount The amount of tokens that should be transferred
        function transferToken (
            address token,
            address receiver,
            uint256 amount
        )
            internal
            returns (bool transferred)
        {
            bytes memory data = abi.encodeWithSignature("transfer(address,uint256)", receiver, amount);
            // solium-disable-next-line security/no-inline-assembly
            assembly {
                let success := call(sub(gas, 10000), token, 0, add(data, 0x20), mload(data), 0, 0)
                let ptr := mload(0x40)
                mstore(0x40, add(ptr, returndatasize()))
                returndatacopy(ptr, 0, returndatasize())
                switch returndatasize()
                case 0 { transferred := success }
                case 0x20 { transferred := iszero(or(iszero(success), iszero(mload(ptr)))) }
                default { transferred := 0 }
            }
        }
    }
    
    
    
    
    
    
    
    
    
    
    /// @title Module Manager - A contract that manages modules that can execute transactions via this contract
    /// @author Stefan George - <[email protected]>
    /// @author Richard Meissner - <[email protected]>
    contract ModuleManager is SelfAuthorized, Executor {
    
        event EnabledModule(Module module);
        event DisabledModule(Module module);
        event ExecutionFromModuleSuccess(address indexed module);
        event ExecutionFromModuleFailure(address indexed module);
    
        address internal constant SENTINEL_MODULES = address(0x1);
    
        mapping (address => address) internal modules;
    
        function setupModules(address to, bytes memory data)
            internal
        {
            require(modules[SENTINEL_MODULES] == address(0), "Modules have already been initialized");
            modules[SENTINEL_MODULES] = SENTINEL_MODULES;
            if (to != address(0))
                // Setup has to complete successfully or transaction fails.
                require(executeDelegateCall(to, data, gasleft()), "Could not finish initialization");
        }
    
        /// @dev Allows to add a module to the whitelist.
        ///      This can only be done via a Safe transaction.
        /// @param module Module to be whitelisted.
        function enableModule(Module module)
            public
            authorized
        {
            // Module address cannot be null or sentinel.
            require(address(module) != address(0) && address(module) != SENTINEL_MODULES, "Invalid module address provided");
            // Module cannot be added twice.
            require(modules[address(module)] == address(0), "Module has already been added");
            modules[address(module)] = modules[SENTINEL_MODULES];
            modules[SENTINEL_MODULES] = address(module);
            emit EnabledModule(module);
        }
    
        /// @dev Allows to remove a module from the whitelist.
        ///      This can only be done via a Safe transaction.
        /// @param prevModule Module that pointed to the module to be removed in the linked list
        /// @param module Module to be removed.
        function disableModule(Module prevModule, Module module)
            public
            authorized
        {
            // Validate module address and check that it corresponds to module index.
            require(address(module) != address(0) && address(module) != SENTINEL_MODULES, "Invalid module address provided");
            require(modules[address(prevModule)] == address(module), "Invalid prevModule, module pair provided");
            modules[address(prevModule)] = modules[address(module)];
            modules[address(module)] = address(0);
            emit DisabledModule(module);
        }
    
        /// @dev Allows a Module to execute a Safe transaction without any further confirmations.
        /// @param to Destination address of module transaction.
        /// @param value Ether value of module transaction.
        /// @param data Data payload of module transaction.
        /// @param operation Operation type of module transaction.
        function execTransactionFromModule(address to, uint256 value, bytes memory data, Enum.Operation operation)
            public
            returns (bool success)
        {
            // Only whitelisted modules are allowed.
            require(msg.sender != SENTINEL_MODULES && modules[msg.sender] != address(0), "Method can only be called from an enabled module");
            // Execute transaction without further confirmations.
            success = execute(to, value, data, operation, gasleft());
            if (success) emit ExecutionFromModuleSuccess(msg.sender);
            else emit ExecutionFromModuleFailure(msg.sender);
        }
    
        /// @dev Allows a Module to execute a Safe transaction without any further confirmations and return data
        /// @param to Destination address of module transaction.
        /// @param value Ether value of module transaction.
        /// @param data Data payload of module transaction.
        /// @param operation Operation type of module transaction.
        function execTransactionFromModuleReturnData(address to, uint256 value, bytes memory data, Enum.Operation operation)
            public
            returns (bool success, bytes memory returnData)
        {
            success = execTransactionFromModule(to, value, data, operation);
            // solium-disable-next-line security/no-inline-assembly
            assembly {
                // Load free memory location
                let ptr := mload(0x40)
                // We allocate memory for the return data by setting the free memory location to
                // current free memory location + data size + 32 bytes for data size value
                mstore(0x40, add(ptr, add(returndatasize(), 0x20)))
                // Store the size
                mstore(ptr, returndatasize())
                // Store the data
                returndatacopy(add(ptr, 0x20), 0, returndatasize())
                // Point the return data to the correct memory location
                returnData := ptr
            }
        }
    
        /// @dev Returns array of first 10 modules.
        /// @return Array of modules.
        function getModules()
            public
            view
            returns (address[] memory)
        {
            (address[] memory array,) = getModulesPaginated(SENTINEL_MODULES, 10);
            return array;
        }
    
        /// @dev Returns array of modules.
        /// @param start Start of the page.
        /// @param pageSize Maximum number of modules that should be returned.
        /// @return Array of modules.
        function getModulesPaginated(address start, uint256 pageSize)
            public
            view
            returns (address[] memory array, address next)
        {
            // Init array with max page size
            array = new address[](pageSize);
    
            // Populate return array
            uint256 moduleCount = 0;
            address currentModule = modules[start];
            while(currentModule != address(0x0) && currentModule != SENTINEL_MODULES && moduleCount < pageSize) {
                array[moduleCount] = currentModule;
                currentModule = modules[currentModule];
                moduleCount++;
            }
            next = currentModule;
            // Set correct size of returned array
            // solium-disable-next-line security/no-inline-assembly
            assembly {
                mstore(array, moduleCount)
            }
        }
    }
    
    
    
    
    /// @title OwnerManager - Manages a set of owners and a threshold to perform actions.
    /// @author Stefan George - <[email protected]>
    /// @author Richard Meissner - <[email protected]>
    contract OwnerManager is SelfAuthorized {
    
        event AddedOwner(address owner);
        event RemovedOwner(address owner);
        event ChangedThreshold(uint256 threshold);
    
        address internal constant SENTINEL_OWNERS = address(0x1);
    
        mapping(address => address) internal owners;
        uint256 ownerCount;
        uint256 internal threshold;
    
        /// @dev Setup function sets initial storage of contract.
        /// @param _owners List of Safe owners.
        /// @param _threshold Number of required confirmations for a Safe transaction.
        function setupOwners(address[] memory _owners, uint256 _threshold)
            internal
        {
            // Threshold can only be 0 at initialization.
            // Check ensures that setup function can only be called once.
            require(threshold == 0, "Owners have already been setup");
            // Validate that threshold is smaller than number of added owners.
            require(_threshold <= _owners.length, "Threshold cannot exceed owner count");
            // There has to be at least one Safe owner.
            require(_threshold >= 1, "Threshold needs to be greater than 0");
            // Initializing Safe owners.
            address currentOwner = SENTINEL_OWNERS;
            for (uint256 i = 0; i < _owners.length; i++) {
                // Owner address cannot be null.
                address owner = _owners[i];
                require(owner != address(0) && owner != SENTINEL_OWNERS, "Invalid owner address provided");
                // No duplicate owners allowed.
                require(owners[owner] == address(0), "Duplicate owner address provided");
                owners[currentOwner] = owner;
                currentOwner = owner;
            }
            owners[currentOwner] = SENTINEL_OWNERS;
            ownerCount = _owners.length;
            threshold = _threshold;
        }
    
        /// @dev Allows to add a new owner to the Safe and update the threshold at the same time.
        ///      This can only be done via a Safe transaction.
        /// @param owner New owner address.
        /// @param _threshold New threshold.
        function addOwnerWithThreshold(address owner, uint256 _threshold)
            public
            authorized
        {
            // Owner address cannot be null.
            require(owner != address(0) && owner != SENTINEL_OWNERS, "Invalid owner address provided");
            // No duplicate owners allowed.
            require(owners[owner] == address(0), "Address is already an owner");
            owners[owner] = owners[SENTINEL_OWNERS];
            owners[SENTINEL_OWNERS] = owner;
            ownerCount++;
            emit AddedOwner(owner);
            // Change threshold if threshold was changed.
            if (threshold != _threshold)
                changeThreshold(_threshold);
        }
    
        /// @dev Allows to remove an owner from the Safe and update the threshold at the same time.
        ///      This can only be done via a Safe transaction.
        /// @param prevOwner Owner that pointed to the owner to be removed in the linked list
        /// @param owner Owner address to be removed.
        /// @param _threshold New threshold.
        function removeOwner(address prevOwner, address owner, uint256 _threshold)
            public
            authorized
        {
            // Only allow to remove an owner, if threshold can still be reached.
            require(ownerCount - 1 >= _threshold, "New owner count needs to be larger than new threshold");
            // Validate owner address and check that it corresponds to owner index.
            require(owner != address(0) && owner != SENTINEL_OWNERS, "Invalid owner address provided");
            require(owners[prevOwner] == owner, "Invalid prevOwner, owner pair provided");
            owners[prevOwner] = owners[owner];
            owners[owner] = address(0);
            ownerCount--;
            emit RemovedOwner(owner);
            // Change threshold if threshold was changed.
            if (threshold != _threshold)
                changeThreshold(_threshold);
        }
    
        /// @dev Allows to swap/replace an owner from the Safe with another address.
        ///      This can only be done via a Safe transaction.
        /// @param prevOwner Owner that pointed to the owner to be replaced in the linked list
        /// @param oldOwner Owner address to be replaced.
        /// @param newOwner New owner address.
        function swapOwner(address prevOwner, address oldOwner, address newOwner)
            public
            authorized
        {
            // Owner address cannot be null.
            require(newOwner != address(0) && newOwner != SENTINEL_OWNERS, "Invalid owner address provided");
            // No duplicate owners allowed.
            require(owners[newOwner] == address(0), "Address is already an owner");
            // Validate oldOwner address and check that it corresponds to owner index.
            require(oldOwner != address(0) && oldOwner != SENTINEL_OWNERS, "Invalid owner address provided");
            require(owners[prevOwner] == oldOwner, "Invalid prevOwner, owner pair provided");
            owners[newOwner] = owners[oldOwner];
            owners[prevOwner] = newOwner;
            owners[oldOwner] = address(0);
            emit RemovedOwner(oldOwner);
            emit AddedOwner(newOwner);
        }
    
        /// @dev Allows to update the number of required confirmations by Safe owners.
        ///      This can only be done via a Safe transaction.
        /// @param _threshold New threshold.
        function changeThreshold(uint256 _threshold)
            public
            authorized
        {
            // Validate that threshold is smaller than number of owners.
            require(_threshold <= ownerCount, "Threshold cannot exceed owner count");
            // There has to be at least one Safe owner.
            require(_threshold >= 1, "Threshold needs to be greater than 0");
            threshold = _threshold;
            emit ChangedThreshold(threshold);
        }
    
        function getThreshold()
            public
            view
            returns (uint256)
        {
            return threshold;
        }
    
        function isOwner(address owner)
            public
            view
            returns (bool)
        {
            return owner != SENTINEL_OWNERS && owners[owner] != address(0);
        }
    
        /// @dev Returns array of owners.
        /// @return Array of Safe owners.
        function getOwners()
            public
            view
            returns (address[] memory)
        {
            address[] memory array = new address[](ownerCount);
    
            // populate return array
            uint256 index = 0;
            address currentOwner = owners[SENTINEL_OWNERS];
            while(currentOwner != SENTINEL_OWNERS) {
                array[index] = currentOwner;
                currentOwner = owners[currentOwner];
                index ++;
            }
            return array;
        }
    }
    
    
    
    
    
    /// @title Fallback Manager - A contract that manages fallback calls made to this contract
    /// @author Richard Meissner - <[email protected]>
    contract FallbackManager is SelfAuthorized {
    
        // keccak256("fallback_manager.handler.address")
        bytes32 internal constant FALLBACK_HANDLER_STORAGE_SLOT = 0x6c9a6c4a39284e37ed1cf53d337577d14212a4870fb976a4366c693b939918d5;
    
        function internalSetFallbackHandler(address handler) internal {
            bytes32 slot = FALLBACK_HANDLER_STORAGE_SLOT;
            // solium-disable-next-line security/no-inline-assembly
            assembly {
                sstore(slot, handler)
            }
        }
    
        /// @dev Allows to add a contract to handle fallback calls.
        ///      Only fallback calls without value and with data will be forwarded.
        ///      This can only be done via a Safe transaction.
        /// @param handler contract to handle fallbacks calls.
        function setFallbackHandler(address handler)
            public
            authorized
        {
            internalSetFallbackHandler(handler);
        }
    
        function ()
            external
            payable
        {
            // Only calls without value and with data will be forwarded
            if (msg.value > 0 || msg.data.length == 0) {
                return;
            }
            bytes32 slot = FALLBACK_HANDLER_STORAGE_SLOT;
            address handler;
            // solium-disable-next-line security/no-inline-assembly
            assembly {
                handler := sload(slot)
            }
    
            if (handler != address(0)) {
                // solium-disable-next-line security/no-inline-assembly
                assembly {
                    calldatacopy(0, 0, calldatasize())
                    let success := call(gas, handler, 0, 0, calldatasize(), 0, 0)
                    returndatacopy(0, 0, returndatasize())
                    if eq(success, 0) { revert(0, returndatasize()) }
                    return(0, returndatasize())
                }
            }
        }
    }
    
    
    
    
    
    
    
    /// @title SignatureDecoder - Decodes signatures that a encoded as bytes
    /// @author Ricardo Guilherme Schmidt (Status Research & Development GmbH)
    /// @author Richard Meissner - <[email protected]>
    contract SignatureDecoder {
        
        /// @dev Recovers address who signed the message
        /// @param messageHash operation ethereum signed message hash
        /// @param messageSignature message `txHash` signature
        /// @param pos which signature to read
        function recoverKey (
            bytes32 messageHash,
            bytes memory messageSignature,
            uint256 pos
        )
            internal
            pure
            returns (address)
        {
            uint8 v;
            bytes32 r;
            bytes32 s;
            (v, r, s) = signatureSplit(messageSignature, pos);
            return ecrecover(messageHash, v, r, s);
        }
    
        /// @dev divides bytes signature into `uint8 v, bytes32 r, bytes32 s`.
        /// @notice Make sure to peform a bounds check for @param pos, to avoid out of bounds access on @param signatures
        /// @param pos which signature to read. A prior bounds check of this parameter should be performed, to avoid out of bounds access
        /// @param signatures concatenated rsv signatures
        function signatureSplit(bytes memory signatures, uint256 pos)
            internal
            pure
            returns (uint8 v, bytes32 r, bytes32 s)
        {
            // The signature format is a compact form of:
            //   {bytes32 r}{bytes32 s}{uint8 v}
            // Compact means, uint8 is not padded to 32 bytes.
            // solium-disable-next-line security/no-inline-assembly
            assembly {
                let signaturePos := mul(0x41, pos)
                r := mload(add(signatures, add(signaturePos, 0x20)))
                s := mload(add(signatures, add(signaturePos, 0x40)))
                // Here we are loading the last 32 bytes, including 31 bytes
                // of 's'. There is no 'mload8' to do this.
                //
                // 'byte' is not working due to the Solidity parser, so lets
                // use the second best option, 'and'
                v := and(mload(add(signatures, add(signaturePos, 0x41))), 0xff)
            }
        }
    }
    
    
    
    
    contract ISignatureValidatorConstants {
        // bytes4(keccak256("isValidSignature(bytes,bytes)")
        bytes4 constant internal EIP1271_MAGIC_VALUE = 0x20c13b0b;
    }
    
    contract ISignatureValidator is ISignatureValidatorConstants {
    
        /**
        * @dev Should return whether the signature provided is valid for the provided data
        * @param _data Arbitrary length data signed on the behalf of address(this)
        * @param _signature Signature byte array associated with _data
        *
        * MUST return the bytes4 magic value 0x20c13b0b when function passes.
        * MUST NOT modify state (using STATICCALL for solc < 0.5, view modifier for solc > 0.5)
        * MUST allow external calls
        */
        function isValidSignature(
            bytes memory _data,
            bytes memory _signature)
            public
            view
            returns (bytes4);
    }
    
    
    /**
     * @title SafeMath
     * @dev Math operations with safety checks that revert on error
     * TODO: remove once open zeppelin update to solc 0.5.0
     */
    library SafeMath {
    
      /**
      * @dev Multiplies two numbers, reverts on overflow.
      */
      function mul(uint256 a, uint256 b) internal pure returns (uint256) {
        // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
        // benefit is lost if 'b' is also tested.
        // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
        if (a == 0) {
          return 0;
        }
    
        uint256 c = a * b;
        require(c / a == b);
    
        return c;
      }
    
      /**
      * @dev Integer division of two numbers truncating the quotient, reverts on division by zero.
      */
      function div(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b > 0); // Solidity only automatically asserts when dividing by 0
        uint256 c = a / b;
        // assert(a == b * c + a % b); // There is no case in which this doesn't hold
    
        return c;
      }
    
      /**
      * @dev Subtracts two numbers, reverts on overflow (i.e. if subtrahend is greater than minuend).
      */
      function sub(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b <= a);
        uint256 c = a - b;
    
        return c;
      }
    
      /**
      * @dev Adds two numbers, reverts on overflow.
      */
      function add(uint256 a, uint256 b) internal pure returns (uint256) {
        uint256 c = a + b;
        require(c >= a);
    
        return c;
      }
    
      /**
      * @dev Divides two numbers and returns the remainder (unsigned integer modulo),
      * reverts when dividing by zero.
      */
      function mod(uint256 a, uint256 b) internal pure returns (uint256) {
        require(b != 0);
        return a % b;
      }
    }
    
    /// @title Gnosis Safe - A multisignature wallet with support for confirmations using signed messages based on ERC191.
    /// @author Stefan George - <[email protected]>
    /// @author Richard Meissner - <[email protected]>
    /// @author Ricardo Guilherme Schmidt - (Status Research & Development GmbH) - Gas Token Payment
    contract GnosisSafe
        is MasterCopy, ModuleManager, OwnerManager, SignatureDecoder, SecuredTokenTransfer, ISignatureValidatorConstants, FallbackManager {
    
        using SafeMath for uint256;
    
        string public constant NAME = "Gnosis Safe";
        string public constant VERSION = "1.1.1";
    
        //keccak256(
        //    "EIP712Domain(address verifyingContract)"
        //);
        bytes32 private constant DOMAIN_SEPARATOR_TYPEHASH = 0x035aff83d86937d35b32e04f0ddc6ff469290eef2f1b692d8a815c89404d4749;
    
        //keccak256(
        //    "SafeTx(address to,uint256 value,bytes data,uint8 operation,uint256 safeTxGas,uint256 baseGas,uint256 gasPrice,address gasToken,address refundReceiver,uint256 nonce)"
        //);
        bytes32 private constant SAFE_TX_TYPEHASH = 0xbb8310d486368db6bd6f849402fdd73ad53d316b5a4b2644ad6efe0f941286d8;
    
        //keccak256(
        //    "SafeMessage(bytes message)"
        //);
        bytes32 private constant SAFE_MSG_TYPEHASH = 0x60b3cbf8b4a223d68d641b3b6ddf9a298e7f33710cf3d3a9d1146b5a6150fbca;
    
        event ApproveHash(
            bytes32 indexed approvedHash,
            address indexed owner
        );
        event SignMsg(
            bytes32 indexed msgHash
        );
        event ExecutionFailure(
            bytes32 txHash, uint256 payment
        );
        event ExecutionSuccess(
            bytes32 txHash, uint256 payment
        );
    
        uint256 public nonce;
        bytes32 public domainSeparator;
        // Mapping to keep track of all message hashes that have been approve by ALL REQUIRED owners
        mapping(bytes32 => uint256) public signedMessages;
        // Mapping to keep track of all hashes (message or transaction) that have been approve by ANY owners
        mapping(address => mapping(bytes32 => uint256)) public approvedHashes;
    
        // This constructor ensures that this contract can only be used as a master copy for Proxy contracts
        constructor() public {
            // By setting the threshold it is not possible to call setup anymore,
            // so we create a Safe with 0 owners and threshold 1.
            // This is an unusable Safe, perfect for the mastercopy
            threshold = 1;
        }
    
        /// @dev Setup function sets initial storage of contract.
        /// @param _owners List of Safe owners.
        /// @param _threshold Number of required confirmations for a Safe transaction.
        /// @param to Contract address for optional delegate call.
        /// @param data Data payload for optional delegate call.
        /// @param fallbackHandler Handler for fallback calls to this contract
        /// @param paymentToken Token that should be used for the payment (0 is ETH)
        /// @param payment Value that should be paid
        /// @param paymentReceiver Adddress that should receive the payment (or 0 if tx.origin)
        function setup(
            address[] calldata _owners,
            uint256 _threshold,
            address to,
            bytes calldata data,
            address fallbackHandler,
            address paymentToken,
            uint256 payment,
            address payable paymentReceiver
        )
            external
        {
            require(domainSeparator == 0, "Domain Separator already set!");
            domainSeparator = keccak256(abi.encode(DOMAIN_SEPARATOR_TYPEHASH, this));
            setupOwners(_owners, _threshold);
            if (fallbackHandler != address(0)) internalSetFallbackHandler(fallbackHandler);
            // As setupOwners can only be called if the contract has not been initialized we don't need a check for setupModules
            setupModules(to, data);
    
            if (payment > 0) {
                // To avoid running into issues with EIP-170 we reuse the handlePayment function (to avoid adjusting code of that has been verified we do not adjust the method itself)
                // baseGas = 0, gasPrice = 1 and gas = payment => amount = (payment + 0) * 1 = payment
                handlePayment(payment, 0, 1, paymentToken, paymentReceiver);
            }
        }
    
        /// @dev Allows to execute a Safe transaction confirmed by required number of owners and then pays the account that submitted the transaction.
        ///      Note: The fees are always transfered, even if the user transaction fails.
        /// @param to Destination address of Safe transaction.
        /// @param value Ether value of Safe transaction.
        /// @param data Data payload of Safe transaction.
        /// @param operation Operation type of Safe transaction.
        /// @param safeTxGas Gas that should be used for the Safe transaction.
        /// @param baseGas Gas costs for that are indipendent of the transaction execution(e.g. base transaction fee, signature check, payment of the refund)
        /// @param gasPrice Gas price that should be used for the payment calculation.
        /// @param gasToken Token address (or 0 if ETH) that is used for the payment.
        /// @param refundReceiver Address of receiver of gas payment (or 0 if tx.origin).
        /// @param signatures Packed signature data ({bytes32 r}{bytes32 s}{uint8 v})
        function execTransaction(
            address to,
            uint256 value,
            bytes calldata data,
            Enum.Operation operation,
            uint256 safeTxGas,
            uint256 baseGas,
            uint256 gasPrice,
            address gasToken,
            address payable refundReceiver,
            bytes calldata signatures
        )
            external
            returns (bool success)
        {
            bytes32 txHash;
            // Use scope here to limit variable lifetime and prevent `stack too deep` errors
            {
                bytes memory txHashData = encodeTransactionData(
                    to, value, data, operation, // Transaction info
                    safeTxGas, baseGas, gasPrice, gasToken, refundReceiver, // Payment info
                    nonce
                );
                // Increase nonce and execute transaction.
                nonce++;
                txHash = keccak256(txHashData);
                checkSignatures(txHash, txHashData, signatures, true);
            }
            require(gasleft() >= safeTxGas, "Not enough gas to execute safe transaction");
            // Use scope here to limit variable lifetime and prevent `stack too deep` errors
            {
                uint256 gasUsed = gasleft();
                // If no safeTxGas has been set and the gasPrice is 0 we assume that all available gas can be used
                success = execute(to, value, data, operation, safeTxGas == 0 && gasPrice == 0 ? gasleft() : safeTxGas);
                gasUsed = gasUsed.sub(gasleft());
                // We transfer the calculated tx costs to the tx.origin to avoid sending it to intermediate contracts that have made calls
                uint256 payment = 0;
                if (gasPrice > 0) {
                    payment = handlePayment(gasUsed, baseGas, gasPrice, gasToken, refundReceiver);
                }
                if (success) emit ExecutionSuccess(txHash, payment);
                else emit ExecutionFailure(txHash, payment);
            }
        }
    
        function handlePayment(
            uint256 gasUsed,
            uint256 baseGas,
            uint256 gasPrice,
            address gasToken,
            address payable refundReceiver
        )
            private
            returns (uint256 payment)
        {
            // solium-disable-next-line security/no-tx-origin
            address payable receiver = refundReceiver == address(0) ? tx.origin : refundReceiver;
            if (gasToken == address(0)) {
                // For ETH we will only adjust the gas price to not be higher than the actual used gas price
                payment = gasUsed.add(baseGas).mul(gasPrice < tx.gasprice ? gasPrice : tx.gasprice);
                // solium-disable-next-line security/no-send
                require(receiver.send(payment), "Could not pay gas costs with ether");
            } else {
                payment = gasUsed.add(baseGas).mul(gasPrice);
                require(transferToken(gasToken, receiver, payment), "Could not pay gas costs with token");
            }
        }
    
        /**
        * @dev Checks whether the signature provided is valid for the provided data, hash. Will revert otherwise.
        * @param dataHash Hash of the data (could be either a message hash or transaction hash)
        * @param data That should be signed (this is passed to an external validator contract)
        * @param signatures Signature data that should be verified. Can be ECDSA signature, contract signature (EIP-1271) or approved hash.
        * @param consumeHash Indicates that in case of an approved hash the storage can be freed to save gas
        */
        function checkSignatures(bytes32 dataHash, bytes memory data, bytes memory signatures, bool consumeHash)
            internal
        {
            // Load threshold to avoid multiple storage loads
            uint256 _threshold = threshold;
            // Check that a threshold is set
            require(_threshold > 0, "Threshold needs to be defined!");
            // Check that the provided signature data is not too short
            require(signatures.length >= _threshold.mul(65), "Signatures data too short");
            // There cannot be an owner with address 0.
            address lastOwner = address(0);
            address currentOwner;
            uint8 v;
            bytes32 r;
            bytes32 s;
            uint256 i;
            for (i = 0; i < _threshold; i++) {
                (v, r, s) = signatureSplit(signatures, i);
                // If v is 0 then it is a contract signature
                if (v == 0) {
                    // When handling contract signatures the address of the contract is encoded into r
                    currentOwner = address(uint256(r));
    
                    // Check that signature data pointer (s) is not pointing inside the static part of the signatures bytes
                    // This check is not completely accurate, since it is possible that more signatures than the threshold are send.
                    // Here we only check that the pointer is not pointing inside the part that is being processed
                    require(uint256(s) >= _threshold.mul(65), "Invalid contract signature location: inside static part");
    
                    // Check that signature data pointer (s) is in bounds (points to the length of data -> 32 bytes)
                    require(uint256(s).add(32) <= signatures.length, "Invalid contract signature location: length not present");
    
                    // Check if the contract signature is in bounds: start of data is s + 32 and end is start + signature length
                    uint256 contractSignatureLen;
                    // solium-disable-next-line security/no-inline-assembly
                    assembly {
                        contractSignatureLen := mload(add(add(signatures, s), 0x20))
                    }
                    require(uint256(s).add(32).add(contractSignatureLen) <= signatures.length, "Invalid contract signature location: data not complete");
    
                    // Check signature
                    bytes memory contractSignature;
                    // solium-disable-next-line security/no-inline-assembly
                    assembly {
                        // The signature data for contract signatures is appended to the concatenated signatures and the offset is stored in s
                        contractSignature := add(add(signatures, s), 0x20)
                    }
                    require(ISignatureValidator(currentOwner).isValidSignature(data, contractSignature) == EIP1271_MAGIC_VALUE, "Invalid contract signature provided");
                // If v is 1 then it is an approved hash
                } else if (v == 1) {
                    // When handling approved hashes the address of the approver is encoded into r
                    currentOwner = address(uint256(r));
                    // Hashes are automatically approved by the sender of the message or when they have been pre-approved via a separate transaction
                    require(msg.sender == currentOwner || approvedHashes[currentOwner][dataHash] != 0, "Hash has not been approved");
                    // Hash has been marked for consumption. If this hash was pre-approved free storage
                    if (consumeHash && msg.sender != currentOwner) {
                        approvedHashes[currentOwner][dataHash] = 0;
                    }
                } else if (v > 30) {
                    // To support eth_sign and similar we adjust v and hash the messageHash with the Ethereum message prefix before applying ecrecover
                    currentOwner = ecrecover(keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", dataHash)), v - 4, r, s);
                } else {
                    // Use ecrecover with the messageHash for EOA signatures
                    currentOwner = ecrecover(dataHash, v, r, s);
                }
                require (
                    currentOwner > lastOwner && owners[currentOwner] != address(0) && currentOwner != SENTINEL_OWNERS,
                    "Invalid owner provided"
                );
                lastOwner = currentOwner;
            }
        }
    
        /// @dev Allows to estimate a Safe transaction.
        ///      This method is only meant for estimation purpose, therefore two different protection mechanism against execution in a transaction have been made:
        ///      1.) The method can only be called from the safe itself
        ///      2.) The response is returned with a revert
        ///      When estimating set `from` to the address of the safe.
        ///      Since the `estimateGas` function includes refunds, call this method to get an estimated of the costs that are deducted from the safe with `execTransaction`
        /// @param to Destination address of Safe transaction.
        /// @param value Ether value of Safe transaction.
        /// @param data Data payload of Safe transaction.
        /// @param operation Operation type of Safe transaction.
        /// @return Estimate without refunds and overhead fees (base transaction and payload data gas costs).
        function requiredTxGas(address to, uint256 value, bytes calldata data, Enum.Operation operation)
            external
            authorized
            returns (uint256)
        {
            uint256 startGas = gasleft();
            // We don't provide an error message here, as we use it to return the estimate
            // solium-disable-next-line error-reason
            require(execute(to, value, data, operation, gasleft()));
            uint256 requiredGas = startGas - gasleft();
            // Convert response to string and return via error message
            revert(string(abi.encodePacked(requiredGas)));
        }
    
        /**
        * @dev Marks a hash as approved. This can be used to validate a hash that is used by a signature.
        * @param hashToApprove The hash that should be marked as approved for signatures that are verified by this contract.
        */
        function approveHash(bytes32 hashToApprove)
            external
        {
            require(owners[msg.sender] != address(0), "Only owners can approve a hash");
            approvedHashes[msg.sender][hashToApprove] = 1;
            emit ApproveHash(hashToApprove, msg.sender);
        }
    
        /**
        * @dev Marks a message as signed
        * @param _data Arbitrary length data that should be marked as signed on the behalf of address(this)
        */
        function signMessage(bytes calldata _data)
            external
            authorized
        {
            bytes32 msgHash = getMessageHash(_data);
            signedMessages[msgHash] = 1;
            emit SignMsg(msgHash);
        }
    
        /**
        * Implementation of ISignatureValidator (see `interfaces/ISignatureValidator.sol`)
        * @dev Should return whether the signature provided is valid for the provided data.
        *       The save does not implement the interface since `checkSignatures` is not a view method.
        *       The method will not perform any state changes (see parameters of `checkSignatures`)
        * @param _data Arbitrary length data signed on the behalf of address(this)
        * @param _signature Signature byte array associated with _data
        * @return a bool upon valid or invalid signature with corresponding _data
        */
        function isValidSignature(bytes calldata _data, bytes calldata _signature)
            external
            returns (bytes4)
        {
            bytes32 messageHash = getMessageHash(_data);
            if (_signature.length == 0) {
                require(signedMessages[messageHash] != 0, "Hash not approved");
            } else {
                // consumeHash needs to be false, as the state should not be changed
                checkSignatures(messageHash, _data, _signature, false);
            }
            return EIP1271_MAGIC_VALUE;
        }
    
        /// @dev Returns hash of a message that can be signed by owners.
        /// @param message Message that should be hashed
        /// @return Message hash.
        function getMessageHash(
            bytes memory message
        )
            public
            view
            returns (bytes32)
        {
            bytes32 safeMessageHash = keccak256(
                abi.encode(SAFE_MSG_TYPEHASH, keccak256(message))
            );
            return keccak256(
                abi.encodePacked(byte(0x19), byte(0x01), domainSeparator, safeMessageHash)
            );
        }
    
        /// @dev Returns the bytes that are hashed to be signed by owners.
        /// @param to Destination address.
        /// @param value Ether value.
        /// @param data Data payload.
        /// @param operation Operation type.
        /// @param safeTxGas Fas that should be used for the safe transaction.
        /// @param baseGas Gas costs for data used to trigger the safe transaction.
        /// @param gasPrice Maximum gas price that should be used for this transaction.
        /// @param gasToken Token address (or 0 if ETH) that is used for the payment.
        /// @param refundReceiver Address of receiver of gas payment (or 0 if tx.origin).
        /// @param _nonce Transaction nonce.
        /// @return Transaction hash bytes.
        function encodeTransactionData(
            address to,
            uint256 value,
            bytes memory data,
            Enum.Operation operation,
            uint256 safeTxGas,
            uint256 baseGas,
            uint256 gasPrice,
            address gasToken,
            address refundReceiver,
            uint256 _nonce
        )
            public
            view
            returns (bytes memory)
        {
            bytes32 safeTxHash = keccak256(
                abi.encode(SAFE_TX_TYPEHASH, to, value, keccak256(data), operation, safeTxGas, baseGas, gasPrice, gasToken, refundReceiver, _nonce)
            );
            return abi.encodePacked(byte(0x19), byte(0x01), domainSeparator, safeTxHash);
        }
    
        /// @dev Returns hash to be signed by owners.
        /// @param to Destination address.
        /// @param value Ether value.
        /// @param data Data payload.
        /// @param operation Operation type.
        /// @param safeTxGas Fas that should be used for the safe transaction.
        /// @param baseGas Gas costs for data used to trigger the safe transaction.
        /// @param gasPrice Maximum gas price that should be used for this transaction.
        /// @param gasToken Token address (or 0 if ETH) that is used for the payment.
        /// @param refundReceiver Address of receiver of gas payment (or 0 if tx.origin).
        /// @param _nonce Transaction nonce.
        /// @return Transaction hash.
        function getTransactionHash(
            address to,
            uint256 value,
            bytes memory data,
            Enum.Operation operation,
            uint256 safeTxGas,
            uint256 baseGas,
            uint256 gasPrice,
            address gasToken,
            address refundReceiver,
            uint256 _nonce
        )
            public
            view
            returns (bytes32)
        {
            return keccak256(encodeTransactionData(to, value, data, operation, safeTxGas, baseGas, gasPrice, gasToken, refundReceiver, _nonce));
        }
    }