// File: contracts/mondo-megabits.sol



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;

import "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts/token/common/ERC2981.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/access/AccessControlEnumerable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
import "@openzeppelin/contracts/utils/cryptography/ECDSA.sol";
import "@openzeppelin/contracts/utils/Address.sol";

import "./mondo-marketplace.sol";

contract MondoTokens is ERC1155, ERC2981, Pausable, AccessControlEnumerable, ReentrancyGuard {
    /* ========================================================================================== */
    /*                                            ROLES                                           */
    /* ========================================================================================== */
    bytes32 public constant PAUSER_ROLE = keccak256("PAUSER_ROLE");
    bytes32 public constant WITHDRAWER_ROLE = keccak256("WITHDRAWER_ROLE");

    /* --------------------------------------- MARKETPLACE -------------------------------------- */
    MondoMarketplace public _mondoMarketplace;

    /* ========================================================================================== */
    /*                                     MINTING INFORMATION                                    */
    /* ========================================================================================== */
    enum MintingStatus {
        WAITING,
        PAUSED,
        STANDARD,
        ADVANCED,
        DONE
    }

    MintingStatus public mintingStatus = MintingStatus.WAITING;

    // Merkle root for the whitelist
    bytes32 public root = 0x36a3f277fab998f2083548bd5c2787cfea178f0898274a95941a60a2c72a3ca4;

    // Signer address for verifying signatures
    address public signer = 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266;

    string public constant PROVENANCE_RECORD = "magnet:?xt=urn:btih:000519fbe6f5ffb26f9d505b334265aa0f8b58c3&dn=Mondo%20Megabits%20Drop%201%20-%20Fake%20Tech";

    // Total supply
    uint256 public constant TOTAL_COPIES_EVER = 4038;

    uint256 public totalMinted = 0;

    bool public whitelistEnabled = true;

    // Maximum minted tokens for a single address
    uint8 public maxMintCountPerAddress = 5;

    // Tokens to be minted.
    // Using uint8 because it will be packed to be smaller so that it's possible to fit this many numbers.
    // Gas will be higher due to packing after modifying a storage variable that doesn't take up a full slot.
    // We have to make this tradeoff though.
    uint8[] tokenPool = [
        63,
        11,
        80,
        81,
        42,
        100,
        55,
        60,
        75,
        28,
        16,
        86,
        96,
        81,
        22,
        39,
        62,
        93,
        83,
        5,
        65,
        31,
        25,
        5,
        22,
        29,
        47,
        51,
        12,
        62,
        84,
        100,
        96,
        20,
        100,
        73,
        70,
        51,
        42,
        97,
        29,
        44,
        75,
        55,
        89,
        60,
        49,
        67,
        87,
        93,
        99,
        57,
        34,
        94,
        24,
        45,
        27,
        99,
        55,
        95,
        48,
        23,
        66,
        84,
        20,
        22,
        6,
        80,
        37,
        73,
        52,
        95,
        3,
        96,
        55,
        90,
        13,
        27,
        80,
        89,
        100,
        39,
        63,
        58,
        94,
        45,
        2,
        72,
        60,
        5,
        85,
        16,
        36,
        19,
        14,
        59,
        55,
        7,
        90,
        10,
        83,
        73,
        75,
        89,
        81,
        32,
        51,
        23,
        6,
        93,
        15,
        49,
        100,
        97,
        20,
        51,
        34,
        53,
        56,
        59,
        22,
        75,
        30,
        63,
        42,
        13,
        65,
        12,
        10,
        2,
        3,
        32,
        100,
        90,
        2,
        73,
        19,
        89,
        63,
        89,
        75,
        21,
        55,
        53,
        75,
        95,
        94,
        83,
        85,
        77,
        75,
        58,
        70,
        76,
        41,
        64,
        15,
        97,
        81,
        19,
        32,
        78,
        3,
        35,
        65,
        40,
        33,
        83,
        29,
        43,
        71,
        66,
        61,
        92,
        129,
        13,
        62,
        35,
        12,
        80,
        33,
        28,
        19,
        33,
        85,
        54,
        88,
        54,
        75,
        91,
        10,
        29,
        73,
        55,
        26,
        89,
        49,
        31,
        96,
        81,
        55,
        13,
        100,
        28,
        6,
        10,
        4,
        88,
        49,
        25,
        66,
        85,
        72,
        30,
        67,
        60,
        64,
        26,
        8,
        44,
        65,
        49,
        94,
        81,
        68,
        4,
        68,
        1,
        77,
        16,
        90,
        53,
        42,
        67,
        21,
        73,
        18,
        62,
        21,
        49,
        53,
        92,
        22,
        32,
        42,
        58,
        89,
        72,
        44,
        6,
        38,
        45,
        44,
        34,
        63,
        27,
        91,
        70,
        35,
        37,
        9,
        45,
        53,
        87,
        27,
        10,
        49,
        83,
        30,
        89,
        57,
        55,
        31,
        71,
        4,
        87,
        4,
        73,
        94,
        7,
        9,
        81,
        88,
        70,
        42,
        49,
        22,
        29,
        27,
        77,
        27,
        32,
        52,
        29,
        73,
        21,
        62,
        37,
        78,
        18,
        32,
        34,
        14,
        4,
        85,
        19,
        22,
        39,
        39,
        81,
        84,
        91,
        75,
        35,
        27,
        60,
        67,
        6,
        86,
        53,
        49,
        90,
        94,
        33,
        117,
        22,
        3,
        71,
        93,
        32,
        69,
        21,
        36,
        84,
        37,
        6,
        29,
        27,
        2,
        29,
        28,
        8,
        79,
        63,
        84,
        5,
        13,
        86,
        79,
        94,
        75,
        51,
        75,
        2,
        4,
        90,
        16,
        45,
        47,
        49,
        42,
        65,
        17,
        2,
        49,
        93,
        36,
        31,
        54,
        27,
        19,
        25,
        22,
        84,
        27,
        36,
        86,
        99,
        63,
        66,
        25,
        87,
        4,
        92,
        89,
        88,
        7,
        32,
        7,
        87,
        34,
        7,
        25,
        11,
        55,
        23,
        39,
        21,
        10,
        22,
        28,
        76,
        44,
        76,
        78,
        67,
        78,
        64,
        60,
        22,
        85,
        99,
        60,
        93,
        47,
        44,
        95,
        91,
        84,
        63,
        7,
        75,
        21,
        93,
        54,
        27,
        99,
        30,
        89,
        32,
        6,
        32,
        5,
        87,
        47,
        30,
        11,
        81,
        86,
        12,
        68,
        32,
        62,
        89,
        7,
        79,
        13,
        7,
        12,
        87,
        96,
        63,
        25,
        6,
        38,
        15,
        100,
        94,
        13,
        64,
        32,
        81,
        57,
        110,
        34,
        83,
        90,
        3,
        94,
        28,
        49,
        12,
        97,
        76,
        34,
        23,
        75,
        94,
        95,
        5,
        44,
        24,
        7,
        96,
        81,
        77,
        1,
        95,
        85,
        96,
        81,
        2,
        29,
        10,
        71,
        34,
        96,
        28,
        42,
        24,
        91,
        54,
        44,
        28,
        62,
        59,
        56,
        49,
        25,
        8,
        66,
        2,
        30,
        86,
        95,
        99,
        14,
        85,
        7,
        3,
        100,
        27,
        78,
        6,
        92,
        26,
        11,
        61,
        12,
        7,
        13,
        99,
        52,
        76,
        49,
        106,
        35,
        29,
        78,
        49,
        28,
        33,
        3,
        105,
        7,
        68,
        69,
        34,
        6,
        12,
        73,
        88,
        55,
        54,
        68,
        14,
        63,
        62,
        47,
        73,
        33,
        60,
        89,
        36,
        42,
        12,
        27,
        120,
        3,
        85,
        10,
        57,
        89,
        9,
        66,
        31,
        15,
        83,
        32,
        22,
        16,
        81,
        29,
        71,
        8,
        16,
        25,
        65,
        96,
        51,
        64,
        70,
        90,
        68,
        7,
        7,
        76,
        64,
        13,
        7,
        94,
        52,
        56,
        20,
        73,
        63,
        49,
        7,
        71,
        31,
        27,
        52,
        39,
        27,
        89,
        14,
        24,
        22,
        80,
        20,
        54,
        68,
        42,
        11,
        39,
        44,
        2,
        75,
        17,
        23,
        99,
        92,
        68,
        5,
        90,
        72,
        81,
        76,
        10,
        49,
        23,
        47,
        73,
        81,
        85,
        59,
        25,
        71,
        67,
        90,
        36,
        94,
        63,
        90,
        76,
        39,
        69,
        30,
        69,
        24,
        100,
        42,
        43,
        55,
        43,
        34,
        14,
        6,
        90,
        67,
        67,
        6,
        51,
        5,
        51,
        60,
        22,
        91,
        83,
        39,
        29,
        69,
        19,
        34,
        11,
        73,
        32,
        21,
        45,
        78,
        16,
        59,
        77,
        52,
        97,
        31,
        4,
        44,
        17,
        13,
        25,
        42,
        30,
        28,
        91,
        33,
        91,
        69,
        6,
        56,
        2,
        6,
        12,
        39,
        86,
        68,
        15,
        66,
        10,
        9,
        52,
        75,
        99,
        25,
        12,
        56,
        83,
        5,
        52,
        16,
        37,
        35,
        18,
        32,
        90,
        49,
        71,
        9,
        26,
        51,
        7,
        63,
        62,
        44,
        73,
        16,
        71,
        99,
        23,
        42,
        6,
        33,
        19,
        85,
        33,
        100,
        23,
        96,
        21,
        76,
        34,
        13,
        51,
        30,
        30,
        42,
        27,
        11,
        54,
        88,
        15,
        6,
        88,
        11,
        80,
        18,
        35,
        12,
        15,
        9,
        39,
        37,
        68,
        99,
        44,
        83,
        8,
        30,
        28,
        94,
        21,
        87,
        6,
        24,
        35,
        54,
        75,
        16,
        58,
        97,
        92,
        39,
        62,
        35,
        52,
        3,
        78,
        48,
        97,
        59,
        79,
        88,
        76,
        7,
        37,
        83,
        3,
        22,
        79,
        22,
        64,
        85,
        68,
        34,
        26,
        31,
        75,
        32,
        7,
        70,
        93,
        47,
        44,
        62,
        96,
        5,
        99,
        91,
        84,
        3,
        86,
        47,
        75,
        85,
        9,
        51,
        80,
        39,
        6,
        32,
        11,
        64,
        57,
        48,
        95,
        47,
        85,
        36,
        28,
        39,
        81,
        57,
        75,
        78,
        73,
        42,
        55,
        35,
        92,
        65,
        36,
        5,
        66,
        69,
        8,
        19,
        87,
        69,
        6,
        92,
        86,
        94,
        37,
        19,
        28,
        75,
        72,
        52,
        88,
        62,
        65,
        52,
        13,
        62,
        2,
        68,
        96,
        32,
        7,
        60,
        86,
        44,
        69,
        86,
        22,
        87,
        6,
        2,
        16,
        5,
        70,
        16,
        75,
        47,
        13,
        90,
        52,
        12,
        28,
        71,
        8,
        23,
        67,
        92,
        70,
        75,
        73,
        54,
        89,
        14,
        6,
        80,
        44,
        70,
        95,
        3,
        75,
        73,
        71,
        37,
        6,
        1,
        81,
        25,
        33,
        88,
        15,
        29,
        93,
        93,
        14,
        100,
        81,
        100,
        44,
        34,
        37,
        6,
        12,
        18,
        36,
        4,
        20,
        86,
        38,
        4,
        83,
        4,
        75,
        52,
        49,
        100,
        62,
        33,
        22,
        11,
        13,
        62,
        100,
        44,
        71,
        16,
        47,
        67,
        47,
        6,
        81,
        22,
        31,
        66,
        31,
        12,
        78,
        54,
        94,
        44,
        39,
        93,
        95,
        86,
        20,
        61,
        87,
        12,
        18,
        23,
        76,
        10,
        71,
        36,
        39,
        65,
        57,
        66,
        63,
        54,
        26,
        72,
        16,
        26,
        100,
        60,
        4,
        53,
        95,
        75,
        62,
        43,
        18,
        55,
        32,
        65,
        66,
        75,
        27,
        2,
        3,
        17,
        77,
        54,
        47,
        83,
        27,
        26,
        11,
        63,
        35,
        89,
        78,
        63,
        91,
        75,
        17,
        36,
        75,
        30,
        44,
        92,
        111,
        47,
        14,
        1,
        78,
        125,
        86,
        33,
        92,
        8,
        84,
        81,
        35,
        100,
        66,
        55,
        48,
        62,
        17,
        35,
        9,
        69,
        87,
        91,
        37,
        63,
        39,
        4,
        37,
        88,
        23,
        70,
        28,
        86,
        87,
        93,
        9,
        21,
        92,
        51,
        34,
        62,
        83,
        84,
        28,
        99,
        18,
        11,
        26,
        89,
        19,
        29,
        49,
        92,
        73,
        39,
        32,
        75,
        28,
        63,
        95,
        35,
        38,
        90,
        20,
        2,
        89,
        87,
        19,
        39,
        55,
        76,
        76,
        28,
        10,
        26,
        30,
        65,
        56,
        43,
        25,
        53,
        84,
        34,
        53,
        77,
        46,
        67,
        85,
        90,
        29,
        48,
        85,
        77,
        75,
        90,
        90,
        6,
        24,
        69,
        21,
        49,
        54,
        73,
        15,
        86,
        97,
        65,
        7,
        52,
        76,
        7,
        88,
        6,
        34,
        87,
        9,
        1,
        69,
        60,
        70,
        25,
        11,
        15,
        85,
        10,
        7,
        42,
        70,
        21,
        32,
        58,
        15,
        68,
        28,
        49,
        7,
        44,
        47,
        62,
        26,
        26,
        89,
        29,
        60,
        17,
        49,
        19,
        29,
        57,
        87,
        81,
        30,
        95,
        21,
        73,
        93,
        74,
        60,
        28,
        22,
        32,
        34,
        73,
        52,
        87,
        62,
        4,
        100,
        87,
        22,
        93,
        98,
        14,
        54,
        86,
        77,
        17,
        34,
        29,
        86,
        6,
        42,
        34,
        28,
        87,
        70,
        65,
        103,
        60,
        4,
        13,
        87,
        43,
        88,
        72,
        3,
        29,
        36,
        90,
        49,
        73,
        76,
        88,
        48,
        83,
        92,
        102,
        52,
        28,
        96,
        6,
        39,
        63,
        31,
        44,
        47,
        92,
        81,
        90,
        27,
        33,
        33,
        30,
        38,
        30,
        70,
        93,
        100,
        75,
        39,
        87,
        84,
        62,
        21,
        86,
        80,
        84,
        66,
        3,
        91,
        28,
        100,
        87,
        13,
        60,
        63,
        93,
        35,
        34,
        33,
        68,
        5,
        7,
        63,
        95,
        124,
        99,
        38,
        58,
        89,
        92,
        44,
        64,
        73,
        49,
        20,
        36,
        88,
        32,
        69,
        42,
        81,
        6,
        63,
        92,
        36,
        9,
        13,
        12,
        35,
        57,
        34,
        99,
        27,
        11,
        29,
        21,
        93,
        31,
        60,
        86,
        75,
        95,
        90,
        91,
        91,
        100,
        80,
        5,
        58,
        93,
        84,
        89,
        57,
        16,
        67,
        94,
        62,
        53,
        118,
        89,
        70,
        39,
        85,
        64,
        25,
        94,
        9,
        88,
        10,
        63,
        37,
        1,
        83,
        10,
        27,
        15,
        29,
        94,
        29,
        44,
        51,
        73,
        55,
        6,
        24,
        10,
        89,
        29,
        28,
        73,
        25,
        71,
        26,
        22,
        10,
        95,
        30,
        42,
        20,
        5,
        54,
        114,
        5,
        42,
        36,
        115,
        45,
        72,
        31,
        29,
        79,
        79,
        5,
        44,
        97,
        23,
        35,
        53,
        87,
        62,
        81,
        70,
        49,
        97,
        68,
        108,
        26,
        61,
        29,
        97,
        63,
        45,
        80,
        89,
        38,
        79,
        35,
        70,
        2,
        27,
        57,
        5,
        55,
        21,
        90,
        61,
        52,
        65,
        100,
        49,
        20,
        58,
        4,
        79,
        75,
        81,
        3,
        19,
        76,
        87,
        89,
        44,
        64,
        73,
        23,
        95,
        35,
        99,
        4,
        76,
        42,
        26,
        94,
        39,
        37,
        49,
        90,
        83,
        92,
        100,
        12,
        88,
        37,
        9,
        48,
        13,
        6,
        86,
        8,
        4,
        78,
        64,
        37,
        39,
        19,
        38,
        30,
        27,
        6,
        28,
        83,
        29,
        32,
        9,
        86,
        19,
        20,
        94,
        31,
        13,
        9,
        80,
        2,
        19,
        80,
        68,
        19,
        69,
        49,
        13,
        31,
        70,
        16,
        16,
        93,
        76,
        66,
        92,
        29,
        33,
        17,
        51,
        28,
        52,
        39,
        1,
        22,
        30,
        54,
        126,
        48,
        16,
        90,
        51,
        65,
        35,
        81,
        36,
        64,
        76,
        25,
        88,
        78,
        83,
        4,
        87,
        95,
        26,
        36,
        95,
        69,
        79,
        13,
        54,
        6,
        87,
        9,
        62,
        68,
        22,
        76,
        3,
        30,
        34,
        52,
        11,
        49,
        37,
        33,
        69,
        81,
        78,
        65,
        11,
        29,
        71,
        14,
        27,
        73,
        89,
        53,
        6,
        23,
        73,
        51,
        36,
        91,
        45,
        67,
        79,
        91,
        2,
        61,
        35,
        73,
        44,
        66,
        9,
        19,
        12,
        69,
        93,
        29,
        47,
        89,
        61,
        6,
        28,
        27,
        67,
        35,
        89,
        7,
        62,
        28,
        99,
        24,
        94,
        7,
        71,
        21,
        81,
        69,
        85,
        81,
        71,
        73,
        26,
        88,
        12,
        32,
        85,
        84,
        78,
        28,
        3,
        51,
        65,
        5,
        44,
        39,
        35,
        13,
        83,
        84,
        33,
        77,
        35,
        93,
        9,
        59,
        68,
        76,
        27,
        20,
        19,
        27,
        81,
        59,
        79,
        64,
        73,
        95,
        85,
        77,
        5,
        78,
        75,
        51,
        4,
        24,
        54,
        13,
        33,
        70,
        75,
        11,
        24,
        83,
        3,
        79,
        19,
        97,
        79,
        34,
        95,
        35,
        85,
        33,
        60,
        61,
        85,
        69,
        84,
        96,
        97,
        80,
        69,
        13,
        73,
        37,
        94,
        96,
        75,
        22,
        94,
        60,
        90,
        20,
        6,
        91,
        11,
        75,
        21,
        16,
        93,
        94,
        17,
        97,
        31,
        64,
        75,
        66,
        1,
        58,
        81,
        100,
        49,
        34,
        62,
        59,
        6,
        19,
        100,
        30,
        3,
        71,
        86,
        79,
        57,
        62,
        91,
        29,
        87,
        100,
        94,
        49,
        39,
        28,
        93,
        66,
        5,
        38,
        29,
        27,
        5,
        25,
        54,
        89,
        71,
        92,
        91,
        88,
        51,
        33,
        99,
        72,
        30,
        54,
        89,
        55,
        35,
        54,
        49,
        4,
        84,
        83,
        82,
        68,
        32,
        90,
        21,
        2,
        32,
        53,
        53,
        24,
        33,
        33,
        37,
        64,
        53,
        1,
        78,
        84,
        39,
        19,
        99,
        33,
        7,
        16,
        97,
        99,
        13,
        21,
        70,
        63,
        72,
        104,
        28,
        59,
        66,
        80,
        39,
        22,
        6,
        13,
        85,
        26,
        72,
        20,
        28,
        86,
        70,
        58,
        63,
        100,
        8,
        88,
        90,
        52,
        70,
        85,
        81,
        5,
        21,
        22,
        85,
        32,
        35,
        31,
        78,
        22,
        70,
        84,
        90,
        53,
        8,
        22,
        68,
        83,
        64,
        27,
        30,
        95,
        29,
        96,
        91,
        83,
        64,
        89,
        39,
        78,
        85,
        87,
        23,
        26,
        53,
        3,
        8,
        64,
        96,
        14,
        26,
        67,
        1,
        63,
        69,
        83,
        97,
        55,
        18,
        31,
        52,
        83,
        75,
        94,
        55,
        36,
        62,
        7,
        35,
        68,
        93,
        57,
        3,
        85,
        75,
        99,
        85,
        27,
        1,
        9,
        57,
        62,
        90,
        39,
        34,
        6,
        66,
        37,
        7,
        85,
        71,
        54,
        86,
        69,
        25,
        27,
        99,
        51,
        34,
        16,
        30,
        95,
        70,
        99,
        94,
        62,
        73,
        100,
        62,
        22,
        85,
        49,
        4,
        35,
        73,
        84,
        109,
        71,
        45,
        68,
        14,
        11,
        27,
        38,
        97,
        52,
        36,
        80,
        100,
        62,
        12,
        3,
        16,
        35,
        15,
        32,
        24,
        79,
        55,
        51,
        84,
        26,
        43,
        49,
        7,
        32,
        94,
        70,
        43,
        83,
        96,
        35,
        52,
        94,
        5,
        13,
        66,
        61,
        48,
        1,
        25,
        64,
        64,
        14,
        5,
        91,
        14,
        3,
        85,
        15,
        6,
        4,
        63,
        96,
        5,
        27,
        100,
        94,
        29,
        23,
        101,
        5,
        56,
        90,
        2,
        14,
        31,
        76,
        2,
        100,
        28,
        92,
        97,
        1,
        19,
        33,
        77,
        83,
        52,
        4,
        23,
        8,
        33,
        34,
        16,
        66,
        64,
        31,
        44,
        27,
        80,
        52,
        25,
        38,
        97,
        51,
        89,
        94,
        25,
        25,
        9,
        7,
        12,
        24,
        91,
        26,
        72,
        51,
        34,
        22,
        33,
        97,
        29,
        69,
        21,
        4,
        22,
        89,
        19,
        4,
        44,
        48,
        92,
        62,
        25,
        21,
        67,
        6,
        63,
        96,
        52,
        67,
        92,
        63,
        29,
        95,
        66,
        62,
        51,
        23,
        28,
        95,
        79,
        67,
        52,
        93,
        83,
        35,
        58,
        13,
        51,
        87,
        70,
        79,
        54,
        21,
        96,
        63,
        35,
        2,
        63,
        61,
        73,
        94,
        79,
        19,
        67,
        28,
        60,
        78,
        15,
        29,
        33,
        39,
        71,
        62,
        75,
        33,
        21,
        68,
        79,
        55,
        36,
        52,
        25,
        37,
        12,
        16,
        90,
        31,
        45,
        91,
        9,
        5,
        84,
        94,
        5,
        83,
        30,
        37,
        85,
        33,
        6,
        85,
        30,
        75,
        52,
        33,
        83,
        23,
        94,
        84,
        35,
        60,
        88,
        94,
        25,
        55,
        13,
        38,
        97,
        38,
        23,
        99,
        63,
        100,
        81,
        19,
        37,
        89,
        49,
        7,
        35,
        26,
        86,
        6,
        95,
        2,
        9,
        69,
        97,
        100,
        8,
        34,
        81,
        4,
        69,
        11,
        27,
        37,
        85,
        44,
        38,
        57,
        21,
        65,
        35,
        63,
        69,
        30,
        70,
        55,
        12,
        4,
        16,
        36,
        6,
        33,
        55,
        24,
        100,
        21,
        19,
        94,
        72,
        29,
        94,
        87,
        27,
        38,
        91,
        29,
        51,
        81,
        83,
        94,
        27,
        100,
        95,
        66,
        3,
        90,
        90,
        78,
        3,
        83,
        53,
        73,
        68,
        93,
        31,
        94,
        88,
        39,
        34,
        36,
        71,
        35,
        26,
        66,
        2,
        14,
        69,
        63,
        17,
        64,
        52,
        25,
        61,
        83,
        81,
        81,
        65,
        94,
        96,
        11,
        33,
        2,
        24,
        73,
        34,
        12,
        65,
        36,
        2,
        48,
        43,
        55,
        100,
        17,
        96,
        68,
        52,
        12,
        27,
        54,
        19,
        39,
        69,
        38,
        26,
        33,
        44,
        83,
        54,
        10,
        88,
        80,
        64,
        26,
        68,
        54,
        11,
        76,
        100,
        81,
        57,
        61,
        3,
        14,
        36,
        4,
        32,
        77,
        9,
        11,
        70,
        10,
        84,
        12,
        78,
        83,
        25,
        89,
        64,
        83,
        8,
        66,
        27,
        23,
        10,
        29,
        28,
        53,
        5,
        44,
        97,
        79,
        53,
        85,
        53,
        12,
        11,
        19,
        17,
        33,
        64,
        15,
        85,
        69,
        36,
        31,
        51,
        60,
        22,
        54,
        72,
        54,
        17,
        31,
        57,
        5,
        14,
        60,
        93,
        44,
        92,
        39,
        51,
        34,
        95,
        15,
        32,
        57,
        55,
        34,
        79,
        54,
        6,
        89,
        42,
        17,
        35,
        3,
        51,
        64,
        44,
        13,
        5,
        93,
        75,
        81,
        34,
        47,
        64,
        76,
        95,
        100,
        63,
        58,
        55,
        66,
        59,
        51,
        22,
        12,
        70,
        81,
        49,
        17,
        81,
        1,
        5,
        9,
        85,
        30,
        20,
        15,
        22,
        97,
        80,
        1,
        37,
        75,
        44,
        9,
        51,
        20,
        24,
        62,
        2,
        54,
        89,
        17,
        2,
        70,
        89,
        80,
        19,
        44,
        14,
        29,
        71,
        100,
        20,
        93,
        30,
        44,
        31,
        97,
        83,
        42,
        57,
        13,
        68,
        45,
        94,
        67,
        9,
        37,
        42,
        56,
        99,
        26,
        22,
        62,
        89,
        78,
        35,
        71,
        55,
        33,
        13,
        83,
        30,
        86,
        83,
        5,
        77,
        86,
        8,
        107,
        12,
        69,
        94,
        73,
        31,
        97,
        86,
        26,
        73,
        57,
        37,
        49,
        30,
        88,
        27,
        57,
        13,
        38,
        20,
        39,
        67,
        81,
        81,
        96,
        130,
        94,
        9,
        7,
        85,
        51,
        4,
        33,
        60,
        19,
        35,
        62,
        52,
        4,
        9,
        28,
        36,
        10,
        31,
        22,
        5,
        17,
        63,
        62,
        49,
        2,
        56,
        21,
        49,
        30,
        49,
        33,
        52,
        53,
        95,
        88,
        21,
        60,
        88,
        29,
        67,
        86,
        39,
        68,
        89,
        23,
        25,
        88,
        39,
        36,
        9,
        54,
        2,
        28,
        5,
        10,
        2,
        67,
        75,
        81,
        91,
        97,
        38,
        32,
        94,
        19,
        22,
        51,
        26,
        96,
        12,
        6,
        79,
        62,
        68,
        85,
        34,
        25,
        39,
        14,
        12,
        35,
        83,
        93,
        33,
        112,
        38,
        54,
        94,
        53,
        70,
        97,
        55,
        95,
        32,
        33,
        11,
        85,
        33,
        49,
        76,
        28,
        34,
        73,
        2,
        34,
        86,
        87,
        17,
        43,
        80,
        22,
        33,
        128,
        73,
        61,
        30,
        39,
        26,
        10,
        60,
        20,
        22,
        2,
        6,
        87,
        62,
        67,
        56,
        19,
        76,
        92,
        54,
        69,
        25,
        71,
        55,
        27,
        37,
        6,
        81,
        10,
        10,
        100,
        87,
        43,
        70,
        88,
        86,
        119,
        32,
        64,
        92,
        54,
        90,
        30,
        39,
        89,
        7,
        76,
        94,
        81,
        21,
        27,
        80,
        29,
        9,
        2,
        28,
        86,
        99,
        27,
        72,
        75,
        68,
        29,
        90,
        13,
        47,
        8,
        57,
        51,
        6,
        75,
        27,
        95,
        73,
        13,
        86,
        78,
        77,
        93,
        2,
        20,
        80,
        38,
        38,
        75,
        55,
        85,
        100,
        86,
        70,
        76,
        83,
        91,
        99,
        16,
        60,
        75,
        51,
        73,
        35,
        9,
        5,
        113,
        100,
        73,
        32,
        94,
        13,
        22,
        51,
        78,
        5,
        5,
        6,
        62,
        5,
        95,
        5,
        81,
        8,
        58,
        21,
        52,
        85,
        14,
        97,
        25,
        26,
        7,
        39,
        95,
        76,
        83,
        22,
        67,
        17,
        48,
        88,
        100,
        30,
        27,
        70,
        87,
        33,
        25,
        54,
        8,
        60,
        8,
        7,
        72,
        78,
        65,
        3,
        100,
        36,
        76,
        85,
        13,
        7,
        19,
        70,
        26,
        16,
        78,
        94,
        47,
        87,
        16,
        59,
        2,
        78,
        56,
        6,
        55,
        44,
        65,
        31,
        67,
        49,
        37,
        53,
        11,
        14,
        31,
        62,
        9,
        88,
        58,
        72,
        37,
        9,
        27,
        72,
        19,
        39,
        52,
        70,
        24,
        56,
        34,
        57,
        64,
        34,
        13,
        73,
        53,
        84,
        3,
        55,
        55,
        45,
        45,
        33,
        63,
        73,
        99,
        99,
        44,
        94,
        23,
        62,
        81,
        19,
        33,
        29,
        5,
        27,
        39,
        58,
        39,
        12,
        36,
        16,
        87,
        53,
        5,
        86,
        13,
        70,
        81,
        16,
        2,
        56,
        64,
        22,
        52,
        90,
        22,
        57,
        85,
        13,
        71,
        34,
        55,
        47,
        91,
        76,
        9,
        22,
        57,
        85,
        95,
        32,
        32,
        5,
        71,
        89,
        43,
        63,
        11,
        1,
        18,
        29,
        95,
        13,
        32,
        68,
        71,
        49,
        39,
        48,
        27,
        75,
        39,
        49,
        96,
        68,
        47,
        49,
        68,
        68,
        88,
        70,
        87,
        63,
        68,
        19,
        22,
        28,
        60,
        21,
        70,
        54,
        16,
        67,
        131,
        18,
        11,
        85,
        83,
        89,
        52,
        18,
        26,
        45,
        3,
        29,
        20,
        7,
        58,
        4,
        34,
        1,
        30,
        63,
        86,
        38,
        14,
        76,
        26,
        85,
        78,
        36,
        17,
        88,
        83,
        5,
        34,
        5,
        84,
        34,
        32,
        57,
        7,
        8,
        62,
        36,
        79,
        2,
        62,
        48,
        35,
        3,
        4,
        53,
        72,
        6,
        85,
        49,
        71,
        15,
        60,
        36,
        7,
        99,
        67,
        56,
        44,
        5,
        96,
        26,
        30,
        62,
        57,
        90,
        52,
        58,
        96,
        5,
        31,
        21,
        54,
        7,
        123,
        83,
        99,
        69,
        62,
        54,
        99,
        60,
        86,
        68,
        7,
        31,
        5,
        52,
        33,
        49,
        7,
        87,
        26,
        70,
        83,
        5,
        95,
        42,
        127,
        71,
        30,
        10,
        89,
        48,
        64,
        33,
        11,
        90,
        13,
        9,
        69,
        36,
        36,
        73,
        53,
        13,
        11,
        3,
        2,
        27,
        76,
        67,
        99,
        22,
        5,
        28,
        91,
        52,
        96,
        65,
        68,
        30,
        19,
        75,
        7,
        17,
        4,
        7,
        90,
        33,
        55,
        86,
        23,
        9,
        19,
        52,
        67,
        23,
        16,
        16,
        52,
        79,
        90,
        14,
        71,
        58,
        99,
        100,
        44,
        19,
        81,
        39,
        100,
        88,
        37,
        33,
        5,
        13,
        89,
        59,
        25,
        4,
        75,
        39,
        49,
        83,
        21,
        1,
        44,
        56,
        17,
        100,
        8,
        75,
        4,
        51,
        6,
        87,
        12,
        93,
        90,
        67,
        77,
        100,
        39,
        5,
        30,
        7,
        85,
        53,
        71,
        94,
        31,
        83,
        42,
        32,
        48,
        63,
        70,
        96,
        51,
        71,
        99,
        35,
        100,
        38,
        96,
        71,
        14,
        69,
        85,
        88,
        95,
        29,
        68,
        22,
        23,
        53,
        49,
        63,
        26,
        12,
        48,
        31,
        21,
        33,
        64,
        32,
        59,
        15,
        53,
        52,
        62,
        1,
        55,
        22,
        73,
        47,
        16,
        19,
        61,
        90,
        81,
        65,
        59,
        96,
        86,
        38,
        90,
        84,
        86,
        81,
        27,
        73,
        73,
        66,
        52,
        59,
        76,
        26,
        26,
        86,
        70,
        62,
        87,
        87,
        77,
        16,
        39,
        66,
        39,
        90,
        57,
        44,
        66,
        68,
        8,
        59,
        83,
        16,
        94,
        39,
        19,
        63,
        76,
        69,
        11,
        34,
        35,
        1,
        9,
        83,
        35,
        13,
        68,
        78,
        68,
        88,
        71,
        35,
        9,
        6,
        94,
        78,
        64,
        78,
        88,
        19,
        21,
        34,
        24,
        44,
        57,
        57,
        35,
        100,
        100,
        100,
        47,
        22,
        84,
        39,
        64,
        47,
        28,
        76,
        75,
        17,
        6,
        88,
        88,
        62,
        38,
        78,
        24,
        32,
        84,
        26,
        37,
        44,
        71,
        28,
        69,
        33,
        64,
        9,
        34,
        42,
        90,
        30,
        34,
        91,
        60,
        32,
        60,
        90,
        55,
        29,
        37,
        88,
        73,
        14,
        49,
        86,
        100,
        26,
        19,
        26,
        66,
        95,
        7,
        51,
        26,
        9,
        88,
        38,
        77,
        95,
        5,
        91,
        87,
        62,
        88,
        5,
        91,
        53,
        22,
        12,
        89,
        100,
        90,
        52,
        32,
        73,
        36,
        34,
        90,
        21,
        96,
        66,
        70,
        49,
        34,
        13,
        15,
        95,
        13,
        95,
        92,
        5,
        15,
        67,
        65,
        25,
        88,
        81,
        15,
        90,
        93,
        27,
        32,
        86,
        29,
        76,
        83,
        69,
        22,
        7,
        45,
        69,
        59,
        31,
        70,
        9,
        34,
        55,
        57,
        5,
        57,
        75,
        68,
        11,
        7,
        13,
        33,
        89,
        38,
        22,
        83,
        14,
        22,
        69,
        76,
        54,
        24,
        31,
        99,
        34,
        11,
        29,
        95,
        86,
        8,
        2,
        4,
        67,
        26,
        34,
        38,
        94,
        7,
        49,
        87,
        13,
        16,
        81,
        34,
        57,
        28,
        87,
        26,
        52,
        49,
        79,
        81,
        100,
        71,
        95,
        11,
        35,
        26,
        44,
        91,
        66,
        67,
        29,
        33,
        63,
        52,
        94,
        15,
        79,
        88,
        52,
        1,
        96,
        52,
        21,
        11,
        54,
        87,
        27,
        68,
        70,
        33,
        55,
        68,
        31,
        1,
        96,
        38,
        78,
        21,
        35,
        35,
        36,
        71,
        52,
        57,
        62,
        35,
        12,
        21,
        69,
        25,
        85,
        90,
        42,
        57,
        34,
        33,
        58,
        64,
        52,
        71,
        33,
        69,
        23,
        97,
        76,
        65,
        21,
        55,
        27,
        19,
        85,
        27,
        64,
        27,
        55,
        18,
        16,
        49,
        92,
        70,
        17,
        60,
        9,
        8,
        29,
        96,
        53,
        25,
        79,
        1,
        30,
        95,
        67,
        32,
        79,
        9,
        73,
        11,
        53,
        81,
        96,
        15,
        8,
        23,
        66,
        90,
        7,
        75,
        19,
        78,
        8,
        37,
        13,
        11,
        43,
        11,
        33,
        85,
        30,
        8,
        99,
        73,
        76,
        55,
        52,
        15,
        48,
        37,
        83,
        92,
        16,
        21,
        6,
        57,
        73,
        83,
        94,
        24,
        35,
        15,
        69,
        15,
        57,
        90,
        70,
        39,
        29,
        69,
        81,
        24,
        9,
        80,
        78,
        88,
        52,
        58,
        7,
        62,
        86,
        69,
        78,
        21,
        68,
        31,
        39,
        76,
        100,
        39,
        54,
        52,
        60,
        76,
        44,
        53,
        31,
        91,
        52,
        63,
        99,
        79,
        94,
        3,
        24,
        30,
        76,
        91,
        80,
        63,
        91,
        52,
        95,
        19,
        87,
        27,
        21,
        13,
        35,
        73,
        62,
        67,
        7,
        30,
        53,
        55,
        64,
        56,
        1,
        34,
        1,
        36,
        11,
        63,
        85,
        9,
        73,
        93,
        96,
        54,
        93,
        17,
        26,
        90,
        27,
        85,
        94,
        3,
        64,
        37,
        20,
        76,
        55,
        27,
        43,
        76,
        16,
        66,
        67,
        90,
        7,
        100,
        26,
        54,
        75,
        61,
        81,
        49,
        88,
        6,
        69,
        64,
        88,
        28,
        24,
        10,
        49,
        45,
        54,
        33,
        94,
        2,
        29,
        35,
        85,
        81,
        91,
        65,
        48,
        43,
        93,
        78,
        9,
        11,
        68,
        27,
        22,
        80,
        21,
        10,
        16,
        64,
        73,
        64,
        16,
        25,
        90,
        22,
        16,
        68,
        9,
        45,
        7,
        18,
        90,
        53,
        55,
        22,
        22,
        66,
        21,
        65,
        19,
        1,
        57,
        3,
        35,
        27,
        20,
        93,
        20,
        24,
        96,
        75,
        42,
        18,
        17,
        100,
        95,
        47,
        43,
        24,
        49,
        43,
        55,
        28,
        23,
        86,
        54,
        45,
        30,
        86,
        64,
        95,
        66,
        73,
        10,
        30,
        30,
        53,
        20,
        37,
        14,
        66,
        29,
        52,
        70,
        29,
        93,
        100,
        62,
        87,
        35,
        66,
        76,
        53,
        88,
        73,
        17,
        77,
        63,
        96,
        95,
        49,
        68,
        3,
        88,
        54,
        88,
        60,
        62,
        33,
        21,
        14,
        86,
        13,
        8,
        63,
        12,
        44,
        44,
        70,
        13,
        35,
        80,
        64,
        1,
        20,
        64,
        85,
        21,
        16,
        29,
        28,
        9,
        16,
        21,
        8,
        9,
        39,
        4,
        20,
        68,
        54,
        62,
        5,
        13,
        3,
        57,
        37,
        1,
        76,
        81,
        75,
        39,
        3,
        83,
        121,
        6,
        6,
        66,
        93,
        32,
        24,
        80,
        80,
        17,
        17,
        4,
        78,
        94,
        64,
        57,
        19,
        122,
        83,
        4,
        80,
        60,
        86,
        67,
        35,
        7,
        89,
        99,
        87,
        47,
        1,
        43,
        16,
        83,
        33,
        12,
        61,
        65,
        22,
        68,
        1,
        23,
        29,
        26,
        95,
        60,
        91,
        25,
        47,
        64,
        32,
        87,
        66,
        26,
        31,
        52,
        37,
        92,
        83,
        64,
        25,
        10,
        116,
        91,
        69,
        9,
        55,
        76,
        90,
        29,
        13,
        22,
        16,
        69,
        69,
        61,
        32,
        34,
        61,
        13,
        5,
        13,
        90,
        77,
        83,
        100,
        67,
        67,
        13,
        28,
        28,
        64,
        89,
        93,
        7,
        16
    ];

    // [owner] -> amount they minted
    mapping(address => uint8) public mintCount;

    // [owner] -> nonce
    mapping(address => uint16) public ownerNonces;

    /* ============================================================================================================== */
    /*                                                  CONSTRUCTION                                                  */
    /* ============================================================================================================== */
    constructor() ERC1155("https://metadata.mondomegabits.com/{id}") {
        _grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
        _grantRole(PAUSER_ROLE, msg.sender);
        _grantRole(WITHDRAWER_ROLE, msg.sender);
        _grantRole(WITHDRAWER_ROLE, 0xe87F4610c48E17ee0EE0c72Fe280450CA660399B);

        _setDefaultRoyalty(address(this), 1000);
    }

    bool private hasGiftedTokens = false;

    function giveGiftTokens(uint256[] memory ids, uint256[] memory counts)
        external
        nonReentrant
        onlyRole(DEFAULT_ADMIN_ROLE)
    {
        require(hasGiftedTokens == false, "Already gave tokens");
        hasGiftedTokens = true;

        totalMinted += 80;

        // Gifted cards
        _mint(0x690892db2FA439A703115D36E4205088E0C55D1a, 43, 1, "");
        _mint(0xC2f158234da1956623A0D0E4ceAAcC764A609B81, 61, 1, "");
        _mint(0x06056Dcdc6471439e31e878492f594B6F0D8F9D0, 52, 1, "");
        _mint(0x1be4AB962fbCA955de52450BB78B46052eac0a62, 62, 1, "");
        _mint(0x3cB85f3C1E18f1ec13B425669Cb8F6b7086f1D0C, 50, 1, "");
        _mint(0xcbc808e97dCc154B6e8df4c44f05d9C22011BeD1, 18, 1, "");
        _mint(0x0Faa2615BaC776207034620Bdb87D8F3d032Ac90, 13, 1, "");
        _mint(0x229600cfd8E158A42527E09513BfF42fdFd09Fa8, 94, 1, "");
        _mint(0xA4dD7f4A4BAf918D97ED921Dfd918FD2aeF7e451, 30, 1, "");
        _mint(0x6D8FB06f9233352CBF4F2274F22b13DD3B37015A, 91, 1, "");
        _mint(0xbCB5E08E412Fa901973C638E59eFDA1EcB9Ef354, 48, 1, "");
        _mint(0x8A78cEef8752a7E90c27C4aEAcE13f3C4Af56e33, 91, 1, "");
        _mint(0x7C4a7d85623CdF7bBf67ceaf2b862E3028Fe9aCb, 36, 1, "");
        _mint(0xe873e624431024e46de133987498d45ba9E8A123, 89, 1, "");
        _mint(0xbf67e713ddEf50496c6F27C41Eaeecee3A9FA063, 90, 1, "");
        _mint(0x395ff02870F6C146E2b50512b9755fECb57E83C0, 56, 1, "");
        _mint(0x85dC88dABE0fb9988190B5B228c9Aa5804070864, 69, 1, "");
        _mint(0xAE17e8F2aC7C0B8c3a77d29fF60E73Af1A5Bf089, 43, 1, "");
        _mint(0x19355237CdB7b76bC166E05F6338F270B084ecFc, 76, 1, "");
        _mint(0x524Aa222370690cDc108a2A54d9054dC4C31146c, 72, 1, "");
        _mint(0xb7397e25a2e167941dEd07e605953EE620756a22, 39, 1, "");
        _mint(0xd4D3A6EACF401Fa50033114A6952220D6f60B648, 37, 1, "");
        _mint(0x0e6114613A5e28F628f41Da0Ec204Ca505956be8, 56, 1, "");
        _mint(0x81Ed8BC0C08b6871738b7514C36d347B2cC172E8, 42, 1, "");
        _mint(0xba1a95c258290Ae6A0cbDAbfA8fB6e471a48032F, 12, 1, "");
        _mint(0xEe34a1CcE595270AD3Ecc4D50D34dF978954584c, 20, 1, "");
        _mint(0x448A1BF01d6B7cc9D2c8cD1d05d246bcd840517F, 20, 1, "");
        _mint(0x386Fa68BeF515dab808358935418e37Fa1748C32, 7, 1, "");
        _mint(0x30Da874E35Fbd3a1580ed1E9eFaeEe2E180b4aCD, 90, 1, "");
        _mint(0xbCB5E08E412Fa901973C638E59eFDA1EcB9Ef354, 87, 1, "");
        _mint(0xAE17e8F2aC7C0B8c3a77d29fF60E73Af1A5Bf089, 62, 1, "");
        _mint(0xcAB58f3BacDBe6F8F6e41Ff322ce97b2196024f4, 53, 1, "");
        _mint(0x43D588cb4411e83e860c6166763112e1ccF76EA9, 56, 1, "");
        _mint(0x24Ef7784caC2c628Eef21b98bE9f70044b5e3Be4, 31, 1, "");
        _mint(0xCAC3DDD55551D0ec3176FFb7347C8c290dcb866F, 60, 1, "");
        _mint(0x229600cfd8E158A42527E09513BfF42fdFd09Fa8, 51, 1, "");
        _mint(0x240224AC042e7F0Eb7A16AF0a49907061c9894C8, 65, 1, "");
        _mint(0xDc5CbDF0Ff309b49A332dbFa3264b775c2cAfd09, 85, 1, "");
        _mint(0xB3407C29463951902aeF4a6a87265Ff0263b5689, 99, 1, "");
        _mint(0xe1677D385bC7c62486ED53c7B6CdE63FE9EE1F2d, 85, 1, "");
        _mint(0x1a6763326dE2837048E72190d9842F38ee0C491B, 30, 1, "");
        _mint(0xc665AB82Bae93188D39bF6Aa0c2Cf2ca66Fc66b4, 18, 1, "");
        _mint(0xDF017621ebdFe522dBA6F08A4a1db6E791216ecF, 38, 1, "");
        _mint(0xaA608298eA894013eC76e5D963752bEf6d097a85, 56, 1, "");
        _mint(0xaA608298eA894013eC76e5D963752bEf6d097a85, 52, 1, "");
        _mintBatch(0xd3DEB100f6Be210F29F087b707F29ffa93F7e50A, ids, counts, "");
    }

    /* ========================================================================================== */
    /*                                          METADATA                                          */
    /* ========================================================================================== */
    function contractURI() external pure returns (string memory) {
        return "https://metadata.mondomegabits.com/contract";
    }

    function supportsInterface(bytes4 interfaceId)
        public
        view
        override(ERC1155, ERC2981, AccessControlEnumerable)
        returns (bool)
    {
        return super.supportsInterface(interfaceId);
    }

    /* ============================================================================================================== */
    /*                                         PSEUDORANDOM NUMBER GENERATION                                         */
    /* ============================================================================================================== */

    // Generates a random number from 0 to uint256.max
    function rand() private view returns (uint256) {
        return uint256(keccak256(abi.encodePacked(block.difficulty, block.timestamp)));
    }

    function randomMintIdx() private view returns (uint16) {
        return uint16(rand() % (tokenPool.length));
    }

    /* ============================================================================================================== */
    /*                                                    ROYALTIES                                                   */
    /* ============================================================================================================== */
    uint256 public constant ROYALTY_DIVISOR = 20; // 5% royalties: 1/.05 = 20

    /* ============================================================================================================== */
    /*                                                     MINTING                                                    */
    /* ============================================================================================================== */
    event CardMinted(uint256 id, address owner);

    /* ----------------------------------- START STANDARD MINT ---------------------------------- */

    function verifyLeaf(bytes32[] calldata proof, bytes32 leaf) internal view returns (bool) {
        return MerkleProof.verifyCalldata(proof, root, leaf);
    }

    function mint(uint8 count, bytes32[] calldata proof) external payable returns (uint256[] memory ids) {
        /* ====================================================================================== */
        /*                                         CHECKS                                         */
        /* ====================================================================================== */
        require(mintingStatus == MintingStatus.STANDARD);

        require(msg.value == (.25 ether * count), "Incorrect price.");
        require(tx.origin == msg.sender && !Address.isContract(msg.sender), "Contracts can't mint.");
        require(count > 0, "Must mint at least 1");
        require(totalMinted + count <= TOTAL_COPIES_EVER, "Cannot mint more than total copies ever");
        require(mintCount[msg.sender] + count <= maxMintCountPerAddress, "Above max mintable cards.");
        if (whitelistEnabled) {
            require(verifyLeaf(proof, bytes32(keccak256(abi.encodePacked(msg.sender)))), "Not whitelisted");
        }

        /* ====================================================================================== */
        /*                                         EFFECTS                                        */
        /* ====================================================================================== */
        ids = new uint256[](count);

        for (uint8 i = 0; i < count; i++) {
            uint16 tokenPoolIdx = randomMintIdx();
            uint16 tokenPoolLastIdx = uint16(tokenPool.length) - 1;
            uint16 id = uint16(tokenPool[tokenPoolIdx]);

            // Pop and swap:
            if (tokenPoolIdx == tokenPoolLastIdx) {
                tokenPool.pop();
            } else {
                tokenPool[tokenPoolIdx] = tokenPool[tokenPoolLastIdx];
                tokenPool.pop();
            }

            ids[i] = id;
            mintCount[msg.sender]++;

            totalMinted++;

            if (totalMinted == TOTAL_COPIES_EVER) {
                mintingStatus = MintingStatus.DONE;
            }

            emit CardMinted(id, msg.sender);
        }

        /* ====================================================================================== */
        /*                                      INTERACTIONS                                      */
        /* ====================================================================================== */

        if (count == 1) {
            _mint(msg.sender, ids[0], 1, "");
        } else {
            uint256[] memory amounts = new uint256[](count);
            for (uint8 i = 0; i < count; i++) {
                amounts[i] = 1;
            }

            _mintBatch(msg.sender, ids, amounts, "");
        }

        return ids;
    }

    /* ------------------------------------ END STANDARD MINT ----------------------------------- */

    /* ----------------------------------- START ADVANCED MINT ---------------------------------- */
    function advancedMint(
        uint256[] calldata _ids,
        uint256 _expiryTimestamp,
        uint256 _nonce,
        bytes32 _messageHash,
        bytes calldata _signature
    ) external payable returns (uint256[] calldata) {
        /* ====================================================================================== */
        /*                                         CHECKS                                         */
        /* ====================================================================================== */
        require(mintingStatus == MintingStatus.ADVANCED);

        uint256 count = _ids.length;
        require(count > 0, "Must mint at least 1");
        require(msg.value == (.25 ether * count), "Incorrect price.");
        require(totalMinted + count <= TOTAL_COPIES_EVER, "Cannot mint more than total copies ever");
        require(mintCount[msg.sender] + count <= maxMintCountPerAddress, "Above max mintable cards.");

        require(tx.origin == msg.sender && !Address.isContract(msg.sender), "Contracts can't mint.");
        require(block.timestamp < _expiryTimestamp, "Expired timestamp, try again in a couple minutes.");
        require(ownerNonces[msg.sender] < _nonce, "Nonce already used.");

        /* ---------------------------------- SIGNATURE CHECKS ---------------------------------- */
        bytes32 ethSignedMessageHash = keccak256(
            abi.encodePacked(
                "\x19Ethereum Signed Message:\n32",
                keccak256(abi.encode(_ids, _expiryTimestamp, msg.sender, _nonce))
            )
        );
        require(_messageHash == ethSignedMessageHash, "Invalid message hash");
        require(ECDSA.recover(ethSignedMessageHash, _signature) == signer, "Invalid signature");

        /* ====================================================================================== */
        /*                                         EFFECTS                                        */
        /* ====================================================================================== */
        ownerNonces[msg.sender] = uint16(_nonce);

        for (uint8 i = 0; i < count; ++i) {
            mintCount[msg.sender]++;

            totalMinted++;

            if (totalMinted >= TOTAL_COPIES_EVER) {
                mintingStatus = MintingStatus.DONE;
            }

            emit CardMinted(_ids[i], msg.sender);
        }

        /* ====================================================================================== */
        /*                                      INTERACTIONS                                      */
        /* ====================================================================================== */
        if (count == 1) {
            _mint(msg.sender, _ids[0], 1, "");
        } else {
            uint256[] memory amounts = new uint256[](count);
            for (uint8 i = 0; i < count; i++) {
                amounts[i] = 1;
            }

            _mintBatch(msg.sender, _ids, amounts, "");
        }

        return _ids;
    }

    /* ============================================================================================================== */
    /*                                                   MARKETPLACE                                                  */
    /* ============================================================================================================== */
    function _beforeTokenTransfer(
        address operator,
        address from,
        address to,
        uint256[] memory ids,
        uint256[] memory amounts,
        bytes memory data
    ) internal virtual override(ERC1155) whenNotPaused {
        super._beforeTokenTransfer(operator, from, to, ids, amounts, data);
        if (from != address(0)) {
            uint16 length = uint16(ids.length);
            for (uint16 i = 0; i < length; i++) {
                uint16 amountOwned = uint16(balanceOf(from, ids[i]));
                uint16 amountSelling = uint16(_mondoMarketplace.getForSaleCount(ids[i], from));
                uint16 amountLeftAfterSales = amountOwned - amountSelling;
                uint16 amountToCancel = uint16(amounts[i]) - amountLeftAfterSales;

                if (amountToCancel > 0) {
                    _mondoMarketplace.cancelSaleToTransfer(from, ids[i], uint8(amountToCancel));
                }
            }
        }
    }

    function isApprovedForAll(address account, address operator) public view virtual override(ERC1155) returns (bool) {
        if (operator == address(_mondoMarketplace)) {
            return true;
        } else {
            return super.isApprovedForAll(account, operator);
        }
    }

    /* ============================================================================================================== */
    /*                                          ADMINISTRATIVE FUNCTIONALITY                                          */
    /* ============================================================================================================== */

    /* --------------------------------------- WITHDRAWING -------------------------------------- */
    function withdraw() external onlyRole(WITHDRAWER_ROLE) {
        uint256 balance = address(this).balance;
        (bool success, ) = msg.sender.call{value: balance}("");
        require(success, "Failed to send Ether");
    }

    function grantWithdrawerRole(address newWithdrawer) external onlyRole(DEFAULT_ADMIN_ROLE) {
        require(newWithdrawer != address(0), "Must provide a valid address");
        require(newWithdrawer != msg.sender, "Cannot grant yourself withdrawer role");
        require(!hasRole(WITHDRAWER_ROLE, newWithdrawer), "Already has withdrawer role");
        grantRole(WITHDRAWER_ROLE, newWithdrawer);
    }

    function removeWithdrawerRole(address oldWithdrawer) external onlyRole(DEFAULT_ADMIN_ROLE) {
        require(oldWithdrawer != address(0), "Must provide a valid address");
        require(oldWithdrawer != msg.sender, "Cannot remove yourself withdrawer role");
        require(hasRole(WITHDRAWER_ROLE, oldWithdrawer), "Does not have withdrawer role");
        revokeRole(WITHDRAWER_ROLE, oldWithdrawer);
    }

    /* --------------------------------------- MARKETPLACE -------------------------------------- */

    function setMondoMarketplace(address mondoMarketplaceAddress) external onlyRole(DEFAULT_ADMIN_ROLE) {
        _mondoMarketplace = MondoMarketplace(mondoMarketplaceAddress);
    }

    /* ------------------------------------ MINTING SETTINGS ------------------------------------ */

    function setWhitelist(bool enabled) external onlyRole(DEFAULT_ADMIN_ROLE) {
        whitelistEnabled = enabled;
    }

    function changeMerkleRoot(bytes32 newRoot) external onlyRole(DEFAULT_ADMIN_ROLE) {
        root = newRoot;
    }

    function changeSigner(address newSigner) external onlyRole(DEFAULT_ADMIN_ROLE) {
        signer = newSigner;
    }

    function setMintingStatus(MintingStatus status) external onlyRole(DEFAULT_ADMIN_ROLE) {
        mintingStatus = status;
    }

    function changeMaxMintCountPerAddress(uint8 newMaxMintCountPerAddress) external onlyRole(DEFAULT_ADMIN_ROLE) {
        maxMintCountPerAddress = newMaxMintCountPerAddress;
    }

    /* ----------------------------------------- PAUSING ---------------------------------------- */

    function pause() external onlyRole(PAUSER_ROLE) {
        _pause();
    }

    function unpause() external onlyRole(PAUSER_ROLE) {
        _unpause();
    }

    function grantPauserRole(address newPauser) external onlyRole(DEFAULT_ADMIN_ROLE) {
        require(newPauser != address(0), "Must provide a valid address");
        require(newPauser != msg.sender, "Cannot grant yourself pauser role");
        require(!hasRole(PAUSER_ROLE, newPauser), "Already has pauser role");
        grantRole(PAUSER_ROLE, newPauser);
    }

    function removePauserRole(address oldPauser) external onlyRole(DEFAULT_ADMIN_ROLE) {
        require(oldPauser != address(0), "Must provide a valid address");
        require(oldPauser != msg.sender, "Cannot remove yourself from the pauser role");
        require(hasRole(PAUSER_ROLE, oldPauser), "Does not have pauser role");
        revokeRole(PAUSER_ROLE, oldPauser);
    }
}

// File: contracts/mondo-marketplace.sol



// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;

import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/PullPaymentUpgradeable.sol";
import "./mondo-megabits.sol";

contract MondoMarketplace is
    Initializable,
    OwnableUpgradeable,
    ReentrancyGuardUpgradeable,
    PullPaymentUpgradeable,
    UUPSUpgradeable
{
    MondoTokens _mondoTokensContract;

    /// @custom:oz-upgrades-unsafe-allow constructor
    constructor() {
        _disableInitializers();
    }

    function initialize(address mondoTokensAddress) public payable initializer {
        __Ownable_init();
        __ReentrancyGuard_init();
        __PullPayment_init();
        __UUPSUpgradeable_init();

        _mondoTokensContract = MondoTokens(mondoTokensAddress);
    }

    /* ----------------------------------------- EVENTS ----------------------------------------- */
    event TokenForSale(uint256 id, address owner, uint256 price);
    event TokenSaleCancelled(uint256 id, address owner, uint256 price);
    event TokenSold(uint256 id, address soldBy, uint256 price, address soldTo);

    /* ========================================================================================== */
    /*                                      SALE INFORMATION                                      */
    /* ========================================================================================== */

    // [tokenID][owner] -> uint256[] Prices
    mapping(uint256 => mapping(address => uint256[])) private _forSalePrices;

    // Returns the length of the array of prices for a given tokenID and owner.
    function getForSaleCount(uint256 tokenID, address owner) external view returns (uint256) {
        return _forSalePrices[tokenID][owner].length;
    }

    /* ========================================================================================== */
    /*                                  MARKETPLACE FUNCTIONALITY                                 */
    /* ========================================================================================== */

    function putUpForSale(uint256 id, uint256 price) external {
        require(price > 0, "Price isn't >0");
        require(
            _mondoTokensContract.balanceOf(_msgSender(), id) > _forSalePrices[id][_msgSender()].length,
            "None to sell"
        );

        _forSalePrices[id][_msgSender()].push(price);

        emit TokenForSale(id, _msgSender(), price);
    }

    function cancelSaleToTransfer(
        address owner,
        uint256 id,
        uint8 count
    ) external {
        require(_msgSender() == address(_mondoTokensContract));

        uint8 amountForSale = uint8(_forSalePrices[id][owner].length);
        require(count > 0 && count <= amountForSale, "Invalid amount to cancel");

        for (uint8 i = 0; i < count; i++) {
            uint256 price = _forSalePrices[id][owner][_forSalePrices[id][owner].length - 1];
            emit TokenSaleCancelled(id, owner, price);
        }
    }

    function cancelSale(uint256 id, uint256 price) external {
        require(removeFromSale(id, _msgSender(), price));
        emit TokenSaleCancelled(id, _msgSender(), price);
    }

    function removeFromSale(
        uint256 id,
        address owner,
        uint256 price
    ) private returns (bool) {
        uint8 salesLength = uint8(_forSalePrices[id][owner].length);
        require(salesLength > 0, "None for sale!");

        if (salesLength == 1) {
            if (price == _forSalePrices[id][owner][0]) {
                _forSalePrices[id][owner].pop();
                return true;
            } else {
                revert("Price doesn't match");
            }
        } else {
            uint8 priceIdx;
            bool priceIdxFound = false;
            for (uint8 i = 0; i < salesLength; ++i) {
                if (_forSalePrices[id][owner][i] == price) {
                    priceIdxFound = true;
                    priceIdx = i;
                    break;
                }
            }
            require(priceIdxFound, "Price not found");

            uint16 lastIdx = salesLength - 1;

            // Pop and swap:
            if (priceIdx == lastIdx) {
                _forSalePrices[id][owner].pop();
            } else {
                _forSalePrices[id][owner][priceIdx] = _forSalePrices[id][owner][lastIdx];
                _forSalePrices[id][owner].pop();
            }
            return true;
        }
    }

    function buy(uint256 id, address owner) external payable nonReentrant {
        uint256 price = (_mondoTokensContract.ROYALTY_DIVISOR() * msg.value) /
            (_mondoTokensContract.ROYALTY_DIVISOR() + 1); // price without royalties
        require(price > 0);

        uint8 salesLength = uint8(_forSalePrices[id][owner].length);
        require(salesLength > 0, "None for sale!");

        removeFromSale(id, owner, price);

        emit TokenSold(id, owner, price, _msgSender());

        // pay the seller
        _asyncTransfer(owner, price);

        // transfer token to the buyer
        _mondoTokensContract.safeTransferFrom(owner, _msgSender(), id, 1, "");
    }

    /* ============================================================================================================== */
    /*                                          ADMINISTRATIVE FUNCTIONALITY                                          */
    /* ============================================================================================================== */

    // For transferring all Eth to the owner.
    function withdraw() external onlyOwner {
        uint256 balance = address(this).balance;
        (bool success, ) = _msgSender().call{value: balance}("");
        require(success, "Failed to send Ether");
    }

    function _authorizeUpgrade(address newImplementation) internal virtual override(UUPSUpgradeable) onlyOwner {}
}

// File: @openzeppelin/contracts/access/AccessControlEnumerable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (access/AccessControlEnumerable.sol)

pragma solidity ^0.8.0;

import "./IAccessControlEnumerable.sol";
import "./AccessControl.sol";
import "../utils/structs/EnumerableSet.sol";

/**
 * @dev Extension of {AccessControl} that allows enumerating the members of each role.
 */
abstract contract AccessControlEnumerable is IAccessControlEnumerable, AccessControl {
    using EnumerableSet for EnumerableSet.AddressSet;

    mapping(bytes32 => EnumerableSet.AddressSet) private _roleMembers;

    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
        return interfaceId == type(IAccessControlEnumerable).interfaceId || super.supportsInterface(interfaceId);
    }

    /**
     * @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
     * https://forum.openzeppelin.com/t/iterating-over-elements-on-enumerableset-in-openzeppelin-contracts/2296[forum post]
     * for more information.
     */
    function getRoleMember(bytes32 role, uint256 index) public view virtual override returns (address) {
        return _roleMembers[role].at(index);
    }

    /**
     * @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) public view virtual override returns (uint256) {
        return _roleMembers[role].length();
    }

    /**
     * @dev Overload {_grantRole} to track enumerable memberships
     */
    function _grantRole(bytes32 role, address account) internal virtual override {
        super._grantRole(role, account);
        _roleMembers[role].add(account);
    }

    /**
     * @dev Overload {_revokeRole} to track enumerable memberships
     */
    function _revokeRole(bytes32 role, address account) internal virtual override {
        super._revokeRole(role, account);
        _roleMembers[role].remove(account);
    }
}

// File: @openzeppelin/contracts/security/ReentrancyGuard.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (security/ReentrancyGuard.sol)

pragma solidity ^0.8.0;

/**
 * @dev Contract module that helps prevent reentrant calls to a function.
 *
 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
 * available, which can be applied to functions to make sure there are no nested
 * (reentrant) calls to them.
 *
 * Note that because there is a single `nonReentrant` guard, functions marked as
 * `nonReentrant` may not call one another. This can be worked around by making
 * those functions `private`, and then adding `external` `nonReentrant` entry
 * points to them.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 */
abstract contract ReentrancyGuard {
    // Booleans are more expensive than uint256 or any type that takes up a full
    // word because each write operation emits an extra SLOAD to first read the
    // slot's contents, replace the bits taken up by the boolean, and then write
    // back. This is the compiler's defense against contract upgrades and
    // pointer aliasing, and it cannot be disabled.

    // The values being non-zero value makes deployment a bit more expensive,
    // but in exchange the refund on every call to nonReentrant will be lower in
    // amount. Since refunds are capped to a percentage of the total
    // transaction's gas, it is best to keep them low in cases like this one, to
    // increase the likelihood of the full refund coming into effect.
    uint256 private constant _NOT_ENTERED = 1;
    uint256 private constant _ENTERED = 2;

    uint256 private _status;

    constructor() {
        _status = _NOT_ENTERED;
    }

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and making it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        // On the first call to nonReentrant, _notEntered will be true
        require(_status != _ENTERED, "ReentrancyGuard: reentrant call");

        // Any calls to nonReentrant after this point will fail
        _status = _ENTERED;

        _;

        // By storing the original value once again, a refund is triggered (see
        // https://eips.ethereum.org/EIPS/eip-2200)
        _status = _NOT_ENTERED;
    }
}

// File: @openzeppelin/contracts/security/Pausable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (security/Pausable.sol)

pragma solidity ^0.8.0;

import "../utils/Context.sol";

/**
 * @dev Contract module which allows children to implement an emergency stop
 * mechanism that can be triggered by an authorized account.
 *
 * This module is used through inheritance. It will make available the
 * modifiers `whenNotPaused` and `whenPaused`, which can be applied to
 * the functions of your contract. Note that they will not be pausable by
 * simply including this module, only once the modifiers are put in place.
 */
abstract contract Pausable is Context {
    /**
     * @dev Emitted when the pause is triggered by `account`.
     */
    event Paused(address account);

    /**
     * @dev Emitted when the pause is lifted by `account`.
     */
    event Unpaused(address account);

    bool private _paused;

    /**
     * @dev Initializes the contract in unpaused state.
     */
    constructor() {
        _paused = false;
    }

    /**
     * @dev Modifier to make a function callable only when the contract is not paused.
     *
     * Requirements:
     *
     * - The contract must not be paused.
     */
    modifier whenNotPaused() {
        _requireNotPaused();
        _;
    }

    /**
     * @dev Modifier to make a function callable only when the contract is paused.
     *
     * Requirements:
     *
     * - The contract must be paused.
     */
    modifier whenPaused() {
        _requirePaused();
        _;
    }

    /**
     * @dev Returns true if the contract is paused, and false otherwise.
     */
    function paused() public view virtual returns (bool) {
        return _paused;
    }

    /**
     * @dev Throws if the contract is paused.
     */
    function _requireNotPaused() internal view virtual {
        require(!paused(), "Pausable: paused");
    }

    /**
     * @dev Throws if the contract is not paused.
     */
    function _requirePaused() internal view virtual {
        require(paused(), "Pausable: not paused");
    }

    /**
     * @dev Triggers stopped state.
     *
     * Requirements:
     *
     * - The contract must not be paused.
     */
    function _pause() internal virtual whenNotPaused {
        _paused = true;
        emit Paused(_msgSender());
    }

    /**
     * @dev Returns to normal state.
     *
     * Requirements:
     *
     * - The contract must be paused.
     */
    function _unpause() internal virtual whenPaused {
        _paused = false;
        emit Unpaused(_msgSender());
    }
}

// File: @openzeppelin/contracts/token/ERC1155/ERC1155.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (token/ERC1155/ERC1155.sol)

pragma solidity ^0.8.0;

import "./IERC1155.sol";
import "./IERC1155Receiver.sol";
import "./extensions/IERC1155MetadataURI.sol";
import "../../utils/Address.sol";
import "../../utils/Context.sol";
import "../../utils/introspection/ERC165.sol";

/**
 * @dev Implementation of the basic standard multi-token.
 * See https://eips.ethereum.org/EIPS/eip-1155
 * Originally based on code by Enjin: https://github.com/enjin/erc-1155
 *
 * _Available since v3.1._
 */
contract ERC1155 is Context, ERC165, IERC1155, IERC1155MetadataURI {
    using Address for address;

    // Mapping from token ID to account balances
    mapping(uint256 => mapping(address => uint256)) private _balances;

    // Mapping from account to operator approvals
    mapping(address => mapping(address => bool)) private _operatorApprovals;

    // Used as the URI for all token types by relying on ID substitution, e.g. https://token-cdn-domain/{id}.json
    string private _uri;

    /**
     * @dev See {_setURI}.
     */
    constructor(string memory uri_) {
        _setURI(uri_);
    }

    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
        return
            interfaceId == type(IERC1155).interfaceId ||
            interfaceId == type(IERC1155MetadataURI).interfaceId ||
            super.supportsInterface(interfaceId);
    }

    /**
     * @dev See {IERC1155MetadataURI-uri}.
     *
     * This implementation returns the same URI for *all* token types. It relies
     * on the token type ID substitution mechanism
     * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
     *
     * Clients calling this function must replace the `\{id\}` substring with the
     * actual token type ID.
     */
    function uri(uint256) public view virtual override returns (string memory) {
        return _uri;
    }

    /**
     * @dev See {IERC1155-balanceOf}.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     */
    function balanceOf(address account, uint256 id) public view virtual override returns (uint256) {
        require(account != address(0), "ERC1155: address zero is not a valid owner");
        return _balances[id][account];
    }

    /**
     * @dev See {IERC1155-balanceOfBatch}.
     *
     * Requirements:
     *
     * - `accounts` and `ids` must have the same length.
     */
    function balanceOfBatch(address[] memory accounts, uint256[] memory ids)
        public
        view
        virtual
        override
        returns (uint256[] memory)
    {
        require(accounts.length == ids.length, "ERC1155: accounts and ids length mismatch");

        uint256[] memory batchBalances = new uint256[](accounts.length);

        for (uint256 i = 0; i < accounts.length; ++i) {
            batchBalances[i] = balanceOf(accounts[i], ids[i]);
        }

        return batchBalances;
    }

    /**
     * @dev See {IERC1155-setApprovalForAll}.
     */
    function setApprovalForAll(address operator, bool approved) public virtual override {
        _setApprovalForAll(_msgSender(), operator, approved);
    }

    /**
     * @dev See {IERC1155-isApprovedForAll}.
     */
    function isApprovedForAll(address account, address operator) public view virtual override returns (bool) {
        return _operatorApprovals[account][operator];
    }

    /**
     * @dev See {IERC1155-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 id,
        uint256 amount,
        bytes memory data
    ) public virtual override {
        require(
            from == _msgSender() || isApprovedForAll(from, _msgSender()),
            "ERC1155: caller is not token owner nor approved"
        );
        _safeTransferFrom(from, to, id, amount, data);
    }

    /**
     * @dev See {IERC1155-safeBatchTransferFrom}.
     */
    function safeBatchTransferFrom(
        address from,
        address to,
        uint256[] memory ids,
        uint256[] memory amounts,
        bytes memory data
    ) public virtual override {
        require(
            from == _msgSender() || isApprovedForAll(from, _msgSender()),
            "ERC1155: caller is not token owner nor approved"
        );
        _safeBatchTransferFrom(from, to, ids, amounts, data);
    }

    /**
     * @dev Transfers `amount` tokens of token type `id` from `from` to `to`.
     *
     * Emits a {TransferSingle} event.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - `from` must have a balance of tokens of type `id` of at least `amount`.
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
     * acceptance magic value.
     */
    function _safeTransferFrom(
        address from,
        address to,
        uint256 id,
        uint256 amount,
        bytes memory data
    ) internal virtual {
        require(to != address(0), "ERC1155: transfer to the zero address");

        address operator = _msgSender();
        uint256[] memory ids = _asSingletonArray(id);
        uint256[] memory amounts = _asSingletonArray(amount);

        _beforeTokenTransfer(operator, from, to, ids, amounts, data);

        uint256 fromBalance = _balances[id][from];
        require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
        unchecked {
            _balances[id][from] = fromBalance - amount;
        }
        _balances[id][to] += amount;

        emit TransferSingle(operator, from, to, id, amount);

        _afterTokenTransfer(operator, from, to, ids, amounts, data);

        _doSafeTransferAcceptanceCheck(operator, from, to, id, amount, data);
    }

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_safeTransferFrom}.
     *
     * Emits a {TransferBatch} event.
     *
     * Requirements:
     *
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
     * acceptance magic value.
     */
    function _safeBatchTransferFrom(
        address from,
        address to,
        uint256[] memory ids,
        uint256[] memory amounts,
        bytes memory data
    ) internal virtual {
        require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");
        require(to != address(0), "ERC1155: transfer to the zero address");

        address operator = _msgSender();

        _beforeTokenTransfer(operator, from, to, ids, amounts, data);

        for (uint256 i = 0; i < ids.length; ++i) {
            uint256 id = ids[i];
            uint256 amount = amounts[i];

            uint256 fromBalance = _balances[id][from];
            require(fromBalance >= amount, "ERC1155: insufficient balance for transfer");
            unchecked {
                _balances[id][from] = fromBalance - amount;
            }
            _balances[id][to] += amount;
        }

        emit TransferBatch(operator, from, to, ids, amounts);

        _afterTokenTransfer(operator, from, to, ids, amounts, data);

        _doSafeBatchTransferAcceptanceCheck(operator, from, to, ids, amounts, data);
    }

    /**
     * @dev Sets a new URI for all token types, by relying on the token type ID
     * substitution mechanism
     * https://eips.ethereum.org/EIPS/eip-1155#metadata[defined in the EIP].
     *
     * By this mechanism, any occurrence of the `\{id\}` substring in either the
     * URI or any of the amounts in the JSON file at said URI will be replaced by
     * clients with the token type ID.
     *
     * For example, the `https://token-cdn-domain/\{id\}.json` URI would be
     * interpreted by clients as
     * `https://token-cdn-domain/000000000000000000000000000000000000000000000000000000000004cce0.json`
     * for token type ID 0x4cce0.
     *
     * See {uri}.
     *
     * Because these URIs cannot be meaningfully represented by the {URI} event,
     * this function emits no events.
     */
    function _setURI(string memory newuri) internal virtual {
        _uri = newuri;
    }

    /**
     * @dev Creates `amount` tokens of token type `id`, and assigns them to `to`.
     *
     * Emits a {TransferSingle} event.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
     * acceptance magic value.
     */
    function _mint(
        address to,
        uint256 id,
        uint256 amount,
        bytes memory data
    ) internal virtual {
        require(to != address(0), "ERC1155: mint to the zero address");

        address operator = _msgSender();
        uint256[] memory ids = _asSingletonArray(id);
        uint256[] memory amounts = _asSingletonArray(amount);

        _beforeTokenTransfer(operator, address(0), to, ids, amounts, data);

        _balances[id][to] += amount;
        emit TransferSingle(operator, address(0), to, id, amount);

        _afterTokenTransfer(operator, address(0), to, ids, amounts, data);

        _doSafeTransferAcceptanceCheck(operator, address(0), to, id, amount, data);
    }

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_mint}.
     *
     * Emits a {TransferBatch} event.
     *
     * Requirements:
     *
     * - `ids` and `amounts` must have the same length.
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
     * acceptance magic value.
     */
    function _mintBatch(
        address to,
        uint256[] memory ids,
        uint256[] memory amounts,
        bytes memory data
    ) internal virtual {
        require(to != address(0), "ERC1155: mint to the zero address");
        require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");

        address operator = _msgSender();

        _beforeTokenTransfer(operator, address(0), to, ids, amounts, data);

        for (uint256 i = 0; i < ids.length; i++) {
            _balances[ids[i]][to] += amounts[i];
        }

        emit TransferBatch(operator, address(0), to, ids, amounts);

        _afterTokenTransfer(operator, address(0), to, ids, amounts, data);

        _doSafeBatchTransferAcceptanceCheck(operator, address(0), to, ids, amounts, data);
    }

    /**
     * @dev Destroys `amount` tokens of token type `id` from `from`
     *
     * Emits a {TransferSingle} event.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `from` must have at least `amount` tokens of token type `id`.
     */
    function _burn(
        address from,
        uint256 id,
        uint256 amount
    ) internal virtual {
        require(from != address(0), "ERC1155: burn from the zero address");

        address operator = _msgSender();
        uint256[] memory ids = _asSingletonArray(id);
        uint256[] memory amounts = _asSingletonArray(amount);

        _beforeTokenTransfer(operator, from, address(0), ids, amounts, "");

        uint256 fromBalance = _balances[id][from];
        require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");
        unchecked {
            _balances[id][from] = fromBalance - amount;
        }

        emit TransferSingle(operator, from, address(0), id, amount);

        _afterTokenTransfer(operator, from, address(0), ids, amounts, "");
    }

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {_burn}.
     *
     * Emits a {TransferBatch} event.
     *
     * Requirements:
     *
     * - `ids` and `amounts` must have the same length.
     */
    function _burnBatch(
        address from,
        uint256[] memory ids,
        uint256[] memory amounts
    ) internal virtual {
        require(from != address(0), "ERC1155: burn from the zero address");
        require(ids.length == amounts.length, "ERC1155: ids and amounts length mismatch");

        address operator = _msgSender();

        _beforeTokenTransfer(operator, from, address(0), ids, amounts, "");

        for (uint256 i = 0; i < ids.length; i++) {
            uint256 id = ids[i];
            uint256 amount = amounts[i];

            uint256 fromBalance = _balances[id][from];
            require(fromBalance >= amount, "ERC1155: burn amount exceeds balance");
            unchecked {
                _balances[id][from] = fromBalance - amount;
            }
        }

        emit TransferBatch(operator, from, address(0), ids, amounts);

        _afterTokenTransfer(operator, from, address(0), ids, amounts, "");
    }

    /**
     * @dev Approve `operator` to operate on all of `owner` tokens
     *
     * Emits an {ApprovalForAll} event.
     */
    function _setApprovalForAll(
        address owner,
        address operator,
        bool approved
    ) internal virtual {
        require(owner != operator, "ERC1155: setting approval status for self");
        _operatorApprovals[owner][operator] = approved;
        emit ApprovalForAll(owner, operator, approved);
    }

    /**
     * @dev Hook that is called before any token transfer. This includes minting
     * and burning, as well as batched variants.
     *
     * The same hook is called on both single and batched variants. For single
     * transfers, the length of the `ids` and `amounts` arrays will be 1.
     *
     * Calling conditions (for each `id` and `amount` pair):
     *
     * - When `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * of token type `id` will be  transferred to `to`.
     * - When `from` is zero, `amount` tokens of token type `id` will be minted
     * for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens of token type `id`
     * will be burned.
     * - `from` and `to` are never both zero.
     * - `ids` and `amounts` have the same, non-zero length.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(
        address operator,
        address from,
        address to,
        uint256[] memory ids,
        uint256[] memory amounts,
        bytes memory data
    ) internal virtual {}

    /**
     * @dev Hook that is called after any token transfer. This includes minting
     * and burning, as well as batched variants.
     *
     * The same hook is called on both single and batched variants. For single
     * transfers, the length of the `id` and `amount` arrays will be 1.
     *
     * Calling conditions (for each `id` and `amount` pair):
     *
     * - When `from` and `to` are both non-zero, `amount` of ``from``'s tokens
     * of token type `id` will be  transferred to `to`.
     * - When `from` is zero, `amount` tokens of token type `id` will be minted
     * for `to`.
     * - when `to` is zero, `amount` of ``from``'s tokens of token type `id`
     * will be burned.
     * - `from` and `to` are never both zero.
     * - `ids` and `amounts` have the same, non-zero length.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _afterTokenTransfer(
        address operator,
        address from,
        address to,
        uint256[] memory ids,
        uint256[] memory amounts,
        bytes memory data
    ) internal virtual {}

    function _doSafeTransferAcceptanceCheck(
        address operator,
        address from,
        address to,
        uint256 id,
        uint256 amount,
        bytes memory data
    ) private {
        if (to.isContract()) {
            try IERC1155Receiver(to).onERC1155Received(operator, from, id, amount, data) returns (bytes4 response) {
                if (response != IERC1155Receiver.onERC1155Received.selector) {
                    revert("ERC1155: ERC1155Receiver rejected tokens");
                }
            } catch Error(string memory reason) {
                revert(reason);
            } catch {
                revert("ERC1155: transfer to non ERC1155Receiver implementer");
            }
        }
    }

    function _doSafeBatchTransferAcceptanceCheck(
        address operator,
        address from,
        address to,
        uint256[] memory ids,
        uint256[] memory amounts,
        bytes memory data
    ) private {
        if (to.isContract()) {
            try IERC1155Receiver(to).onERC1155BatchReceived(operator, from, ids, amounts, data) returns (
                bytes4 response
            ) {
                if (response != IERC1155Receiver.onERC1155BatchReceived.selector) {
                    revert("ERC1155: ERC1155Receiver rejected tokens");
                }
            } catch Error(string memory reason) {
                revert(reason);
            } catch {
                revert("ERC1155: transfer to non ERC1155Receiver implementer");
            }
        }
    }

    function _asSingletonArray(uint256 element) private pure returns (uint256[] memory) {
        uint256[] memory array = new uint256[](1);
        array[0] = element;

        return array;
    }
}

// File: @openzeppelin/contracts/token/common/ERC2981.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (token/common/ERC2981.sol)

pragma solidity ^0.8.0;

import "../../interfaces/IERC2981.sol";
import "../../utils/introspection/ERC165.sol";

/**
 * @dev Implementation of the NFT Royalty Standard, a standardized way to retrieve royalty payment information.
 *
 * Royalty information can be specified globally for all token ids via {_setDefaultRoyalty}, and/or individually for
 * specific token ids via {_setTokenRoyalty}. The latter takes precedence over the first.
 *
 * Royalty is specified as a fraction of sale price. {_feeDenominator} is overridable but defaults to 10000, meaning the
 * fee is specified in basis points by default.
 *
 * IMPORTANT: ERC-2981 only specifies a way to signal royalty information and does not enforce its payment. See
 * https://eips.ethereum.org/EIPS/eip-2981#optional-royalty-payments[Rationale] in the EIP. Marketplaces are expected to
 * voluntarily pay royalties together with sales, but note that this standard is not yet widely supported.
 *
 * _Available since v4.5._
 */
abstract contract ERC2981 is IERC2981, ERC165 {
    struct RoyaltyInfo {
        address receiver;
        uint96 royaltyFraction;
    }

    RoyaltyInfo private _defaultRoyaltyInfo;
    mapping(uint256 => RoyaltyInfo) private _tokenRoyaltyInfo;

    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override(IERC165, ERC165) returns (bool) {
        return interfaceId == type(IERC2981).interfaceId || super.supportsInterface(interfaceId);
    }

    /**
     * @inheritdoc IERC2981
     */
    function royaltyInfo(uint256 _tokenId, uint256 _salePrice) public view virtual override returns (address, uint256) {
        RoyaltyInfo memory royalty = _tokenRoyaltyInfo[_tokenId];

        if (royalty.receiver == address(0)) {
            royalty = _defaultRoyaltyInfo;
        }

        uint256 royaltyAmount = (_salePrice * royalty.royaltyFraction) / _feeDenominator();

        return (royalty.receiver, royaltyAmount);
    }

    /**
     * @dev The denominator with which to interpret the fee set in {_setTokenRoyalty} and {_setDefaultRoyalty} as a
     * fraction of the sale price. Defaults to 10000 so fees are expressed in basis points, but may be customized by an
     * override.
     */
    function _feeDenominator() internal pure virtual returns (uint96) {
        return 10000;
    }

    /**
     * @dev Sets the royalty information that all ids in this contract will default to.
     *
     * Requirements:
     *
     * - `receiver` cannot be the zero address.
     * - `feeNumerator` cannot be greater than the fee denominator.
     */
    function _setDefaultRoyalty(address receiver, uint96 feeNumerator) internal virtual {
        require(feeNumerator <= _feeDenominator(), "ERC2981: royalty fee will exceed salePrice");
        require(receiver != address(0), "ERC2981: invalid receiver");

        _defaultRoyaltyInfo = RoyaltyInfo(receiver, feeNumerator);
    }

    /**
     * @dev Removes default royalty information.
     */
    function _deleteDefaultRoyalty() internal virtual {
        delete _defaultRoyaltyInfo;
    }

    /**
     * @dev Sets the royalty information for a specific token id, overriding the global default.
     *
     * Requirements:
     *
     * - `receiver` cannot be the zero address.
     * - `feeNumerator` cannot be greater than the fee denominator.
     */
    function _setTokenRoyalty(
        uint256 tokenId,
        address receiver,
        uint96 feeNumerator
    ) internal virtual {
        require(feeNumerator <= _feeDenominator(), "ERC2981: royalty fee will exceed salePrice");
        require(receiver != address(0), "ERC2981: Invalid parameters");

        _tokenRoyaltyInfo[tokenId] = RoyaltyInfo(receiver, feeNumerator);
    }

    /**
     * @dev Resets royalty information for the token id back to the global default.
     */
    function _resetTokenRoyalty(uint256 tokenId) internal virtual {
        delete _tokenRoyaltyInfo[tokenId];
    }
}

// File: @openzeppelin/contracts/utils/Address.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.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
     * ====
     *
     * [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://diligence.consensys.net/posts/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.5.11/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 functionCall(target, data, "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");
        require(isContract(target), "Address: call to non-contract");

        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return verifyCallResult(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) {
        require(isContract(target), "Address: static call to non-contract");

        (bool success, bytes memory returndata) = target.staticcall(data);
        return verifyCallResult(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) {
        require(isContract(target), "Address: delegate call to non-contract");

        (bool success, bytes memory returndata) = target.delegatecall(data);
        return verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason 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 {
            // 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: @openzeppelin/contracts/utils/cryptography/ECDSA.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.3) (utils/cryptography/ECDSA.sol)

pragma solidity ^0.8.0;

import "../Strings.sol";

/**
 * @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations.
 *
 * These functions can be used to verify that a message was signed by the holder
 * of the private keys of a given address.
 */
library ECDSA {
    enum RecoverError {
        NoError,
        InvalidSignature,
        InvalidSignatureLength,
        InvalidSignatureS,
        InvalidSignatureV
    }

    function _throwError(RecoverError error) private pure {
        if (error == RecoverError.NoError) {
            return; // no error: do nothing
        } else if (error == RecoverError.InvalidSignature) {
            revert("ECDSA: invalid signature");
        } else if (error == RecoverError.InvalidSignatureLength) {
            revert("ECDSA: invalid signature length");
        } else if (error == RecoverError.InvalidSignatureS) {
            revert("ECDSA: invalid signature 's' value");
        } else if (error == RecoverError.InvalidSignatureV) {
            revert("ECDSA: invalid signature 'v' value");
        }
    }

    /**
     * @dev Returns the address that signed a hashed message (`hash`) with
     * `signature` or error string. This address can then be used for verification purposes.
     *
     * The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
     * this function rejects them by requiring the `s` value to be in the lower
     * half order, and the `v` value to be either 27 or 28.
     *
     * IMPORTANT: `hash` _must_ be the result of a hash operation for the
     * verification to be secure: it is possible to craft signatures that
     * recover to arbitrary addresses for non-hashed data. A safe way to ensure
     * this is by receiving a hash of the original message (which may otherwise
     * be too long), and then calling {toEthSignedMessageHash} on it.
     *
     * Documentation for signature generation:
     * - with https://web3js.readthedocs.io/en/v1.3.4/web3-eth-accounts.html#sign[Web3.js]
     * - with https://docs.ethers.io/v5/api/signer/#Signer-signMessage[ethers]
     *
     * _Available since v4.3._
     */
    function tryRecover(bytes32 hash, bytes memory signature) internal pure returns (address, RecoverError) {
        if (signature.length == 65) {
            bytes32 r;
            bytes32 s;
            uint8 v;
            // ecrecover takes the signature parameters, and the only way to get them
            // currently is to use assembly.
            /// @solidity memory-safe-assembly
            assembly {
                r := mload(add(signature, 0x20))
                s := mload(add(signature, 0x40))
                v := byte(0, mload(add(signature, 0x60)))
            }
            return tryRecover(hash, v, r, s);
        } else {
            return (address(0), RecoverError.InvalidSignatureLength);
        }
    }

    /**
     * @dev Returns the address that signed a hashed message (`hash`) with
     * `signature`. This address can then be used for verification purposes.
     *
     * The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
     * this function rejects them by requiring the `s` value to be in the lower
     * half order, and the `v` value to be either 27 or 28.
     *
     * IMPORTANT: `hash` _must_ be the result of a hash operation for the
     * verification to be secure: it is possible to craft signatures that
     * recover to arbitrary addresses for non-hashed data. A safe way to ensure
     * this is by receiving a hash of the original message (which may otherwise
     * be too long), and then calling {toEthSignedMessageHash} on it.
     */
    function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
        (address recovered, RecoverError error) = tryRecover(hash, signature);
        _throwError(error);
        return recovered;
    }

    /**
     * @dev Overload of {ECDSA-tryRecover} that receives the `r` and `vs` short-signature fields separately.
     *
     * See https://eips.ethereum.org/EIPS/eip-2098[EIP-2098 short signatures]
     *
     * _Available since v4.3._
     */
    function tryRecover(
        bytes32 hash,
        bytes32 r,
        bytes32 vs
    ) internal pure returns (address, RecoverError) {
        bytes32 s = vs & bytes32(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
        uint8 v = uint8((uint256(vs) >> 255) + 27);
        return tryRecover(hash, v, r, s);
    }

    /**
     * @dev Overload of {ECDSA-recover} that receives the `r and `vs` short-signature fields separately.
     *
     * _Available since v4.2._
     */
    function recover(
        bytes32 hash,
        bytes32 r,
        bytes32 vs
    ) internal pure returns (address) {
        (address recovered, RecoverError error) = tryRecover(hash, r, vs);
        _throwError(error);
        return recovered;
    }

    /**
     * @dev Overload of {ECDSA-tryRecover} that receives the `v`,
     * `r` and `s` signature fields separately.
     *
     * _Available since v4.3._
     */
    function tryRecover(
        bytes32 hash,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) internal pure returns (address, RecoverError) {
        // EIP-2 still allows signature malleability for ecrecover(). Remove this possibility and make the signature
        // unique. Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
        // the valid range for s in (301): 0 < s < secp256k1n ÷ 2 + 1, and for v in (302): v ∈ {27, 28}. Most
        // signatures from current libraries generate a unique signature with an s-value in the lower half order.
        //
        // If your library generates malleable signatures, such as s-values in the upper range, calculate a new s-value
        // with 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 - s1 and flip v from 27 to 28 or
        // vice versa. If your library also generates signatures with 0/1 for v instead 27/28, add 27 to v to accept
        // these malleable signatures as well.
        if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
            return (address(0), RecoverError.InvalidSignatureS);
        }
        if (v != 27 && v != 28) {
            return (address(0), RecoverError.InvalidSignatureV);
        }

        // If the signature is valid (and not malleable), return the signer address
        address signer = ecrecover(hash, v, r, s);
        if (signer == address(0)) {
            return (address(0), RecoverError.InvalidSignature);
        }

        return (signer, RecoverError.NoError);
    }

    /**
     * @dev Overload of {ECDSA-recover} that receives the `v`,
     * `r` and `s` signature fields separately.
     */
    function recover(
        bytes32 hash,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) internal pure returns (address) {
        (address recovered, RecoverError error) = tryRecover(hash, v, r, s);
        _throwError(error);
        return recovered;
    }

    /**
     * @dev Returns an Ethereum Signed Message, created from a `hash`. This
     * produces hash corresponding to the one signed with the
     * https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`]
     * JSON-RPC method as part of EIP-191.
     *
     * See {recover}.
     */
    function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) {
        // 32 is the length in bytes of hash,
        // enforced by the type signature above
        return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
    }

    /**
     * @dev Returns an Ethereum Signed Message, created from `s`. This
     * produces hash corresponding to the one signed with the
     * https://eth.wiki/json-rpc/API#eth_sign[`eth_sign`]
     * JSON-RPC method as part of EIP-191.
     *
     * See {recover}.
     */
    function toEthSignedMessageHash(bytes memory s) internal pure returns (bytes32) {
        return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n", Strings.toString(s.length), s));
    }

    /**
     * @dev Returns an Ethereum Signed Typed Data, created from a
     * `domainSeparator` and a `structHash`. This produces hash corresponding
     * to the one signed with the
     * https://eips.ethereum.org/EIPS/eip-712[`eth_signTypedData`]
     * JSON-RPC method as part of EIP-712.
     *
     * See {recover}.
     */
    function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32) {
        return keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
    }
}

// File: @openzeppelin/contracts/utils/cryptography/MerkleProof.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (utils/cryptography/MerkleProof.sol)

pragma solidity ^0.8.0;

/**
 * @dev These functions deal with verification of Merkle Tree proofs.
 *
 * The proofs can be generated using the JavaScript library
 * https://github.com/miguelmota/merkletreejs[merkletreejs].
 * Note: the hashing algorithm should be keccak256 and pair sorting should be enabled.
 *
 * See `test/utils/cryptography/MerkleProof.test.js` for some examples.
 *
 * WARNING: You should avoid using leaf values that are 64 bytes long prior to
 * hashing, or use a hash function other than keccak256 for hashing leaves.
 * This is because the concatenation of a sorted pair of internal nodes in
 * the merkle tree could be reinterpreted as a leaf value.
 */
library MerkleProof {
    /**
     * @dev Returns true if a `leaf` can be proved to be a part of a Merkle tree
     * defined by `root`. For this, a `proof` must be provided, containing
     * sibling hashes on the branch from the leaf to the root of the tree. Each
     * pair of leaves and each pair of pre-images are assumed to be sorted.
     */
    function verify(
        bytes32[] memory proof,
        bytes32 root,
        bytes32 leaf
    ) internal pure returns (bool) {
        return processProof(proof, leaf) == root;
    }

    /**
     * @dev Calldata version of {verify}
     *
     * _Available since v4.7._
     */
    function verifyCalldata(
        bytes32[] calldata proof,
        bytes32 root,
        bytes32 leaf
    ) internal pure returns (bool) {
        return processProofCalldata(proof, leaf) == root;
    }

    /**
     * @dev Returns the rebuilt hash obtained by traversing a Merkle tree up
     * from `leaf` using `proof`. A `proof` is valid if and only if the rebuilt
     * hash matches the root of the tree. When processing the proof, the pairs
     * of leafs & pre-images are assumed to be sorted.
     *
     * _Available since v4.4._
     */
    function processProof(bytes32[] memory proof, bytes32 leaf) internal pure returns (bytes32) {
        bytes32 computedHash = leaf;
        for (uint256 i = 0; i < proof.length; i++) {
            computedHash = _hashPair(computedHash, proof[i]);
        }
        return computedHash;
    }

    /**
     * @dev Calldata version of {processProof}
     *
     * _Available since v4.7._
     */
    function processProofCalldata(bytes32[] calldata proof, bytes32 leaf) internal pure returns (bytes32) {
        bytes32 computedHash = leaf;
        for (uint256 i = 0; i < proof.length; i++) {
            computedHash = _hashPair(computedHash, proof[i]);
        }
        return computedHash;
    }

    /**
     * @dev Returns true if the `leaves` can be proved to be a part of a Merkle tree defined by
     * `root`, according to `proof` and `proofFlags` as described in {processMultiProof}.
     *
     * _Available since v4.7._
     */
    function multiProofVerify(
        bytes32[] memory proof,
        bool[] memory proofFlags,
        bytes32 root,
        bytes32[] memory leaves
    ) internal pure returns (bool) {
        return processMultiProof(proof, proofFlags, leaves) == root;
    }

    /**
     * @dev Calldata version of {multiProofVerify}
     *
     * _Available since v4.7._
     */
    function multiProofVerifyCalldata(
        bytes32[] calldata proof,
        bool[] calldata proofFlags,
        bytes32 root,
        bytes32[] memory leaves
    ) internal pure returns (bool) {
        return processMultiProofCalldata(proof, proofFlags, leaves) == root;
    }

    /**
     * @dev Returns the root of a tree reconstructed from `leaves` and the sibling nodes in `proof`,
     * consuming from one or the other at each step according to the instructions given by
     * `proofFlags`.
     *
     * _Available since v4.7._
     */
    function processMultiProof(
        bytes32[] memory proof,
        bool[] memory proofFlags,
        bytes32[] memory leaves
    ) internal pure returns (bytes32 merkleRoot) {
        // This function rebuild the root hash by traversing the tree up from the leaves. The root is rebuilt by
        // consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
        // `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
        // the merkle tree.
        uint256 leavesLen = leaves.length;
        uint256 totalHashes = proofFlags.length;

        // Check proof validity.
        require(leavesLen + proof.length - 1 == totalHashes, "MerkleProof: invalid multiproof");

        // The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
        // `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
        bytes32[] memory hashes = new bytes32[](totalHashes);
        uint256 leafPos = 0;
        uint256 hashPos = 0;
        uint256 proofPos = 0;
        // At each step, we compute the next hash using two values:
        // - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
        //   get the next hash.
        // - depending on the flag, either another value for the "main queue" (merging branches) or an element from the
        //   `proof` array.
        for (uint256 i = 0; i < totalHashes; i++) {
            bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
            bytes32 b = proofFlags[i] ? leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++] : proof[proofPos++];
            hashes[i] = _hashPair(a, b);
        }

        if (totalHashes > 0) {
            return hashes[totalHashes - 1];
        } else if (leavesLen > 0) {
            return leaves[0];
        } else {
            return proof[0];
        }
    }

    /**
     * @dev Calldata version of {processMultiProof}
     *
     * _Available since v4.7._
     */
    function processMultiProofCalldata(
        bytes32[] calldata proof,
        bool[] calldata proofFlags,
        bytes32[] memory leaves
    ) internal pure returns (bytes32 merkleRoot) {
        // This function rebuild the root hash by traversing the tree up from the leaves. The root is rebuilt by
        // consuming and producing values on a queue. The queue starts with the `leaves` array, then goes onto the
        // `hashes` array. At the end of the process, the last hash in the `hashes` array should contain the root of
        // the merkle tree.
        uint256 leavesLen = leaves.length;
        uint256 totalHashes = proofFlags.length;

        // Check proof validity.
        require(leavesLen + proof.length - 1 == totalHashes, "MerkleProof: invalid multiproof");

        // The xxxPos values are "pointers" to the next value to consume in each array. All accesses are done using
        // `xxx[xxxPos++]`, which return the current value and increment the pointer, thus mimicking a queue's "pop".
        bytes32[] memory hashes = new bytes32[](totalHashes);
        uint256 leafPos = 0;
        uint256 hashPos = 0;
        uint256 proofPos = 0;
        // At each step, we compute the next hash using two values:
        // - a value from the "main queue". If not all leaves have been consumed, we get the next leaf, otherwise we
        //   get the next hash.
        // - depending on the flag, either another value for the "main queue" (merging branches) or an element from the
        //   `proof` array.
        for (uint256 i = 0; i < totalHashes; i++) {
            bytes32 a = leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++];
            bytes32 b = proofFlags[i] ? leafPos < leavesLen ? leaves[leafPos++] : hashes[hashPos++] : proof[proofPos++];
            hashes[i] = _hashPair(a, b);
        }

        if (totalHashes > 0) {
            return hashes[totalHashes - 1];
        } else if (leavesLen > 0) {
            return leaves[0];
        } else {
            return proof[0];
        }
    }

    function _hashPair(bytes32 a, bytes32 b) private pure returns (bytes32) {
        return a < b ? _efficientHash(a, b) : _efficientHash(b, a);
    }

    function _efficientHash(bytes32 a, bytes32 b) private pure returns (bytes32 value) {
        /// @solidity memory-safe-assembly
        assembly {
            mstore(0x00, a)
            mstore(0x20, b)
            value := keccak256(0x00, 0x40)
        }
    }
}

// File: @openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)

pragma solidity ^0.8.0;

import "../utils/ContextUpgradeable.sol";
import "../proxy/utils/Initializable.sol";

/**
 * @dev Contract module which provides a basic access control mechanism, where
 * there is an account (an owner) that can be granted exclusive access to
 * specific functions.
 *
 * By default, the owner account will be the one that deploys the contract. This
 * can later be changed with {transferOwnership}.
 *
 * This module is used through inheritance. It will make available the modifier
 * `onlyOwner`, which can be applied to your functions to restrict their use to
 * the owner.
 */
abstract contract OwnableUpgradeable is Initializable, ContextUpgradeable {
    address private _owner;

    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    function __Ownable_init() internal onlyInitializing {
        __Ownable_init_unchained();
    }

    function __Ownable_init_unchained() internal onlyInitializing {
        _transferOwnership(_msgSender());
    }

    /**
     * @dev Throws if called by any account other than the owner.
     */
    modifier onlyOwner() {
        _checkOwner();
        _;
    }

    /**
     * @dev Returns the address of the current owner.
     */
    function owner() public view virtual returns (address) {
        return _owner;
    }

    /**
     * @dev Throws if the sender is not the owner.
     */
    function _checkOwner() internal view virtual {
        require(owner() == _msgSender(), "Ownable: caller is not the owner");
    }

    /**
     * @dev Leaves the contract without owner. It will not be possible to call
     * `onlyOwner` functions anymore. Can only be called by the current owner.
     *
     * NOTE: Renouncing ownership will leave the contract without an owner,
     * thereby removing any functionality that is only available to the owner.
     */
    function renounceOwnership() public virtual onlyOwner {
        _transferOwnership(address(0));
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Can only be called by the current owner.
     */
    function transferOwnership(address newOwner) public virtual onlyOwner {
        require(newOwner != address(0), "Ownable: new owner is the zero address");
        _transferOwnership(newOwner);
    }

    /**
     * @dev Transfers ownership of the contract to a new account (`newOwner`).
     * Internal function without access restriction.
     */
    function _transferOwnership(address newOwner) internal virtual {
        address oldOwner = _owner;
        _owner = newOwner;
        emit OwnershipTransferred(oldOwner, newOwner);
    }

    /**
     * @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[49] private __gap;
}

// File: @openzeppelin/contracts-upgradeable/security/ReentrancyGuardUpgradeable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (security/ReentrancyGuard.sol)

pragma solidity ^0.8.0;
import "../proxy/utils/Initializable.sol";

/**
 * @dev Contract module that helps prevent reentrant calls to a function.
 *
 * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
 * available, which can be applied to functions to make sure there are no nested
 * (reentrant) calls to them.
 *
 * Note that because there is a single `nonReentrant` guard, functions marked as
 * `nonReentrant` may not call one another. This can be worked around by making
 * those functions `private`, and then adding `external` `nonReentrant` entry
 * points to them.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 */
abstract contract ReentrancyGuardUpgradeable is Initializable {
    // Booleans are more expensive than uint256 or any type that takes up a full
    // word because each write operation emits an extra SLOAD to first read the
    // slot's contents, replace the bits taken up by the boolean, and then write
    // back. This is the compiler's defense against contract upgrades and
    // pointer aliasing, and it cannot be disabled.

    // The values being non-zero value makes deployment a bit more expensive,
    // but in exchange the refund on every call to nonReentrant will be lower in
    // amount. Since refunds are capped to a percentage of the total
    // transaction's gas, it is best to keep them low in cases like this one, to
    // increase the likelihood of the full refund coming into effect.
    uint256 private constant _NOT_ENTERED = 1;
    uint256 private constant _ENTERED = 2;

    uint256 private _status;

    function __ReentrancyGuard_init() internal onlyInitializing {
        __ReentrancyGuard_init_unchained();
    }

    function __ReentrancyGuard_init_unchained() internal onlyInitializing {
        _status = _NOT_ENTERED;
    }

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and making it call a
     * `private` function that does the actual work.
     */
    modifier nonReentrant() {
        // On the first call to nonReentrant, _notEntered will be true
        require(_status != _ENTERED, "ReentrancyGuard: reentrant call");

        // Any calls to nonReentrant after this point will fail
        _status = _ENTERED;

        _;

        // By storing the original value once again, a refund is triggered (see
        // https://eips.ethereum.org/EIPS/eip-2200)
        _status = _NOT_ENTERED;
    }

    /**
     * @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[49] private __gap;
}

// File: @openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.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]
 * ```
 * 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. Equivalent to `reinitializer(1)`.
     */
    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.
     *
     * `initializer` is equivalent to `reinitializer(1)`, so 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.
     *
     * 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.
     */
    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.
     */
    function _disableInitializers() internal virtual {
        require(!_initializing, "Initializable: contract is initializing");
        if (_initialized < type(uint8).max) {
            _initialized = type(uint8).max;
            emit Initialized(type(uint8).max);
        }
    }
}

// File: @openzeppelin/contracts-upgradeable/security/PullPaymentUpgradeable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (security/PullPayment.sol)

pragma solidity ^0.8.0;

import "../utils/escrow/EscrowUpgradeable.sol";
import "../proxy/utils/Initializable.sol";

/**
 * @dev Simple implementation of a
 * https://consensys.github.io/smart-contract-best-practices/recommendations/#favor-pull-over-push-for-external-calls[pull-payment]
 * strategy, where the paying contract doesn't interact directly with the
 * receiver account, which must withdraw its payments itself.
 *
 * Pull-payments are often considered the best practice when it comes to sending
 * Ether, security-wise. It prevents recipients from blocking execution, and
 * eliminates reentrancy concerns.
 *
 * TIP: If you would like to learn more about reentrancy and alternative ways
 * to protect against it, check out our blog post
 * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
 *
 * To use, derive from the `PullPayment` contract, and use {_asyncTransfer}
 * instead of Solidity's `transfer` function. Payees can query their due
 * payments with {payments}, and retrieve them with {withdrawPayments}.
 *
 * @custom:storage-size 51
 */
abstract contract PullPaymentUpgradeable is Initializable {
    EscrowUpgradeable private _escrow;

    function __PullPayment_init() internal onlyInitializing {
        __PullPayment_init_unchained();
    }

    function __PullPayment_init_unchained() internal onlyInitializing {
        _escrow = new EscrowUpgradeable();
        _escrow.initialize();
    }

    /**
     * @dev Withdraw accumulated payments, forwarding all gas to the recipient.
     *
     * Note that _any_ account can call this function, not just the `payee`.
     * This means that contracts unaware of the `PullPayment` protocol can still
     * receive funds this way, by having a separate account call
     * {withdrawPayments}.
     *
     * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
     * Make sure you trust the recipient, or are either following the
     * checks-effects-interactions pattern or using {ReentrancyGuard}.
     *
     * @param payee Whose payments will be withdrawn.
     *
     * Causes the `escrow` to emit a {Withdrawn} event.
     */
    function withdrawPayments(address payable payee) public virtual {
        _escrow.withdraw(payee);
    }

    /**
     * @dev Returns the payments owed to an address.
     * @param dest The creditor's address.
     */
    function payments(address dest) public view returns (uint256) {
        return _escrow.depositsOf(dest);
    }

    /**
     * @dev Called by the payer to store the sent amount as credit to be pulled.
     * Funds sent in this way are stored in an intermediate {Escrow} contract, so
     * there is no danger of them being spent before withdrawal.
     *
     * @param dest The destination address of the funds.
     * @param amount The amount to transfer.
     *
     * Causes the `escrow` to emit a {Deposited} event.
     */
    function _asyncTransfer(address dest, uint256 amount) internal virtual {
        _escrow.deposit{value: amount}(dest);
    }

    /**
     * @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;
}

// File: @openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (proxy/utils/UUPSUpgradeable.sol)

pragma solidity ^0.8.0;

import "../../interfaces/draft-IERC1822Upgradeable.sol";
import "../ERC1967/ERC1967UpgradeUpgradeable.sol";
import "./Initializable.sol";

/**
 * @dev An upgradeability mechanism designed for UUPS proxies. The functions included here can perform an upgrade of an
 * {ERC1967Proxy}, when this contract is set as the implementation behind such a proxy.
 *
 * A security mechanism ensures that an upgrade does not turn off upgradeability accidentally, although this risk is
 * reinstated if the upgrade retains upgradeability but removes the security mechanism, e.g. by replacing
 * `UUPSUpgradeable` with a custom implementation of upgrades.
 *
 * The {_authorizeUpgrade} function must be overridden to include access restriction to the upgrade mechanism.
 *
 * _Available since v4.1._
 */
abstract contract UUPSUpgradeable is Initializable, IERC1822ProxiableUpgradeable, ERC1967UpgradeUpgradeable {
    function __UUPSUpgradeable_init() internal onlyInitializing {
    }

    function __UUPSUpgradeable_init_unchained() internal onlyInitializing {
    }
    /// @custom:oz-upgrades-unsafe-allow state-variable-immutable state-variable-assignment
    address private immutable __self = address(this);

    /**
     * @dev Check that the execution is being performed through a delegatecall call and that the execution context is
     * a proxy contract with an implementation (as defined in ERC1967) pointing to self. This should only be the case
     * for UUPS and transparent proxies that are using the current contract as their implementation. Execution of a
     * function through ERC1167 minimal proxies (clones) would not normally pass this test, but is not guaranteed to
     * fail.
     */
    modifier onlyProxy() {
        require(address(this) != __self, "Function must be called through delegatecall");
        require(_getImplementation() == __self, "Function must be called through active proxy");
        _;
    }

    /**
     * @dev Check that the execution is not being performed through a delegate call. This allows a function to be
     * callable on the implementing contract but not through proxies.
     */
    modifier notDelegated() {
        require(address(this) == __self, "UUPSUpgradeable: must not be called through delegatecall");
        _;
    }

    /**
     * @dev Implementation of the ERC1822 {proxiableUUID} function. This returns the storage slot used by the
     * implementation. It is used to validate that the this implementation remains valid after an upgrade.
     *
     * IMPORTANT: A proxy pointing at a proxiable contract should not be considered proxiable itself, because this risks
     * bricking a proxy that upgrades to it, by delegating to itself until out of gas. Thus it is critical that this
     * function revert if invoked through a proxy. This is guaranteed by the `notDelegated` modifier.
     */
    function proxiableUUID() external view virtual override notDelegated returns (bytes32) {
        return _IMPLEMENTATION_SLOT;
    }

    /**
     * @dev Upgrade the implementation of the proxy to `newImplementation`.
     *
     * Calls {_authorizeUpgrade}.
     *
     * Emits an {Upgraded} event.
     */
    function upgradeTo(address newImplementation) external virtual onlyProxy {
        _authorizeUpgrade(newImplementation);
        _upgradeToAndCallUUPS(newImplementation, new bytes(0), false);
    }

    /**
     * @dev Upgrade the implementation of the proxy to `newImplementation`, and subsequently execute the function call
     * encoded in `data`.
     *
     * Calls {_authorizeUpgrade}.
     *
     * Emits an {Upgraded} event.
     */
    function upgradeToAndCall(address newImplementation, bytes memory data) external payable virtual onlyProxy {
        _authorizeUpgrade(newImplementation);
        _upgradeToAndCallUUPS(newImplementation, data, true);
    }

    /**
     * @dev Function that should revert when `msg.sender` is not authorized to upgrade the contract. Called by
     * {upgradeTo} and {upgradeToAndCall}.
     *
     * Normally, this function will use an xref:access.adoc[access control] modifier such as {Ownable-onlyOwner}.
     *
     * ```solidity
     * function _authorizeUpgrade(address) internal override onlyOwner {}
     * ```
     */
    function _authorizeUpgrade(address newImplementation) internal virtual;

    /**
     * @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;
}

// File: @openzeppelin/contracts-upgradeable/utils/ContextUpgradeable.sol



// 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;
}

// File: @openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.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
     * ====
     *
     * [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://diligence.consensys.net/posts/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.5.11/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 functionCall(target, data, "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");
        require(isContract(target), "Address: call to non-contract");

        (bool success, bytes memory returndata) = target.call{value: value}(data);
        return verifyCallResult(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) {
        require(isContract(target), "Address: static call to non-contract");

        (bool success, bytes memory returndata) = target.staticcall(data);
        return verifyCallResult(success, returndata, errorMessage);
    }

    /**
     * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
     * revert reason 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 {
            // 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: @openzeppelin/contracts-upgradeable/utils/escrow/EscrowUpgradeable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (utils/escrow/Escrow.sol)

pragma solidity ^0.8.0;

import "../../access/OwnableUpgradeable.sol";
import "../AddressUpgradeable.sol";
import "../../proxy/utils/Initializable.sol";

/**
 * @title Escrow
 * @dev Base escrow contract, holds funds designated for a payee until they
 * withdraw them.
 *
 * Intended usage: This contract (and derived escrow contracts) should be a
 * standalone contract, that only interacts with the contract that instantiated
 * it. That way, it is guaranteed that all Ether will be handled according to
 * the `Escrow` rules, and there is no need to check for payable functions or
 * transfers in the inheritance tree. The contract that uses the escrow as its
 * payment method should be its owner, and provide public methods redirecting
 * to the escrow's deposit and withdraw.
 */
contract EscrowUpgradeable is Initializable, OwnableUpgradeable {
    function __Escrow_init() internal onlyInitializing {
        __Ownable_init_unchained();
    }

    function __Escrow_init_unchained() internal onlyInitializing {
    }
    function initialize() public virtual initializer {
        __Escrow_init();
    }
    using AddressUpgradeable for address payable;

    event Deposited(address indexed payee, uint256 weiAmount);
    event Withdrawn(address indexed payee, uint256 weiAmount);

    mapping(address => uint256) private _deposits;

    function depositsOf(address payee) public view returns (uint256) {
        return _deposits[payee];
    }

    /**
     * @dev Stores the sent amount as credit to be withdrawn.
     * @param payee The destination address of the funds.
     *
     * Emits a {Deposited} event.
     */
    function deposit(address payee) public payable virtual onlyOwner {
        uint256 amount = msg.value;
        _deposits[payee] += amount;
        emit Deposited(payee, amount);
    }

    /**
     * @dev Withdraw accumulated balance for a payee, forwarding all gas to the
     * recipient.
     *
     * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
     * Make sure you trust the recipient, or are either following the
     * checks-effects-interactions pattern or using {ReentrancyGuard}.
     *
     * @param payee The address whose funds will be withdrawn and transferred to.
     *
     * Emits a {Withdrawn} event.
     */
    function withdraw(address payable payee) public virtual onlyOwner {
        uint256 payment = _deposits[payee];

        _deposits[payee] = 0;

        payee.sendValue(payment);

        emit Withdrawn(payee, payment);
    }

    /**
     * @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[49] private __gap;
}

// File: @openzeppelin/contracts-upgradeable/interfaces/draft-IERC1822Upgradeable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (interfaces/draft-IERC1822.sol)

pragma solidity ^0.8.0;

/**
 * @dev ERC1822: Universal Upgradeable Proxy Standard (UUPS) documents a method for upgradeability through a simplified
 * proxy whose upgrades are fully controlled by the current implementation.
 */
interface IERC1822ProxiableUpgradeable {
    /**
     * @dev Returns the storage slot that the proxiable contract assumes is being used to store the implementation
     * address.
     *
     * IMPORTANT: A proxy pointing at a proxiable contract should not be considered proxiable itself, because this risks
     * bricking a proxy that upgrades to it, by delegating to itself until out of gas. Thus it is critical that this
     * function revert if invoked through a proxy.
     */
    function proxiableUUID() external view returns (bytes32);
}

// File: @openzeppelin/contracts-upgradeable/proxy/ERC1967/ERC1967UpgradeUpgradeable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (proxy/ERC1967/ERC1967Upgrade.sol)

pragma solidity ^0.8.2;

import "../beacon/IBeaconUpgradeable.sol";
import "../../interfaces/draft-IERC1822Upgradeable.sol";
import "../../utils/AddressUpgradeable.sol";
import "../../utils/StorageSlotUpgradeable.sol";
import "../utils/Initializable.sol";

/**
 * @dev This abstract contract provides getters and event emitting update functions for
 * https://eips.ethereum.org/EIPS/eip-1967[EIP1967] slots.
 *
 * _Available since v4.1._
 *
 * @custom:oz-upgrades-unsafe-allow delegatecall
 */
abstract contract ERC1967UpgradeUpgradeable is Initializable {
    function __ERC1967Upgrade_init() internal onlyInitializing {
    }

    function __ERC1967Upgrade_init_unchained() internal onlyInitializing {
    }
    // This is the keccak-256 hash of "eip1967.proxy.rollback" subtracted by 1
    bytes32 private constant _ROLLBACK_SLOT = 0x4910fdfa16fed3260ed0e7147f7cc6da11a60208b5b9406d12a635614ffd9143;

    /**
     * @dev Storage slot with the address of the current implementation.
     * This is the keccak-256 hash of "eip1967.proxy.implementation" subtracted by 1, and is
     * validated in the constructor.
     */
    bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;

    /**
     * @dev Emitted when the implementation is upgraded.
     */
    event Upgraded(address indexed implementation);

    /**
     * @dev Returns the current implementation address.
     */
    function _getImplementation() internal view returns (address) {
        return StorageSlotUpgradeable.getAddressSlot(_IMPLEMENTATION_SLOT).value;
    }

    /**
     * @dev Stores a new address in the EIP1967 implementation slot.
     */
    function _setImplementation(address newImplementation) private {
        require(AddressUpgradeable.isContract(newImplementation), "ERC1967: new implementation is not a contract");
        StorageSlotUpgradeable.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
    }

    /**
     * @dev Perform implementation upgrade
     *
     * Emits an {Upgraded} event.
     */
    function _upgradeTo(address newImplementation) internal {
        _setImplementation(newImplementation);
        emit Upgraded(newImplementation);
    }

    /**
     * @dev Perform implementation upgrade with additional setup call.
     *
     * Emits an {Upgraded} event.
     */
    function _upgradeToAndCall(
        address newImplementation,
        bytes memory data,
        bool forceCall
    ) internal {
        _upgradeTo(newImplementation);
        if (data.length > 0 || forceCall) {
            _functionDelegateCall(newImplementation, data);
        }
    }

    /**
     * @dev Perform implementation upgrade with security checks for UUPS proxies, and additional setup call.
     *
     * Emits an {Upgraded} event.
     */
    function _upgradeToAndCallUUPS(
        address newImplementation,
        bytes memory data,
        bool forceCall
    ) internal {
        // Upgrades from old implementations will perform a rollback test. This test requires the new
        // implementation to upgrade back to the old, non-ERC1822 compliant, implementation. Removing
        // this special case will break upgrade paths from old UUPS implementation to new ones.
        if (StorageSlotUpgradeable.getBooleanSlot(_ROLLBACK_SLOT).value) {
            _setImplementation(newImplementation);
        } else {
            try IERC1822ProxiableUpgradeable(newImplementation).proxiableUUID() returns (bytes32 slot) {
                require(slot == _IMPLEMENTATION_SLOT, "ERC1967Upgrade: unsupported proxiableUUID");
            } catch {
                revert("ERC1967Upgrade: new implementation is not UUPS");
            }
            _upgradeToAndCall(newImplementation, data, forceCall);
        }
    }

    /**
     * @dev Storage slot with the admin of the contract.
     * This is the keccak-256 hash of "eip1967.proxy.admin" subtracted by 1, and is
     * validated in the constructor.
     */
    bytes32 internal constant _ADMIN_SLOT = 0xb53127684a568b3173ae13b9f8a6016e243e63b6e8ee1178d6a717850b5d6103;

    /**
     * @dev Emitted when the admin account has changed.
     */
    event AdminChanged(address previousAdmin, address newAdmin);

    /**
     * @dev Returns the current admin.
     */
    function _getAdmin() internal view returns (address) {
        return StorageSlotUpgradeable.getAddressSlot(_ADMIN_SLOT).value;
    }

    /**
     * @dev Stores a new address in the EIP1967 admin slot.
     */
    function _setAdmin(address newAdmin) private {
        require(newAdmin != address(0), "ERC1967: new admin is the zero address");
        StorageSlotUpgradeable.getAddressSlot(_ADMIN_SLOT).value = newAdmin;
    }

    /**
     * @dev Changes the admin of the proxy.
     *
     * Emits an {AdminChanged} event.
     */
    function _changeAdmin(address newAdmin) internal {
        emit AdminChanged(_getAdmin(), newAdmin);
        _setAdmin(newAdmin);
    }

    /**
     * @dev The storage slot of the UpgradeableBeacon contract which defines the implementation for this proxy.
     * This is bytes32(uint256(keccak256('eip1967.proxy.beacon')) - 1)) and is validated in the constructor.
     */
    bytes32 internal constant _BEACON_SLOT = 0xa3f0ad74e5423aebfd80d3ef4346578335a9a72aeaee59ff6cb3582b35133d50;

    /**
     * @dev Emitted when the beacon is upgraded.
     */
    event BeaconUpgraded(address indexed beacon);

    /**
     * @dev Returns the current beacon.
     */
    function _getBeacon() internal view returns (address) {
        return StorageSlotUpgradeable.getAddressSlot(_BEACON_SLOT).value;
    }

    /**
     * @dev Stores a new beacon in the EIP1967 beacon slot.
     */
    function _setBeacon(address newBeacon) private {
        require(AddressUpgradeable.isContract(newBeacon), "ERC1967: new beacon is not a contract");
        require(
            AddressUpgradeable.isContract(IBeaconUpgradeable(newBeacon).implementation()),
            "ERC1967: beacon implementation is not a contract"
        );
        StorageSlotUpgradeable.getAddressSlot(_BEACON_SLOT).value = newBeacon;
    }

    /**
     * @dev Perform beacon upgrade with additional setup call. Note: This upgrades the address of the beacon, it does
     * not upgrade the implementation contained in the beacon (see {UpgradeableBeacon-_setImplementation} for that).
     *
     * Emits a {BeaconUpgraded} event.
     */
    function _upgradeBeaconToAndCall(
        address newBeacon,
        bytes memory data,
        bool forceCall
    ) internal {
        _setBeacon(newBeacon);
        emit BeaconUpgraded(newBeacon);
        if (data.length > 0 || forceCall) {
            _functionDelegateCall(IBeaconUpgradeable(newBeacon).implementation(), data);
        }
    }

    /**
     * @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) private returns (bytes memory) {
        require(AddressUpgradeable.isContract(target), "Address: delegate call to non-contract");

        // solhint-disable-next-line avoid-low-level-calls
        (bool success, bytes memory returndata) = target.delegatecall(data);
        return AddressUpgradeable.verifyCallResult(success, returndata, "Address: low-level delegate call failed");
    }

    /**
     * @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;
}

// File: @openzeppelin/contracts-upgradeable/utils/StorageSlotUpgradeable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (utils/StorageSlot.sol)

pragma solidity ^0.8.0;

/**
 * @dev Library for reading and writing primitive types to specific storage slots.
 *
 * Storage slots are often used to avoid storage conflict when dealing with upgradeable contracts.
 * This library helps with reading and writing to such slots without the need for inline assembly.
 *
 * The functions in this library return Slot structs that contain a `value` member that can be used to read or write.
 *
 * Example usage to set ERC1967 implementation slot:
 * ```
 * contract ERC1967 {
 *     bytes32 internal constant _IMPLEMENTATION_SLOT = 0x360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc;
 *
 *     function _getImplementation() internal view returns (address) {
 *         return StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value;
 *     }
 *
 *     function _setImplementation(address newImplementation) internal {
 *         require(Address.isContract(newImplementation), "ERC1967: new implementation is not a contract");
 *         StorageSlot.getAddressSlot(_IMPLEMENTATION_SLOT).value = newImplementation;
 *     }
 * }
 * ```
 *
 * _Available since v4.1 for `address`, `bool`, `bytes32`, and `uint256`._
 */
library StorageSlotUpgradeable {
    struct AddressSlot {
        address value;
    }

    struct BooleanSlot {
        bool value;
    }

    struct Bytes32Slot {
        bytes32 value;
    }

    struct Uint256Slot {
        uint256 value;
    }

    /**
     * @dev Returns an `AddressSlot` with member `value` located at `slot`.
     */
    function getAddressSlot(bytes32 slot) internal pure returns (AddressSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `BooleanSlot` with member `value` located at `slot`.
     */
    function getBooleanSlot(bytes32 slot) internal pure returns (BooleanSlot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `Bytes32Slot` with member `value` located at `slot`.
     */
    function getBytes32Slot(bytes32 slot) internal pure returns (Bytes32Slot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }

    /**
     * @dev Returns an `Uint256Slot` with member `value` located at `slot`.
     */
    function getUint256Slot(bytes32 slot) internal pure returns (Uint256Slot storage r) {
        /// @solidity memory-safe-assembly
        assembly {
            r.slot := slot
        }
    }
}

// File: @openzeppelin/contracts-upgradeable/proxy/beacon/IBeaconUpgradeable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (proxy/beacon/IBeacon.sol)

pragma solidity ^0.8.0;

/**
 * @dev This is the interface that {BeaconProxy} expects of its beacon.
 */
interface IBeaconUpgradeable {
    /**
     * @dev Must return an address that can be used as a delegate call target.
     *
     * {BeaconProxy} will check that this address is a contract.
     */
    function implementation() external view returns (address);
}

// File: @openzeppelin/contracts/access/IAccessControlEnumerable.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (access/IAccessControlEnumerable.sol)

pragma solidity ^0.8.0;

import "./IAccessControl.sol";

/**
 * @dev External interface of AccessControlEnumerable declared to support ERC165 detection.
 */
interface IAccessControlEnumerable is IAccessControl {
    /**
     * @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
     * https://forum.openzeppelin.com/t/iterating-over-elements-on-enumerableset-in-openzeppelin-contracts/2296[forum post]
     * 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);
}

// File: @openzeppelin/contracts/access/AccessControl.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/AccessControl.sol)

pragma solidity ^0.8.0;

import "./IAccessControl.sol";
import "../utils/Context.sol";
import "../utils/Strings.sol";
import "../utils/introspection/ERC165.sol";

/**
 * @dev Contract module that allows children to implement role-based access
 * control mechanisms. This is a lightweight version that doesn't allow enumerating role
 * members except through off-chain means by accessing the contract event logs. Some
 * applications may benefit from on-chain enumerability, for those cases see
 * {AccessControlEnumerable}.
 *
 * Roles are referred to by their `bytes32` identifier. These should be exposed
 * in the external API and be unique. The best way to achieve this is by
 * using `public constant` hash digests:
 *
 * ```
 * bytes32 public constant MY_ROLE = keccak256("MY_ROLE");
 * ```
 *
 * Roles can be used to represent a set of permissions. To restrict access to a
 * function call, use {hasRole}:
 *
 * ```
 * function foo() public {
 *     require(hasRole(MY_ROLE, msg.sender));
 *     ...
 * }
 * ```
 *
 * Roles can be granted and revoked dynamically via the {grantRole} and
 * {revokeRole} functions. Each role has an associated admin role, and only
 * accounts that have a role's admin role can call {grantRole} and {revokeRole}.
 *
 * By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means
 * that only accounts with this role will be able to grant or revoke other
 * roles. More complex role relationships can be created by using
 * {_setRoleAdmin}.
 *
 * WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to
 * grant and revoke this role. Extra precautions should be taken to secure
 * accounts that have been granted it.
 */
abstract contract AccessControl is Context, IAccessControl, ERC165 {
    struct RoleData {
        mapping(address => bool) members;
        bytes32 adminRole;
    }

    mapping(bytes32 => RoleData) private _roles;

    bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;

    /**
     * @dev Modifier that checks that an account has a specific role. Reverts
     * with a standardized message including the required role.
     *
     * The format of the revert reason is given by the following regular expression:
     *
     *  /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/
     *
     * _Available since v4.1._
     */
    modifier onlyRole(bytes32 role) {
        _checkRole(role);
        _;
    }

    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
        return interfaceId == type(IAccessControl).interfaceId || super.supportsInterface(interfaceId);
    }

    /**
     * @dev Returns `true` if `account` has been granted `role`.
     */
    function hasRole(bytes32 role, address account) public view virtual override returns (bool) {
        return _roles[role].members[account];
    }

    /**
     * @dev Revert with a standard message if `_msgSender()` is missing `role`.
     * Overriding this function changes the behavior of the {onlyRole} modifier.
     *
     * Format of the revert message is described in {_checkRole}.
     *
     * _Available since v4.6._
     */
    function _checkRole(bytes32 role) internal view virtual {
        _checkRole(role, _msgSender());
    }

    /**
     * @dev Revert with a standard message if `account` is missing `role`.
     *
     * The format of the revert reason is given by the following regular expression:
     *
     *  /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/
     */
    function _checkRole(bytes32 role, address account) internal view virtual {
        if (!hasRole(role, account)) {
            revert(
                string(
                    abi.encodePacked(
                        "AccessControl: account ",
                        Strings.toHexString(uint160(account), 20),
                        " is missing role ",
                        Strings.toHexString(uint256(role), 32)
                    )
                )
            );
        }
    }

    /**
     * @dev Returns the admin role that controls `role`. See {grantRole} and
     * {revokeRole}.
     *
     * To change a role's admin, use {_setRoleAdmin}.
     */
    function getRoleAdmin(bytes32 role) public view virtual override returns (bytes32) {
        return _roles[role].adminRole;
    }

    /**
     * @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.
     *
     * May emit a {RoleGranted} event.
     */
    function grantRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
        _grantRole(role, account);
    }

    /**
     * @dev Revokes `role` from `account`.
     *
     * If `account` had been granted `role`, emits a {RoleRevoked} event.
     *
     * Requirements:
     *
     * - the caller must have ``role``'s admin role.
     *
     * May emit a {RoleRevoked} event.
     */
    function revokeRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
        _revokeRole(role, account);
    }

    /**
     * @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 revoked `role`, emits a {RoleRevoked}
     * event.
     *
     * Requirements:
     *
     * - the caller must be `account`.
     *
     * May emit a {RoleRevoked} event.
     */
    function renounceRole(bytes32 role, address account) public virtual override {
        require(account == _msgSender(), "AccessControl: can only renounce roles for self");

        _revokeRole(role, account);
    }

    /**
     * @dev Grants `role` to `account`.
     *
     * If `account` had not been already granted `role`, emits a {RoleGranted}
     * event. Note that unlike {grantRole}, this function doesn't perform any
     * checks on the calling account.
     *
     * May emit a {RoleGranted} event.
     *
     * [WARNING]
     * ====
     * This function should only be called from the constructor when setting
     * up the initial roles for the system.
     *
     * Using this function in any other way is effectively circumventing the admin
     * system imposed by {AccessControl}.
     * ====
     *
     * NOTE: This function is deprecated in favor of {_grantRole}.
     */
    function _setupRole(bytes32 role, address account) internal virtual {
        _grantRole(role, account);
    }

    /**
     * @dev Sets `adminRole` as ``role``'s admin role.
     *
     * Emits a {RoleAdminChanged} event.
     */
    function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
        bytes32 previousAdminRole = getRoleAdmin(role);
        _roles[role].adminRole = adminRole;
        emit RoleAdminChanged(role, previousAdminRole, adminRole);
    }

    /**
     * @dev Grants `role` to `account`.
     *
     * Internal function without access restriction.
     *
     * May emit a {RoleGranted} event.
     */
    function _grantRole(bytes32 role, address account) internal virtual {
        if (!hasRole(role, account)) {
            _roles[role].members[account] = true;
            emit RoleGranted(role, account, _msgSender());
        }
    }

    /**
     * @dev Revokes `role` from `account`.
     *
     * Internal function without access restriction.
     *
     * May emit a {RoleRevoked} event.
     */
    function _revokeRole(bytes32 role, address account) internal virtual {
        if (hasRole(role, account)) {
            _roles[role].members[account] = false;
            emit RoleRevoked(role, account, _msgSender());
        }
    }
}

// File: @openzeppelin/contracts/utils/structs/EnumerableSet.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (utils/structs/EnumerableSet.sol)

pragma solidity ^0.8.0;

/**
 * @dev Library for managing
 * https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
 * types.
 *
 * Sets have the following properties:
 *
 * - Elements are added, removed, and checked for existence in constant time
 * (O(1)).
 * - Elements are enumerated in O(n). No guarantees are made on the ordering.
 *
 * ```
 * contract Example {
 *     // Add the library methods
 *     using EnumerableSet for EnumerableSet.AddressSet;
 *
 *     // Declare a set state variable
 *     EnumerableSet.AddressSet private mySet;
 * }
 * ```
 *
 * As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)
 * and `uint256` (`UintSet`) are supported.
 *
 * [WARNING]
 * ====
 *  Trying to delete such a structure from storage will likely result in data corruption, rendering the structure unusable.
 *  See https://github.com/ethereum/solidity/pull/11843[ethereum/solidity#11843] for more info.
 *
 *  In order to clean an EnumerableSet, you can either remove all elements one by one or create a fresh instance using an array of EnumerableSet.
 * ====
 */
library EnumerableSet {
    // To implement this library for multiple types with as little code
    // repetition as possible, we write it in terms of a generic Set type with
    // bytes32 values.
    // The Set implementation uses private functions, and user-facing
    // implementations (such as AddressSet) are just wrappers around the
    // underlying Set.
    // This means that we can only create new EnumerableSets for types that fit
    // in bytes32.

    struct Set {
        // Storage of set values
        bytes32[] _values;
        // Position of the value in the `values` array, plus 1 because index 0
        // means a value is not in the set.
        mapping(bytes32 => uint256) _indexes;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function _add(Set storage set, bytes32 value) private returns (bool) {
        if (!_contains(set, value)) {
            set._values.push(value);
            // The value is stored at length-1, but we add 1 to all indexes
            // and use 0 as a sentinel value
            set._indexes[value] = set._values.length;
            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function _remove(Set storage set, bytes32 value) private returns (bool) {
        // We read and store the value's index to prevent multiple reads from the same storage slot
        uint256 valueIndex = set._indexes[value];

        if (valueIndex != 0) {
            // Equivalent to contains(set, value)
            // To delete an element from the _values array in O(1), we swap the element to delete with the last one in
            // the array, and then remove the last element (sometimes called as 'swap and pop').
            // This modifies the order of the array, as noted in {at}.

            uint256 toDeleteIndex = valueIndex - 1;
            uint256 lastIndex = set._values.length - 1;

            if (lastIndex != toDeleteIndex) {
                bytes32 lastValue = set._values[lastIndex];

                // Move the last value to the index where the value to delete is
                set._values[toDeleteIndex] = lastValue;
                // Update the index for the moved value
                set._indexes[lastValue] = valueIndex; // Replace lastValue's index to valueIndex
            }

            // Delete the slot where the moved value was stored
            set._values.pop();

            // Delete the index for the deleted slot
            delete set._indexes[value];

            return true;
        } else {
            return false;
        }
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function _contains(Set storage set, bytes32 value) private view returns (bool) {
        return set._indexes[value] != 0;
    }

    /**
     * @dev Returns the number of values on the set. O(1).
     */
    function _length(Set storage set) private view returns (uint256) {
        return set._values.length;
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function _at(Set storage set, uint256 index) private view returns (bytes32) {
        return set._values[index];
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function _values(Set storage set) private view returns (bytes32[] memory) {
        return set._values;
    }

    // Bytes32Set

    struct Bytes32Set {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _add(set._inner, value);
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
        return _remove(set._inner, value);
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
        return _contains(set._inner, value);
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(Bytes32Set storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
        return _at(set._inner, index);
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {
        return _values(set._inner);
    }

    // AddressSet

    struct AddressSet {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(AddressSet storage set, address value) internal returns (bool) {
        return _add(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(AddressSet storage set, address value) internal returns (bool) {
        return _remove(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(AddressSet storage set, address value) internal view returns (bool) {
        return _contains(set._inner, bytes32(uint256(uint160(value))));
    }

    /**
     * @dev Returns the number of values in the set. O(1).
     */
    function length(AddressSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(AddressSet storage set, uint256 index) internal view returns (address) {
        return address(uint160(uint256(_at(set._inner, index))));
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(AddressSet storage set) internal view returns (address[] memory) {
        bytes32[] memory store = _values(set._inner);
        address[] memory result;

        /// @solidity memory-safe-assembly
        assembly {
            result := store
        }

        return result;
    }

    // UintSet

    struct UintSet {
        Set _inner;
    }

    /**
     * @dev Add a value to a set. O(1).
     *
     * Returns true if the value was added to the set, that is if it was not
     * already present.
     */
    function add(UintSet storage set, uint256 value) internal returns (bool) {
        return _add(set._inner, bytes32(value));
    }

    /**
     * @dev Removes a value from a set. O(1).
     *
     * Returns true if the value was removed from the set, that is if it was
     * present.
     */
    function remove(UintSet storage set, uint256 value) internal returns (bool) {
        return _remove(set._inner, bytes32(value));
    }

    /**
     * @dev Returns true if the value is in the set. O(1).
     */
    function contains(UintSet storage set, uint256 value) internal view returns (bool) {
        return _contains(set._inner, bytes32(value));
    }

    /**
     * @dev Returns the number of values on the set. O(1).
     */
    function length(UintSet storage set) internal view returns (uint256) {
        return _length(set._inner);
    }

    /**
     * @dev Returns the value stored at position `index` in the set. O(1).
     *
     * Note that there are no guarantees on the ordering of values inside the
     * array, and it may change when more values are added or removed.
     *
     * Requirements:
     *
     * - `index` must be strictly less than {length}.
     */
    function at(UintSet storage set, uint256 index) internal view returns (uint256) {
        return uint256(_at(set._inner, index));
    }

    /**
     * @dev Return the entire set in an array
     *
     * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
     * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
     * this function has an unbounded cost, and using it as part of a state-changing function may render the function
     * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
     */
    function values(UintSet storage set) internal view returns (uint256[] memory) {
        bytes32[] memory store = _values(set._inner);
        uint256[] memory result;

        /// @solidity memory-safe-assembly
        assembly {
            result := store
        }

        return result;
    }
}

// File: @openzeppelin/contracts/access/IAccessControl.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (access/IAccessControl.sol)

pragma solidity ^0.8.0;

/**
 * @dev External interface of AccessControl declared to support ERC165 detection.
 */
interface IAccessControl {
    /**
     * @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;
}

// File: @openzeppelin/contracts/utils/Strings.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (utils/Strings.sol)

pragma solidity ^0.8.0;

/**
 * @dev String operations.
 */
library Strings {
    bytes16 private constant _HEX_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) {
        // 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 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);
    }
}

// File: @openzeppelin/contracts/utils/Context.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)

pragma solidity ^0.8.0;

/**
 * @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 Context {
    function _msgSender() internal view virtual returns (address) {
        return msg.sender;
    }

    function _msgData() internal view virtual returns (bytes calldata) {
        return msg.data;
    }
}

// File: @openzeppelin/contracts/utils/introspection/ERC165.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/ERC165.sol)

pragma solidity ^0.8.0;

import "./IERC165.sol";

/**
 * @dev Implementation of the {IERC165} interface.
 *
 * Contracts that want to implement ERC165 should inherit from this contract and override {supportsInterface} to check
 * for the additional interface id that will be supported. For example:
 *
 * ```solidity
 * function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
 *     return interfaceId == type(MyInterface).interfaceId || super.supportsInterface(interfaceId);
 * }
 * ```
 *
 * Alternatively, {ERC165Storage} provides an easier to use but more expensive implementation.
 */
abstract contract ERC165 is IERC165 {
    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
        return interfaceId == type(IERC165).interfaceId;
    }
}

// File: @openzeppelin/contracts/utils/introspection/IERC165.sol



// 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 IERC165 {
    /**
     * @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);
}

// File: @openzeppelin/contracts/token/ERC1155/IERC1155.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (token/ERC1155/IERC1155.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev Required interface of an ERC1155 compliant contract, as defined in the
 * https://eips.ethereum.org/EIPS/eip-1155[EIP].
 *
 * _Available since v3.1._
 */
interface IERC1155 is IERC165 {
    /**
     * @dev Emitted when `value` tokens of token type `id` are transferred from `from` to `to` by `operator`.
     */
    event TransferSingle(address indexed operator, address indexed from, address indexed to, uint256 id, uint256 value);

    /**
     * @dev Equivalent to multiple {TransferSingle} events, where `operator`, `from` and `to` are the same for all
     * transfers.
     */
    event TransferBatch(
        address indexed operator,
        address indexed from,
        address indexed to,
        uint256[] ids,
        uint256[] values
    );

    /**
     * @dev Emitted when `account` grants or revokes permission to `operator` to transfer their tokens, according to
     * `approved`.
     */
    event ApprovalForAll(address indexed account, address indexed operator, bool approved);

    /**
     * @dev Emitted when the URI for token type `id` changes to `value`, if it is a non-programmatic URI.
     *
     * If an {URI} event was emitted for `id`, the standard
     * https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[guarantees] that `value` will equal the value
     * returned by {IERC1155MetadataURI-uri}.
     */
    event URI(string value, uint256 indexed id);

    /**
     * @dev Returns the amount of tokens of token type `id` owned by `account`.
     *
     * Requirements:
     *
     * - `account` cannot be the zero address.
     */
    function balanceOf(address account, uint256 id) external view returns (uint256);

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {balanceOf}.
     *
     * Requirements:
     *
     * - `accounts` and `ids` must have the same length.
     */
    function balanceOfBatch(address[] calldata accounts, uint256[] calldata ids)
        external
        view
        returns (uint256[] memory);

    /**
     * @dev Grants or revokes permission to `operator` to transfer the caller's tokens, according to `approved`,
     *
     * Emits an {ApprovalForAll} event.
     *
     * Requirements:
     *
     * - `operator` cannot be the caller.
     */
    function setApprovalForAll(address operator, bool approved) external;

    /**
     * @dev Returns true if `operator` is approved to transfer ``account``'s tokens.
     *
     * See {setApprovalForAll}.
     */
    function isApprovedForAll(address account, address operator) external view returns (bool);

    /**
     * @dev Transfers `amount` tokens of token type `id` from `from` to `to`.
     *
     * Emits a {TransferSingle} event.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - If the caller is not `from`, it must have been approved to spend ``from``'s tokens via {setApprovalForAll}.
     * - `from` must have a balance of tokens of type `id` of at least `amount`.
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155Received} and return the
     * acceptance magic value.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 id,
        uint256 amount,
        bytes calldata data
    ) external;

    /**
     * @dev xref:ROOT:erc1155.adoc#batch-operations[Batched] version of {safeTransferFrom}.
     *
     * Emits a {TransferBatch} event.
     *
     * Requirements:
     *
     * - `ids` and `amounts` must have the same length.
     * - If `to` refers to a smart contract, it must implement {IERC1155Receiver-onERC1155BatchReceived} and return the
     * acceptance magic value.
     */
    function safeBatchTransferFrom(
        address from,
        address to,
        uint256[] calldata ids,
        uint256[] calldata amounts,
        bytes calldata data
    ) external;
}

// File: @openzeppelin/contracts/token/ERC1155/IERC1155Receiver.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC1155/IERC1155Receiver.sol)

pragma solidity ^0.8.0;

import "../../utils/introspection/IERC165.sol";

/**
 * @dev _Available since v3.1._
 */
interface IERC1155Receiver is IERC165 {
    /**
     * @dev Handles the receipt of a single ERC1155 token type. This function is
     * called at the end of a `safeTransferFrom` after the balance has been updated.
     *
     * NOTE: To accept the transfer, this must return
     * `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))`
     * (i.e. 0xf23a6e61, or its own function selector).
     *
     * @param operator The address which initiated the transfer (i.e. msg.sender)
     * @param from The address which previously owned the token
     * @param id The ID of the token being transferred
     * @param value The amount of tokens being transferred
     * @param data Additional data with no specified format
     * @return `bytes4(keccak256("onERC1155Received(address,address,uint256,uint256,bytes)"))` if transfer is allowed
     */
    function onERC1155Received(
        address operator,
        address from,
        uint256 id,
        uint256 value,
        bytes calldata data
    ) external returns (bytes4);

    /**
     * @dev Handles the receipt of a multiple ERC1155 token types. This function
     * is called at the end of a `safeBatchTransferFrom` after the balances have
     * been updated.
     *
     * NOTE: To accept the transfer(s), this must return
     * `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))`
     * (i.e. 0xbc197c81, or its own function selector).
     *
     * @param operator The address which initiated the batch transfer (i.e. msg.sender)
     * @param from The address which previously owned the token
     * @param ids An array containing ids of each token being transferred (order and length must match values array)
     * @param values An array containing amounts of each token being transferred (order and length must match ids array)
     * @param data Additional data with no specified format
     * @return `bytes4(keccak256("onERC1155BatchReceived(address,address,uint256[],uint256[],bytes)"))` if transfer is allowed
     */
    function onERC1155BatchReceived(
        address operator,
        address from,
        uint256[] calldata ids,
        uint256[] calldata values,
        bytes calldata data
    ) external returns (bytes4);
}

// File: @openzeppelin/contracts/token/ERC1155/extensions/IERC1155MetadataURI.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC1155/extensions/IERC1155MetadataURI.sol)

pragma solidity ^0.8.0;

import "../IERC1155.sol";

/**
 * @dev Interface of the optional ERC1155MetadataExtension interface, as defined
 * in the https://eips.ethereum.org/EIPS/eip-1155#metadata-extensions[EIP].
 *
 * _Available since v3.1._
 */
interface IERC1155MetadataURI is IERC1155 {
    /**
     * @dev Returns the URI for token type `id`.
     *
     * If the `\{id\}` substring is present in the URI, it must be replaced by
     * clients with the actual token type ID.
     */
    function uri(uint256 id) external view returns (string memory);
}

// File: @openzeppelin/contracts/interfaces/IERC2981.sol



// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (interfaces/IERC2981.sol)

pragma solidity ^0.8.0;

import "../utils/introspection/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 paid in that same unit of exchange.
     */
    function royaltyInfo(uint256 tokenId, uint256 salePrice)
        external
        view
        returns (address receiver, uint256 royaltyAmount);
}