ETH Price: $3,332.37 (-0.84%)

Contract

0x7a1D4c6E8edbd8a9314034356b29419e1b1B44f0
 

Overview

ETH Balance

0.654180309631440572 ETH

Eth Value

$2,179.97 (@ $3,332.37/ETH)

Multichain Info

No addresses found
Transaction Hash
Method
Block
From
To
Create Pool ETH174842662023-06-15 9:01:59561 days ago1686819719IN
0x7a1D4c6E...e1b1B44f0
1.16904854 ETH0.0241973419.01474269
Create Pool ETH173120342023-05-22 3:00:59585 days ago1684724459IN
0x7a1D4c6E...e1b1B44f0
9.49336152 ETH0.011428435.58819441
Create Pool ETH173120252023-05-22 2:59:11585 days ago1684724351IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.016444730.74679661
Create Pool ETH173120152023-05-22 2:57:11585 days ago1684724231IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.018406636.40042415
Create Pool ETH173120022023-05-22 2:54:35585 days ago1684724075IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.0255634933.32042442
Create Pool ETH ...173119892023-05-22 2:51:59585 days ago1684723919IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.0211040536.81269036
Create Pool ETH173119712023-05-22 2:48:23585 days ago1684723703IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.0310395144.32943601
Create Pool ETH ...172960982023-05-19 21:09:11588 days ago1684530551IN
0x7a1D4c6E...e1b1B44f0
6.58694136 ETH0.0376935843.0150648
Create Pool ETH ...172870342023-05-18 14:29:35589 days ago1684420175IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.0246520467.62785613
Create Pool ETH ...172870172023-05-18 14:26:11589 days ago1684419971IN
0x7a1D4c6E...e1b1B44f0
13.63596086 ETH0.0232779162.95889319
Create Pool ETH172869052023-05-18 14:03:23589 days ago1684418603IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.0251229961.96095194
Create Pool ETH ...172868892023-05-18 14:00:11589 days ago1684418411IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.036846963.75359456
Create Pool ETH ...172868742023-05-18 13:56:59589 days ago1684418219IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.0205033656.48994984
Create Pool ETH ...172868402023-05-18 13:50:11589 days ago1684417811IN
0x7a1D4c6E...e1b1B44f0
1.09419813 ETH0.0182891247.80520022
Create Pool ETH172855092023-05-18 9:21:11589 days ago1684401671IN
0x7a1D4c6E...e1b1B44f0
0.50459596 ETH0.0114915335.78345942
Create Pool ETH172854862023-05-18 9:16:35589 days ago1684401395IN
0x7a1D4c6E...e1b1B44f0
5.58279282 ETH0.0410328338.38482007
Create Pool ETH ...172852662023-05-18 8:31:59589 days ago1684398719IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.0260752136.1510507
Create Pool ETH172850022023-05-18 7:38:11589 days ago1684395491IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.0198617338.1330888
Set Base URI172778542023-05-17 7:23:23590 days ago1684308203IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.0063590846.32708961
Set Router Allow...172778532023-05-17 7:23:11590 days ago1684308191IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.0023829846.37595204
Set Bonding Curv...172778502023-05-17 7:22:35590 days ago1684308155IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.0021887845.35968098
Set Bonding Curv...172778492023-05-17 7:22:23590 days ago1684308143IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.0021853545.28856106
Set Bonding Curv...172778482023-05-17 7:22:11590 days ago1684308131IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.0020191541.84420855
Set Bonding Curv...172778472023-05-17 7:21:59590 days ago1684308119IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.0020424642.32727551
Set Bonding Curv...172778472023-05-17 7:21:59590 days ago1684308119IN
0x7a1D4c6E...e1b1B44f0
0 ETH0.0020424642.32727551
View all transactions

Latest 25 internal transactions (View All)

Advanced mode:
Parent Transaction Hash Block
From
To
176446152023-07-07 21:32:35539 days ago1688765555
0x7a1D4c6E...e1b1B44f0
0.00165708 ETH
176311882023-07-06 0:16:47541 days ago1688602607
0x7a1D4c6E...e1b1B44f0
0.00170679 ETH
176232422023-07-04 21:30:35542 days ago1688506235
0x7a1D4c6E...e1b1B44f0
0.001758 ETH
176209432023-07-04 13:45:23542 days ago1688478323
0x7a1D4c6E...e1b1B44f0
0.00181074 ETH
175675752023-06-27 1:53:59550 days ago1687830839
0x7a1D4c6E...e1b1B44f0
0.01155033 ETH
175675342023-06-27 1:45:47550 days ago1687830347
0x7a1D4c6E...e1b1B44f0
0.01189684 ETH
175672762023-06-27 0:53:35550 days ago1687827215
0x7a1D4c6E...e1b1B44f0
0.00971481 ETH
175659752023-06-26 20:30:11550 days ago1687811411
0x7a1D4c6E...e1b1B44f0
0.01000626 ETH
175658122023-06-26 19:57:35550 days ago1687809455
0x7a1D4c6E...e1b1B44f0
0.00261682 ETH
175656442023-06-26 19:23:23550 days ago1687807403
0x7a1D4c6E...e1b1B44f0
0.01030644 ETH
175656372023-06-26 19:21:59550 days ago1687807319
0x7a1D4c6E...e1b1B44f0
0.01061564 ETH
175587662023-06-25 20:07:47551 days ago1687723667
0x7a1D4c6E...e1b1B44f0
0.01225374 ETH
175587172023-06-25 19:57:59551 days ago1687723079
0x7a1D4c6E...e1b1B44f0
0.01262135 ETH
175580642023-06-25 17:45:47551 days ago1687715147
0x7a1D4c6E...e1b1B44f0
0.01287378 ETH
175541122023-06-25 4:25:23551 days ago1687667123
0x7a1D4c6E...e1b1B44f0
0.01262135 ETH
175539212023-06-25 3:46:35551 days ago1687664795
0x7a1D4c6E...e1b1B44f0
0.013 ETH
175514782023-06-24 19:33:59552 days ago1687635239
0x7a1D4c6E...e1b1B44f0
0.0028 ETH
175493862023-06-24 12:29:11552 days ago1687609751
0x7a1D4c6E...e1b1B44f0
0.00184695 ETH
175482882023-06-24 8:46:11552 days ago1687596371
0x7a1D4c6E...e1b1B44f0
0.07918 ETH
175435422023-06-23 16:45:35553 days ago1687538735
0x7a1D4c6E...e1b1B44f0
0.00285599 ETH
175388772023-06-23 0:59:59554 days ago1687481999
0x7a1D4c6E...e1b1B44f0
0.01093411 ETH
175388452023-06-23 0:53:35554 days ago1687481615
0x7a1D4c6E...e1b1B44f0
0.01126213 ETH
175352332023-06-22 12:43:35554 days ago1687437815
0x7a1D4c6E...e1b1B44f0
0.0028 ETH
175296622023-06-21 17:56:35555 days ago1687370195
0x7a1D4c6E...e1b1B44f0
0.002996 ETH
175204502023-06-20 10:55:47556 days ago1687258547
0x7a1D4c6E...e1b1B44f0
0.0012383 ETH
View All Internal Transactions
Loading...
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
CollectionPoolFactory

Compiler Version
v0.8.17+commit.8df45f5f

Optimization Enabled:
Yes with 150 runs

Other Settings:
default evmVersion
File 1 of 52 : CollectionPoolFactory.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {ERC721} from "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import {ERC721Enumerable} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import {ERC721URIStorage} from "@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import {IERC721Enumerable} from "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol";
import {IERC1820Registry} from "@openzeppelin/contracts/utils/introspection/IERC1820Registry.sol";

// @dev Solmate's ERC20 is used instead of OZ's ERC20 so we can use safeTransferLib for cheaper safeTransfers for
// ETH and ERC20 tokens
import {ERC20} from "solmate/src/tokens/ERC20.sol";
import {SafeTransferLib} from "solmate/src/utils/SafeTransferLib.sol";
import {ReentrancyGuard} from "../lib/ReentrancyGuard.sol";
import {TransferLib} from "../lib/TransferLib.sol";

import {ICollectionPool} from "./ICollectionPool.sol";
import {CollectionPool} from "./CollectionPool.sol";
import {CollectionRouter} from "../routers/CollectionRouter.sol";
import {CollectionPoolETH} from "./CollectionPoolETH.sol";
import {ICurve} from "../bonding-curves/ICurve.sol";
import {CollectionPoolERC20} from "./CollectionPoolERC20.sol";
import {CollectionPoolCloner} from "../lib/CollectionPoolCloner.sol";
import {ICollectionPoolFactory} from "./ICollectionPoolFactory.sol";
import {CollectionPoolEnumerableETH} from "./CollectionPoolEnumerableETH.sol";
import {CollectionPoolEnumerableERC20} from "./CollectionPoolEnumerableERC20.sol";
import {CollectionPoolMissingEnumerableETH} from "./CollectionPoolMissingEnumerableETH.sol";
import {CollectionPoolMissingEnumerableERC20} from "./CollectionPoolMissingEnumerableERC20.sol";
import {MultiPauser} from "../lib/MultiPauser.sol";
import {
    PoolVariant,
    CreateETHPoolParams,
    NFTFilterParams,
    CreateERC20PoolParams,
    RouterStatus,
    RoyaltyDue
} from "./CollectionStructsAndEnums.sol";

/**
 * @dev The ETH balance of this contract is used both to store protocol fees
 * which the owner can withdraw, as well as royalties accumulated from swaps
 * made against pools deployed by this contract.
 */
contract CollectionPoolFactory is
    Ownable,
    ReentrancyGuard,
    ERC721,
    ERC721URIStorage,
    MultiPauser,
    ICollectionPoolFactory
{
    using CollectionPoolCloner for address;
    using SafeTransferLib for address payable;
    using SafeTransferLib for ERC20;

    IERC1820Registry internal constant _ERC1820_REGISTRY = IERC1820Registry(0x1820a4B7618BdE71Dce8cdc73aAB6C95905faD24);

    bytes4 private constant _INTERFACE_ID_ERC2981 = 0x2a55205a;
    bytes4 private constant INTERFACE_ID_ERC721_ENUMERABLE = type(IERC721Enumerable).interfaceId;

    uint256 private constant CREATION_PAUSE = 0;
    uint256 private constant SWAP_PAUSE = 1;
    uint256 private constant DEPOSIT_PAUSE = 2;
    /// @notice For pausing everything that isn't covered by the above lock
    /// besides withdrawals
    uint256 private constant OTHERS_PAUSE = 3;

    /**
     * @dev The MAX_PROTOCOL_FEE constant specifies the maximum fee that can be charged by the AMM pool contract
     * for facilitating token or NFT swaps on the decentralized exchange.
     * This fee is charged as a flat percentage of the final traded price for each swap,
     * and it is used to cover the costs associated with running the AMM pool contract and providing liquidity to the decentralized exchange.
     * This is used for NFT/TOKEN trading pools, that have a limited amount of dry powder
     */
    uint256 internal constant MAX_PROTOCOL_FEE = 0.1e6; // 10%, must <= 1 - MAX_FEE
    /**
     * @dev The MAX_CARRY_FEE constant specifies the maximum fee that can be charged by the AMM pool contract for facilitating token
     * or NFT swaps on the decentralized exchange. This fee is charged as a percentage of the fee set by the trading pool creator,
     * which is itself a percentage of the final traded price. This is used for TRADE pools, that form a continuous liquidity pool
     */
    uint256 internal constant MAX_CARRY_FEE = 0.5e6; // 50%

    address public constant ETH_ADDRESS = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
    CollectionPoolEnumerableETH public immutable enumerableETHTemplate;
    CollectionPoolMissingEnumerableETH public immutable missingEnumerableETHTemplate;
    CollectionPoolEnumerableERC20 public immutable enumerableERC20Template;
    CollectionPoolMissingEnumerableERC20 public immutable missingEnumerableERC20Template;
    address payable public override protocolFeeRecipient;

    // Units are in base 1e6
    uint24 public override protocolFeeMultiplier;

    // Units are in base 1e6
    uint24 public override carryFeeMultiplier;

    mapping(ICurve => bool) public bondingCurveAllowed;
    mapping(address => bool) public override callAllowed;
    /// @dev Used to track how much royalties are stored in this contract balance.
    /// This is to prevent the factory owner from withdrawing more than they should
    mapping(ERC20 => uint256) royaltiesStored;
    /// @dev Uses address 0 for ETH royalties
    mapping(address => mapping(ERC20 => uint256)) public royaltiesClaimable;

    mapping(CollectionRouter => RouterStatus) public override routerStatus;

    string public baseURI;

    modifier whenCreationNotPaused() {
        require(!creationPaused(), "Pool creation is paused");
        _;
    }

    modifier whenOthersNotPaused() {
        require(!othersPaused(), "Function is paused");
        _;
    }

    event NewPool(address indexed collection, address indexed poolAddress);
    event TokenDeposit(address indexed poolAddress);
    event ProtocolFeeRecipientUpdate(address indexed recipientAddress);
    event ProtocolFeeMultiplierUpdate(uint24 newMultiplier);
    event CarryFeeMultiplierUpdate(uint24 newMultiplier);
    event BondingCurveStatusUpdate(ICurve indexed bondingCurve, bool isAllowed);
    event CallTargetStatusUpdate(address indexed target, bool isAllowed);
    event RouterStatusUpdate(CollectionRouter indexed router, bool isAllowed);
    event CreationPause();
    event CreationUnpause();
    event SwapPause();
    event SwapUnpause();
    event OthersPause();
    event OthersUnpause();
    event DepositPause();
    event DepositUnpause();

    error InsufficientValue(uint256 msgValue, uint256 amountRequired);

    constructor(
        CollectionPoolEnumerableETH _enumerableETHTemplate,
        CollectionPoolMissingEnumerableETH _missingEnumerableETHTemplate,
        CollectionPoolEnumerableERC20 _enumerableERC20Template,
        CollectionPoolMissingEnumerableERC20 _missingEnumerableERC20Template,
        address payable _protocolFeeRecipient,
        uint24 _protocolFeeMultiplier,
        uint24 _carryFeeMultiplier
    ) ERC721("Collectionswap", "CollectionLP") {
        require(_protocolFeeMultiplier <= MAX_PROTOCOL_FEE, "Protocol fee too large");
        require(_carryFeeMultiplier <= MAX_CARRY_FEE, "Carry fee too large");
        enumerableETHTemplate = _enumerableETHTemplate;
        missingEnumerableETHTemplate = _missingEnumerableETHTemplate;
        enumerableERC20Template = _enumerableERC20Template;
        missingEnumerableERC20Template = _missingEnumerableERC20Template;
        protocolFeeRecipient = _protocolFeeRecipient;
        protocolFeeMultiplier = _protocolFeeMultiplier;
        carryFeeMultiplier = _carryFeeMultiplier;
    }

    /**
     * External view functions. Not pausable.
     */

    function requireAuthorizedForToken(address spender, uint256 tokenId) external view {
        require(_isApprovedOrOwner(spender, tokenId), "Not approved");
    }

    /**
     * @dev See {ICollectionPoolFactory-poolOf}.
     */
    function poolOf(uint256 tokenId) public pure returns (ICollectionPool) {
        return ICollectionPool(address(uint160(tokenId)));
    }

    /**
     * @notice Check if a pool is any of the templates deployed with this factory
     */
    function isPool(address potentialPool) public view returns (bool) {
        return isPoolVariant(potentialPool, PoolVariant.ENUMERABLE_ERC20)
            || isPoolVariant(potentialPool, PoolVariant.ENUMERABLE_ETH)
            || isPoolVariant(potentialPool, PoolVariant.MISSING_ENUMERABLE_ERC20)
            || isPoolVariant(potentialPool, PoolVariant.MISSING_ENUMERABLE_ETH);
    }

    /**
     * @notice Checks if an address is a CollectionPool. Uses the fact that the pools are EIP-1167 minimal proxies.
     * @param potentialPool The address to check
     * @param variant The pool variant (NFT is enumerable or not, pool uses ETH or ERC20)
     * @dev The PoolCloner contract is a utility contract that is used by the PoolFactory contract to create new instances of automated market maker (AMM) pools.
     * @return True if the address is the specified pool variant, false otherwise
     */
    function isPoolVariant(address potentialPool, PoolVariant variant) public view returns (bool) {
        if (variant == PoolVariant.ENUMERABLE_ERC20) {
            return CollectionPoolCloner.isERC20PoolClone(address(this), address(enumerableERC20Template), potentialPool);
        } else if (variant == PoolVariant.MISSING_ENUMERABLE_ERC20) {
            return CollectionPoolCloner.isERC20PoolClone(
                address(this), address(missingEnumerableERC20Template), potentialPool
            );
        } else if (variant == PoolVariant.ENUMERABLE_ETH) {
            return CollectionPoolCloner.isETHPoolClone(address(this), address(enumerableETHTemplate), potentialPool);
        } else if (variant == PoolVariant.MISSING_ENUMERABLE_ETH) {
            return
                CollectionPoolCloner.isETHPoolClone(address(this), address(missingEnumerableETHTemplate), potentialPool);
        } else {
            // invalid input
            return false;
        }
    }

    function tokenURI(uint256 tokenId) public view override (ERC721, ERC721URIStorage) returns (string memory) {
        return super.tokenURI(tokenId);
    }

    function supportsInterface(bytes4 interfaceId) public view virtual override (IERC165, ERC721) returns (bool) {
        return super.supportsInterface(interfaceId);
    }

    function swapPaused() external view returns (bool) {
        return isPaused(SWAP_PAUSE);
    }

    function creationPaused() public view returns (bool) {
        return isPaused(CREATION_PAUSE);
    }

    function depositPaused() external view returns (bool) {
        return isPaused(DEPOSIT_PAUSE);
    }

    function othersPaused() public view returns (bool) {
        return isPaused(OTHERS_PAUSE);
    }

    /**
     * Pool creation functions. Pausable
     */
    function createPoolETH(CreateETHPoolParams calldata params)
        external
        payable
        whenCreationNotPaused
        returns (ICollectionPool pool, uint256 tokenId)
    {
        (pool, tokenId) = _createPoolETH(params);

        _initializePoolETH(pool, params);
    }

    /**
     * @notice Creates a filtered pool contract using EIP-1167.
     * @param params The parameters to create ETH pool
     * @param filterParams The parameters needed for the filtering functionality
     * @return pool The new pool
     */
    function createPoolETHFiltered(CreateETHPoolParams calldata params, NFTFilterParams calldata filterParams)
        external
        payable
        whenCreationNotPaused
        returns (ICollectionPool pool, uint256 tokenId)
    {
        (pool, tokenId) = _createPoolETH(params);

        // Check if nfts are allowed before initializing to save gas on transferring nfts on revert.
        // If not, we could re-use createPoolETH and check later.
        pool.setTokenIDFilter(filterParams.merkleRoot, filterParams.encodedTokenIDs);
        require(
            pool.acceptsTokenIDs(params.initialNFTIDs, filterParams.initialProof, filterParams.initialProofFlags),
            "NFT not allowed"
        );
        pool.setExternalFilter(address(filterParams.externalFilter));

        _initializePoolETH(pool, params);
    }

    function createPoolERC20(CreateERC20PoolParams calldata params)
        external
        whenCreationNotPaused
        returns (ICollectionPool pool, uint256 tokenId)
    {
        address is777 = _ERC1820_REGISTRY.getInterfaceImplementer(address(params.token), keccak256("ERC777Token"));
        require(is777 == address(0), "ERC777 not supported");

        (pool, tokenId) = _createPoolERC20(params);

        _initializePoolERC20(pool, params);
    }

    function createPoolERC20Filtered(CreateERC20PoolParams calldata params, NFTFilterParams calldata filterParams)
        external
        whenCreationNotPaused
        returns (ICollectionPool pool, uint256 tokenId)
    {
        address is777 = _ERC1820_REGISTRY.getInterfaceImplementer(address(params.token), keccak256("ERC777Token"));
        require(is777 == address(0), "ERC777 not supported");

        (pool, tokenId) = _createPoolERC20(params);

        // Check if nfts are allowed before initializing to save gas on transferring nfts on revert.
        // If not, we could re-use createPoolERC20 and check later.
        pool.setTokenIDFilter(filterParams.merkleRoot, filterParams.encodedTokenIDs);
        require(
            pool.acceptsTokenIDs(params.initialNFTIDs, filterParams.initialProof, filterParams.initialProofFlags),
            "NFT not allowed"
        );
        pool.setExternalFilter(address(filterParams.externalFilter));

        _initializePoolERC20(pool, params);
    }

    /**
     * @notice Update royalty bookkeeping in `token` currency according to
     * `royaltiesDue`. Only callable by pools
     * @param token The ERC20 token that royalties are in. ERC20(address(0)) for ETH
     * @param royaltiesDue An array of the recipients and amounts due each address
     * @param poolVariant the variant of the pool being interacted with
     */
    function depositRoyaltiesNotification(ERC20 token, RoyaltyDue[] calldata royaltiesDue, PoolVariant poolVariant)
        external
        payable
        whenOthersNotPaused
    {
        require(isPoolVariant(msg.sender, poolVariant), "Not pool");
        uint256 length = royaltiesDue.length;
        uint256 amountRequired;
        // Do internal bookkeeping of who can claim portions of the tokens about
        // to be transferred in
        for (uint256 i; i < length;) {
            address recipient = royaltiesDue[i].recipient;
            if (msg.sender != recipient) {
                amountRequired += royaltiesDue[i].amount;
                royaltiesClaimable[royaltiesDue[i].recipient][token] += royaltiesDue[i].amount;
            }

            unchecked {
                ++i;
            }
        }

        royaltiesStored[token] += amountRequired;
    }

    /*
     * @notice NFTs that don't match filter and any airdropped assets  must be rescued prior to calling this function.
     * Requires LP token owner to give allowance to this factory contract for asset withdrawals
     * which are sent directly to the LP token owner.
     */
    function burn(uint256 tokenId) external nonReentrant {
        require(_isApprovedOrOwner(msg.sender, tokenId), "Not approved");
        ICollectionPool pool = poolOf(tokenId);
        PoolVariant poolVariant = pool.poolVariant();

        // withdraw all ETH / ERC20
        if (poolVariant == PoolVariant.ENUMERABLE_ETH || poolVariant == PoolVariant.MISSING_ENUMERABLE_ETH) {
            // withdraw ETH, sent to owner of LP token
            CollectionPoolETH(payable(address(pool))).withdrawAllETH();
        } else if (poolVariant == PoolVariant.ENUMERABLE_ERC20 || poolVariant == PoolVariant.MISSING_ENUMERABLE_ERC20) {
            // withdraw ERC20
            CollectionPoolERC20(address(pool)).withdrawAllERC20();
        }
        // then withdraw NFTs
        pool.withdrawERC721(pool.nft(), pool.getAllHeldIds());

        _burn(tokenId);
    }

    /**
     * Admin functions. Not pausable. Pointless because pauser/unpauser is owner
     * and all admin functions are onlyOwner.
     */

    function pauseCreation() external onlyOwner {
        pause(CREATION_PAUSE);
        emit CreationPause();
    }

    function unpauseCreation() external onlyOwner {
        unpause(CREATION_PAUSE);
        emit CreationUnpause();
    }

    function pauseSwap() external onlyOwner {
        pause(SWAP_PAUSE);
        emit SwapPause();
    }

    function unpauseSwap() external onlyOwner {
        unpause(SWAP_PAUSE);
        emit SwapUnpause();
    }

    function pauseDeposit() external onlyOwner {
        pause(DEPOSIT_PAUSE);
        emit DepositPause();
    }

    function unpauseDeposit() external onlyOwner {
        unpause(DEPOSIT_PAUSE);
        emit DepositUnpause();
    }

    function pauseOthers() external onlyOwner {
        pause(OTHERS_PAUSE);
        emit OthersPause();
    }

    function unpauseOthers() external onlyOwner {
        unpause(OTHERS_PAUSE);
        emit OthersUnpause();
    }

    /**
     * Withdrawal functions. Not pausable.
     */

    /**
     * @notice Withdraw all `token` royalties awardable to `recipient`. If the
     * zero address is passed as `token`, then ETH royalties are paid. Does not
     * use msg.sender so this function can be called on behalf of contract
     * royalty recipients
     */
    function withdrawRoyalties(address payable recipient, ERC20 token) external {
        uint256 totalRoyaltiesWithdrawn;
        if (address(token) == address(0)) {
            totalRoyaltiesWithdrawn = withdrawETHRoyalties(recipient);
        } else {
            totalRoyaltiesWithdrawn = withdrawERC20Royalties(recipient, token);
        }

        royaltiesStored[token] -= totalRoyaltiesWithdrawn;
    }

    /**
     * @dev Internal helper function for withdrawing ETH royalties for a single
     * address. Does NOT update `royaltiesStored[token]`
     */
    function withdrawETHRoyalties(address payable recipient) internal returns (uint256 amount) {
        amount = royaltiesClaimable[recipient][ERC20(address(0))];
        royaltiesClaimable[recipient][ERC20(address(0))] = 0;
        recipient.safeTransferETH(amount);
    }

    /**
     * @dev Internal helper function for withdrawing ERC20 royalties for a single
     * address. Does NOT update `royaltiesStored[token]`
     */
    function withdrawERC20Royalties(address payable recipient, ERC20 token) internal returns (uint256 amount) {
        require(address(token) != address(0), "Use withdrawETHRoyalties instead");
        amount = royaltiesClaimable[recipient][token];
        royaltiesClaimable[recipient][token] = 0;
        token.safeTransfer(recipient, amount);
    }

    /**
     * @dev Uses the internal helper functions to batch writes to `royaltiesStored`
     */
    function withdrawRoyaltiesMultipleRecipients(address payable[] calldata recipients, ERC20 token) public {
        uint256 length = recipients.length;
        uint256 totalRoyaltiesWithdrawn;
        uint256 amount;
        if (address(token) == address(0)) {
            for (uint256 i; i < length;) {
                amount = withdrawETHRoyalties(recipients[i]);
                totalRoyaltiesWithdrawn += amount;

                unchecked {
                    ++i;
                }
            }
        } else {
            for (uint256 i; i < length;) {
                amount = withdrawERC20Royalties(recipients[i], token);
                totalRoyaltiesWithdrawn += amount;

                unchecked {
                    ++i;
                }
            }
        }

        royaltiesStored[token] -= totalRoyaltiesWithdrawn;
    }

    function withdrawRoyaltiesMultipleCurrencies(address payable recipient, ERC20[] calldata tokens) external {
        uint256 length = tokens.length;
        uint256 amount;
        for (uint256 i; i < length; i++) {
            ERC20 token = tokens[i];
            amount = royaltiesClaimable[recipient][token];
            royaltiesClaimable[recipient][token] = 0;

            /// @dev Need to repeat this check for every token iterated over
            if (address(token) == address(0)) {
                recipient.safeTransferETH(amount);
            } else {
                token.safeTransfer(recipient, amount);
            }

            royaltiesStored[token] -= amount;

            unchecked {
                ++i;
            }
        }
    }

    /**
     * @notice Withdraw royalties for ALL combinations of recipients and tokens
     * in the given arguments
     *
     * @dev Iterate over tokens as outer loop to reduce stores/loads to `royaltiesStored`
     * and the number of `address(token) == address(0)` condition checks from
     * O(m * n) to O(n)
     */
    function withdrawRoyaltiesMultipleRecipientsAndCurrencies(
        address payable[] calldata recipients,
        ERC20[] calldata tokens
    ) external {
        uint256 length = tokens.length;
        for (uint256 i; i < length; i++) {
            ERC20 token = tokens[i];
            withdrawRoyaltiesMultipleRecipients(recipients, token);

            unchecked {
                ++i;
            }
        }
    }

    /**
     * @notice Withdraws the ETH balance to the protocol fee recipient.
     * Only callable by the owner.
     */
    function withdrawETHProtocolFees() external onlyOwner {
        protocolFeeRecipient.safeTransferETH(address(this).balance - royaltiesStored[ERC20(address(0))]);
    }

    /**
     * @notice Withdraws ERC20 tokens to the protocol fee recipient. Only callable by the owner.
     * @param token The token to transfer
     */
    function withdrawERC20ProtocolFees(ERC20 token) external onlyOwner {
        token.safeTransfer(protocolFeeRecipient, token.balanceOf(address(this)) - royaltiesStored[token]);
    }

    /**
     * @notice Changes the protocol fee recipient address. Only callable by the owner.
     * @param _protocolFeeRecipient The new fee recipient
     */
    function changeProtocolFeeRecipient(address payable _protocolFeeRecipient) external onlyOwner {
        require(_protocolFeeRecipient != address(0), "0 address");
        if (protocolFeeRecipient != _protocolFeeRecipient) {
            protocolFeeRecipient = _protocolFeeRecipient;
            emit ProtocolFeeRecipientUpdate(_protocolFeeRecipient);
        }
    }

    /**
     * @notice Changes the protocol fee multiplier. Only callable by the owner.
     * @param _protocolFeeMultiplier The new fee multiplier, 18 decimals
     */
    function changeProtocolFeeMultiplier(uint24 _protocolFeeMultiplier) external onlyOwner {
        require(_protocolFeeMultiplier <= MAX_PROTOCOL_FEE, "Fee too large");
        if (protocolFeeMultiplier != _protocolFeeMultiplier) {
            protocolFeeMultiplier = _protocolFeeMultiplier;
            emit ProtocolFeeMultiplierUpdate(_protocolFeeMultiplier);
        }
    }

    /**
     * @notice Changes the carry fee multiplier. Only callable by the owner.
     * @param _carryFeeMultiplier The new fee multiplier, 18 decimals
     */
    function changeCarryFeeMultiplier(uint24 _carryFeeMultiplier) external onlyOwner {
        require(_carryFeeMultiplier <= MAX_CARRY_FEE, "Fee too large");
        if (carryFeeMultiplier != _carryFeeMultiplier) {
            carryFeeMultiplier = _carryFeeMultiplier;
            emit CarryFeeMultiplierUpdate(_carryFeeMultiplier);
        }
    }

    /**
     * @notice Sets the whitelist status of a bonding curve contract. Only callable by the owner.
     * @param bondingCurve The bonding curve contract
     * @param isAllowed True to whitelist, false to remove from whitelist
     */
    function setBondingCurveAllowed(ICurve bondingCurve, bool isAllowed) external onlyOwner {
        bool wasAllowed = bondingCurveAllowed[bondingCurve];
        if (wasAllowed != isAllowed) {
            bondingCurveAllowed[bondingCurve] = isAllowed;
            emit BondingCurveStatusUpdate(bondingCurve, isAllowed);
        }
    }

    /**
     * @notice Sets the whitelist status of a contract to be called arbitrarily by a pool.
     * Only callable by the owner.
     * @param target The target contract
     * @param isAllowed True to whitelist, false to remove from whitelist
     */
    function setCallAllowed(address payable target, bool isAllowed) external onlyOwner {
        // ensure target is not / was not ever a router
        if (isAllowed) {
            require(!routerStatus[CollectionRouter(target)].wasEverAllowed, "Can't call router");
        }

        bool wasAllowed = callAllowed[target];
        if (wasAllowed != isAllowed) {
            callAllowed[target] = isAllowed;
            emit CallTargetStatusUpdate(target, isAllowed);
        }
    }

    /**
     * @notice Updates the router whitelist. Only callable by the owner.
     * @param _router The router
     * @param isAllowed True to whitelist, false to remove from whitelist
     */
    function setRouterAllowed(CollectionRouter _router, bool isAllowed) external onlyOwner {
        // ensure target is not arbitrarily callable by pools
        if (isAllowed) {
            require(!callAllowed[address(_router)], "Can't call router");
        }

        bool wasAllowed = routerStatus[_router].allowed;
        if (wasAllowed != isAllowed) {
            routerStatus[_router] = RouterStatus({allowed: isAllowed, wasEverAllowed: true});
            emit RouterStatusUpdate(_router, isAllowed);
        }
    }

    function setBaseURI(string calldata _uri) external onlyOwner {
        baseURI = _uri;
    }

    function setTokenURI(string calldata _uri, uint256 tokenId) external onlyOwner {
        _setTokenURI(tokenId, _uri);
    }

    /**
     * Internal functions
     */
    function _baseURI() internal view override returns (string memory) {
        return baseURI;
    }

    function _createPoolETH(CreateETHPoolParams calldata params)
        internal
        returns (ICollectionPool pool, uint256 tokenId)
    {
        require(bondingCurveAllowed[params.bondingCurve], "Bonding curve not whitelisted");

        require(
            validRoyaltyState(params.royaltyNumerator, params.royaltyRecipientFallback, params.nft),
            "Nonzero royalty for non ERC2981 without fallback"
        );

        // Check to see if the NFT supports Enumerable to determine which template to use
        address template;
        try IERC165(address(params.nft)).supportsInterface(INTERFACE_ID_ERC721_ENUMERABLE) returns (bool isEnumerable) {
            template = isEnumerable ? address(enumerableETHTemplate) : address(missingEnumerableETHTemplate);
        } catch {
            template = address(missingEnumerableETHTemplate);
        }

        address poolAddress = template.cloneETHPool(this, params.bondingCurve, params.nft, uint8(params.poolType));

        // issue new token
        tokenId = mint(params.receiver, CollectionPool(poolAddress));

        emit NewPool(address(params.nft), poolAddress);

        return (ICollectionPool(poolAddress), tokenId);
    }

    function _initializePoolETH(ICollectionPool _pool, CreateETHPoolParams calldata _params) internal {
        // initialize pool
        _pool.initialize(
            _params.assetRecipient,
            _params.delta,
            _params.fee,
            _params.spotPrice,
            _params.props,
            _params.state,
            _params.royaltyNumerator,
            _params.royaltyRecipientFallback
        );

        // transfer initial ETH to pool
        payable(address(_pool)).safeTransferETH(msg.value);

        // transfer initial NFTs from sender to pool and notify pool
        _depositNFTs(_params.nft, _params.initialNFTIDs, _pool);
    }

    function _createPoolERC20(CreateERC20PoolParams calldata params)
        internal
        returns (ICollectionPool pool, uint256 tokenId)
    {
        require(bondingCurveAllowed[params.bondingCurve], "Bonding curve not whitelisted");

        require(
            validRoyaltyState(params.royaltyNumerator, params.royaltyRecipientFallback, params.nft),
            "Nonzero royalty for non ERC2981 without fallback"
        );

        // Check to see if the NFT supports Enumerable to determine which template to use
        address template;
        try IERC165(address(params.nft)).supportsInterface(INTERFACE_ID_ERC721_ENUMERABLE) returns (bool isEnumerable) {
            template = isEnumerable ? address(enumerableERC20Template) : address(missingEnumerableERC20Template);
        } catch {
            template = address(missingEnumerableERC20Template);
        }

        address poolAddress =
            template.cloneERC20Pool(this, params.bondingCurve, params.nft, uint8(params.poolType), params.token);

        // issue new token
        tokenId = mint(params.receiver, CollectionPool(poolAddress));

        emit NewPool(address(params.nft), poolAddress);

        return (ICollectionPool(poolAddress), tokenId);
    }

    function _initializePoolERC20(ICollectionPool _pool, CreateERC20PoolParams calldata _params) internal {
        // initialize pool
        _pool.initialize(
            _params.assetRecipient,
            _params.delta,
            _params.fee,
            _params.spotPrice,
            _params.props,
            _params.state,
            _params.royaltyNumerator,
            _params.royaltyRecipientFallback
        );

        // transfer initial tokens to pool
        _params.token.safeTransferFrom(msg.sender, address(_pool), _params.initialTokenBalance);

        // transfer initial NFTs from sender to pool and notify pool
        _depositNFTs(_params.nft, _params.initialNFTIDs, _pool);
    }

    /**
     * @dev Transfers NFTs from sender and notifies pool. `ids` must already have been verified
     */
    function _depositNFTs(IERC721 _nft, uint256[] calldata nftIds, ICollectionPool pool) internal {
        // transfer NFTs from caller to recipient
        TransferLib.bulkSafeTransferERC721From(_nft, msg.sender, address(pool), nftIds);
        pool.depositNFTsNotification(nftIds);
    }

    /*
     * @dev Mints LP token using pool address as Token ID
     */
    function mint(address recipient, CollectionPool pool) internal returns (uint256 tokenId) {
        tokenId = uint160(address(pool));
        _safeMint(recipient, tokenId);
    }

    function validRoyaltyState(uint24 royaltyNumerator, address royaltyRecipientFallback, IERC721 nft)
        internal
        view
        returns (bool)
    {
        return royaltyNumerator == 0 || royaltyRecipientFallback != address(0)
            || IERC165(nft).supportsInterface(_INTERFACE_ID_ERC2981);
    }

    /**
     * Required override functions
     */

    /**
     * @notice Allows receiving ETH in order to receive protocol fees
     */
    receive() external payable {
        /**
         * No logic needed here:
         * - Factory needs to accept all transfers so that swaps don't fail to send token to
         * appropriate recipients when paying protocol fees/royalties.
         * - No bookkeeping is needed upon ETH receipt.
         */
    }

    function _burn(uint256 tokenId) internal override (ERC721, ERC721URIStorage) {
        super._burn(tokenId);
    }
}

File 2 of 52 : TransferLib.sol
pragma solidity ^0.8.0;

import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";

/**
 * A helper library for common transfer methods such as transferring multiple token ids of the same collection, or multiple single token id of one collection.
 */
library TransferLib {
    using SafeERC20 for IERC20;

    /**
     * @notice Safe transfer N token ids of 1 ERC721
     */
    function bulkSafeTransferERC721From(IERC721 token, address from, address to, uint256[] calldata tokenIds)
        internal
    {
        uint256 length = tokenIds.length;
        for (uint256 i; i < length;) {
            token.safeTransferFrom(from, to, tokenIds[i]);
            unchecked {
                ++i;
            }
        }
    }
}

File 3 of 52 : ReentrancyGuard.sol
// SPDX-License-Identifier: MIT
// Forked from OpenZeppelin Contracts v4.4.1 (security/ReentrancyGuard.sol), 
// to use a custom error

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;

    error Reentrancy();

    function __ReentrancyGuard_init() internal {
      _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
        if (_status == _ENTERED) revert Reentrancy();

        // 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 4 of 52 : ICollectionPool.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {ERC20} from "solmate/src/tokens/ERC20.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import {PoolVariant, PoolType, RoyaltyDue, NFTs} from "./CollectionStructsAndEnums.sol";
import {ICurve} from "../bonding-curves/ICurve.sol";
import {ITokenIDFilter} from "../filter/ITokenIDFilter.sol";
import {IExternalFilter} from "../filter/IExternalFilter.sol";

interface ICollectionPool is ITokenIDFilter {
    function bondingCurve() external view returns (ICurve);

    function curveParams() external view returns (ICurve.Params memory params);

    /**
     * @notice Only tracked IDs are returned
     */
    function getAllHeldIds() external view returns (uint256[] memory);

    function poolVariant() external view returns (PoolVariant);

    function fee() external view returns (uint24);

    function nft() external view returns (IERC721);

    function poolType() external view returns (PoolType);

    function royaltyNumerator() external view returns (uint24);

    function externalFilter() external view returns (IExternalFilter);

    /**
     * @notice The usable balance of the pool. This is the amount the pool needs to have to buy NFTs and pay out royalties.
     */
    function liquidity() external view returns (uint256);

    function balanceToFulfillSellNFT(uint256 numNFTs) external view returns (uint256 balance);

    /**
     * @notice Rescues a specified set of NFTs owned by the pool to the owner address. (onlyOwnable modifier is in the implemented function)
     * @dev If the NFT is the pool's collection, we also remove it from the id tracking
     * @param a The NFT to transfer
     * @param nftIds The list of IDs of the NFTs to send to the owner
     */
    function withdrawERC721(IERC721 a, uint256[] calldata nftIds) external;

    /**
     * @notice Rescues ERC20 tokens from the pool to the owner. Only callable by the owner (onlyOwnable modifier is in the implemented function).
     * @param a The token to transfer
     * @param amount The amount of tokens to send to the owner
     */
    function withdrawERC20(ERC20 a, uint256 amount) external;

    function withdrawERC1155(IERC1155 a, uint256[] calldata ids, uint256[] calldata amounts) external;

    function getSellNFTQuote(uint256 numNFTs)
        external
        view
        returns (ICurve.Params memory newParams, uint256 totalAmount, uint256 outputAmount, ICurve.Fees memory fees);

    /**
     * @dev Deposit NFTs into pool and emit event for indexing.
     */
    function depositNFTs(uint256[] calldata ids, bytes32[] calldata proof, bool[] calldata proofFlags) external;

    /**
     * @dev Used by factory to indicate deposited NFTs.
     * @dev Must only be called by factory. NFT IDs must have been validated against the filter.
     */
    function depositNFTsNotification(uint256[] calldata nftIds) external;

    /**
     * @notice Returns number of NFTs in pool that matches filter
     */
    function NFTsCount() external view returns (uint256);

    /**
     * @notice Sets NFT token ID filter to allow only some NFTs into this pool. Pool must be empty
     * to call this function. This filter is checked on deposits and swapping NFTs into the pool.
     * Selling into the pool may require an additional check (see `setExternalFilter`).
     * @param merkleRoot Merkle root representing all allowed IDs
     * @param encodedTokenIDs Opaque encoded list of token IDs
     */
    function setTokenIDFilter(bytes32 merkleRoot, bytes calldata encodedTokenIDs) external;

    /**
     * @notice Checks if list of NFTs are allowed in this pool using Merkle multiproof and flags
     * @param tokenIDs List of NFT IDs
     * @param proof Merkle multiproof
     * @param proofFlags Merkle multiproof flags
     */
    function acceptsTokenIDs(uint256[] calldata tokenIDs, bytes32[] calldata proof, bool[] calldata proofFlags)
        external
        view
        returns (bool);

    /**
     * @notice Sets an external contract that is consulted before any NFT is swapped into the pool.
     * Typically used to implement dynamic blocklists. Because it is dynamic, deposits are not
     * checked. See also `setTokenIDFilter`.
     */
    function setExternalFilter(address provider) external;

    /**
     * @notice Called during pool creation to set initial parameters
     * @dev Only called once by factory to initialize.
     * We verify this by making sure that the current owner is address(0).
     * The Ownable library we use disallows setting the owner to be address(0), so this condition
     * should only be valid before the first initialize call.
     * @param _assetRecipient The address that will receive the TOKEN or NFT sent to this pool during swaps.
     * NOTE: If set to address(0), they will go to the pool itself.
     * @param _delta The initial delta of the bonding curve
     * @param _fee The initial % fee taken, if this is a trade pool
     * @param _spotPrice The initial price to sell an asset into the pool
     * @param _royaltyNumerator All trades will result in `royaltyNumerator` * <trade amount> / 1e6
     * being sent to the account to which the traded NFT's royalties are awardable.
     * Must be 0 if `_nft` is not IERC2981 and no recipient fallback is set.
     * @param _royaltyRecipientFallback An address to which all royalties will be paid to if not address(0).
     * This is a fallback to ERC2981 royalties set by the NFT creator, and allows sending royalties to
     * arbitrary addresses if a collection does not support ERC2981.
     */
    function initialize(
        address payable _assetRecipient,
        uint128 _delta,
        uint24 _fee,
        uint128 _spotPrice,
        bytes calldata _props,
        bytes calldata _state,
        uint24 _royaltyNumerator,
        address payable _royaltyRecipientFallback
    ) external payable;

    /**
     * @notice Sends token to the pool in exchange for any `numNFTs` NFTs
     * @dev To compute the amount of token to send, call bondingCurve.getBuyInfo.
     * This swap function is meant for users who are ID agnostic
     * @dev The nonReentrant modifier is in swapTokenForSpecificNFTs
     * @param numNFTs The number of NFTs to purchase
     * @param maxExpectedTokenInput The maximum acceptable cost from the sender. If the actual
     * amount is greater than this value, the transaction will be reverted.
     * @param nftRecipient The recipient of the NFTs
     * @param isRouter True if calling from CollectionRouter, false otherwise. Not used for
     * ETH pools.
     * @param routerCaller If isRouter is true, ERC20 tokens will be transferred from this address. Not used for
     * ETH pools.
     * @return inputAmount The amount of token used for purchase
     */
    function swapTokenForAnyNFTs(
        uint256 numNFTs,
        uint256 maxExpectedTokenInput,
        address nftRecipient,
        bool isRouter,
        address routerCaller
    ) external payable returns (uint256 inputAmount);

    /**
     * @dev Used as read function to query the bonding curve for buy pricing info
     * @param numNFTs The number of NFTs to buy from the pool
     */
    function getBuyNFTQuote(uint256 numNFTs)
        external
        view
        returns (ICurve.Params memory newParams, uint256 totalAmount, uint256 inputAmount, ICurve.Fees memory fees);

    /**
     * @notice Updates the fee taken by the LP. Only callable by the owner.
     * Only callable if the pool is a Trade pool. Reverts if the fee is >=
     * MAX_FEE.
     * @param newFee The new LP fee percentage, 18 decimals
     */
    function changeFee(uint24 newFee) external;

    /**
     * @notice Sends a set of NFTs to the pool in exchange for token. Token must be allowed by
     * filters, see `setTokenIDFilter` and `setExternalFilter`.
     * @dev To compute the amount of token to that will be received, call bondingCurve.getSellInfo.
     * @param nfts The list of IDs of the NFTs to sell to the pool along with its Merkle multiproof.
     * @param minExpectedTokenOutput The minimum acceptable token received by the sender. If the actual
     * amount is less than this value, the transaction will be reverted.
     * @param tokenRecipient The recipient of the token output
     * @param isRouter True if calling from CollectionRouter, false otherwise. Not used for
     * ETH pools.
     * @param routerCaller If isRouter is true, ERC20 tokens will be transferred from this address. Not used for
     * ETH pools.
     * @return outputAmount The amount of token received
     */
    function swapNFTsForToken(
        NFTs calldata nfts,
        uint256 minExpectedTokenOutput,
        address payable tokenRecipient,
        bool isRouter,
        address routerCaller,
        bytes calldata externalFilterContext
    ) external returns (uint256 outputAmount);
}

interface ICollectionPoolETH is ICollectionPool {
    function withdrawAllETH() external;
}

File 5 of 52 : CollectionPoolETH.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {ERC20} from "solmate/src/tokens/ERC20.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {SafeTransferLib} from "solmate/src/utils/SafeTransferLib.sol";
import {ICollectionPool} from "./ICollectionPool.sol";
import {CollectionPool} from "./CollectionPool.sol";
import {ICollectionPoolFactory} from "./ICollectionPoolFactory.sol";
import {ICurve} from "../bonding-curves/ICurve.sol";
import {IPoolActivityMonitor} from "./IPoolActivityMonitor.sol";
import {CollectionRouter} from "../routers/CollectionRouter.sol";
import {RoyaltyDue, EventType} from "./CollectionStructsAndEnums.sol";

/**
 * @title An NFT/Token pool where the token is ETH
 * @author Collection
 */
abstract contract CollectionPoolETH is CollectionPool {
    using SafeTransferLib for address payable;
    using SafeTransferLib for ERC20;

    uint256 internal constant IMMUTABLE_PARAMS_LENGTH = 65;

    /// @inheritdoc ICollectionPool
    function liquidity() public view returns (uint256) {
        uint256 _balance = address(this).balance;
        uint256 _accruedTradeFee = accruedTradeFee;
        if (_balance < _accruedTradeFee) revert InsufficientLiquidity(_balance, _accruedTradeFee);

        return _balance - _accruedTradeFee;
    }

    /// @inheritdoc CollectionPool
    function _pullTokenInputAndPayProtocolFee(
        uint256 inputAmount,
        bool, /*isRouter*/
        address, /*routerCaller*/
        ICollectionPoolFactory _factory,
        uint256 protocolFee,
        RoyaltyDue[] memory royaltiesDue
    ) internal override {
        require(msg.value >= inputAmount, "Sent too little ETH");

        // Pay royalties first to obtain total amount of royalties paid
        (uint256 totalRoyaltiesPaid,) = _payRoyaltiesAndProtocolFee(_factory, royaltiesDue, protocolFee);

        // Transfer inputAmount ETH to assetRecipient if it's been set
        address payable _assetRecipient = getAssetRecipient();
        if (_assetRecipient != address(this)) {
            _assetRecipient.safeTransferETH(inputAmount - protocolFee - totalRoyaltiesPaid);
        }
    }

    /// @inheritdoc CollectionPool
    function _refundTokenToSender(uint256 inputAmount) internal override {
        // Give excess ETH back to caller
        if (msg.value > inputAmount) {
            payable(msg.sender).safeTransferETH(msg.value - inputAmount);
        }
    }

    /**
     * @notice Pay royalties to the factory, which should never revert. The factory
     * serves as a single contract to which royalty recipients can make a single
     * transaction to receive all royalties due as opposed to having to send
     * transactions to arbitrary numbers of pools
     *
     * In addition, pay protocol fees in the same transfer since both go to factory
     *
     * @return totalRoyaltiesPaid The amount of royalties which were paid including
     * royalties whose resolved recipient is this contract itself
     * @return royaltiesSentToFactory `totalRoyaltiesPaid` less the amount whose
     * resolved recipient is this contract itself
     */
    function _payRoyaltiesAndProtocolFee(
        ICollectionPoolFactory _factory,
        RoyaltyDue[] memory royaltiesDue,
        uint256 protocolFee
    ) internal returns (uint256 totalRoyaltiesPaid, uint256 royaltiesSentToFactory) {
        /// @dev For ETH pools, calculate how much to send in total since factory
        /// can't call safeTransferFrom.
        uint256 length = royaltiesDue.length;
        for (uint256 i = 0; i < length;) {
            uint256 royaltyAmount = royaltiesDue[i].amount;
            if (royaltyAmount > 0) {
                totalRoyaltiesPaid += royaltyAmount;
                address finalRecipient = getRoyaltyRecipient(payable(royaltiesDue[i].recipient));
                if (finalRecipient != address(this)) {
                    royaltiesSentToFactory += royaltyAmount;
                    royaltiesDue[i].recipient = finalRecipient;
                }
            }

            unchecked {
                ++i;
            }
        }

        _factory.depositRoyaltiesNotification{value: royaltiesSentToFactory + protocolFee}(
            ERC20(address(0)), royaltiesDue, poolVariant()
        );
    }

    /// @inheritdoc CollectionPool
    function _sendTokenOutputAndPayProtocolFees(
        ICollectionPoolFactory _factory,
        address payable tokenRecipient,
        uint256 outputAmount,
        RoyaltyDue[] memory royaltiesDue,
        uint256 protocolFee
    ) internal override {
        /// @dev Pay royalties and protocol fee
        _payRoyaltiesAndProtocolFee(_factory, royaltiesDue, protocolFee);

        /// @dev Send ETH to caller
        if (outputAmount > 0) {
            require(liquidity() >= outputAmount, "Too little ETH");
            tokenRecipient.safeTransferETH(outputAmount);
        }
    }

    /// @inheritdoc CollectionPool
    // @dev see CollectionPoolCloner for params length calculation
    function _immutableParamsLength() internal pure override returns (uint256) {
        return IMMUTABLE_PARAMS_LENGTH;
    }

    /**
     * @notice Withdraws all token owned by the pool to the owner address.
     * @dev Only callable by the owner.
     */
    function withdrawAllETH() external {
        requireAuthorized();
        uint256 _accruedTradeFee = accruedTradeFee;
        accruedTradeFee = 0;

        uint256 amount = address(this).balance;
        payable(owner()).safeTransferETH(amount);

        if (_accruedTradeFee >= amount) {
            _accruedTradeFee = amount;
            amount = 0;
        } else {
            amount -= _accruedTradeFee;
        }

        // emit event since ETH is the pool token
        IERC721 _nft = nft();
        emit TokenWithdrawal(_nft, ERC20(address(0)), amount);
        emit AccruedTradeFeeWithdrawal(_nft, ERC20(address(0)), _accruedTradeFee);
    }

    /**
     * @notice Withdraws a specified amount of token owned by the pool to the owner address.
     * @dev Only callable by the owner.
     * @param amount The amount of token to send to the owner. If the pool's balance is less than
     * this value, the transaction will be reverted.
     */
    function withdrawETH(uint256 amount) external {
        requireAuthorized();
        require(liquidity() >= amount, "Too little ETH");

        payable(owner()).safeTransferETH(amount);

        // emit event since ETH is the pool token
        emit TokenWithdrawal(nft(), ERC20(address(0)), amount);
    }

    /// @inheritdoc ICollectionPool
    function withdrawERC20(ERC20 a, uint256 amount) external {
        requireAuthorized();
        a.safeTransfer(owner(), amount);
    }

    /// @inheritdoc CollectionPool
    function withdrawAccruedTradeFee() external override onlyOwner {
        uint256 _accruedTradeFee = accruedTradeFee;
        if (_accruedTradeFee > 0) {
            accruedTradeFee = 0;

            payable(owner()).safeTransferETH(_accruedTradeFee);

            // emit event since ETH is the pool token
            emit AccruedTradeFeeWithdrawal(nft(), ERC20(address(0)), _accruedTradeFee);
        }
    }

    /**
     * @dev All ETH transfers into the pool are accepted. This is the main method
     * for the owner to top up the pool's token reserves.
     */
    receive() external payable {
        assertDepositsNotPaused();
        emit TokenDeposit(nft(), ERC20(address(0)), msg.value);
        notifyDeposit(EventType.DEPOSIT_TOKEN, msg.value);
    }

    /**
     * @dev All ETH transfers into the pool are accepted. This is the main method
     * for the owner to top up the pool's token reserves.
     */
    fallback() external payable {
        assertDepositsNotPaused();
        // Only allow calls without function selector
        require(msg.data.length == _immutableParamsLength());
        emit TokenDeposit(nft(), ERC20(address(0)), msg.value);
        notifyDeposit(EventType.DEPOSIT_TOKEN, msg.value);
    }
}

File 6 of 52 : CollectionPoolCloner.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";

import {ERC20} from "solmate/src/tokens/ERC20.sol";

import {ICurve} from "../bonding-curves/ICurve.sol";
import {ICollectionPoolFactory} from "../pools/ICollectionPoolFactory.sol";

library CollectionPoolCloner {
    /**
     * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
     *
     * This function uses the create opcode, which should never revert.
     *
     * During the delegate call, extra data is copied into the calldata which can then be
     * accessed by the implementation contract.
     */
    function cloneETHPool(
        address implementation,
        ICollectionPoolFactory factory,
        ICurve bondingCurve,
        IERC721 nft,
        uint8 poolType
    ) internal returns (address instance) {
        uint256 blockNumber = block.number;

        assembly {
            let ptr := mload(0x40)

            // -------------------------------------------------------------------------------------------------------------
            // CREATION (9 bytes)
            // -------------------------------------------------------------------------------------------------------------

            // creation size = 09
            // runtime size = 76
            // 60 runtime  | PUSH1 runtime (r)     | r                       | –
            // 3d          | RETURNDATASIZE        | 0 r                     | –
            // 81          | DUP2                  | r 0 r                   | –
            // 60 creation | PUSH1 creation (c)    | c r 0 r                 | –
            // 3d          | RETURNDATASIZE        | 0 c r 0 r               | –
            // 39          | CODECOPY              | 0 r                     | [0-runSize): runtime code
            // f3          | RETURN                |                         | [0-runSize): runtime code

            // -------------------------------------------------------------------------------------------------------------
            // RUNTIME (53 bytes of code + 65 bytes of extra data = 118 bytes)
            // -------------------------------------------------------------------------------------------------------------

            // extra data size = 41
            // 3d          | RETURNDATASIZE        | 0                       | –
            // 3d          | RETURNDATASIZE        | 0 0                     | –
            // 3d          | RETURNDATASIZE        | 0 0 0                   | –
            // 3d          | RETURNDATASIZE        | 0 0 0 0                 | –
            // 36          | CALLDATASIZE          | cds 0 0 0 0             | –
            // 3d          | RETURNDATASIZE        | 0 cds 0 0 0 0           | –
            // 3d          | RETURNDATASIZE        | 0 0 cds 0 0 0 0         | –
            // 37          | CALLDATACOPY          | 0 0 0 0                 | [0, cds) = calldata
            // 60 extra    | PUSH1 extra           | extra 0 0 0 0           | [0, cds) = calldata
            // 60 0x35     | PUSH1 0x35            | 0x35 extra 0 0 0 0      | [0, cds) = calldata // 0x35 (53) is runtime size - data
            // 36          | CALLDATASIZE          | cds 0x35 extra 0 0 0 0  | [0, cds) = calldata
            // 39          | CODECOPY              | 0 0 0 0                 | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 36          | CALLDATASIZE          | cds 0 0 0 0             | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 60 extra    | PUSH1 extra           | extra cds 0 0 0 0       | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 01          | ADD                   | cds+extra 0 0 0 0       | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 3d          | RETURNDATASIZE        | 0 cds 0 0 0 0           | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 73 addr     | PUSH20 0x123…         | addr 0 cds 0 0 0 0      | [0, cds) = calldata, [cds, cds+0x35) = extraData
            mstore(
                ptr,
                hex"60_76_3d_81_60_09_3d_39_f3_3d_3d_3d_3d_36_3d_3d_37_60_41_60_35_36_39_36_60_41_01_3d_73_00_00_00"
            )
            mstore(add(ptr, 0x1d), shl(0x60, implementation))

            // 5a          | GAS                   | gas addr 0 cds 0 0 0 0  | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // f4          | DELEGATECALL          | success 0 0             | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 3d          | RETURNDATASIZE        | rds success 0 0         | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 3d          | RETURNDATASIZE        | rds rds success 0 0     | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 93          | SWAP4                 | 0 rds success 0 rds     | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 80          | DUP1                  | 0 0 rds success 0 rds   | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 3e          | RETURNDATACOPY        | success 0 rds           | [0, rds) = return data (there might be some irrelevant leftovers in memory [rds, cds+0x37) when rds < cds+0x37)
            // 60 0x33     | PUSH1 0x33            | 0x33 sucess 0 rds       | [0, rds) = return data
            // 57          | JUMPI                 | 0 rds                   | [0, rds) = return data
            // fd          | REVERT                | –                       | [0, rds) = return data
            // 5b          | JUMPDEST              | 0 rds                   | [0, rds) = return data
            // f3          | RETURN                | –                       | [0, rds) = return data
            mstore(
                add(ptr, 0x31),
                hex"5a_f4_3d_3d_93_80_3e_60_33_57_fd_5b_f3_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00"
            )

            // -------------------------------------------------------------------------------------------------------------
            // EXTRA DATA (65 bytes)
            // -------------------------------------------------------------------------------------------------------------

            mstore(add(ptr, 0x3e), shl(0x60, factory))
            mstore(add(ptr, 0x52), shl(0x60, bondingCurve))
            mstore(add(ptr, 0x66), shl(0x60, nft))
            mstore8(add(ptr, 0x7a), poolType)
            mstore(add(ptr, 0x7b), shl(0xe0, blockNumber)) // lower 32 bits only

            instance := create(0, ptr, 0x7f)
        }
    }

    /**
     * @dev Deploys and returns the address of a clone that mimics the behaviour of `implementation`.
     *
     * This function uses the create opcode, which should never revert.
     *
     * During the delegate call, extra data is copied into the calldata which can then be
     * accessed by the implementation contract.
     */
    function cloneERC20Pool(
        address implementation,
        ICollectionPoolFactory factory,
        ICurve bondingCurve,
        IERC721 nft,
        uint8 poolType,
        ERC20 token
    ) internal returns (address instance) {
        uint256 blockNumber = block.number;

        assembly {
            let ptr := mload(0x40)

            // -------------------------------------------------------------------------------------------------------------
            // CREATION (9 bytes)
            // -------------------------------------------------------------------------------------------------------------

            // creation size = 09
            // runtime size = 8a
            // 60 runtime  | PUSH1 runtime (r)     | r                       | –
            // 3d          | RETURNDATASIZE        | 0 r                     | –
            // 81          | DUP2                  | r 0 r                   | –
            // 60 creation | PUSH1 creation (c)    | c r 0 r                 | –
            // 3d          | RETURNDATASIZE        | 0 c r 0 r               | –
            // 39          | CODECOPY              | 0 r                     | [0-runSize): runtime code
            // f3          | RETURN                |                         | [0-runSize): runtime code

            // -------------------------------------------------------------------------------------------------------------
            // RUNTIME (53 bytes of code + 85 bytes of extra data = 138 bytes)
            // -------------------------------------------------------------------------------------------------------------

            // extra data size = 55
            // 3d          | RETURNDATASIZE        | 0                       | –
            // 3d          | RETURNDATASIZE        | 0 0                     | –
            // 3d          | RETURNDATASIZE        | 0 0 0                   | –
            // 3d          | RETURNDATASIZE        | 0 0 0 0                 | –
            // 36          | CALLDATASIZE          | cds 0 0 0 0             | –
            // 3d          | RETURNDATASIZE        | 0 cds 0 0 0 0           | –
            // 3d          | RETURNDATASIZE        | 0 0 cds 0 0 0 0         | –
            // 37          | CALLDATACOPY          | 0 0 0 0                 | [0, cds) = calldata
            // 60 extra    | PUSH1 extra           | extra 0 0 0 0           | [0, cds) = calldata
            // 60 0x35     | PUSH1 0x35            | 0x35 extra 0 0 0 0      | [0, cds) = calldata // 0x35 (53) is runtime size - data
            // 36          | CALLDATASIZE          | cds 0x35 extra 0 0 0 0  | [0, cds) = calldata
            // 39          | CODECOPY              | 0 0 0 0                 | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 36          | CALLDATASIZE          | cds 0 0 0 0             | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 60 extra    | PUSH1 extra           | extra cds 0 0 0 0       | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 01          | ADD                   | cds+extra 0 0 0 0       | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 3d          | RETURNDATASIZE        | 0 cds 0 0 0 0           | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 73 addr     | PUSH20 0x123…         | addr 0 cds 0 0 0 0      | [0, cds) = calldata, [cds, cds+0x35) = extraData
            mstore(
                ptr,
                hex"60_8a_3d_81_60_09_3d_39_f3_3d_3d_3d_3d_36_3d_3d_37_60_55_60_35_36_39_36_60_55_01_3d_73_00_00_00"
            )
            mstore(add(ptr, 0x1d), shl(0x60, implementation))

            // 5a          | GAS                   | gas addr 0 cds 0 0 0 0  | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // f4          | DELEGATECALL          | success 0 0             | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 3d          | RETURNDATASIZE        | rds success 0 0         | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 3d          | RETURNDATASIZE        | rds rds success 0 0     | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 93          | SWAP4                 | 0 rds success 0 rds     | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 80          | DUP1                  | 0 0 rds success 0 rds   | [0, cds) = calldata, [cds, cds+0x35) = extraData
            // 3e          | RETURNDATACOPY        | success 0 rds           | [0, rds) = return data (there might be some irrelevant leftovers in memory [rds, cds+0x37) when rds < cds+0x37)
            // 60 0x33     | PUSH1 0x33            | 0x33 sucess 0 rds       | [0, rds) = return data
            // 57          | JUMPI                 | 0 rds                   | [0, rds) = return data
            // fd          | REVERT                | –                       | [0, rds) = return data
            // 5b          | JUMPDEST              | 0 rds                   | [0, rds) = return data
            // f3          | RETURN                | –                       | [0, rds) = return data
            mstore(
                add(ptr, 0x31),
                hex"5a_f4_3d_3d_93_80_3e_60_33_57_fd_5b_f3_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00"
            )

            // -------------------------------------------------------------------------------------------------------------
            // EXTRA DATA (85 bytes)
            // -------------------------------------------------------------------------------------------------------------

            mstore(add(ptr, 0x3e), shl(0x60, factory))
            mstore(add(ptr, 0x52), shl(0x60, bondingCurve))
            mstore(add(ptr, 0x66), shl(0x60, nft))
            mstore8(add(ptr, 0x7a), poolType)
            mstore(add(ptr, 0x7b), shl(0xe0, blockNumber)) // lower 32 bits only
            mstore(add(ptr, 0x7f), shl(0x60, token))

            instance := create(0, ptr, 0x93)
        }
    }

    /**
     * @notice Checks if a contract is a clone of a CollectionPoolETH.
     * @dev Only checks the runtime bytecode, does not check the extra data.
     * @param factory the factory that deployed the clone
     * @param implementation the CollectionPoolETH implementation contract
     * @param query the contract to check
     * @return result True if the contract is a clone, false otherwise
     */
    function isETHPoolClone(
        address factory,
        address implementation,
        address query
    ) internal view returns (bool result) {
        // solhint-disable-next-line no-inline-assembly
        assembly {
            let ptr := mload(0x40)
            mstore(
                ptr,
                hex"3d_3d_3d_3d_36_3d_3d_37_60_41_60_35_36_39_36_60_41_01_3d_73_00_00_00_00_00_00_00_00_00_00_00_00"
            )
            mstore(add(ptr, 0x14), shl(0x60, implementation))
            mstore(
                add(ptr, 0x28),
                hex"5a_f4_3d_3d_93_80_3e_60_33_57_fd_5b_f3_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00"
            )
            mstore(add(ptr, 0x35), shl(0x60, factory))

            // compare expected bytecode with that of the queried contract
            let other := add(ptr, 0x49)
            extcodecopy(query, other, 0, 0x49)
            result := and(
                eq(mload(ptr), mload(other)),
                and(
                    eq(mload(add(ptr, 0x20)), mload(add(other, 0x20))),
                    eq(mload(add(ptr, 0x29)), mload(add(other, 0x29)))
                )
            )
        }
    }

    /**
     * @notice Checks if a contract is a clone of a CollectionPoolERC20.
     * @dev Only checks the runtime bytecode, does not check the extra data.
     * @param implementation the CollectionPoolERC20 implementation contract
     * @param query the contract to check
     * @return result True if the contract is a clone, false otherwise
     */
    function isERC20PoolClone(
        address factory,
        address implementation,
        address query
    ) internal view returns (bool result) {
        // solhint-disable-next-line no-inline-assembly
        assembly {
            let ptr := mload(0x40)
            mstore(
                ptr,
                hex"3d_3d_3d_3d_36_3d_3d_37_60_55_60_35_36_39_36_60_55_01_3d_73_00_00_00_00_00_00_00_00_00_00_00_00"
            )
            mstore(add(ptr, 0x14), shl(0x60, implementation))
            mstore(
                add(ptr, 0x28),
                hex"5a_f4_3d_3d_93_80_3e_60_33_57_fd_5b_f3_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00"
            )
            mstore(add(ptr, 0x35), shl(0x60, factory))

            // compare expected bytecode with that of the queried contract
            let other := add(ptr, 0x49)
            extcodecopy(query, other, 0, 0x49)
            result := and(
                eq(mload(ptr), mload(other)),
                and(
                    eq(mload(add(ptr, 0x20)), mload(add(other, 0x20))),
                    eq(mload(add(ptr, 0x29)), mload(add(other, 0x29)))
                )
            )
        }
    }
}

File 7 of 52 : CollectionRouter.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {ERC20} from "solmate/src/tokens/ERC20.sol";
import {SafeTransferLib} from "solmate/src/utils/SafeTransferLib.sol";
import {ICollectionPool} from "../pools/ICollectionPool.sol";
import {CollectionPool} from "../pools/CollectionPool.sol";
import {ICollectionPoolFactory} from "../pools/ICollectionPoolFactory.sol";
import {CurveErrorCodes} from "../bonding-curves/CurveErrorCodes.sol";
import {ICurve} from "../bonding-curves/ICurve.sol";
import {PoolVariant, NFTs} from "../pools/CollectionStructsAndEnums.sol";

contract CollectionRouter {
    using SafeTransferLib for address payable;
    using SafeTransferLib for ERC20;

    struct PoolSwapAny {
        CollectionPool pool;
        uint256 numItems;
    }

    struct PoolSwapSpecific {
        CollectionPool pool;
        uint256[] nftIds;
        bytes32[] proof;
        bool[] proofFlags;
        /// @dev only used for selling into pools
        bytes externalFilterContext;
    }

    struct RobustPoolSwapAny {
        PoolSwapAny swapInfo;
        uint256 maxCost;
    }

    struct RobustPoolSwapSpecific {
        PoolSwapSpecific swapInfo;
        uint256 maxCost;
    }

    struct RobustPoolSwapSpecificForToken {
        PoolSwapSpecific swapInfo;
        uint256 minOutput;
    }

    struct NFTsForAnyNFTsTrade {
        PoolSwapSpecific[] nftToTokenTrades;
        PoolSwapAny[] tokenToNFTTrades;
    }

    struct NFTsForSpecificNFTsTrade {
        PoolSwapSpecific[] nftToTokenTrades;
        PoolSwapSpecific[] tokenToNFTTrades;
    }

    struct RobustPoolNFTsFoTokenAndTokenforNFTsTrade {
        RobustPoolSwapSpecific[] tokenToNFTTrades;
        RobustPoolSwapSpecificForToken[] nftToTokenTrades;
        uint256 inputAmount;
        address payable tokenRecipient;
        address nftRecipient;
    }

    modifier checkDeadline(uint256 deadline) {
        _checkDeadline(deadline);
        _;
    }

    ICollectionPoolFactory public immutable factory;

    constructor(ICollectionPoolFactory _factory) {
        factory = _factory;
    }

    /**
     * ETH swaps
     */

    /**
     * @notice Swaps ETH into NFTs using multiple pools.
     * @param swapList The list of pools to trade with and the number of NFTs to buy from each.
     * @param ethRecipient The address that will receive the unspent ETH input
     * @param nftRecipient The address that will receive the NFT output
     * @param deadline The Unix timestamp (in seconds) at/after which the swap will revert
     * @return remainingValue The unspent ETH amount
     */
    function swapETHForAnyNFTs(
        PoolSwapAny[] calldata swapList,
        address payable ethRecipient,
        address nftRecipient,
        uint256 deadline
    ) external payable checkDeadline(deadline) returns (uint256 remainingValue) {
        return _swapETHForAnyNFTs(swapList, msg.value, ethRecipient, nftRecipient);
    }

    /**
     * @notice Swaps ETH into specific NFTs using multiple pools.
     * @param swapList The list of pools to trade with and the IDs of the NFTs to buy from each.
     * @param ethRecipient The address that will receive the unspent ETH input
     * @param nftRecipient The address that will receive the NFT output
     * @param deadline The Unix timestamp (in seconds) at/after which the swap will revert
     * @return remainingValue The unspent ETH amount
     */
    function swapETHForSpecificNFTs(
        PoolSwapSpecific[] calldata swapList,
        address payable ethRecipient,
        address nftRecipient,
        uint256 deadline
    ) external payable checkDeadline(deadline) returns (uint256 remainingValue) {
        return _swapETHForSpecificNFTs(swapList, msg.value, ethRecipient, nftRecipient);
    }

    /**
     * @notice Swaps one set of NFTs into another set of specific NFTs using multiple pools, using
     * ETH as the intermediary.
     * @param trade The struct containing all NFT-to-ETH swaps and ETH-to-NFT swaps.
     * @param minOutput The minimum acceptable total excess ETH received
     * @param ethRecipient The address that will receive the ETH output
     * @param nftRecipient The address that will receive the NFT output
     * @param deadline The Unix timestamp (in seconds) at/after which the swap will revert
     * @return outputAmount The total ETH received
     */
    function swapNFTsForAnyNFTsThroughETH(
        NFTsForAnyNFTsTrade calldata trade,
        uint256 minOutput,
        address payable ethRecipient,
        address nftRecipient,
        uint256 deadline
    ) external payable checkDeadline(deadline) returns (uint256 outputAmount) {
        // Swap NFTs for ETH
        // minOutput of swap set to 0 since we're doing an aggregate slippage check
        outputAmount = _swapNFTsForToken(trade.nftToTokenTrades, 0, payable(address(this)));

        // Add extra value to buy NFTs
        outputAmount += msg.value;

        // Swap ETH for any NFTs
        // cost <= inputValue = outputAmount - minOutput, so outputAmount' = (outputAmount - minOutput - cost) + minOutput >= minOutput
        outputAmount =
            _swapETHForAnyNFTs(trade.tokenToNFTTrades, outputAmount - minOutput, ethRecipient, nftRecipient) + minOutput;
    }

    /**
     * @notice Swaps one set of NFTs into another set of specific NFTs using multiple pools, using
     * ETH as the intermediary.
     * @param trade The struct containing all NFT-to-ETH swaps and ETH-to-NFT swaps.
     * @param minOutput The minimum acceptable total excess ETH received
     * @param ethRecipient The address that will receive the ETH output
     * @param nftRecipient The address that will receive the NFT output
     * @param deadline The Unix timestamp (in seconds) at/after which the swap will revert
     * @return outputAmount The total ETH received
     */
    function swapNFTsForSpecificNFTsThroughETH(
        NFTsForSpecificNFTsTrade calldata trade,
        uint256 minOutput,
        address payable ethRecipient,
        address nftRecipient,
        uint256 deadline
    ) external payable checkDeadline(deadline) returns (uint256 outputAmount) {
        // Swap NFTs for ETH
        // minOutput of swap set to 0 since we're doing an aggregate slippage check
        outputAmount = _swapNFTsForToken(trade.nftToTokenTrades, 0, payable(address(this)));

        // Add extra value to buy NFTs
        outputAmount += msg.value;

        // Swap ETH for specific NFTs
        // cost <= inputValue = outputAmount - minOutput, so outputAmount' = (outputAmount - minOutput - cost) + minOutput >= minOutput
        outputAmount = _swapETHForSpecificNFTs(
            trade.tokenToNFTTrades, outputAmount - minOutput, ethRecipient, nftRecipient
        ) + minOutput;
    }

    /**
     * ERC20 swaps
     *
     * Note: All ERC20 swaps assume that a single ERC20 token is used for all the pools involved.
     * Swapping using multiple tokens in the same transaction is possible, but the slippage checks
     * & the return values will be meaningless, and may lead to undefined behavior.
     *
     * Note: The sender should ideally grant infinite token approval to the router in order for NFT-to-NFT
     * swaps to work smoothly.
     */

    /**
     * @notice Swaps ERC20 tokens into NFTs using multiple pools.
     * @param swapList The list of pools to trade with and the number of NFTs to buy from each.
     * @param inputAmount The amount of ERC20 tokens to add to the ERC20-to-NFT swaps
     * @param nftRecipient The address that will receive the NFT output
     * @param deadline The Unix timestamp (in seconds) at/after which the swap will revert
     * @return remainingValue The unspent token amount
     */
    function swapERC20ForAnyNFTs(
        PoolSwapAny[] calldata swapList,
        uint256 inputAmount,
        address nftRecipient,
        uint256 deadline
    ) external checkDeadline(deadline) returns (uint256 remainingValue) {
        return _swapERC20ForAnyNFTs(swapList, inputAmount, nftRecipient);
    }

    /**
     * @notice Swaps ERC20 tokens into specific NFTs using multiple pools.
     * @param swapList The list of pools to trade with and the IDs of the NFTs to buy from each.
     * @param inputAmount The amount of ERC20 tokens to add to the ERC20-to-NFT swaps
     * @param nftRecipient The address that will receive the NFT output
     * @param deadline The Unix timestamp (in seconds) at/after which the swap will revert
     * @return remainingValue The unspent token amount
     */
    function swapERC20ForSpecificNFTs(
        PoolSwapSpecific[] calldata swapList,
        uint256 inputAmount,
        address nftRecipient,
        uint256 deadline
    ) external checkDeadline(deadline) returns (uint256 remainingValue) {
        return _swapERC20ForSpecificNFTs(swapList, inputAmount, nftRecipient);
    }

    /**
     * @notice Swaps NFTs into ETH/ERC20 using multiple pools.
     * @param swapList The list of pools to trade with and the IDs of the NFTs to sell to each.
     * @param minOutput The minimum acceptable total tokens received
     * @param tokenRecipient The address that will receive the token output
     * @param deadline The Unix timestamp (in seconds) at/after which the swap will revert
     * @return outputAmount The total tokens received
     */
    function swapNFTsForToken(
        PoolSwapSpecific[] calldata swapList,
        uint256 minOutput,
        address tokenRecipient,
        uint256 deadline
    ) external checkDeadline(deadline) returns (uint256 outputAmount) {
        return _swapNFTsForToken(swapList, minOutput, payable(tokenRecipient));
    }

    /**
     * @notice Swaps one set of NFTs into another set of specific NFTs using multiple pools, using
     * an ERC20 token as the intermediary.
     * @param trade The struct containing all NFT-to-ERC20 swaps and ERC20-to-NFT swaps.
     * @param inputAmount The amount of ERC20 tokens to add to the ERC20-to-NFT swaps
     * @param minOutput The minimum acceptable total excess tokens received
     * @param nftRecipient The address that will receive the NFT output
     * @param deadline The Unix timestamp (in seconds) at/after which the swap will revert
     * @return outputAmount The total ERC20 tokens received
     */
    function swapNFTsForAnyNFTsThroughERC20(
        NFTsForAnyNFTsTrade calldata trade,
        uint256 inputAmount,
        uint256 minOutput,
        address nftRecipient,
        uint256 deadline
    ) external checkDeadline(deadline) returns (uint256 outputAmount) {
        // Swap NFTs for ERC20
        // minOutput of swap set to 0 since we're doing an aggregate slippage check
        // output tokens are sent to msg.sender
        outputAmount = _swapNFTsForToken(trade.nftToTokenTrades, 0, payable(msg.sender));

        // Add extra value to buy NFTs
        outputAmount += inputAmount;

        // Swap ERC20 for any NFTs
        // cost <= maxCost = outputAmount - minOutput, so outputAmount' = outputAmount - cost >= minOutput
        // input tokens are taken directly from msg.sender
        outputAmount = _swapERC20ForAnyNFTs(trade.tokenToNFTTrades, outputAmount - minOutput, nftRecipient) + minOutput;
    }

    /**
     * @notice Swaps one set of NFTs into another set of specific NFTs using multiple pools, using
     * an ERC20 token as the intermediary.
     * @param trade The struct containing all NFT-to-ERC20 swaps and ERC20-to-NFT swaps.
     * @param inputAmount The amount of ERC20 tokens to add to the ERC20-to-NFT swaps
     * @param minOutput The minimum acceptable total excess tokens received
     * @param nftRecipient The address that will receive the NFT output
     * @param deadline The Unix timestamp (in seconds) at/after which the swap will revert
     * @return outputAmount The total ERC20 tokens received
     */
    function swapNFTsForSpecificNFTsThroughERC20(
        NFTsForSpecificNFTsTrade calldata trade,
        uint256 inputAmount,
        uint256 minOutput,
        address nftRecipient,
        uint256 deadline
    ) external checkDeadline(deadline) returns (uint256 outputAmount) {
        // Swap NFTs for ERC20
        // minOutput of swap set to 0 since we're doing an aggregate slippage check
        // output tokens are sent to msg.sender
        outputAmount = _swapNFTsForToken(trade.nftToTokenTrades, 0, payable(msg.sender));

        // Add extra value to buy NFTs
        outputAmount += inputAmount;

        // Swap ERC20 for specific NFTs
        // cost <= maxCost = outputAmount - minOutput, so outputAmount' = outputAmount - cost >= minOutput
        // input tokens are taken directly from msg.sender
        outputAmount =
            _swapERC20ForSpecificNFTs(trade.tokenToNFTTrades, outputAmount - minOutput, nftRecipient) + minOutput;
    }

    /**
     * Robust Swaps
     * These are "robust" versions of the NFT<>Token swap functions which will never revert due to slippage
     * Instead, users specify a per-swap max cost. If the price changes more than the user specifies, no swap is attempted. This allows users to specify a batch of swaps, and execute as many of them as possible.
     */

    /**
     * @dev We assume msg.value >= sum of values in maxCostPerPool
     * @notice Swaps as much ETH for any NFTs as possible, respecting the per-swap max cost.
     * @param swapList The list of pools to trade with and the number of NFTs to buy from each.
     * @param ethRecipient The address that will receive the unspent ETH input
     * @param nftRecipient The address that will receive the NFT output
     * @param deadline The Unix timestamp (in seconds) at/after which the swap will revert
     * @return remainingValue The unspent token amount
     */
    function robustSwapETHForAnyNFTs(
        RobustPoolSwapAny[] calldata swapList,
        address payable ethRecipient,
        address nftRecipient,
        uint256 deadline
    ) external payable virtual checkDeadline(deadline) returns (uint256 remainingValue) {
        remainingValue = msg.value;

        // Try doing each swap
        uint256 poolCost;
        uint256 numSwaps = swapList.length;
        for (uint256 i; i < numSwaps;) {
            // Calculate actual cost per swap
            (,, poolCost,) = swapList[i].swapInfo.pool.getBuyNFTQuote(swapList[i].swapInfo.numItems);

            // If within our maxCost, proceed
            if (poolCost <= swapList[i].maxCost) {
                // We know how much ETH to send because we already did the math above
                // So we just send that much
                remainingValue -= swapList[i].swapInfo.pool.swapTokenForAnyNFTs{value: poolCost}(
                    swapList[i].swapInfo.numItems, poolCost, nftRecipient, true, msg.sender
                );
            }

            unchecked {
                ++i;
            }
        }

        // Return remaining value to sender
        if (remainingValue > 0) {
            ethRecipient.safeTransferETH(remainingValue);
        }
    }

    /**
     * @dev We assume msg.value >= sum of values in maxCostPerPool
     * @param swapList The list of pools to trade with and the IDs of the NFTs to buy from each.
     * @param ethRecipient The address that will receive the unspent ETH input
     * @param nftRecipient The address that will receive the NFT output
     * @param deadline The Unix timestamp (in seconds) at/after which the swap will revert
     * @return remainingValue The unspent token amount
     */
    function robustSwapETHForSpecificNFTs(
        RobustPoolSwapSpecific[] calldata swapList,
        address payable ethRecipient,
        address nftRecipient,
        uint256 deadline
    ) public payable virtual checkDeadline(deadline) returns (uint256 remainingValue) {
        remainingValue = msg.value;
        uint256 poolCost;

        // Try doing each swap
        uint256 numSwaps = swapList.length;
        for (uint256 i; i < numSwaps;) {
            // Calculate actual cost per swap
            (,, poolCost,) = swapList[i].swapInfo.pool.getBuyNFTQuote(swapList[i].swapInfo.nftIds.length);

            // If within our maxCost, proceed
            if (poolCost <= swapList[i].maxCost) {
                // We know how much ETH to send because we already did the math above
                // So we just send that much
                remainingValue -= swapList[i].swapInfo.pool.swapTokenForSpecificNFTs{value: poolCost}(
                    swapList[i].swapInfo.nftIds, poolCost, nftRecipient, true, msg.sender
                );
            }

            unchecked {
                ++i;
            }
        }

        // Return remaining value to sender
        if (remainingValue > 0) {
            ethRecipient.safeTransferETH(remainingValue);
        }
    }

    /**
     * @notice Swaps as many ERC20 tokens for any NFTs as possible, respecting the per-swap max cost.
     * @param swapList The list of pools to trade with and the number of NFTs to buy from each.
     * @param inputAmount The amount of ERC20 tokens to add to the ERC20-to-NFT swaps
     * @param nftRecipient The address that will receive the NFT output
     * @param deadline The Unix timestamp (in seconds) at/after which the swap will revert
     * @return remainingValue The unspent token amount
     */
    function robustSwapERC20ForAnyNFTs(
        RobustPoolSwapAny[] calldata swapList,
        uint256 inputAmount,
        address nftRecipient,
        uint256 deadline
    ) external virtual checkDeadline(deadline) returns (uint256 remainingValue) {
        remainingValue = inputAmount;
        uint256 poolCost;

        // Try doing each swap
        uint256 numSwaps = swapList.length;
        for (uint256 i; i < numSwaps;) {
            // Calculate actual cost per swap
            (,, poolCost,) = swapList[i].swapInfo.pool.getBuyNFTQuote(swapList[i].swapInfo.numItems);

            // If within our maxCost, proceed
            if (poolCost <= swapList[i].maxCost) {
                remainingValue -= swapList[i].swapInfo.pool.swapTokenForAnyNFTs(
                    swapList[i].swapInfo.numItems, poolCost, nftRecipient, true, msg.sender
                );
            }

            unchecked {
                ++i;
            }
        }
    }

    /**
     * @notice Swaps as many ERC20 tokens for specific NFTs as possible, respecting the per-swap max cost.
     * @param swapList The list of pools to trade with and the IDs of the NFTs to buy from each.
     * @param inputAmount The amount of ERC20 tokens to add to the ERC20-to-NFT swaps
     *
     * @param nftRecipient The address that will receive the NFT output
     * @param deadline The Unix timestamp (in seconds) at/after which the swap will revert
     * @return remainingValue The unspent token amount
     */
    function robustSwapERC20ForSpecificNFTs(
        RobustPoolSwapSpecific[] calldata swapList,
        uint256 inputAmount,
        address nftRecipient,
        uint256 deadline
    ) public virtual checkDeadline(deadline) returns (uint256 remainingValue) {
        remainingValue = inputAmount;
        uint256 poolCost;

        // Try doing each swap
        uint256 numSwaps = swapList.length;
        for (uint256 i; i < numSwaps;) {
            // Calculate actual cost per swap
            (,, poolCost,) = swapList[i].swapInfo.pool.getBuyNFTQuote(swapList[i].swapInfo.nftIds.length);

            // If within our maxCost, proceed
            if (poolCost <= swapList[i].maxCost) {
                remainingValue -= swapList[i].swapInfo.pool.swapTokenForSpecificNFTs(
                    swapList[i].swapInfo.nftIds, poolCost, nftRecipient, true, msg.sender
                );
            }

            unchecked {
                ++i;
            }
        }
    }

    /**
     * @notice Swaps as many NFTs for tokens as possible, respecting the per-swap min output
     * @param swapList The list of pools to trade with and the IDs of the NFTs to sell to each.
     * @param tokenRecipient The address that will receive the token output
     * @param deadline The Unix timestamp (in seconds) at/after which the swap will revert
     * @return outputAmount The total ETH/ERC20 received
     */
    function robustSwapNFTsForToken(
        RobustPoolSwapSpecificForToken[] calldata swapList,
        address payable tokenRecipient,
        uint256 deadline
    ) public virtual checkDeadline(deadline) returns (uint256 outputAmount) {
        // Try doing each swap
        uint256 numSwaps = swapList.length;
        for (uint256 i; i < numSwaps;) {
            PoolSwapSpecific calldata swapInfo = swapList[i].swapInfo;
            // Locally scoped to avoid stack too deep error
            {
                try swapInfo.pool.getSellNFTQuote(swapInfo.nftIds.length) returns (
                    ICurve.Params memory, uint256, uint256 poolOutput, ICurve.Fees memory
                ) {
                    // If at least equal to our minOutput, proceed
                    if (poolOutput >= swapList[i].minOutput) {
                        // Do the swap and update outputAmount with how many tokens we got
                        outputAmount += swapInfo.pool.swapNFTsForToken(
                            NFTs(swapInfo.nftIds, swapInfo.proof, swapInfo.proofFlags),
                            0,
                            tokenRecipient,
                            true,
                            msg.sender,
                            swapInfo.externalFilterContext
                        );
                    }
                } catch {}

                unchecked {
                    ++i;
                }
            }
        }
    }

    /**
     * @notice Buys NFTs with ETH and sells them for tokens in one transaction
     * @param params All the parameters for the swap (packed in struct to avoid stack too deep), containing:
     * - ethToNFTSwapList The list of NFTs to buy
     * - nftToTokenSwapList The list of NFTs to sell
     * - inputAmount The max amount of tokens to send (if ERC20)
     * - tokenRecipient The address that receives tokens from the NFTs sold
     * - nftRecipient The address that receives NFTs
     * - deadline UNIX timestamp deadline for the swap
     */
    function robustSwapETHForSpecificNFTsAndNFTsToToken(RobustPoolNFTsFoTokenAndTokenforNFTsTrade calldata params)
        external
        payable
        virtual
        returns (uint256 remainingValue, uint256 outputAmount)
    {
        {
            remainingValue = msg.value;
            uint256 poolCost;

            // Try doing each swap
            uint256 numSwaps = params.tokenToNFTTrades.length;
            for (uint256 i; i < numSwaps;) {
                // Calculate actual cost per swap
                (,, poolCost,) = params.tokenToNFTTrades[i].swapInfo.pool.getBuyNFTQuote(
                    params.tokenToNFTTrades[i].swapInfo.nftIds.length
                );

                // If within our maxCost, proceed
                if (poolCost <= params.tokenToNFTTrades[i].maxCost) {
                    // We know how much ETH to send because we already did the math above
                    // So we just send that much
                    remainingValue -= params.tokenToNFTTrades[i].swapInfo.pool.swapTokenForSpecificNFTs{value: poolCost}(
                        params.tokenToNFTTrades[i].swapInfo.nftIds, poolCost, params.nftRecipient, true, msg.sender
                    );
                }

                unchecked {
                    ++i;
                }
            }

            // Return remaining value to sender
            if (remainingValue > 0) {
                params.tokenRecipient.safeTransferETH(remainingValue);
            }
        }
        {
            // Try doing each swap
            uint256 numSwaps = params.nftToTokenTrades.length;
            for (uint256 i; i < numSwaps;) {
                // Locally scoped to avoid stack too deep error
                {
                    try params.nftToTokenTrades[i].swapInfo.pool.getSellNFTQuote(
                        params.nftToTokenTrades[i].swapInfo.nftIds.length
                    ) returns (ICurve.Params memory, uint256, uint256 poolOutput, ICurve.Fees memory) {
                        // If at least equal to our minOutput, proceed
                        if (poolOutput >= params.nftToTokenTrades[i].minOutput) {
                            // Do the swap and update outputAmount with how many tokens we got
                            outputAmount += params.nftToTokenTrades[i].swapInfo.pool.swapNFTsForToken(
                                NFTs(
                                    params.nftToTokenTrades[i].swapInfo.nftIds,
                                    params.nftToTokenTrades[i].swapInfo.proof,
                                    params.nftToTokenTrades[i].swapInfo.proofFlags
                                ),
                                0,
                                params.tokenRecipient,
                                true,
                                msg.sender,
                                params.nftToTokenTrades[i].swapInfo.externalFilterContext
                            );
                        }
                    } catch {}

                    unchecked {
                        ++i;
                    }
                }
            }
        }
    }

    /**
     * @notice Buys NFTs with ERC20, and sells them for tokens in one transaction
     * @param params All the parameters for the swap (packed in struct to avoid stack too deep), containing:
     * - ethToNFTSwapList The list of NFTs to buy
     * - nftToTokenSwapList The list of NFTs to sell
     * - inputAmount The max amount of tokens to send (if ERC20)
     * - tokenRecipient The address that receives tokens from the NFTs sold
     * - nftRecipient The address that receives NFTs
     * - deadline UNIX timestamp deadline for the swap
     */
    function robustSwapERC20ForSpecificNFTsAndNFTsToToken(RobustPoolNFTsFoTokenAndTokenforNFTsTrade calldata params)
        external
        payable
        virtual
        returns (uint256 remainingValue, uint256 outputAmount)
    {
        {
            remainingValue = params.inputAmount;
            uint256 poolCost;

            // Try doing each swap
            uint256 numSwaps = params.tokenToNFTTrades.length;
            for (uint256 i; i < numSwaps;) {
                // Calculate actual cost per swap
                (,, poolCost,) = params.tokenToNFTTrades[i].swapInfo.pool.getBuyNFTQuote(
                    params.tokenToNFTTrades[i].swapInfo.nftIds.length
                );

                // If within our maxCost, proceed
                if (poolCost <= params.tokenToNFTTrades[i].maxCost) {
                    remainingValue -= params.tokenToNFTTrades[i].swapInfo.pool.swapTokenForSpecificNFTs(
                        params.tokenToNFTTrades[i].swapInfo.nftIds, poolCost, params.nftRecipient, true, msg.sender
                    );
                }

                unchecked {
                    ++i;
                }
            }
        }
        {
            // Try doing each swap
            uint256 numSwaps = params.nftToTokenTrades.length;
            for (uint256 i; i < numSwaps;) {
                // Locally scoped to avoid stack too deep error
                {
                    try params.nftToTokenTrades[i].swapInfo.pool.getSellNFTQuote(
                        params.nftToTokenTrades[i].swapInfo.nftIds.length
                    ) returns (ICurve.Params memory, uint256, uint256 poolOutput, ICurve.Fees memory) {
                        // If at least equal to our minOutput, proceed
                        if (poolOutput >= params.nftToTokenTrades[i].minOutput) {
                            // Do the swap and update outputAmount with how many tokens we got
                            outputAmount += params.nftToTokenTrades[i].swapInfo.pool.swapNFTsForToken(
                                NFTs(
                                    params.nftToTokenTrades[i].swapInfo.nftIds,
                                    params.nftToTokenTrades[i].swapInfo.proof,
                                    params.nftToTokenTrades[i].swapInfo.proofFlags
                                ),
                                0,
                                params.tokenRecipient,
                                true,
                                msg.sender,
                                params.nftToTokenTrades[i].swapInfo.externalFilterContext
                            );
                        }
                    } catch {}

                    unchecked {
                        ++i;
                    }
                }
            }
        }
    }

    receive() external payable {}

    /**
     * Restricted functions
     */

    /**
     * @dev Allows an ERC20 pool contract to transfer ERC20 tokens directly from
     * the sender, in order to minimize the number of token transfers. Only callable by an ERC20 pool.
     * @param token The ERC20 token to transfer
     * @param from The address to transfer tokens from
     * @param to The address to transfer tokens to
     * @param amount The amount of tokens to transfer
     * @param variant The pool variant of the pool contract
     */
    function poolTransferERC20From(ERC20 token, address from, address to, uint256 amount, PoolVariant variant)
        external
    {
        // verify caller is a trusted pool contract
        require(factory.isPoolVariant(msg.sender, variant), "Not pool");

        // verify caller is an ERC20 pool
        require(
            variant == PoolVariant.ENUMERABLE_ERC20 || variant == PoolVariant.MISSING_ENUMERABLE_ERC20, "Not ERC20 pool"
        );

        // transfer tokens to pool
        token.safeTransferFrom(from, to, amount);
    }

    /**
     * @dev Allows a pool contract to transfer ERC721 NFTs directly from
     * the sender, in order to minimize the number of token transfers. Only callable by a pool.
     * @param nft The ERC721 NFT to transfer
     * @param from The address to transfer tokens from
     * @param to The address to transfer tokens to
     * @param id The ID of the NFT to transfer
     * @param variant The pool variant of the pool contract
     */
    function poolTransferNFTFrom(IERC721 nft, address from, address to, uint256 id, PoolVariant variant) external {
        // verify caller is a trusted pool contract
        require(factory.isPoolVariant(msg.sender, variant), "Not pool");

        // transfer NFTs to pool
        nft.safeTransferFrom(from, to, id);
    }

    /**
     * Internal functions
     */

    /**
     * @param deadline The last valid time for a swap
     */
    function _checkDeadline(uint256 deadline) internal view {
        require(block.timestamp <= deadline, "Deadline passed");
    }

    /**
     * @notice Internal function used to swap ETH for any NFTs
     * @param swapList The list of pools and swap calldata
     * @param inputAmount The total amount of ETH to send
     * @param ethRecipient The address receiving excess ETH
     * @param nftRecipient The address receiving the NFTs from the pools
     * @return remainingValue The unspent token amount
     */
    function _swapETHForAnyNFTs(
        PoolSwapAny[] calldata swapList,
        uint256 inputAmount,
        address payable ethRecipient,
        address nftRecipient
    ) internal virtual returns (uint256 remainingValue) {
        remainingValue = inputAmount;

        uint256 poolCost;

        // Do swaps
        uint256 numSwaps = swapList.length;
        for (uint256 i; i < numSwaps;) {
            // Calculate the cost per swap first to send exact amount of ETH over, saves gas by avoiding the need to send back excess ETH
            (,, poolCost,) = swapList[i].pool.getBuyNFTQuote(swapList[i].numItems);

            // Total ETH taken from sender cannot exceed inputAmount
            // because otherwise the deduction from remainingValue will fail
            remainingValue -= swapList[i].pool.swapTokenForAnyNFTs{value: poolCost}(
                swapList[i].numItems, remainingValue, nftRecipient, true, msg.sender
            );

            unchecked {
                ++i;
            }
        }

        // Return remaining value to sender
        if (remainingValue > 0) {
            ethRecipient.safeTransferETH(remainingValue);
        }
    }

    /**
     * @notice Internal function used to swap ETH for a specific set of NFTs
     * @param swapList The list of pools and swap calldata
     * @param inputAmount The total amount of ETH to send
     * @param ethRecipient The address receiving excess ETH
     * @param nftRecipient The address receiving the NFTs from the pools
     * @return remainingValue The unspent token amount
     */
    function _swapETHForSpecificNFTs(
        PoolSwapSpecific[] calldata swapList,
        uint256 inputAmount,
        address payable ethRecipient,
        address nftRecipient
    ) internal virtual returns (uint256 remainingValue) {
        remainingValue = inputAmount;

        uint256 poolCost;

        // Do swaps
        uint256 numSwaps = swapList.length;
        for (uint256 i; i < numSwaps;) {
            // Calculate the cost per swap first to send exact amount of ETH over, saves gas by avoiding the need to send back excess ETH
            (,, poolCost,) = swapList[i].pool.getBuyNFTQuote(swapList[i].nftIds.length);

            // Total ETH taken from sender cannot exceed inputAmount
            // because otherwise the deduction from remainingValue will fail
            remainingValue -= swapList[i].pool.swapTokenForSpecificNFTs{value: poolCost}(
                swapList[i].nftIds, remainingValue, nftRecipient, true, msg.sender
            );

            unchecked {
                ++i;
            }
        }

        // Return remaining value to sender
        if (remainingValue > 0) {
            ethRecipient.safeTransferETH(remainingValue);
        }
    }

    /**
     * @notice Internal function used to swap an ERC20 token for any NFTs
     * @dev Note that we don't need to query the pool's bonding curve first for pricing data because
     * we just calculate and take the required amount from the caller during swap time.
     * However, we can't "pull" ETH, which is why for the ETH->NFT swaps, we need to calculate the pricing info
     * to figure out how much the router should send to the pool.
     * @param swapList The list of pools and swap calldata
     * @param inputAmount The total amount of ERC20 tokens to send
     * @param nftRecipient The address receiving the NFTs from the pools
     * @return remainingValue The unspent token amount
     */
    function _swapERC20ForAnyNFTs(PoolSwapAny[] calldata swapList, uint256 inputAmount, address nftRecipient)
        internal
        virtual
        returns (uint256 remainingValue)
    {
        remainingValue = inputAmount;

        // Do swaps
        uint256 numSwaps = swapList.length;
        for (uint256 i; i < numSwaps;) {
            // Tokens are transferred in by the pool calling router.poolTransferERC20From
            // Total tokens taken from sender cannot exceed inputAmount
            // because otherwise the deduction from remainingValue will fail
            remainingValue -= swapList[i].pool.swapTokenForAnyNFTs(
                swapList[i].numItems, remainingValue, nftRecipient, true, msg.sender
            );

            unchecked {
                ++i;
            }
        }
    }

    /**
     * @notice Internal function used to swap an ERC20 token for specific NFTs
     * @dev Note that we don't need to query the pool's bonding curve first for pricing data because
     * we just calculate and take the required amount from the caller during swap time.
     * However, we can't "pull" ETH, which is why for the ETH->NFT swaps, we need to calculate the pricing info
     * to figure out how much the router should send to the pool.
     * @param swapList The list of pools and swap calldata
     * @param inputAmount The total amount of ERC20 tokens to send
     * @param nftRecipient The address receiving the NFTs from the pools
     * @return remainingValue The unspent token amount
     */
    function _swapERC20ForSpecificNFTs(PoolSwapSpecific[] calldata swapList, uint256 inputAmount, address nftRecipient)
        internal
        virtual
        returns (uint256 remainingValue)
    {
        remainingValue = inputAmount;

        // Do swaps
        uint256 numSwaps = swapList.length;
        for (uint256 i; i < numSwaps;) {
            // Tokens are transferred in by the pool calling router.poolTransferERC20From
            // Total tokens taken from sender cannot exceed inputAmount
            // because otherwise the deduction from remainingValue will fail
            remainingValue -= swapList[i].pool.swapTokenForSpecificNFTs(
                swapList[i].nftIds, remainingValue, nftRecipient, true, msg.sender
            );

            unchecked {
                ++i;
            }
        }
    }

    /**
     * @notice Swaps NFTs for tokens, designed to be used for 1 token at a time
     * @dev Calling with multiple tokens is permitted, BUT minOutput will be
     * far from enough of a safety check because different tokens almost certainly have different unit prices.
     * @param swapList The list of pools and swap calldata
     * @param minOutput The minimum number of tokens to be receieved from the swaps
     * @param tokenRecipient The address that receives the tokens
     * @return outputAmount The number of tokens to be received
     */
    function _swapNFTsForToken(PoolSwapSpecific[] calldata swapList, uint256 minOutput, address payable tokenRecipient)
        internal
        virtual
        returns (uint256 outputAmount)
    {
        // Do swaps
        uint256 numSwaps = swapList.length;
        for (uint256 i; i < numSwaps;) {
            // Do the swap for token and then update outputAmount
            // Note: minExpectedTokenOutput is set to 0 since we're doing an aggregate slippage check below
            outputAmount += swapList[i].pool.swapNFTsForToken(
                NFTs(swapList[i].nftIds, swapList[i].proof, swapList[i].proofFlags),
                0,
                tokenRecipient,
                true,
                msg.sender,
                swapList[i].externalFilterContext
            );

            unchecked {
                ++i;
            }
        }

        // Aggregate slippage check
        require(outputAmount >= minOutput, "outputAmount too low");
    }
}

File 8 of 52 : CollectionPool.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {ERC20} from "solmate/src/tokens/ERC20.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {IERC1155} from "@openzeppelin/contracts/token/ERC1155/IERC1155.sol";
import {ERC1155Holder} from "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import {IERC2981} from "@openzeppelin/contracts/interfaces/IERC2981.sol";
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol";
import {ReentrancyGuard} from "../lib/ReentrancyGuard.sol";
import {TransferLib} from "../lib/TransferLib.sol";
import {ICurve} from "../bonding-curves/ICurve.sol";
import {CollectionRouter} from "../routers/CollectionRouter.sol";
import {ICollectionPool} from "./ICollectionPool.sol";
import {ICollectionPoolFactory} from "./ICollectionPoolFactory.sol";
import {CurveErrorCodes} from "../bonding-curves/CurveErrorCodes.sol";
import {IExternalFilter} from "../filter/IExternalFilter.sol";
import {TokenIDFilter} from "../filter/TokenIDFilter.sol";
import {MultiPauser} from "../lib/MultiPauser.sol";
import {IPoolActivityMonitor} from "./IPoolActivityMonitor.sol";
import {PoolVariant, PoolType, RoyaltyDue, NFTs, EventType} from "../pools/CollectionStructsAndEnums.sol";

/// @title The base contract for an NFT/TOKEN AMM pool
/// @author Collection
/// @notice This implements the core swap logic from NFT to TOKEN
abstract contract CollectionPool is ReentrancyGuard, ERC1155Holder, TokenIDFilter, MultiPauser, ICollectionPool {
    using Strings for uint256;
    /**
     * @dev The _INTERFACE_ID_ERC2981 constant specifies the interface ID for the ERC2981 standard. This standard is used for tracking
     * royalties on non-fungible tokens (NFTs). It defines a standard interface for NFTs that includes metadata about the royalties that
     * are due on the NFT when it is swapped or transferred.
     * @dev The _INTERFACE_ID_ERC2981 constant is used in the AMM pool contract to check whether an NFT being swapped implements the ERC2981
     * standard. If it does, the contract can use the metadata provided by the ERC2981 interface to facilitate the payment of royalties on the
     * NFT swap. If the NFT does not implement the ERC2981 standard, the contract will not track or pay royalties on the NFT swap.
     * This can be overridden by the royaltyNumerator field in the AMM pool contract.
     * @dev For more information about the ERC2981 standard, see https://eips.ethereum.org/EIPS/eip-2981
     */

    bytes4 private constant _INTERFACE_ID_ERC2981 = 0x2a55205a;

    bool private initialized;

    /**
     * @dev The MAX_FEE constant specifies the maximum fee you, the user, are allowed to charge for this AMM pool.
     * It is used to limit the amount of fees that can be charged by the AMM pool contract on NFT/token swaps.
     * @dev The MAX_FEE constant is used to ensure that the AMM pool does not charge excessive fees on NFT/token swaps.
     * It also helps to protect users from paying excessive fees when using the AMM pool contract.
     * @dev usage: 90%, must <= 1 - MAX_PROTOCOL_FEE (set in CollectionPoolFactory)
     * @dev If the bid/ask is 9/10 and the fee is set to 1%, then the fee is calculated as follows:
     * @dev For a buy order, the fee would be the bid price multiplied by the fee rate, or 9 * 1% = 0.09
     * @dev For a sell order, the fee would be the ask price multiplied by the fee rate, or 10 * 1% = 0.1
     * @dev The fee is charged as a percentage of the bid/ask price, and it is used to cover the costs associated with running the AMM pool
     * contract and providing liquidity to the decentralized exchange. The fee is deducted from the final price of the token or NFT swap,
     * and it is paid to the contract owner or to a designated fee recipient. The exact fee rate and fee recipient can be configured by the
     * contract owner when the AMM pool contract is deployed.
     */
    uint24 internal constant MAX_FEE = 0.9e6;

    // The spread between buy and sell prices, set to be a multiplier we apply to the buy price
    // Fee is only relevant for TRADE pools
    // Units are in base 1e6
    uint24 public fee;

    // For every NFT swapped, a fraction of the cost will be sent to the
    // ERC2981 payable address for the NFT swapped. The fraction is equal to
    // `royaltyNumerator / 1e6`
    uint24 public royaltyNumerator;

    uint24 internal constant MAX_ROYALTY_NUMERATOR = 1e6;

    // An address to which all royalties will be paid to if not address(0). This
    // is a fallback to ERC2981 royalties set by the NFT creator, and allows sending
    // royalties to arbitrary addresses if a collection does not support ERC2981.
    address payable public royaltyRecipientFallback;

    uint256 internal constant POOL_SWAP_PAUSE = 0;

    // The current price of the NFT
    // @dev This is generally used to mean the immediate sell price for the next marginal NFT.
    // However, this should NOT be assumed, as future bonding curves may use spotPrice in different ways.
    // Use getBuyNFTQuote and getSellNFTQuote for accurate pricing info.
    uint128 internal spotPrice;

    // The parameter for the pool's bonding curve.
    // Units and meaning are bonding curve dependent.
    uint128 internal delta;

    // If set to 0, NFTs/tokens sent by traders during trades will be sent to the pool.
    // Otherwise, assets will be sent to the set address. Not available for TRADE pools.
    address payable public assetRecipient;

    // The trade fee accrued from trades.
    uint256 public accruedTradeFee;

    // The properties used by the pool's bonding curve.
    bytes internal props;

    // The state used by the pool's bonding curve.
    bytes internal state;

    // If non-zero, contract implementing IExternalFilter checked on (pool buying) swaps
    IExternalFilter public externalFilter;

    // Events
    event SwapNFTInPool(
        uint256[] nftIds, uint256 inputAmount, uint256 tradeFee, uint256 protocolFee, RoyaltyDue[] royaltyDue
    );
    event SwapNFTOutPool(
        uint256[] nftIds, uint256 outputAmount, uint256 tradeFee, uint256 protocolFee, RoyaltyDue[] royaltyDue
    );
    event SpotPriceUpdate(uint128 newSpotPrice);
    event TokenDeposit(IERC721 indexed collection, ERC20 indexed token, uint256 amount);
    event TokenWithdrawal(IERC721 indexed collection, ERC20 indexed token, uint256 amount);
    event AccruedTradeFeeWithdrawal(IERC721 indexed collection, ERC20 token, uint256 amount);
    event NFTDeposit(IERC721 indexed collection, uint256 numNFTs, uint256 rawBuyPrice, uint256 rawSellPrice);
    event NFTWithdrawal(IERC721 indexed collection, uint256 numNFTs, uint256 rawBuyPrice, uint256 rawSellPrice);
    event DeltaUpdate(uint128 newDelta);
    event FeeUpdate(uint96 newFee);
    event AssetRecipientChange(address indexed a);
    event PropsUpdate(bytes newProps);
    event StateUpdate(bytes newState);
    event RoyaltyNumeratorUpdate(uint24 newRoyaltyNumerator);
    event RoyaltyRecipientFallbackUpdate(address payable indexed newFallback);
    event PoolSwapPause();
    event PoolSwapUnpause();
    event ExternalFilterSet(address indexed collection, address indexed filterAddress);

    // Parameterized Errors
    error InsufficientLiquidity(uint256 balance, uint256 accruedTradeFee);
    error RoyaltyNumeratorOverflow();
    error SwapsArePaused();
    error InvalidPoolParams();
    error NotAuthorized();
    error InvalidModification();
    error InvalidSwapQuantity();
    error InvalidSwap();
    error SlippageExceeded();
    error RouterNotTrusted();
    error NFTsNotAccepted();
    error NFTsNotAllowed();
    error CallNotAllowed();
    error CallError(bytes returnData);
    error MulticallError();
    error InvalidExternalFilter();
    error UseWithdrawERC721Instead();

    modifier onlyFactory() {
        if (msg.sender != address(factory())) revert NotAuthorized();
        _;
    }

    /**
     * @dev Use this whenever modifying the value of royaltyNumerator.
     */
    modifier validRoyaltyNumerator(uint24 _royaltyNumerator) {
        if (_royaltyNumerator >= MAX_ROYALTY_NUMERATOR) revert RoyaltyNumeratorOverflow();
        _;
    }

    function assertPoolSwapsNotPaused() internal view {
        if (poolSwapsPaused()) revert SwapsArePaused();
    }

    function assertDepositsNotPaused() internal view {
        if (factory().depositPaused()) revert SwapsArePaused();
    }

    function poolSwapsPaused() public view returns (bool) {
        return factory().swapPaused() || isPaused(POOL_SWAP_PAUSE);
    }

    /**
     * Ownable functions
     */

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

    /// @dev Throws if called by any account other than the owner.
    modifier onlyOwner() {
        if (msg.sender != owner()) revert NotAuthorized();
        _;
    }

    /// @dev Throws if called by accounts that were not authorized by the owner.
    function requireAuthorized() internal view {
        factory().requireAuthorizedForToken(msg.sender, tokenId());
    }

    /// @dev Transfers ownership of the contract to a new account (`newOwner`).
    /// Disallows setting to the zero address as a way to more gas-efficiently avoid reinitialization
    /// When ownership is transferred, if the new owner implements IOwnershipTransferCallback, we make a callback
    /// Can only be called by the current owner.
    function transferOwnership(address newOwner) public virtual onlyOwner {
        factory().safeTransferFrom(msg.sender, newOwner, tokenId());
    }

    /**
     * @notice Called during pool creation to set initial parameters
     * @dev Only called once by factory to initialize.
     * We verify this by making sure that the current owner is address(0).
     * The Ownable library we use disallows setting the owner to be address(0), so this condition
     * should only be valid before the first initialize call.
     * @param _assetRecipient The address that will receive the TOKEN or NFT sent to this pool during swaps.
     * NOTE: If set to address(0), they will go to the pool itself.
     * @param _delta The initial delta of the bonding curve
     * @param _fee The initial % fee taken, if this is a trade pool
     * @param _spotPrice The initial price to sell an asset into the pool
     * @param _royaltyNumerator All trades will result in `royaltyNumerator` * <trade amount> / 1e6
     * being sent to the account to which the traded NFT's royalties are awardable.
     * Must be 0 if `_nft` is not IERC2981 and no recipient fallback is set.
     * @param _royaltyRecipientFallback An address to which all royalties will be paid to if not address(0).
     * This is a fallback to ERC2981 royalties set by the NFT creator, and allows sending royalties to
     * arbitrary addresses if a collection does not support ERC2981.
     */
    function initialize(
        address payable _assetRecipient,
        uint128 _delta,
        uint24 _fee,
        uint128 _spotPrice,
        bytes calldata _props,
        bytes calldata _state,
        uint24 _royaltyNumerator,
        address payable _royaltyRecipientFallback
    ) external payable validRoyaltyNumerator(_royaltyNumerator) {
        // Do not initialize if already initialized
        if (initialized) revert InvalidPoolParams();
        initialized = true;
        __ReentrancyGuard_init();

        ICurve _bondingCurve = bondingCurve();
        PoolType _poolType = poolType();

        if ((_poolType == PoolType.TOKEN) || (_poolType == PoolType.NFT)) {
            // Only Trade Pools can have nonzero fee
            if (_fee != 0) revert InvalidPoolParams();
            assetRecipient = _assetRecipient;
        } else if (_poolType == PoolType.TRADE) {
            // Trade fee must be less than 90%
            if (_fee >= MAX_FEE) revert InvalidPoolParams();
            // Trade pools can't set asset recipient
            if (_assetRecipient != address(0)) revert InvalidPoolParams();
            fee = _fee;
        }
        if (!_bondingCurve.validate(_delta, _spotPrice, _props, _state)) revert InvalidPoolParams();
        delta = _delta;
        spotPrice = _spotPrice;
        props = _props;
        state = _state;
        royaltyNumerator = _royaltyNumerator;
        royaltyRecipientFallback = _royaltyRecipientFallback;
    }

    /**
     * External state-changing functions
     */

    /**
     * @notice Sets NFT token ID filter to allow only some NFTs into this pool. Pool must be empty
     * to call this function. This filter is checked on deposits and swapping NFTs into the pool.
     * Selling into the pool may require an additional check (see `setExternalFilter`).
     * @param merkleRoot Merkle root representing all allowed IDs
     * @param encodedTokenIDs Opaque encoded list of token IDs
     */
    function setTokenIDFilter(bytes32 merkleRoot, bytes calldata encodedTokenIDs) external {
        if (msg.sender != address(factory()) && msg.sender != owner()) revert NotAuthorized();
        // Pool must be empty to change filter
        if (nft().balanceOf(address(this)) != 0) revert InvalidModification();
        _setRootAndEmitAcceptedIDs(address(nft()), merkleRoot, encodedTokenIDs);
    }

    /**
     * @notice Sets an external contract that is consulted before any NFT is swapped into the pool.
     * Typically used to implement dynamic blocklists. Because it is dynamic, deposits are not
     * checked. See also `setTokenIDFilter`.
     */
    function setExternalFilter(address provider) external {
        if (msg.sender != address(factory()) && msg.sender != owner()) revert NotAuthorized();
        if (provider == address(0)) {
            externalFilter = IExternalFilter(provider);
            emit ExternalFilterSet(address(nft()), address(0));
            return;
        }

        if (isContract(provider)) {
            if (contractImplementsInterface(provider, type(IExternalFilter).interfaceId)) {
                externalFilter = IExternalFilter(provider);
                emit ExternalFilterSet(address(nft()), provider);
                return;
            }
        }

        revert InvalidExternalFilter();
    }

    /**
     * @notice Sends token to the pool in exchange for any `numNFTs` NFTs
     * @dev To compute the amount of token to send, call bondingCurve.getBuyInfo.
     * This swap function is meant for users who are ID agnostic
     * @dev The nonReentrant modifier is in swapTokenForSpecificNFTs
     * @param numNFTs The number of NFTs to purchase
     * @param maxExpectedTokenInput The maximum acceptable cost from the sender. If the actual
     * amount is greater than this value, the transaction will be reverted.
     * @param nftRecipient The recipient of the NFTs
     * @param isRouter True if calling from CollectionRouter, false otherwise. Not used for
     * ETH pools.
     * @param routerCaller If isRouter is true, ERC20 tokens will be transferred from this address. Not used for
     * ETH pools.
     * @return inputAmount The amount of token used for purchase
     */
    function swapTokenForAnyNFTs(
        uint256 numNFTs,
        uint256 maxExpectedTokenInput,
        address nftRecipient,
        bool isRouter,
        address routerCaller
    ) external payable virtual returns (uint256 inputAmount) {
        IERC721 _nft = nft();
        uint256[] memory tokenIds = _selectArbitraryNFTs(_nft, numNFTs);
        if (tokenIds.length == 0) revert InvalidSwapQuantity();
        inputAmount = swapTokenForSpecificNFTs(tokenIds, maxExpectedTokenInput, nftRecipient, isRouter, routerCaller);
    }

    /**
     * @notice Sends token to the pool in exchange for a specific set of NFTs
     * @dev To compute the amount of token to send, call bondingCurve.getBuyInfo
     * This swap is meant for users who want specific IDs. Also higher chance of
     * reverting if some of the specified IDs leave the pool before the swap goes through.
     * @param nftIds The list of IDs of the NFTs to purchase
     * @param maxExpectedTokenInput The maximum acceptable cost from the sender. If the actual
     * amount is greater than this value, the transaction will be reverted.
     * @param nftRecipient The recipient of the NFTs
     * @param isRouter True if calling from CollectionRouter, false otherwise. Not used for
     * ETH pools.
     * @param routerCaller If isRouter is true, ERC20 tokens will be transferred from this address. Not used for
     * ETH pools.
     * @return inputAmount The amount of token used for purchase
     */
    function swapTokenForSpecificNFTs(
        uint256[] memory nftIds,
        uint256 maxExpectedTokenInput,
        address nftRecipient,
        bool isRouter,
        address routerCaller
    ) public payable virtual nonReentrant returns (uint256 inputAmount) {
        assertPoolSwapsNotPaused();
        // Input validation
        // Can only buy from NFT or two sided pools
        if (poolType() == PoolType.TOKEN) revert InvalidSwap();
        if (nftIds.length == 0) revert InvalidSwapQuantity();
        // Prevent users from making a ridiculous pool, buying out their "sucker" price, and
        // then staking this pool with liquidity at really bad prices into a reward vault.
        if (isInCreationBlock()) revert InvalidSwap();

        // Call bonding curve for pricing information
        ICurve.Fees memory fees;
        uint256 lastSwapPrice;
        (inputAmount, fees, lastSwapPrice) =
            _calculateBuyInfoAndUpdatePoolParams(nftIds.length, maxExpectedTokenInput, bondingCurve());

        accruedTradeFee += fees.trade;
        RoyaltyDue[] memory royaltiesDue = _getRoyaltiesDue(nft(), nftIds, fees.royalties);

        _pullTokenInputAndPayProtocolFee(inputAmount, isRouter, routerCaller, factory(), fees.protocol, royaltiesDue);

        _withdrawNFTs(nftRecipient, nftIds);

        _refundTokenToSender(inputAmount);

        emit SwapNFTOutPool(nftIds, inputAmount, fees.trade, fees.protocol, royaltiesDue);

        notifySwap(EventType.BOUGHT_NFT_FROM_POOL, nftIds.length, lastSwapPrice, inputAmount);
    }

    /**
     * @notice Sends a set of NFTs to the pool in exchange for token. Token must be allowed by
     * filters, see `setTokenIDFilter` and `setExternalFilter`.
     * @dev To compute the amount of token to that will be received, call bondingCurve.getSellInfo.
     * @param nfts The list of IDs of the NFTs to sell to the pool along with its Merkle multiproof.
     * @param minExpectedTokenOutput The minimum acceptable token received by the sender. If the actual
     * amount is less than this value, the transaction will be reverted.
     * @param tokenRecipient The recipient of the token output
     * @param isRouter True if calling from CollectionRouter, false otherwise. Not used for
     * ETH pools.
     * @param routerCaller If isRouter is true, ERC20 tokens will be transferred from this address. Not used for
     * ETH pools.
     * @return outputAmount The amount of token received
     */
    function swapNFTsForToken(
        NFTs calldata nfts,
        uint256 minExpectedTokenOutput,
        address payable tokenRecipient,
        bool isRouter,
        address routerCaller,
        bytes calldata externalFilterContext
    ) external virtual nonReentrant returns (uint256 outputAmount) {
        assertPoolSwapsNotPaused();
        // Store locally to remove extra calls
        ICollectionPoolFactory _factory = factory();

        // Input validation
        PoolType _poolType = poolType();
        // Can only sell to Token / 2-sided pools
        if (_poolType == PoolType.NFT) revert InvalidSwap();
        if (nfts.ids.length == 0) revert InvalidSwapQuantity();
        if (!acceptsTokenIDs(nfts.ids, nfts.proof, nfts.proofFlags)) revert NFTsNotAccepted();
        if (
            address(externalFilter) != address(0)
                && !externalFilter.areNFTsAllowed(address(nft()), nfts.ids, externalFilterContext)
        ) {
            revert NFTsNotAllowed();
        }

        // Prevent users from making a ridiculous pool, buying out their "sucker" price, and
        // then staking this pool with liquidity at really bad prices into a reward vault.
        if (isInCreationBlock()) revert InvalidSwap();

        // Call bonding curve for pricing information
        ICurve.Fees memory fees;
        uint256 lastSwapPrice;
        (outputAmount, fees, lastSwapPrice) =
            _calculateSellInfoAndUpdatePoolParams(nfts.ids.length, minExpectedTokenOutput, bondingCurve());

        // Accrue trade fees before sending token output. This ensures that the balance is always sufficient for trade fee withdrawal.
        accruedTradeFee += fees.trade;

        RoyaltyDue[] memory royaltiesDue = _getRoyaltiesDue(nft(), nfts.ids, fees.royalties);

        _sendTokenOutputAndPayProtocolFees(_factory, tokenRecipient, outputAmount, royaltiesDue, fees.protocol);

        _takeNFTsFromSender(nfts.ids, _factory, isRouter, routerCaller);

        emit SwapNFTInPool(nfts.ids, outputAmount, fees.trade, fees.protocol, royaltiesDue);

        notifySwap(EventType.SOLD_NFT_TO_POOL, nfts.ids.length, lastSwapPrice, outputAmount);
    }

    function balanceToFulfillSellNFT(uint256 numNFTs) external view returns (uint256 balance) {
        uint256 totalAmount;
        (, totalAmount,,) = getSellNFTQuote(numNFTs);
        balance = accruedTradeFee + totalAmount;
    }

    /**
     * View functions
     */

    function getRawBuyAndSellPrices() public view returns (uint256 rawBuyPrice, uint256 rawSellPrice) {
        (, rawBuyPrice,,) = bondingCurve().getBuyInfo(
            curveParams(), 1, ICurve.FeeMultipliers({trade: 0, protocol: 0, royaltyNumerator: 0, carry: 0})
        );
        (, rawSellPrice,,) = bondingCurve().getSellInfo(
            curveParams(), 1, ICurve.FeeMultipliers({trade: 0, protocol: 0, royaltyNumerator: 0, carry: 0})
        );
    }

    /**
     * @notice Checks if NFTs is allowed in this pool
     * @param tokenID NFT ID
     * @param proof Merkle proof
     */
    function acceptsTokenID(uint256 tokenID, bytes32[] calldata proof) public view returns (bool) {
        return _acceptsTokenID(tokenID, proof);
    }

    /**
     * @notice Checks if list of NFTs are allowed in this pool using Merkle multiproof and flags
     * @param tokenIDs List of NFT IDs
     * @param proof Merkle multiproof
     * @param proofFlags Merkle multiproof flags
     */
    function acceptsTokenIDs(uint256[] calldata tokenIDs, bytes32[] calldata proof, bool[] calldata proofFlags)
        public
        view
        returns (bool)
    {
        return _acceptsTokenIDs(tokenIDs, proof, proofFlags);
    }

    /**
     * @dev Used as read function to query the bonding curve for buy pricing info
     * @param numNFTs The number of NFTs to buy from the pool
     */
    function getBuyNFTQuote(uint256 numNFTs)
        public
        view
        returns (ICurve.Params memory newParams, uint256 totalAmount, uint256 inputAmount, ICurve.Fees memory fees)
    {
        (newParams, inputAmount, fees,) = bondingCurve().getBuyInfo(curveParams(), numNFTs, feeMultipliers());

        // Since inputAmount is already inclusive of fees.
        totalAmount = inputAmount;
    }

    /**
     * @dev Used as read function to query the bonding curve for sell pricing info
     * @param numNFTs The number of NFTs to sell to the pool
     */
    function getSellNFTQuote(uint256 numNFTs)
        public
        view
        returns (ICurve.Params memory newParams, uint256 totalAmount, uint256 outputAmount, ICurve.Fees memory fees)
    {
        (newParams, outputAmount, fees,) = bondingCurve().getSellInfo(curveParams(), numNFTs, feeMultipliers());

        totalAmount = outputAmount + fees.trade + fees.protocol;
        uint256 length = fees.royalties.length;
        for (uint256 i; i < length;) {
            totalAmount += fees.royalties[i];
            unchecked {
                ++i;
            }
        }
    }

    /**
     * @notice Returns all NFT IDs held by the pool
     */
    function getAllHeldIds() external view virtual returns (uint256[] memory);

    /**
     * @notice Returns LP token ID for this pool
     */
    function tokenId() public view returns (uint256 _tokenId) {
        _tokenId = uint160(address(this));
    }

    /**
     * @notice Returns the pool's variant (NFT is enumerable or not, pool uses ETH or ERC20)
     */
    function poolVariant() public pure virtual returns (PoolVariant);

    function factory() public pure returns (ICollectionPoolFactory _factory) {
        uint256 paramsLength = _immutableParamsLength();
        assembly {
            _factory := shr(0x60, calldataload(sub(calldatasize(), paramsLength)))
        }
    }

    /**
     * @notice Returns the type of bonding curve that parameterizes the pool
     */
    function bondingCurve() public pure returns (ICurve _bondingCurve) {
        uint256 paramsLength = _immutableParamsLength();
        assembly {
            _bondingCurve := shr(0x60, calldataload(add(sub(calldatasize(), paramsLength), 20)))
        }
    }

    /**
     * @notice Returns the NFT collection that parameterizes the pool
     */
    function nft() public pure returns (IERC721 _nft) {
        uint256 paramsLength = _immutableParamsLength();
        assembly {
            _nft := shr(0x60, calldataload(add(sub(calldatasize(), paramsLength), 40)))
        }
    }

    /**
     * @notice Returns the pool's type (TOKEN/NFT/TRADE)
     */
    function poolType() public pure returns (PoolType _poolType) {
        uint256 paramsLength = _immutableParamsLength();
        assembly {
            _poolType := shr(0xf8, calldataload(add(sub(calldatasize(), paramsLength), 60)))
        }
    }

    function isInCreationBlock() private view returns (bool _isInCreationBlock) {
        uint256 paramsLength = _immutableParamsLength();
        uint256 _creationBlockNumber;

        assembly {
            _creationBlockNumber := shr(0xe0, calldataload(add(sub(calldatasize(), paramsLength), 61)))
        }
        // Only the (lower) 32 bits are stored (~2000 years with 15s blocks). We compare with uint32(block.number)
        // so we can still detect if we're in the same block in the unlikely event of an overflow
        _isInCreationBlock = uint32(_creationBlockNumber) == uint32(block.number);
    }

    /**
     * @notice Handles royalty recipient and fallback logic. Attempts to honor
     * ERC2981 where possible, followed by the owner's set fallback. If neither
     * is a valid address, then royalties go to the asset recipient for this
     * pool.
     * @param erc2981Recipient The address to which royalties should be paid as
     * returned by the IERC2981 `royaltyInfo` method. `payable(address(0))` if
     * the nft does not implement IERC2981.
     * @return The address to which royalties should be paid
     */
    function getRoyaltyRecipient(address payable erc2981Recipient) public view returns (address payable) {
        if (erc2981Recipient != address(0)) {
            return erc2981Recipient;
        }
        // No recipient from ERC2981 royaltyInfo method. Check if we have a fallback
        else if (royaltyRecipientFallback != address(0)) {
            return royaltyRecipientFallback;
        }
        // No ERC2981 recipient or recipient fallback. Default to pool's assetRecipient.
        else {
            return getAssetRecipient();
        }
    }

    /**
     * @notice Returns the address that assets that receives assets when a swap is done with this pool
     * Can be set to another address by the owner, if set to address(0), defaults to the pool's own address
     */
    function getAssetRecipient() public view returns (address payable _assetRecipient) {
        // If it's a TRADE pool, we know the recipient is 0 (TRADE pools can't set asset recipients)
        // so just return address(this)
        if (poolType() == PoolType.TRADE) {
            return payable(address(this));
        }

        // Otherwise, we return the recipient if it's been set
        // or replace it with address(this) if it's 0
        _assetRecipient = assetRecipient;
        if (_assetRecipient == address(0)) {
            // Tokens will be transferred to address(this)
            _assetRecipient = payable(address(this));
        }
    }

    function curveParams() public view returns (ICurve.Params memory params) {
        return ICurve.Params(spotPrice, delta, props, state);
    }

    function feeMultipliers() public view returns (ICurve.FeeMultipliers memory) {
        uint24 protocolFeeMultiplier;
        uint24 carryFeeMultiplier;

        PoolType _poolType = poolType();
        if ((_poolType == PoolType.TOKEN) || (_poolType == PoolType.NFT)) {
            protocolFeeMultiplier = factory().protocolFeeMultiplier();
        } else if (_poolType == PoolType.TRADE) {
            carryFeeMultiplier = factory().carryFeeMultiplier();
        }

        return ICurve.FeeMultipliers(fee, protocolFeeMultiplier, royaltyNumerator, carryFeeMultiplier);
    }

    /**
     * Internal functions
     */

    /**
     * Taken from https://eips.ethereum.org/EIPS/eip-165#how-to-detect-if-a-contract-implements-any-given-interface
     */
    function contractImplementsInterface(address _contract, bytes4 _interfaceId) internal view returns (bool) {
        uint256 success;
        uint256 result;

        (success, result) = noThrowCall(_contract, 0x01ffc9a7);
        if ((success == 0) || (result == 0)) {
            return false;
        }

        (success, result) = noThrowCall(_contract, 0xffffffff);
        if ((success == 0) || (result != 0)) {
            return false;
        }

        (success, result) = noThrowCall(_contract, _interfaceId);
        if ((success == 1) && (result == 1)) {
            return true;
        }
        return false;
    }

    /**
     * Taken from https://eips.ethereum.org/EIPS/eip-165#how-to-detect-if-a-contract-implements-any-given-interface
     */
    function noThrowCall(address _contract, bytes4 _interfaceId)
        internal
        view
        returns (uint256 success, uint256 result)
    {
        bytes4 erc165ID = 0x01ffc9a7; // ERC165 ID

        assembly {
            let x := mload(0x40) // Find empty storage location using "free memory pointer"
            mstore(x, erc165ID) // Place signature at beginning of empty storage (ERC165 ID)
            mstore(add(x, 0x04), _interfaceId) // Place first argument directly next to signature

            success := staticcall(30000, _contract, x, 0x24, x, 0x20)

            result := mload(x) // Load the result
        }
    }

    /**
     * @notice Calculates the amount needed to be sent into the pool for a buy and adjusts spot price or delta if necessary
     * @param numNFTs The amount of NFTs to purchase from the pool
     * @param maxExpectedTokenInput The maximum acceptable cost from the sender. If the actual
     * amount is greater than this value, the transaction will be reverted.
     * @return inputAmount The amount of tokens total tokens receive
     * @return fees The amount of tokens to send as fees
     * @return lastSwapPrice The swap price of the last NFT traded with fees applied
     */
    function _calculateBuyInfoAndUpdatePoolParams(uint256 numNFTs, uint256 maxExpectedTokenInput, ICurve _bondingCurve)
        internal
        returns (uint256 inputAmount, ICurve.Fees memory fees, uint256 lastSwapPrice)
    {
        ICurve.Params memory params = curveParams();
        ICurve.Params memory newParams;
        (newParams, inputAmount, fees, lastSwapPrice) = _bondingCurve.getBuyInfo(params, numNFTs, feeMultipliers());

        // Revert if input is more than expected
        if (inputAmount > maxExpectedTokenInput) revert SlippageExceeded();

        _updatePoolParams(params, newParams);
    }

    /**
     * @notice Calculates the amount needed to be sent by the pool for a sell and adjusts spot price or delta if necessary
     * @param numNFTs The amount of NFTs to send to the the pool
     * @param minExpectedTokenOutput The minimum acceptable token received by the sender. If the actual
     * amount is less than this value, the transaction will be reverted.
     * @param _bondingCurve The bonding curve used to fetch pricing information from
     * @return outputAmount The amount of tokens total tokens receive
     * @return fees The amount of tokens to send as fees
     * @return lastSwapPrice The swap price of the last NFT traded with fees applied
     */
    function _calculateSellInfoAndUpdatePoolParams(
        uint256 numNFTs,
        uint256 minExpectedTokenOutput,
        ICurve _bondingCurve
    ) internal returns (uint256 outputAmount, ICurve.Fees memory fees, uint256 lastSwapPrice) {
        ICurve.Params memory params = curveParams();
        ICurve.Params memory newParams;
        (newParams, outputAmount, fees, lastSwapPrice) = _bondingCurve.getSellInfo(params, numNFTs, feeMultipliers());

        // Revert if output is too little
        if (outputAmount < minExpectedTokenOutput) revert SlippageExceeded();

        _updatePoolParams(params, newParams);
    }

    function _updatePoolParams(ICurve.Params memory params, ICurve.Params memory newParams) internal {
        // Consolidate writes to save gas
        if (params.spotPrice != newParams.spotPrice || params.delta != newParams.delta) {
            spotPrice = newParams.spotPrice;
            delta = newParams.delta;
        }

        if (keccak256(params.state) != keccak256(newParams.state)) {
            state = newParams.state;

            emit StateUpdate(newParams.state);
        }

        // Emit spot price update if it has been updated
        if (params.spotPrice != newParams.spotPrice) {
            emit SpotPriceUpdate(newParams.spotPrice);
        }

        // Emit delta update if it has been updated
        if (params.delta != newParams.delta) {
            emit DeltaUpdate(newParams.delta);
        }
    }

    /**
     * @notice Pulls the token input of a trade from the trader and pays the protocol fee.
     * @param inputAmount The amount of tokens to be sent
     * @param isRouter Whether or not the caller is CollectionRouter
     * @param routerCaller If called from CollectionRouter, store the original caller
     * @param _factory The CollectionPoolFactory which stores CollectionRouter allowlist info
     * @param protocolFee The protocol fee to be paid
     * @param royaltyAmounts An array of royalties to pay
     */
    function _pullTokenInputAndPayProtocolFee(
        uint256 inputAmount,
        bool isRouter,
        address routerCaller,
        ICollectionPoolFactory _factory,
        uint256 protocolFee,
        RoyaltyDue[] memory royaltyAmounts
    ) internal virtual;

    /**
     * @notice Sends excess tokens back to the caller (if applicable)
     * @dev We send ETH back to the caller even when called from CollectionRouter because we do an aggregate slippage check for certain bulk swaps. (Instead of sending directly back to the router caller)
     * Excess ETH sent for one swap can then be used to help pay for the next swap.
     */
    function _refundTokenToSender(uint256 inputAmount) internal virtual;

    /**
     * @notice Sends tokens to a recipient, pays protocol fees and royalties owed
     * @param tokenRecipient The address receiving the tokens
     * @param outputAmount The amount of tokens to send
     * @param royaltiesDue An array of royalties to pay
     * @param protocolFee The protocol fee to pay
     */
    function _sendTokenOutputAndPayProtocolFees(
        ICollectionPoolFactory _factory,
        address payable tokenRecipient,
        uint256 outputAmount,
        RoyaltyDue[] memory royaltiesDue,
        uint256 protocolFee
    ) internal virtual;

    /**
     * @notice Select arbitrary NFTs from pool
     * @param _nft The address of the NFT to send
     * @param numNFTs The number of NFTs to send
     * @return tokenIds Returns numNFTs IDs or [] if insufficent
     */
    function _selectArbitraryNFTs(IERC721 _nft, uint256 numNFTs) internal virtual returns (uint256[] memory tokenIds);

    /**
     * @notice Takes NFTs from the caller and sends them into the pool's asset recipient
     * @dev This is used by the CollectionPool's swapNFTForToken function.
     * @param nftIds The specific NFT IDs to take
     * @param isRouter True if calling from CollectionRouter, false otherwise. Not used for * ETH pools.
     * @param routerCaller If isRouter is true, ERC20 tokens will be transferred from this address. Not used for
     * ETH pools.
     */
    function _takeNFTsFromSender(
        uint256[] calldata nftIds,
        ICollectionPoolFactory _factory,
        bool isRouter,
        address routerCaller
    ) internal virtual {
        {
            address _assetRecipient = getAssetRecipient();
            uint256 numNFTs = nftIds.length;

            if (isRouter) {
                // Verify if router is allowed
                CollectionRouter router = CollectionRouter(payable(msg.sender));

                {
                    (bool routerAllowed,) = _factory.routerStatus(router);
                    if (!routerAllowed) revert RouterNotTrusted();
                }

                IERC721 _nft = nft();
                PoolVariant variant = poolVariant();

                // Call router to pull NFTs
                // If more than 1 NFT is being transfered, do balance check instead of ownership check,
                // as pools are indifferent between NFTs from the same collection
                if (numNFTs > 1) {
                    uint256 beforeBalance = _nft.balanceOf(_assetRecipient);
                    for (uint256 i = 0; i < numNFTs;) {
                        /// @dev Variable assignment here prevents stack too deep in next line
                        uint256 nftId = nftIds[i];
                        router.poolTransferNFTFrom(_nft, routerCaller, _assetRecipient, nftId, variant);

                        unchecked {
                            ++i;
                        }
                    }
                    // Check if NFT was transferred
                    if ((_nft.balanceOf(_assetRecipient) - beforeBalance) != numNFTs) revert RouterNotTrusted();
                } else {
                    router.poolTransferNFTFrom(_nft, routerCaller, _assetRecipient, nftIds[0], variant);
                    // Check if NFT was transferred
                    if (_nft.ownerOf(nftIds[0]) != _assetRecipient) revert RouterNotTrusted();
                }

                if (_assetRecipient == address(this)) {
                    _depositNFTsNotification(nftIds);
                }
            } else {
                // Pull NFTs directly from sender
                if (_assetRecipient == address(this)) {
                    _depositNFTs(msg.sender, nftIds);
                } else {
                    TransferLib.bulkSafeTransferERC721From(nft(), msg.sender, _assetRecipient, nftIds);
                }
            }
        }
    }

    /**
     * @dev Used internally to grab pool parameters from calldata, see CollectionPoolCloner for technical details
     */
    function _immutableParamsLength() internal pure virtual returns (uint256);

    /**
     * Owner functions
     */

    /// @inheritdoc ICollectionPool
    function withdrawERC721(IERC721 a, uint256[] calldata nftIds) external override {
        requireAuthorized();
        IERC721 _nft = nft();
        address _owner = owner();

        // If it's not the pool's NFT, just withdraw normally
        if (a != _nft) {
            TransferLib.bulkSafeTransferERC721From(a, address(this), _owner, nftIds);
        }
        // Otherwise, withdraw and also remove the ID from the ID set
        else {
            _withdrawNFTs(_owner, nftIds);

            (uint256 rawBuyPrice, uint256 rawSellPrice) = getRawBuyAndSellPrices();
            emit NFTWithdrawal(_nft, nftIds.length, rawBuyPrice, rawSellPrice);
            /// @dev No need to notify pool monitors as pool monitors own the pool,
            /// thus only the monitor can withdraw from the pool and notifications
            /// are redundant
        }
    }

    /**
     * @notice Rescues ERC1155 tokens from the pool to the owner. Only callable by the owner.
     * If somehow, the pool's nft() is an unholy amalgam of both ERC721 AND * ERC1155, even though
     * there is no good nor indeed any reason for this, and nft() is what we are withdrawing, abort
     * and advise usage of withdrawERC721 instead. Note that the transfer amount would be limited
     * to whatever nft().safeTransferFrom(from, to, id) defaults to.
     * @param a The NFT to transfer
     * @param ids The NFT ids to transfer
     * @param amounts The amounts of each id to transfer
     */
    function withdrawERC1155(IERC1155 a, uint256[] calldata ids, uint256[] calldata amounts) external {
        requireAuthorized();
        if (address(a) == address(nft())) revert UseWithdrawERC721Instead();

        a.safeBatchTransferFrom(address(this), owner(), ids, amounts, "");
    }

    /**
     * @notice Withdraws the accrued trade fee owned by the pool to the owner address.
     * @dev Only callable by the owner.
     */
    function withdrawAccruedTradeFee() external virtual;

    function pausePoolSwaps() external onlyOwner {
        pause(POOL_SWAP_PAUSE);
        emit PoolSwapPause();
    }

    function unpausePoolSwaps() external onlyOwner {
        unpause(POOL_SWAP_PAUSE);
        emit PoolSwapUnpause();
    }

    /**
     * @notice Updates the selling spot price. Only callable by the owner.
     * @param newSpotPrice The new selling spot price value, in Token
     */
    function changeSpotPrice(uint128 newSpotPrice) external onlyOwner {
        ICurve _bondingCurve = bondingCurve();
        if (!_bondingCurve.validateSpotPrice(newSpotPrice)) revert InvalidModification();
        if (spotPrice != newSpotPrice) {
            spotPrice = newSpotPrice;
            emit SpotPriceUpdate(newSpotPrice);
        }
    }

    /**
     * @notice Updates the delta parameter. Only callable by the owner.
     * @param newDelta The new delta parameter
     */
    function changeDelta(uint128 newDelta) external onlyOwner {
        ICurve _bondingCurve = bondingCurve();
        if (!_bondingCurve.validateDelta(newDelta)) revert InvalidModification();
        if (delta != newDelta) {
            delta = newDelta;
            emit DeltaUpdate(newDelta);
        }
    }

    /**
     * @notice Updates the props parameter. Only callable by the owner.
     * @param newProps The new props parameter
     */
    function changeProps(bytes calldata newProps) external onlyOwner {
        ICurve _bondingCurve = bondingCurve();
        if (!_bondingCurve.validateProps(newProps)) revert InvalidModification();
        if (keccak256(props) != keccak256(newProps)) {
            props = newProps;
            emit PropsUpdate(newProps);
        }
    }

    /**
     * @notice Updates the state parameter. Only callable by the owner.
     * @param newState The new state parameter
     */
    function changeState(bytes calldata newState) external onlyOwner {
        ICurve _bondingCurve = bondingCurve();
        if (!_bondingCurve.validateState(newState)) revert InvalidModification();
        if (keccak256(state) != keccak256(newState)) {
            state = newState;
            emit StateUpdate(newState);
        }
    }

    /**
     * @notice Updates the fee taken by the LP. Only callable by the owner.
     * Only callable if the pool is a Trade pool. Reverts if the fee is >=
     * MAX_FEE.
     * @param newFee The new LP fee percentage, 18 decimals
     */
    function changeFee(uint24 newFee) external onlyOwner {
        PoolType _poolType = poolType();
        // Only trade pools can set fee. Max fee must be strictly greater too.
        if (_poolType != PoolType.TRADE || newFee >= MAX_FEE) revert InvalidModification();
        if (fee != newFee) {
            fee = newFee;
            emit FeeUpdate(newFee);
        }
    }

    /**
     * @notice Changes the address that will receive assets received from
     * trades. Only callable by the owner.
     * @param newRecipient The new asset recipient
     */
    function changeAssetRecipient(address payable newRecipient) external onlyOwner {
        PoolType _poolType = poolType();
        // Trade pools cannot set asset recipient
        if (_poolType == PoolType.TRADE) revert InvalidModification();
        if (assetRecipient != newRecipient) {
            assetRecipient = newRecipient;
            emit AssetRecipientChange(newRecipient);
        }
    }

    function changeRoyaltyNumerator(uint24 newRoyaltyNumerator)
        external
        onlyOwner
        validRoyaltyNumerator(newRoyaltyNumerator)
    {
        // Check whether the resulting combination of numerator and fallback is valid
        if (!_validRoyaltyState(newRoyaltyNumerator, royaltyRecipientFallback, nft())) revert InvalidModification();
        royaltyNumerator = newRoyaltyNumerator;
        emit RoyaltyNumeratorUpdate(newRoyaltyNumerator);
    }

    function changeRoyaltyRecipientFallback(address payable newFallback) external onlyOwner {
        if (!_validRoyaltyState(royaltyNumerator, newFallback, nft())) revert InvalidModification();
        royaltyRecipientFallback = newFallback;
        emit RoyaltyRecipientFallbackUpdate(newFallback);
    }

    /**
     * @notice Allows the pool to make arbitrary external calls to contracts
     * whitelisted by the protocol. Only callable by authorized parties.
     * @param target The contract to call
     * @param data The calldata to pass to the contract
     */
    function call(address payable target, bytes calldata data) external returns (bytes memory) {
        requireAuthorized();
        ICollectionPoolFactory _factory = factory();
        // Only whitelisted targets can be called
        if (!_factory.callAllowed(target)) revert CallNotAllowed();
        (bool result, bytes memory returnData) = target.call{value: 0}(data);
        if (!result) revert CallError(returnData);
        return returnData;
    }

    /**
     * @notice Allows owner to batch multiple calls, forked from: https://github.com/boringcrypto/BoringSolidity/blob/master/contracts/BoringBatchable.sol
     * @dev Intended for withdrawing/altering pool pricing in one tx, only callable by owner, cannot change owner
     * @param calls The calldata for each call to make
     * @param revertOnFail Whether or not to revert the entire tx if any of the calls fail
     */
    function multicall(bytes[] calldata calls, bool revertOnFail) external returns (bytes[] memory results) {
        requireAuthorized();
        bool success;
        uint256 length = calls.length;
        results = new bytes[](length);
        for (uint256 i; i < length;) {
            (success, results[i]) = address(this).delegatecall(calls[i]);

            if (!success && revertOnFail) {
                revert(string.concat(i.toString(), ": ", _getRevertMsg(results[i])));
            }

            unchecked {
                ++i;
            }
        }

        // Prevent multicall from malicious frontend sneaking in ownership change
        if (owner() != msg.sender) revert MulticallError();
    }

    /**
     * @param _returnData The data returned from a multicall result
     * @dev Used to grab the revert string from the underlying call
     * Taken from: https://ethereum.stackexchange.com/questions/83528/how-can-i-get-the-revert-reason-of-a-call-in-solidity-so-that-i-can-use-it-in-th
     */
    function _getRevertMsg(bytes memory _returnData) internal pure returns (string memory) {
        // If the _res length is less than 68, then the transaction failed silently (without a revert message)
        if (_returnData.length < 68) return "Transaction reverted silently";

        assembly {
            // Slice the sighash.
            _returnData := add(_returnData, 0x04)
        }
        return abi.decode(_returnData, (string)); // All that remains is the revert string
    }

    function _getRoyaltiesDue(IERC721 _nft, uint256[] memory nftIds, uint256[] memory royaltyAmounts)
        private
        view
        returns (RoyaltyDue[] memory royaltiesDue)
    {
        uint256 length = royaltyAmounts.length;
        royaltiesDue = new RoyaltyDue[](length);
        bool is2981 = IERC165(_nft).supportsInterface(_INTERFACE_ID_ERC2981);
        if (royaltyNumerator != 0) {
            if (is2981) {
                for (uint256 i = 0; i < length;) {
                    // 2981 recipient, if nft is 2981 and recipient is set.
                    (address recipient2981,) = IERC2981(address(_nft)).royaltyInfo(nftIds[i], 0);
                    address recipient = getRoyaltyRecipient(payable(recipient2981));
                    royaltiesDue[i] = RoyaltyDue({amount: royaltyAmounts[i], recipient: recipient});

                    unchecked {
                        ++i;
                    }
                }
            } else {
                for (uint256 i = 0; i < length;) {
                    address recipient = getRoyaltyRecipient(payable(address(0)));
                    royaltiesDue[i] = RoyaltyDue({amount: royaltyAmounts[i], recipient: recipient});

                    unchecked {
                        ++i;
                    }
                }
            }
        }
    }

    /**
     * @notice Returns true if it's valid to set the contract variables to the
     * variables passed to this function.
     */
    function _validRoyaltyState(uint256 _royaltyNumerator, address payable _royaltyRecipientFallback, IERC721 _nft)
        internal
        view
        returns (bool)
    {
        return
        // Royalties will not be paid
        (
            _royaltyNumerator == 0
            // There is a fallback so we always know where to send royaltiers or
            || _royaltyRecipientFallback != address(0)
            // Supports 2981 interface to tell us who gets royalties or
            || IERC165(_nft).supportsInterface(_INTERFACE_ID_ERC2981)
        );
    }

    function notifySwap(EventType eventType, uint256 numNFTs, uint256 lastSwapPrice, uint256 swapValue) internal {
        uint256[] memory amounts = new uint256[](3);
        amounts[0] = numNFTs;
        amounts[1] = lastSwapPrice;
        amounts[2] = swapValue;

        notifyChanges(eventType, amounts);
    }

    function notifyDeposit(EventType eventType, uint256 amount) internal {
        uint256[] memory amounts = new uint256[](1);
        amounts[0] = amount;

        notifyChanges(eventType, amounts);
    }

    /**
     * @dev The only limitation of this function is that contracts calling `isContract`
     * from within their constructor will have extcodesize 0 and thus return false.
     * Thus, note that this function should not be used indirectly by any contract
     * constructors
     */
    function isContract(address _addr) private view returns (bool) {
        uint32 size;
        assembly {
            size := extcodesize(_addr)
        }
        return (size > 0);
    }

    function notifyChanges(EventType eventType, uint256[] memory amounts) internal {
        if (isContract(owner())) {
            if (contractImplementsInterface(owner(), type(IPoolActivityMonitor).interfaceId)) {
                IPoolActivityMonitor(owner()).onBalancesChanged(address(this), eventType, amounts);
            }
        }
    }

    function onERC721Received(address, address, uint256, bytes memory) public virtual returns (bytes4) {
        return this.onERC721Received.selector;
    }

    /// @inheritdoc ICollectionPool
    function depositNFTsNotification(uint256[] calldata nftIds) external override onlyFactory {
        _depositNFTsNotification(nftIds);

        (uint256 rawBuyPrice, uint256 rawSellPrice) = getRawBuyAndSellPrices();
        emit NFTDeposit(nft(), nftIds.length, rawBuyPrice, rawSellPrice);
        notifyDeposit(EventType.DEPOSIT_NFT, nftIds.length);
    }

    function depositNFTs(uint256[] calldata nftIds, bytes32[] calldata proof, bool[] calldata proofFlags) external {
        assertDepositsNotPaused();
        if (!acceptsTokenIDs(nftIds, proof, proofFlags)) revert NFTsNotAccepted();
        _depositNFTs(msg.sender, nftIds);

        (uint256 rawBuyPrice, uint256 rawSellPrice) = getRawBuyAndSellPrices();
        emit NFTDeposit(nft(), nftIds.length, rawBuyPrice, rawSellPrice);

        notifyDeposit(EventType.DEPOSIT_NFT, nftIds.length);
    }

    /**
     * @dev Deposit NFTs from given address. NFT IDs must have been validated against the filter.
     */
    function _depositNFTs(address from, uint256[] calldata nftIds) internal virtual;

    /**
     * @dev Used to indicate deposited NFTs.
     */
    function _depositNFTsNotification(uint256[] calldata nftIds) internal virtual;

    /**
     * @notice Sends specific NFTs to a recipient address
     * @param to The receiving address for the NFTs
     * @param nftIds The specific IDs of NFTs to send
     */
    function _withdrawNFTs(address to, uint256[] memory nftIds) internal virtual;
}

File 9 of 52 : CollectionPoolERC20.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {SafeTransferLib} from "solmate/src/utils/SafeTransferLib.sol";
import {ERC20} from "solmate/src/tokens/ERC20.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {ICollectionPool} from "./ICollectionPool.sol";
import {CollectionPool} from "./CollectionPool.sol";
import {ICollectionPoolFactory} from "./ICollectionPoolFactory.sol";
import {CollectionRouter} from "../routers/CollectionRouter.sol";
import {ICurve} from "../bonding-curves/ICurve.sol";
import {CurveErrorCodes} from "../bonding-curves/CurveErrorCodes.sol";
import {IPoolActivityMonitor} from "./IPoolActivityMonitor.sol";
import {PoolVariant, RoyaltyDue, EventType} from "./CollectionStructsAndEnums.sol";

/**
 * @title An NFT/Token pool where the token is an ERC20
 * @author Collection
 */
abstract contract CollectionPoolERC20 is CollectionPool {
    using SafeTransferLib for ERC20;

    error ReceivedETH();
    error UnallowedRouter(CollectionRouter router);
    error RouterDidNotSend(uint256 recipientBalanceDifference, uint256 expected);
    error InsufficientERC20Liquidity(uint256 liquidity, uint256 required);

    uint256 internal constant IMMUTABLE_PARAMS_LENGTH = 85;

    /**
     * @notice Returns the ERC20 token associated with the pool
     * @dev See CollectionPoolCloner for an explanation on how this works
     */
    function token() public pure returns (ERC20 _token) {
        uint256 paramsLength = _immutableParamsLength();
        assembly {
            _token := shr(0x60, calldataload(add(sub(calldatasize(), paramsLength), 65)))
        }
    }

    /// @inheritdoc ICollectionPool
    function liquidity() public view returns (uint256) {
        uint256 _balance = token().balanceOf(address(this));
        uint256 _accruedTradeFee = accruedTradeFee;
        if (_balance < _accruedTradeFee) revert InsufficientLiquidity(_balance, _accruedTradeFee);

        return _balance - _accruedTradeFee;
    }

    /// @inheritdoc CollectionPool
    function _pullTokenInputAndPayProtocolFee(
        uint256 inputAmount,
        bool isRouter,
        address routerCaller,
        ICollectionPoolFactory _factory,
        uint256 protocolFee,
        RoyaltyDue[] memory royaltiesDue
    ) internal override {
        if (msg.value != 0) revert ReceivedETH();

        ERC20 _token = token();
        PoolVariant variant = poolVariant();

        uint256 totalRoyaltiesPaid;
        uint256 royaltiesSentToFactory;

        if (isRouter) {
            // Verify if router is allowed
            CollectionRouter router = CollectionRouter(payable(msg.sender));

            (bool routerAllowed,) = _factory.routerStatus(router);
            if (!routerAllowed) revert UnallowedRouter(router);

            // Pay royalties first to obtain total amount of royalties paid
            (totalRoyaltiesPaid, royaltiesSentToFactory) = _payRoyaltiesAndProtocolFee(
                _factory, royaltiesDue, protocolFee, routerCaller, isRouter, router, variant
            );

            // Cache state and then call router to transfer tokens from user
            address _assetRecipient = getAssetRecipient();
            uint256 amountToAssetRecipient = inputAmount - protocolFee - royaltiesSentToFactory;
            sendTokenWithRouter(router, _token, routerCaller, _assetRecipient, amountToAssetRecipient, variant);
        } else {
            // Pay royalties first to obtain total amount of royalties paid
            (, royaltiesSentToFactory) = _payRoyaltiesAndProtocolFee(
                _factory,
                royaltiesDue,
                protocolFee,
                msg.sender,
                isRouter,
                CollectionRouter(payable(address(0))),
                variant
            );

            // Transfer tokens directly
            _token.safeTransferFrom(msg.sender, getAssetRecipient(), inputAmount - protocolFee - royaltiesSentToFactory);
        }
    }

    /// @inheritdoc CollectionPool
    function _refundTokenToSender(uint256 inputAmount) internal override {
        // Do nothing since we transferred the exact input amount
    }

    /// @inheritdoc CollectionPool
    function _sendTokenOutputAndPayProtocolFees(
        ICollectionPoolFactory _factory,
        address payable tokenRecipient,
        uint256 outputAmount,
        RoyaltyDue[] memory royaltiesDue,
        uint256 protocolFee
    ) internal override {
        ERC20 _token = token();

        /// @dev Pay royalties
        _payRoyaltiesAndProtocolFee(
            _factory,
            royaltiesDue,
            protocolFee,
            address(this),
            false,
            CollectionRouter(payable(address(0))),
            poolVariant()
        );

        /// @dev Send tokens to caller
        if (outputAmount > 0) {
            uint256 funds = liquidity();
            if (funds < outputAmount) revert InsufficientERC20Liquidity(funds, outputAmount);
            _token.safeTransfer(tokenRecipient, outputAmount);
        }
    }

    /**
     * @notice Pay royalties to the factory, which should never revert. The factory
     * serves as a single contract to which royalty recipients can make a single
     * transaction to receive all royalties due as opposed to having to send
     * transactions to arbitrary numbers of pools
     *
     * @dev For NFTs whose royalty recipients resolve to this contract, no royalties
     * are sent for them. This is to prevent royalties from becoming trapped in
     * the factory as pools do not have a function to withdraw royalties
     *
     * @return totalRoyaltiesPaid The amount of royalties which were paid including
     * royalties whose resolved recipient is this contract itself
     * @return royaltiesSentToFactory `totalRoyaltiesPaid` less the amount whose
     * resolved recipient is this contract itself
     */
    function _payRoyaltiesAndProtocolFee(
        ICollectionPoolFactory _factory,
        RoyaltyDue[] memory royaltiesDue,
        uint256 protocolFee,
        address tokenSender,
        bool isRouter,
        CollectionRouter router,
        PoolVariant poolVariant
    ) internal returns (uint256 totalRoyaltiesPaid, uint256 royaltiesSentToFactory) {
        ERC20 _token = token();
        /// @dev Local scope to prevent stack too deep
        {
            uint256 length = royaltiesDue.length;
            for (uint256 i = 0; i < length;) {
                uint256 royaltyAmount = royaltiesDue[i].amount;
                totalRoyaltiesPaid += royaltyAmount;
                if (royaltyAmount > 0) {
                    address finalRecipient = getRoyaltyRecipient(payable(royaltiesDue[i].recipient));
                    if (finalRecipient != address(this)) {
                        royaltiesSentToFactory += royaltyAmount;
                        royaltiesDue[i].recipient = finalRecipient;
                    }
                }

                unchecked {
                    ++i;
                }
            }
        }

        uint256 amountToSend = royaltiesSentToFactory + protocolFee;

        if (isRouter) {
            sendTokenWithRouter(router, _token, tokenSender, address(_factory), amountToSend, poolVariant);
        } else {
            /// @dev If tokens are being sent from this pool, just use safeTransfer
            /// to avoid making an approve call
            if (tokenSender == address(this)) {
                _token.safeTransfer(address(_factory), amountToSend);
            } else {
                _token.safeTransferFrom(tokenSender, address(_factory), amountToSend);
            }
        }

        _factory.depositRoyaltiesNotification(_token, royaltiesDue, poolVariant);
    }

    /// @inheritdoc CollectionPool
    // @dev see CollectionPoolCloner for params length calculation
    function _immutableParamsLength() internal pure override returns (uint256) {
        return IMMUTABLE_PARAMS_LENGTH;
    }

    /**
     * @dev Deposit ERC20s into pool
     */
    function depositERC20(ERC20 a, uint256 amount) external {
        assertDepositsNotPaused();
        a.safeTransferFrom(msg.sender, address(this), amount);
        if (a == token()) {
            emit TokenDeposit(nft(), a, amount);
            notifyDeposit(EventType.DEPOSIT_TOKEN, amount);
        }
    }

    /**
     * @notice Withdraws all pool token owned by the pool to the owner address.
     * @dev Only callable by the owner.
     */
    function withdrawAllERC20() external {
        requireAuthorized();
        uint256 _accruedTradeFee = accruedTradeFee;
        accruedTradeFee = 0;

        ERC20 _token = token();
        uint256 amount = _token.balanceOf(address(this));
        _token.safeTransfer(owner(), amount);

        if (_accruedTradeFee >= amount) {
            _accruedTradeFee = amount;
            amount = 0;
        } else {
            amount -= _accruedTradeFee;
        }

        // emit event since it is the pool token
        IERC721 _nft = nft();
        emit TokenWithdrawal(_nft, _token, amount);
        emit AccruedTradeFeeWithdrawal(_nft, _token, _accruedTradeFee);
    }

    /// @inheritdoc ICollectionPool
    function withdrawERC20(ERC20 a, uint256 amount) external {
        requireAuthorized();
        if (a == token()) {
            uint256 funds = liquidity();
            if (funds < amount) revert InsufficientERC20Liquidity(funds, amount);

            // emit event since it is the pool token
            emit TokenWithdrawal(nft(), a, amount);
        }

        a.safeTransfer(owner(), amount);
    }

    /// @inheritdoc CollectionPool
    function withdrawAccruedTradeFee() external override onlyOwner {
        uint256 _accruedTradeFee = accruedTradeFee;
        if (_accruedTradeFee > 0) {
            accruedTradeFee = 0;

            ERC20 _token = token();
            _token.safeTransfer(msg.sender, _accruedTradeFee);

            // emit event since it is the pool token
            emit AccruedTradeFeeWithdrawal(nft(), _token, _accruedTradeFee);
        }
    }

    /**
     * @notice Helper function that uses a router to send tokens and check that
     * the tokens were actually transferred
     */
    function sendTokenWithRouter(
        CollectionRouter router,
        ERC20 _token,
        address from,
        address to,
        uint256 amount,
        PoolVariant variant
    ) internal {
        uint256 beforeBalance = _token.balanceOf(to);
        router.poolTransferERC20From(_token, from, to, amount, variant);
        uint256 balanceDifference = _token.balanceOf(to) - beforeBalance;
        if (balanceDifference != amount) {
            revert RouterDidNotSend(balanceDifference, amount);
        }
    }
}

File 10 of 52 : ICollectionPoolFactory.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {ERC20} from "solmate/src/tokens/ERC20.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {IERC721Enumerable} from "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol";
import {CollectionRouter} from "../routers/CollectionRouter.sol";
import {ICurve} from "../bonding-curves/ICurve.sol";
import {ICollectionPool} from "../pools/ICollectionPool.sol";
import {
    PoolVariant,
    NFTFilterParams,
    CreateETHPoolParams,
    CreateERC20PoolParams,
    RoyaltyDue
} from "./CollectionStructsAndEnums.sol";

interface ICollectionPoolFactory is IERC721 {
    function protocolFeeMultiplier() external view returns (uint24);

    function protocolFeeRecipient() external view returns (address payable);

    function carryFeeMultiplier() external view returns (uint24);

    function callAllowed(address target) external view returns (bool);

    function routerStatus(CollectionRouter router) external view returns (bool allowed, bool wasEverAllowed);

    function isPool(address potentialPool) external view returns (bool);

    function isPoolVariant(address potentialPool, PoolVariant variant) external view returns (bool);

    function requireAuthorizedForToken(address spender, uint256 tokenId) external view;

    function swapPaused() external view returns (bool);

    function creationPaused() external view returns (bool);

    function depositPaused() external view returns (bool);

    function othersPaused() external view returns (bool);

    function createPoolETH(CreateETHPoolParams calldata params)
        external
        payable
        returns (ICollectionPool pool, uint256 tokenId);

    function createPoolERC20(CreateERC20PoolParams calldata params)
        external
        returns (ICollectionPool pool, uint256 tokenId);

    function createPoolETHFiltered(CreateETHPoolParams calldata params, NFTFilterParams calldata filterParams)
        external
        payable
        returns (ICollectionPool pool, uint256 tokenId);

    function createPoolERC20Filtered(CreateERC20PoolParams calldata params, NFTFilterParams calldata filterParams)
        external
        returns (ICollectionPool pool, uint256 tokenId);

    function depositRoyaltiesNotification(ERC20 token, RoyaltyDue[] calldata royaltiesDue, PoolVariant poolVariant)
        external
        payable;

    function burn(uint256 tokenId) external;

    /**
     * @dev Returns the pool of the `tokenId` token.
     */
    function poolOf(uint256 tokenId) external view returns (ICollectionPool);

    function withdrawRoyalties(address payable recipient, ERC20 token) external;

    /**
     * @notice Withdraw all `token` royalties awardable to `recipient`. If the
     * zero address is passed as `token`, then ETH royalties are paid. Does not
     * use msg.sender so this function can be called on behalf of contract
     * royalty recipients
     *
     * @dev Does not call `withdrawRoyalties` to avoid making multiple unneeded
     * checks of whether `address(token) == address(0)` for each iteration
     */
    function withdrawRoyaltiesMultipleRecipients(address payable[] calldata recipients, ERC20 token) external;

    function withdrawRoyaltiesMultipleCurrencies(address payable recipient, ERC20[] calldata tokens) external;

    /**
     * @notice Withdraw royalties for ALL combinations of recipients and tokens
     * in the given arguments
     *
     * @dev Iterate over tokens as outer loop to reduce stores/loads to `royaltiesStored`
     * and also reduce the number of `address(token) == address(0)` condition checks
     * from O(m * n) to O(n)
     */
    function withdrawRoyaltiesMultipleRecipientsAndCurrencies(
        address payable[] calldata recipients,
        ERC20[] calldata tokens
    ) external;
}

File 11 of 52 : CollectionPoolEnumerableETH.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {CollectionPoolETH} from "./CollectionPoolETH.sol";
import {CollectionPoolEnumerable} from "./CollectionPoolEnumerable.sol";
import {PoolVariant} from "./CollectionStructsAndEnums.sol";

/**
 * @title An NFT/Token pool where the NFT implements ERC721Enumerable, and the token is ETH
 * @author Collection
 */
contract CollectionPoolEnumerableETH is CollectionPoolEnumerable, CollectionPoolETH {
    /**
     * @notice Returns the CollectionPool type
     */
    function poolVariant() public pure override returns (PoolVariant) {
        return PoolVariant.ENUMERABLE_ETH;
    }
}

File 12 of 52 : CollectionPoolMissingEnumerableETH.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {CollectionPoolETH} from "../pools/CollectionPoolETH.sol";
import {CollectionPoolMissingEnumerable} from "./CollectionPoolMissingEnumerable.sol";
import {PoolVariant} from "./CollectionStructsAndEnums.sol";

contract CollectionPoolMissingEnumerableETH is CollectionPoolMissingEnumerable, CollectionPoolETH {
    function poolVariant() public pure override returns (PoolVariant) {
        return PoolVariant.MISSING_ENUMERABLE_ETH;
    }
}

File 13 of 52 : ICurve.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {CurveErrorCodes} from "./CurveErrorCodes.sol";

interface ICurve {
    /**
     * @param spotPrice The current selling spot price of the pool, in tokens
     * @param delta The delta parameter of the pool, what it means depends on the curve
     * @param props The properties of the pool, what it means depends on the curve
     * @param state The state of the pool, what it means depends on the curve
     */
    struct Params {
        uint128 spotPrice;
        uint128 delta;
        bytes props;
        bytes state;
    }

    /**
     * @param trade The amount of fee to send to the pool, in tokens
     * @param protocol The amount of fee to send to the protocol, in tokens
     * @param royalties The amount to pay for each item in the order they
     * are purchased. Always has length `numItems`.
     */
    struct Fees {
        uint256 trade;
        uint256 protocol;
        uint256[] royalties;
    }

    /**
     * @param feeMultiplier Determines how much fee the LP takes from this trade, 18 decimals
     * @param fees.protocolMultiplier Determines how much fee the protocol takes from this trade, 18 decimals
     * @param royaltyNumerator Determines how much of the trade value is awarded as royalties. 5 decimals
     * @param carryFeeMultiplier Determines how much carry fee the protocol takes from this trade, 18 decimals
     */
    struct FeeMultipliers {
        uint24 trade;
        uint24 protocol;
        uint24 royaltyNumerator;
        uint24 carry;
    }

    /**
     * @notice Validates if a delta value is valid for the curve. The criteria for
     * validity can be different for each type of curve, for instance ExponentialCurve
     * requires delta to be greater than 1.
     * @param delta The delta value to be validated
     * @return valid True if delta is valid, false otherwise
     */
    function validateDelta(uint128 delta) external pure returns (bool valid);

    /**
     * @notice Validates if a new spot price is valid for the curve. Spot price is generally assumed to be the immediate sell price of 1 NFT to the pool, in units of the pool's pooled token.
     * @param newSpotPrice The new spot price to be set
     * @return valid True if the new spot price is valid, false otherwise
     */
    function validateSpotPrice(uint128 newSpotPrice) external view returns (bool valid);

    /**
     * @notice Validates if a props value is valid for the curve. The criteria for validity can be different for each type of curve.
     * @param props The props value to be validated
     * @return valid True if props is valid, false otherwise
     */
    function validateProps(bytes calldata props) external view returns (bool valid);

    /**
     * @notice Validates if a state value is valid for the curve. The criteria for validity can be different for each type of curve.
     * @param state The state value to be validated
     * @return valid True if state is valid, false otherwise
     */
    function validateState(bytes calldata state) external view returns (bool valid);

    /**
     * @notice Validates given delta, spot price, props value and state value for the curve. The criteria for validity can be different for each type of curve.
     * @param delta The delta value to be validated
     * @param newSpotPrice The new spot price to be set
     * @param props The props value to be validated
     * @param state The state value to be validated
     * @return valid True if all parameters are valid, false otherwise
     */
    function validate(uint128 delta, uint128 newSpotPrice, bytes calldata props, bytes calldata state)
        external
        view
        returns (bool valid);

    /**
     * @notice Given the current state of the pool and the trade, computes how much the user
     * should pay to purchase an NFT from the pool, the new spot price, and other values.
     * @dev Do not try to optimize the length of fees.royalties; compiler
     * ^0.8.0 throws a YulException if you try to use an if-guard in the sigmoid
     * calculation loop due to stack depth
     * @param params Parameters of the pool that affect the bonding curve.
     * @param numItems The number of NFTs the user is buying from the pool
     * @param feeMultipliers Determines how much fee is taken from this trade.
     * @return newParams The updated parameters of the pool that affect the bonding curve.
     * @return inputValue The amount that the user should pay, in tokens
     * @return fees The amount of fees
     * @return lastSwapPrice The swap price of the last NFT traded with fees applied
     */
    function getBuyInfo(ICurve.Params calldata params, uint256 numItems, ICurve.FeeMultipliers calldata feeMultipliers)
        external
        view
        returns (ICurve.Params calldata newParams, uint256 inputValue, ICurve.Fees calldata fees, uint256 lastSwapPrice);

    /**
     * @notice Given the current state of the pool and the trade, computes how much the user
     * should receive when selling NFTs to the pool, the new spot price, and other values.
     * @dev Do not try to optimize the length of fees.royalties; compiler
     * ^0.8.0 throws a YulException if you try to use an if-guard in the sigmoid
     * calculation loop due to stack depth
     * @param params Parameters of the pool that affect the bonding curve.
     * @param numItems The number of NFTs the user is selling to the pool
     * @param feeMultipliers Determines how much fee is taken from this trade.
     * @return newParams The updated parameters of the pool that affect the bonding curve.
     * @return outputValue The amount that the user should receive, in tokens
     * @return fees The amount of fees
     * @return lastSwapPrice The swap price of the last NFT traded with fees applied
     */
    function getSellInfo(ICurve.Params calldata params, uint256 numItems, ICurve.FeeMultipliers calldata feeMultipliers)
        external
        view
        returns (
            ICurve.Params calldata newParams,
            uint256 outputValue,
            ICurve.Fees calldata fees,
            uint256 lastSwapPrice
        );
}

File 14 of 52 : CollectionPoolMissingEnumerableERC20.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {CollectionPoolERC20} from "../pools/CollectionPoolERC20.sol";
import {CollectionPoolMissingEnumerable} from "./CollectionPoolMissingEnumerable.sol";
import {PoolVariant} from "./CollectionStructsAndEnums.sol";

contract CollectionPoolMissingEnumerableERC20 is CollectionPoolMissingEnumerable, CollectionPoolERC20 {
    function poolVariant() public pure override returns (PoolVariant) {
        return PoolVariant.MISSING_ENUMERABLE_ERC20;
    }
}

File 15 of 52 : CollectionPoolEnumerableERC20.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {CollectionPoolERC20} from "./CollectionPoolERC20.sol";
import {CollectionPoolEnumerable} from "./CollectionPoolEnumerable.sol";
import {PoolVariant} from "./CollectionStructsAndEnums.sol";

/**
 * @title An NFT/Token pool where the NFT implements ERC721Enumerable, and the token is an ERC20
 * @author Collection
 */
contract CollectionPoolEnumerableERC20 is CollectionPoolEnumerable, CollectionPoolERC20 {
    /**
     * @notice Returns the CollectionPool type
     */
    function poolVariant() public pure override returns (PoolVariant) {
        return PoolVariant.ENUMERABLE_ERC20;
    }
}

File 16 of 52 : MultiPauser.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

/**
 * @notice Manages 256 independent pause states. Idiomatic use of these functions when
 * exposing them as external functions would be to give an appropriate name and
 * abstract away the passing of `index` variables using immutable contract
 * variables. Initializes with all pause states unpaused.
 */
contract MultiPauser {
    uint256 pauseStates;

    modifier validIndex(uint256 index) {
        require(index <= 255, "Invalid pause index");
        _;
    }

    modifier onlyPaused(uint256 index) {
        require(index <= 255, "Invalid pause index");
        require(isPaused(index), "Must be paused");
        _;
    }

    modifier onlyUnpaused(uint256 index) {
        require(index <= 255, "Invalid pause index");
        require(!isPaused(index), "Must be unpaused");
        _;
    }

    /**
     * @notice Pauses the pause with the given index. 0 <= index <= 255.
     * @dev While using a uint8 as the type of index would enforce the
     * precondition for us, it costs extra gas as solidity will carry out
     * bit extensions and truncations to make it word length
     */
    function pause(uint256 index) validIndex(index) internal {
        pauseStates = pauseStates | (1 << index);
    }

    /**
     * @notice Unpauses the pause with the given index. 0 <= index <= 255.
     * @dev While using a uint8 as the type of index would enforce the
     * precondition for us, it costs extra gas as solidity will carry out
     * bit extensions and truncations to make it word length
     */
    function unpause(uint256 index) validIndex(index) internal {
        /**
         * @dev Generate all 1's except in position `index`. Use XOR as no ~ in
         * solidity.
         */ 
        pauseStates &= ~(1 << index);
    }

    /**
     * @notice Returns true iff the pause with the given index is paused
     */
    function isPaused(uint256 index) validIndex(index) internal view returns (bool) {
        return (pauseStates & (1 << index)) > 0;
    }
}

File 17 of 52 : CollectionStructsAndEnums.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {ERC20} from "solmate/src/tokens/ERC20.sol";

import {IExternalFilter} from "../filter/IExternalFilter.sol";
import {ICurve} from "../bonding-curves/ICurve.sol";

enum PoolVariant {
    ENUMERABLE_ETH,
    MISSING_ENUMERABLE_ETH,
    ENUMERABLE_ERC20,
    MISSING_ENUMERABLE_ERC20
}

enum PoolType {
    TOKEN,
    NFT,
    TRADE
}

enum EventType {
    BOUGHT_NFT_FROM_POOL,
    SOLD_NFT_TO_POOL,
    DEPOSIT_TOKEN,
    DEPOSIT_NFT
}

/**
 * @dev The RoyaltyDue struct is used to track information about royalty payments that are due on NFT swaps.
 * It contains two fields:
 * @dev amount: The amount of the royalty payment, in the token's base units.
 * This value is calculated based on the price of the NFT being swapped, and the royaltyNumerator value set in the AMM pool contract.
 * @dev recipient: The address to which the royalty payment should be sent.
 * This value is determined by the NFT being swapped, and it is specified in the ERC2981 metadata for the NFT.
 * @dev When a user swaps an NFT for tokens using the AMM pool contract, a RoyaltyDue struct is created to track the amount
 * and recipient of the royalty payment that is due on the NFT swap. This struct is then used to facilitate the payment of
 * the royalty to the appropriate recipient.
 */

struct RoyaltyDue {
    uint256 amount;
    address recipient;
}

/**
 * @param ids The list of IDs of the NFTs to sell to the pool
 * @param proof Merkle multiproof proving list is allowed by pool
 * @param proofFlags Merkle multiproof flags for proof
 */
struct NFTs {
    uint256[] ids;
    bytes32[] proof;
    bool[] proofFlags;
}

struct RouterStatus {
    bool allowed;
    bool wasEverAllowed;
}

struct LPTokenParams721 {
    address nftAddress;
    address bondingCurveAddress;
    address tokenAddress;
    address payable poolAddress;
    uint24 fee;
    uint128 delta;
    uint24 royaltyNumerator;
}

/**
 * @param merkleRoot Merkle root for NFT ID filter
 * @param encodedTokenIDs Encoded list of acceptable NFT IDs
 * @param initialProof Merkle multiproof for initial NFT IDs
 * @param initialProofFlags Merkle multiproof flags for initial NFT IDs
 * @param externalFilter Address implementing IExternalFilter for external filtering
 */
struct NFTFilterParams {
    bytes32 merkleRoot;
    bytes encodedTokenIDs;
    bytes32[] initialProof;
    bool[] initialProofFlags;
    IExternalFilter externalFilter;
}

/**
 * @notice Creates a pool contract using EIP-1167.
 * @param nft The NFT contract of the collection the pool trades
 * @param bondingCurve The bonding curve for the pool to price NFTs, must be whitelisted
 * @param assetRecipient The address that will receive the assets traders give during trades.
 * If set to address(0), assets will be sent to the pool address. Not available to TRADE pools.
 * @param receiver Receiver of the LP token generated to represent ownership of the pool
 * @param poolType TOKEN, NFT, or TRADE
 * @param delta The delta value used by the bonding curve. The meaning of delta depends
 * on the specific curve.
 * @param fee The fee taken by the LP in each trade. Can only be non-zero if _poolType is Trade.
 * @param spotPrice The initial selling spot price
 * @param royaltyNumerator All trades will result in `royaltyNumerator` * <trade amount> / 1e6
 * being sent to the account to which the traded NFT's royalties are awardable.
 * Must be 0 if `_nft` is not IERC2981 and no recipient fallback is set.
 * @param royaltyRecipientFallback An address to which all royalties will
 * be paid to if not address(0) and ERC2981 is not supported or ERC2981 recipient is not set.
 * @param initialNFTIDs The list of IDs of NFTs to transfer from the sender to the pool
 * @return pool The new pool
 */
struct CreateETHPoolParams {
    IERC721 nft;
    ICurve bondingCurve;
    address payable assetRecipient;
    address receiver;
    PoolType poolType;
    uint128 delta;
    uint24 fee;
    uint128 spotPrice;
    bytes props;
    bytes state;
    uint24 royaltyNumerator;
    address payable royaltyRecipientFallback;
    uint256[] initialNFTIDs;
}

/**
 * @notice Creates a pool contract using EIP-1167.
 * @param token The ERC20 token used for pool swaps
 * @param nft The NFT contract of the collection the pool trades
 * @param bondingCurve The bonding curve for the pool to price NFTs, must be whitelisted
 * @param assetRecipient The address that will receive the assets traders give during trades.
 * If set to address(0), assets will be sent to the pool address. Not available to TRADE pools.
 * @param receiver Receiver of the LP token generated to represent ownership of the pool
 * @param poolType TOKEN, NFT, or TRADE
 * @param delta The delta value used by the bonding curve. The meaning of delta depends on the
 * specific curve.
 * @param fee The fee taken by the LP in each trade. Can only be non-zero if _poolType is Trade.
 * @param spotPrice The initial selling spot price, in ETH
 * @param royaltyNumerator All trades will result in `royaltyNumerator` * <trade amount> / 1e6
 * being sent to the account to which the traded NFT's royalties are awardable.
 * Must be 0 if `_nft` is not IERC2981 and no recipient fallback is set.
 * @param royaltyRecipientFallback An address to which all royalties will
 * be paid to if not address(0) and ERC2981 is not supported or ERC2981 recipient is not set.
 * @param initialNFTIDs The list of IDs of NFTs to transfer from the sender to the pool
 * @param initialTokenBalance The initial token balance sent from the sender to the new pool
 * @return pool The new pool
 */
struct CreateERC20PoolParams {
    ERC20 token;
    IERC721 nft;
    ICurve bondingCurve;
    address payable assetRecipient;
    address receiver;
    PoolType poolType;
    uint128 delta;
    uint24 fee;
    uint128 spotPrice;
    bytes props;
    bytes state;
    uint24 royaltyNumerator;
    address payable royaltyRecipientFallback;
    uint256[] initialNFTIDs;
    uint256 initialTokenBalance;
}

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

pragma solidity ^0.8.0;

import "../utils/Context.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 Ownable is Context {
    address private _owner;

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

    /**
     * @dev Initializes the contract setting the deployer as the initial owner.
     */
    constructor() {
        _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);
    }
}

File 19 of 52 : ERC721.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (token/ERC721/ERC721.sol)

pragma solidity ^0.8.0;

import "./IERC721.sol";
import "./IERC721Receiver.sol";
import "./extensions/IERC721Metadata.sol";
import "../../utils/Address.sol";
import "../../utils/Context.sol";
import "../../utils/Strings.sol";
import "../../utils/introspection/ERC165.sol";

/**
 * @dev Implementation of https://eips.ethereum.org/EIPS/eip-721[ERC721] Non-Fungible Token Standard, including
 * the Metadata extension, but not including the Enumerable extension, which is available separately as
 * {ERC721Enumerable}.
 */
contract ERC721 is Context, ERC165, IERC721, IERC721Metadata {
    using Address for address;
    using Strings for uint256;

    // Token name
    string private _name;

    // Token symbol
    string private _symbol;

    // Mapping from token ID to owner address
    mapping(uint256 => address) private _owners;

    // Mapping owner address to token count
    mapping(address => uint256) private _balances;

    // Mapping from token ID to approved address
    mapping(uint256 => address) private _tokenApprovals;

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

    /**
     * @dev Initializes the contract by setting a `name` and a `symbol` to the token collection.
     */
    constructor(string memory name_, string memory symbol_) {
        _name = name_;
        _symbol = symbol_;
    }

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

    /**
     * @dev See {IERC721-balanceOf}.
     */
    function balanceOf(address owner) public view virtual override returns (uint256) {
        require(owner != address(0), "ERC721: address zero is not a valid owner");
        return _balances[owner];
    }

    /**
     * @dev See {IERC721-ownerOf}.
     */
    function ownerOf(uint256 tokenId) public view virtual override returns (address) {
        address owner = _owners[tokenId];
        require(owner != address(0), "ERC721: invalid token ID");
        return owner;
    }

    /**
     * @dev See {IERC721Metadata-name}.
     */
    function name() public view virtual override returns (string memory) {
        return _name;
    }

    /**
     * @dev See {IERC721Metadata-symbol}.
     */
    function symbol() public view virtual override returns (string memory) {
        return _symbol;
    }

    /**
     * @dev See {IERC721Metadata-tokenURI}.
     */
    function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        _requireMinted(tokenId);

        string memory baseURI = _baseURI();
        return bytes(baseURI).length > 0 ? string(abi.encodePacked(baseURI, tokenId.toString())) : "";
    }

    /**
     * @dev Base URI for computing {tokenURI}. If set, the resulting URI for each
     * token will be the concatenation of the `baseURI` and the `tokenId`. Empty
     * by default, can be overridden in child contracts.
     */
    function _baseURI() internal view virtual returns (string memory) {
        return "";
    }

    /**
     * @dev See {IERC721-approve}.
     */
    function approve(address to, uint256 tokenId) public virtual override {
        address owner = ERC721.ownerOf(tokenId);
        require(to != owner, "ERC721: approval to current owner");

        require(
            _msgSender() == owner || isApprovedForAll(owner, _msgSender()),
            "ERC721: approve caller is not token owner nor approved for all"
        );

        _approve(to, tokenId);
    }

    /**
     * @dev See {IERC721-getApproved}.
     */
    function getApproved(uint256 tokenId) public view virtual override returns (address) {
        _requireMinted(tokenId);

        return _tokenApprovals[tokenId];
    }

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

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

    /**
     * @dev See {IERC721-transferFrom}.
     */
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual override {
        //solhint-disable-next-line max-line-length
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: caller is not token owner nor approved");

        _transfer(from, to, tokenId);
    }

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) public virtual override {
        safeTransferFrom(from, to, tokenId, "");
    }

    /**
     * @dev See {IERC721-safeTransferFrom}.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes memory data
    ) public virtual override {
        require(_isApprovedOrOwner(_msgSender(), tokenId), "ERC721: caller is not token owner nor approved");
        _safeTransfer(from, to, tokenId, data);
    }

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * `data` is additional data, it has no specified format and it is sent in call to `to`.
     *
     * This internal function is equivalent to {safeTransferFrom}, and can be used to e.g.
     * implement alternative mechanisms to perform token transfer, such as signature-based.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function _safeTransfer(
        address from,
        address to,
        uint256 tokenId,
        bytes memory data
    ) internal virtual {
        _transfer(from, to, tokenId);
        require(_checkOnERC721Received(from, to, tokenId, data), "ERC721: transfer to non ERC721Receiver implementer");
    }

    /**
     * @dev Returns whether `tokenId` exists.
     *
     * Tokens can be managed by their owner or approved accounts via {approve} or {setApprovalForAll}.
     *
     * Tokens start existing when they are minted (`_mint`),
     * and stop existing when they are burned (`_burn`).
     */
    function _exists(uint256 tokenId) internal view virtual returns (bool) {
        return _owners[tokenId] != address(0);
    }

    /**
     * @dev Returns whether `spender` is allowed to manage `tokenId`.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function _isApprovedOrOwner(address spender, uint256 tokenId) internal view virtual returns (bool) {
        address owner = ERC721.ownerOf(tokenId);
        return (spender == owner || isApprovedForAll(owner, spender) || getApproved(tokenId) == spender);
    }

    /**
     * @dev Safely mints `tokenId` and transfers it to `to`.
     *
     * Requirements:
     *
     * - `tokenId` must not exist.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function _safeMint(address to, uint256 tokenId) internal virtual {
        _safeMint(to, tokenId, "");
    }

    /**
     * @dev Same as {xref-ERC721-_safeMint-address-uint256-}[`_safeMint`], with an additional `data` parameter which is
     * forwarded in {IERC721Receiver-onERC721Received} to contract recipients.
     */
    function _safeMint(
        address to,
        uint256 tokenId,
        bytes memory data
    ) internal virtual {
        _mint(to, tokenId);
        require(
            _checkOnERC721Received(address(0), to, tokenId, data),
            "ERC721: transfer to non ERC721Receiver implementer"
        );
    }

    /**
     * @dev Mints `tokenId` and transfers it to `to`.
     *
     * WARNING: Usage of this method is discouraged, use {_safeMint} whenever possible
     *
     * Requirements:
     *
     * - `tokenId` must not exist.
     * - `to` cannot be the zero address.
     *
     * Emits a {Transfer} event.
     */
    function _mint(address to, uint256 tokenId) internal virtual {
        require(to != address(0), "ERC721: mint to the zero address");
        require(!_exists(tokenId), "ERC721: token already minted");

        _beforeTokenTransfer(address(0), to, tokenId);

        _balances[to] += 1;
        _owners[tokenId] = to;

        emit Transfer(address(0), to, tokenId);

        _afterTokenTransfer(address(0), to, tokenId);
    }

    /**
     * @dev Destroys `tokenId`.
     * The approval is cleared when the token is burned.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     *
     * Emits a {Transfer} event.
     */
    function _burn(uint256 tokenId) internal virtual {
        address owner = ERC721.ownerOf(tokenId);

        _beforeTokenTransfer(owner, address(0), tokenId);

        // Clear approvals
        _approve(address(0), tokenId);

        _balances[owner] -= 1;
        delete _owners[tokenId];

        emit Transfer(owner, address(0), tokenId);

        _afterTokenTransfer(owner, address(0), tokenId);
    }

    /**
     * @dev Transfers `tokenId` from `from` to `to`.
     *  As opposed to {transferFrom}, this imposes no restrictions on msg.sender.
     *
     * Requirements:
     *
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     *
     * Emits a {Transfer} event.
     */
    function _transfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual {
        require(ERC721.ownerOf(tokenId) == from, "ERC721: transfer from incorrect owner");
        require(to != address(0), "ERC721: transfer to the zero address");

        _beforeTokenTransfer(from, to, tokenId);

        // Clear approvals from the previous owner
        _approve(address(0), tokenId);

        _balances[from] -= 1;
        _balances[to] += 1;
        _owners[tokenId] = to;

        emit Transfer(from, to, tokenId);

        _afterTokenTransfer(from, to, tokenId);
    }

    /**
     * @dev Approve `to` to operate on `tokenId`
     *
     * Emits an {Approval} event.
     */
    function _approve(address to, uint256 tokenId) internal virtual {
        _tokenApprovals[tokenId] = to;
        emit Approval(ERC721.ownerOf(tokenId), to, tokenId);
    }

    /**
     * @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, "ERC721: approve to caller");
        _operatorApprovals[owner][operator] = approved;
        emit ApprovalForAll(owner, operator, approved);
    }

    /**
     * @dev Reverts if the `tokenId` has not been minted yet.
     */
    function _requireMinted(uint256 tokenId) internal view virtual {
        require(_exists(tokenId), "ERC721: invalid token ID");
    }

    /**
     * @dev Internal function to invoke {IERC721Receiver-onERC721Received} on a target address.
     * The call is not executed if the target address is not a contract.
     *
     * @param from address representing the previous owner of the given token ID
     * @param to target address that will receive the tokens
     * @param tokenId uint256 ID of the token to be transferred
     * @param data bytes optional data to send along with the call
     * @return bool whether the call correctly returned the expected magic value
     */
    function _checkOnERC721Received(
        address from,
        address to,
        uint256 tokenId,
        bytes memory data
    ) private returns (bool) {
        if (to.isContract()) {
            try IERC721Receiver(to).onERC721Received(_msgSender(), from, tokenId, data) returns (bytes4 retval) {
                return retval == IERC721Receiver.onERC721Received.selector;
            } catch (bytes memory reason) {
                if (reason.length == 0) {
                    revert("ERC721: transfer to non ERC721Receiver implementer");
                } else {
                    /// @solidity memory-safe-assembly
                    assembly {
                        revert(add(32, reason), mload(reason))
                    }
                }
            }
        } else {
            return true;
        }
    }

    /**
     * @dev Hook that is called before any token transfer. This includes minting
     * and burning.
     *
     * Calling conditions:
     *
     * - When `from` and `to` are both non-zero, ``from``'s `tokenId` will be
     * transferred to `to`.
     * - When `from` is zero, `tokenId` will be minted for `to`.
     * - When `to` is zero, ``from``'s `tokenId` will be burned.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual {}

    /**
     * @dev Hook that is called after any transfer of tokens. This includes
     * minting and burning.
     *
     * Calling conditions:
     *
     * - when `from` and `to` are both non-zero.
     * - `from` and `to` are never both zero.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _afterTokenTransfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual {}
}

File 20 of 52 : IERC721.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (token/ERC721/IERC721.sol)

pragma solidity ^0.8.0;

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

/**
 * @dev Required interface of an ERC721 compliant contract.
 */
interface IERC721 is IERC165 {
    /**
     * @dev Emitted when `tokenId` token is transferred from `from` to `to`.
     */
    event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
     */
    event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);

    /**
     * @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
     */
    event ApprovalForAll(address indexed owner, address indexed operator, bool approved);

    /**
     * @dev Returns the number of tokens in ``owner``'s account.
     */
    function balanceOf(address owner) external view returns (uint256 balance);

    /**
     * @dev Returns the owner of the `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function ownerOf(uint256 tokenId) external view returns (address owner);

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId,
        bytes calldata data
    ) external;

    /**
     * @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
     * are aware of the ERC721 protocol to prevent tokens from being forever locked.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must exist and be owned by `from`.
     * - If the caller is not `from`, it must have been allowed to move this token by either {approve} or {setApprovalForAll}.
     * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
     *
     * Emits a {Transfer} event.
     */
    function safeTransferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Transfers `tokenId` token from `from` to `to`.
     *
     * WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
     *
     * Requirements:
     *
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     * - `tokenId` token must be owned by `from`.
     * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 tokenId
    ) external;

    /**
     * @dev Gives permission to `to` to transfer `tokenId` token to another account.
     * The approval is cleared when the token is transferred.
     *
     * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
     *
     * Requirements:
     *
     * - The caller must own the token or be an approved operator.
     * - `tokenId` must exist.
     *
     * Emits an {Approval} event.
     */
    function approve(address to, uint256 tokenId) external;

    /**
     * @dev Approve or remove `operator` as an operator for the caller.
     * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
     *
     * Requirements:
     *
     * - The `operator` cannot be the caller.
     *
     * Emits an {ApprovalForAll} event.
     */
    function setApprovalForAll(address operator, bool _approved) external;

    /**
     * @dev Returns the account approved for `tokenId` token.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function getApproved(uint256 tokenId) external view returns (address operator);

    /**
     * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
     *
     * See {setApprovalForAll}
     */
    function isApprovedForAll(address owner, address operator) external view returns (bool);
}

File 21 of 52 : 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 22 of 52 : ERC721URIStorage.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (token/ERC721/extensions/ERC721URIStorage.sol)

pragma solidity ^0.8.0;

import "../ERC721.sol";

/**
 * @dev ERC721 token with storage based token URI management.
 */
abstract contract ERC721URIStorage is ERC721 {
    using Strings for uint256;

    // Optional mapping for token URIs
    mapping(uint256 => string) private _tokenURIs;

    /**
     * @dev See {IERC721Metadata-tokenURI}.
     */
    function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
        _requireMinted(tokenId);

        string memory _tokenURI = _tokenURIs[tokenId];
        string memory base = _baseURI();

        // If there is no base URI, return the token URI.
        if (bytes(base).length == 0) {
            return _tokenURI;
        }
        // If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked).
        if (bytes(_tokenURI).length > 0) {
            return string(abi.encodePacked(base, _tokenURI));
        }

        return super.tokenURI(tokenId);
    }

    /**
     * @dev Sets `_tokenURI` as the tokenURI of `tokenId`.
     *
     * Requirements:
     *
     * - `tokenId` must exist.
     */
    function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
        require(_exists(tokenId), "ERC721URIStorage: URI set of nonexistent token");
        _tokenURIs[tokenId] = _tokenURI;
    }

    /**
     * @dev See {ERC721-_burn}. This override additionally checks to see if a
     * token-specific URI was set for the token, and if so, it deletes the token URI from
     * the storage mapping.
     */
    function _burn(uint256 tokenId) internal virtual override {
        super._burn(tokenId);

        if (bytes(_tokenURIs[tokenId]).length != 0) {
            delete _tokenURIs[tokenId];
        }
    }
}

File 23 of 52 : IERC721Enumerable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC721/extensions/IERC721Enumerable.sol)

pragma solidity ^0.8.0;

import "../IERC721.sol";

/**
 * @title ERC-721 Non-Fungible Token Standard, optional enumeration extension
 * @dev See https://eips.ethereum.org/EIPS/eip-721
 */
interface IERC721Enumerable is IERC721 {
    /**
     * @dev Returns the total amount of tokens stored by the contract.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns a token ID owned by `owner` at a given `index` of its token list.
     * Use along with {balanceOf} to enumerate all of ``owner``'s tokens.
     */
    function tokenOfOwnerByIndex(address owner, uint256 index) external view returns (uint256);

    /**
     * @dev Returns a token ID at a given `index` of all the tokens stored by the contract.
     * Use along with {totalSupply} to enumerate all tokens.
     */
    function tokenByIndex(uint256 index) external view returns (uint256);
}

File 24 of 52 : ERC20.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

/// @notice Modern and gas efficient ERC20 + EIP-2612 implementation.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC20.sol)
/// @author Modified from Uniswap (https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol)
/// @dev Do not manually set balances without updating totalSupply, as the sum of all user balances must not exceed it.
abstract contract ERC20 {
    /*//////////////////////////////////////////////////////////////
                                 EVENTS
    //////////////////////////////////////////////////////////////*/

    event Transfer(address indexed from, address indexed to, uint256 amount);

    event Approval(address indexed owner, address indexed spender, uint256 amount);

    /*//////////////////////////////////////////////////////////////
                            METADATA STORAGE
    //////////////////////////////////////////////////////////////*/

    string public name;

    string public symbol;

    uint8 public immutable decimals;

    /*//////////////////////////////////////////////////////////////
                              ERC20 STORAGE
    //////////////////////////////////////////////////////////////*/

    uint256 public totalSupply;

    mapping(address => uint256) public balanceOf;

    mapping(address => mapping(address => uint256)) public allowance;

    /*//////////////////////////////////////////////////////////////
                            EIP-2612 STORAGE
    //////////////////////////////////////////////////////////////*/

    uint256 internal immutable INITIAL_CHAIN_ID;

    bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;

    mapping(address => uint256) public nonces;

    /*//////////////////////////////////////////////////////////////
                               CONSTRUCTOR
    //////////////////////////////////////////////////////////////*/

    constructor(
        string memory _name,
        string memory _symbol,
        uint8 _decimals
    ) {
        name = _name;
        symbol = _symbol;
        decimals = _decimals;

        INITIAL_CHAIN_ID = block.chainid;
        INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
    }

    /*//////////////////////////////////////////////////////////////
                               ERC20 LOGIC
    //////////////////////////////////////////////////////////////*/

    function approve(address spender, uint256 amount) public virtual returns (bool) {
        allowance[msg.sender][spender] = amount;

        emit Approval(msg.sender, spender, amount);

        return true;
    }

    function transfer(address to, uint256 amount) public virtual returns (bool) {
        balanceOf[msg.sender] -= amount;

        // Cannot overflow because the sum of all user
        // balances can't exceed the max uint256 value.
        unchecked {
            balanceOf[to] += amount;
        }

        emit Transfer(msg.sender, to, amount);

        return true;
    }

    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) public virtual returns (bool) {
        uint256 allowed = allowance[from][msg.sender]; // Saves gas for limited approvals.

        if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - amount;

        balanceOf[from] -= amount;

        // Cannot overflow because the sum of all user
        // balances can't exceed the max uint256 value.
        unchecked {
            balanceOf[to] += amount;
        }

        emit Transfer(from, to, amount);

        return true;
    }

    /*//////////////////////////////////////////////////////////////
                             EIP-2612 LOGIC
    //////////////////////////////////////////////////////////////*/

    function permit(
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) public virtual {
        require(deadline >= block.timestamp, "PERMIT_DEADLINE_EXPIRED");

        // Unchecked because the only math done is incrementing
        // the owner's nonce which cannot realistically overflow.
        unchecked {
            address recoveredAddress = ecrecover(
                keccak256(
                    abi.encodePacked(
                        "\x19\x01",
                        DOMAIN_SEPARATOR(),
                        keccak256(
                            abi.encode(
                                keccak256(
                                    "Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
                                ),
                                owner,
                                spender,
                                value,
                                nonces[owner]++,
                                deadline
                            )
                        )
                    )
                ),
                v,
                r,
                s
            );

            require(recoveredAddress != address(0) && recoveredAddress == owner, "INVALID_SIGNER");

            allowance[recoveredAddress][spender] = value;
        }

        emit Approval(owner, spender, value);
    }

    function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
        return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
    }

    function computeDomainSeparator() internal view virtual returns (bytes32) {
        return
            keccak256(
                abi.encode(
                    keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
                    keccak256(bytes(name)),
                    keccak256("1"),
                    block.chainid,
                    address(this)
                )
            );
    }

    /*//////////////////////////////////////////////////////////////
                        INTERNAL MINT/BURN LOGIC
    //////////////////////////////////////////////////////////////*/

    function _mint(address to, uint256 amount) internal virtual {
        totalSupply += amount;

        // Cannot overflow because the sum of all user
        // balances can't exceed the max uint256 value.
        unchecked {
            balanceOf[to] += amount;
        }

        emit Transfer(address(0), to, amount);
    }

    function _burn(address from, uint256 amount) internal virtual {
        balanceOf[from] -= amount;

        // Cannot underflow because a user's balance
        // will never be larger than the total supply.
        unchecked {
            totalSupply -= amount;
        }

        emit Transfer(from, address(0), amount);
    }
}

File 25 of 52 : ERC721Enumerable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC721/extensions/ERC721Enumerable.sol)

pragma solidity ^0.8.0;

import "../ERC721.sol";
import "./IERC721Enumerable.sol";

/**
 * @dev This implements an optional extension of {ERC721} defined in the EIP that adds
 * enumerability of all the token ids in the contract as well as all token ids owned by each
 * account.
 */
abstract contract ERC721Enumerable is ERC721, IERC721Enumerable {
    // Mapping from owner to list of owned token IDs
    mapping(address => mapping(uint256 => uint256)) private _ownedTokens;

    // Mapping from token ID to index of the owner tokens list
    mapping(uint256 => uint256) private _ownedTokensIndex;

    // Array with all token ids, used for enumeration
    uint256[] private _allTokens;

    // Mapping from token id to position in the allTokens array
    mapping(uint256 => uint256) private _allTokensIndex;

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

    /**
     * @dev See {IERC721Enumerable-tokenOfOwnerByIndex}.
     */
    function tokenOfOwnerByIndex(address owner, uint256 index) public view virtual override returns (uint256) {
        require(index < ERC721.balanceOf(owner), "ERC721Enumerable: owner index out of bounds");
        return _ownedTokens[owner][index];
    }

    /**
     * @dev See {IERC721Enumerable-totalSupply}.
     */
    function totalSupply() public view virtual override returns (uint256) {
        return _allTokens.length;
    }

    /**
     * @dev See {IERC721Enumerable-tokenByIndex}.
     */
    function tokenByIndex(uint256 index) public view virtual override returns (uint256) {
        require(index < ERC721Enumerable.totalSupply(), "ERC721Enumerable: global index out of bounds");
        return _allTokens[index];
    }

    /**
     * @dev Hook that is called before any token transfer. This includes minting
     * and burning.
     *
     * Calling conditions:
     *
     * - When `from` and `to` are both non-zero, ``from``'s `tokenId` will be
     * transferred to `to`.
     * - When `from` is zero, `tokenId` will be minted for `to`.
     * - When `to` is zero, ``from``'s `tokenId` will be burned.
     * - `from` cannot be the zero address.
     * - `to` cannot be the zero address.
     *
     * To learn more about hooks, head to xref:ROOT:extending-contracts.adoc#using-hooks[Using Hooks].
     */
    function _beforeTokenTransfer(
        address from,
        address to,
        uint256 tokenId
    ) internal virtual override {
        super._beforeTokenTransfer(from, to, tokenId);

        if (from == address(0)) {
            _addTokenToAllTokensEnumeration(tokenId);
        } else if (from != to) {
            _removeTokenFromOwnerEnumeration(from, tokenId);
        }
        if (to == address(0)) {
            _removeTokenFromAllTokensEnumeration(tokenId);
        } else if (to != from) {
            _addTokenToOwnerEnumeration(to, tokenId);
        }
    }

    /**
     * @dev Private function to add a token to this extension's ownership-tracking data structures.
     * @param to address representing the new owner of the given token ID
     * @param tokenId uint256 ID of the token to be added to the tokens list of the given address
     */
    function _addTokenToOwnerEnumeration(address to, uint256 tokenId) private {
        uint256 length = ERC721.balanceOf(to);
        _ownedTokens[to][length] = tokenId;
        _ownedTokensIndex[tokenId] = length;
    }

    /**
     * @dev Private function to add a token to this extension's token tracking data structures.
     * @param tokenId uint256 ID of the token to be added to the tokens list
     */
    function _addTokenToAllTokensEnumeration(uint256 tokenId) private {
        _allTokensIndex[tokenId] = _allTokens.length;
        _allTokens.push(tokenId);
    }

    /**
     * @dev Private function to remove a token from this extension's ownership-tracking data structures. Note that
     * while the token is not assigned a new owner, the `_ownedTokensIndex` mapping is _not_ updated: this allows for
     * gas optimizations e.g. when performing a transfer operation (avoiding double writes).
     * This has O(1) time complexity, but alters the order of the _ownedTokens array.
     * @param from address representing the previous owner of the given token ID
     * @param tokenId uint256 ID of the token to be removed from the tokens list of the given address
     */
    function _removeTokenFromOwnerEnumeration(address from, uint256 tokenId) private {
        // To prevent a gap in from's tokens array, we store the last token in the index of the token to delete, and
        // then delete the last slot (swap and pop).

        uint256 lastTokenIndex = ERC721.balanceOf(from) - 1;
        uint256 tokenIndex = _ownedTokensIndex[tokenId];

        // When the token to delete is the last token, the swap operation is unnecessary
        if (tokenIndex != lastTokenIndex) {
            uint256 lastTokenId = _ownedTokens[from][lastTokenIndex];

            _ownedTokens[from][tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
            _ownedTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index
        }

        // This also deletes the contents at the last position of the array
        delete _ownedTokensIndex[tokenId];
        delete _ownedTokens[from][lastTokenIndex];
    }

    /**
     * @dev Private function to remove a token from this extension's token tracking data structures.
     * This has O(1) time complexity, but alters the order of the _allTokens array.
     * @param tokenId uint256 ID of the token to be removed from the tokens list
     */
    function _removeTokenFromAllTokensEnumeration(uint256 tokenId) private {
        // To prevent a gap in the tokens array, we store the last token in the index of the token to delete, and
        // then delete the last slot (swap and pop).

        uint256 lastTokenIndex = _allTokens.length - 1;
        uint256 tokenIndex = _allTokensIndex[tokenId];

        // When the token to delete is the last token, the swap operation is unnecessary. However, since this occurs so
        // rarely (when the last minted token is burnt) that we still do the swap here to avoid the gas cost of adding
        // an 'if' statement (like in _removeTokenFromOwnerEnumeration)
        uint256 lastTokenId = _allTokens[lastTokenIndex];

        _allTokens[tokenIndex] = lastTokenId; // Move the last token to the slot of the to-delete token
        _allTokensIndex[lastTokenId] = tokenIndex; // Update the moved token's index

        // This also deletes the contents at the last position of the array
        delete _allTokensIndex[tokenId];
        _allTokens.pop();
    }
}

File 26 of 52 : IERC1820Registry.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (utils/introspection/IERC1820Registry.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the global ERC1820 Registry, as defined in the
 * https://eips.ethereum.org/EIPS/eip-1820[EIP]. Accounts may register
 * implementers for interfaces in this registry, as well as query support.
 *
 * Implementers may be shared by multiple accounts, and can also implement more
 * than a single interface for each account. Contracts can implement interfaces
 * for themselves, but externally-owned accounts (EOA) must delegate this to a
 * contract.
 *
 * {IERC165} interfaces can also be queried via the registry.
 *
 * For an in-depth explanation and source code analysis, see the EIP text.
 */
interface IERC1820Registry {
    event InterfaceImplementerSet(address indexed account, bytes32 indexed interfaceHash, address indexed implementer);

    event ManagerChanged(address indexed account, address indexed newManager);

    /**
     * @dev Sets `newManager` as the manager for `account`. A manager of an
     * account is able to set interface implementers for it.
     *
     * By default, each account is its own manager. Passing a value of `0x0` in
     * `newManager` will reset the manager to this initial state.
     *
     * Emits a {ManagerChanged} event.
     *
     * Requirements:
     *
     * - the caller must be the current manager for `account`.
     */
    function setManager(address account, address newManager) external;

    /**
     * @dev Returns the manager for `account`.
     *
     * See {setManager}.
     */
    function getManager(address account) external view returns (address);

    /**
     * @dev Sets the `implementer` contract as ``account``'s implementer for
     * `interfaceHash`.
     *
     * `account` being the zero address is an alias for the caller's address.
     * The zero address can also be used in `implementer` to remove an old one.
     *
     * See {interfaceHash} to learn how these are created.
     *
     * Emits an {InterfaceImplementerSet} event.
     *
     * Requirements:
     *
     * - the caller must be the current manager for `account`.
     * - `interfaceHash` must not be an {IERC165} interface id (i.e. it must not
     * end in 28 zeroes).
     * - `implementer` must implement {IERC1820Implementer} and return true when
     * queried for support, unless `implementer` is the caller. See
     * {IERC1820Implementer-canImplementInterfaceForAddress}.
     */
    function setInterfaceImplementer(
        address account,
        bytes32 _interfaceHash,
        address implementer
    ) external;

    /**
     * @dev Returns the implementer of `interfaceHash` for `account`. If no such
     * implementer is registered, returns the zero address.
     *
     * If `interfaceHash` is an {IERC165} interface id (i.e. it ends with 28
     * zeroes), `account` will be queried for support of it.
     *
     * `account` being the zero address is an alias for the caller's address.
     */
    function getInterfaceImplementer(address account, bytes32 _interfaceHash) external view returns (address);

    /**
     * @dev Returns the interface hash for an `interfaceName`, as defined in the
     * corresponding
     * https://eips.ethereum.org/EIPS/eip-1820#interface-name[section of the EIP].
     */
    function interfaceHash(string calldata interfaceName) external pure returns (bytes32);

    /**
     * @notice Updates the cache with whether the contract implements an ERC165 interface or not.
     * @param account Address of the contract for which to update the cache.
     * @param interfaceId ERC165 interface for which to update the cache.
     */
    function updateERC165Cache(address account, bytes4 interfaceId) external;

    /**
     * @notice Checks whether a contract implements an ERC165 interface or not.
     * If the result is not cached a direct lookup on the contract address is performed.
     * If the result is not cached or the cached value is out-of-date, the cache MUST be updated manually by calling
     * {updateERC165Cache} with the contract address.
     * @param account Address of the contract to check.
     * @param interfaceId ERC165 interface to check.
     * @return True if `account` implements `interfaceId`, false otherwise.
     */
    function implementsERC165Interface(address account, bytes4 interfaceId) external view returns (bool);

    /**
     * @notice Checks whether a contract implements an ERC165 interface or not without using nor updating the cache.
     * @param account Address of the contract to check.
     * @param interfaceId ERC165 interface to check.
     * @return True if `account` implements `interfaceId`, false otherwise.
     */
    function implementsERC165InterfaceNoCache(address account, bytes4 interfaceId) external view returns (bool);
}

File 27 of 52 : SafeTransferLib.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;

import {ERC20} from "../tokens/ERC20.sol";

/// @notice Safe ETH and ERC20 transfer library that gracefully handles missing return values.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/utils/SafeTransferLib.sol)
/// @dev Use with caution! Some functions in this library knowingly create dirty bits at the destination of the free memory pointer.
/// @dev Note that none of the functions in this library check that a token has code at all! That responsibility is delegated to the caller.
library SafeTransferLib {
    /*//////////////////////////////////////////////////////////////
                             ETH OPERATIONS
    //////////////////////////////////////////////////////////////*/

    function safeTransferETH(address to, uint256 amount) internal {
        bool success;

        assembly {
            // Transfer the ETH and store if it succeeded or not.
            success := call(gas(), to, amount, 0, 0, 0, 0)
        }

        require(success, "ETH_TRANSFER_FAILED");
    }

    /*//////////////////////////////////////////////////////////////
                            ERC20 OPERATIONS
    //////////////////////////////////////////////////////////////*/

    function safeTransferFrom(
        ERC20 token,
        address from,
        address to,
        uint256 amount
    ) internal {
        bool success;

        assembly {
            // Get a pointer to some free memory.
            let freeMemoryPointer := mload(0x40)

            // Write the abi-encoded calldata into memory, beginning with the function selector.
            mstore(freeMemoryPointer, 0x23b872dd00000000000000000000000000000000000000000000000000000000)
            mstore(add(freeMemoryPointer, 4), from) // Append the "from" argument.
            mstore(add(freeMemoryPointer, 36), to) // Append the "to" argument.
            mstore(add(freeMemoryPointer, 68), amount) // Append the "amount" argument.

            success := and(
                // Set success to whether the call reverted, if not we check it either
                // returned exactly 1 (can't just be non-zero data), or had no return data.
                or(and(eq(mload(0), 1), gt(returndatasize(), 31)), iszero(returndatasize())),
                // We use 100 because the length of our calldata totals up like so: 4 + 32 * 3.
                // We use 0 and 32 to copy up to 32 bytes of return data into the scratch space.
                // Counterintuitively, this call must be positioned second to the or() call in the
                // surrounding and() call or else returndatasize() will be zero during the computation.
                call(gas(), token, 0, freeMemoryPointer, 100, 0, 32)
            )
        }

        require(success, "TRANSFER_FROM_FAILED");
    }

    function safeTransfer(
        ERC20 token,
        address to,
        uint256 amount
    ) internal {
        bool success;

        assembly {
            // Get a pointer to some free memory.
            let freeMemoryPointer := mload(0x40)

            // Write the abi-encoded calldata into memory, beginning with the function selector.
            mstore(freeMemoryPointer, 0xa9059cbb00000000000000000000000000000000000000000000000000000000)
            mstore(add(freeMemoryPointer, 4), to) // Append the "to" argument.
            mstore(add(freeMemoryPointer, 36), amount) // Append the "amount" argument.

            success := and(
                // Set success to whether the call reverted, if not we check it either
                // returned exactly 1 (can't just be non-zero data), or had no return data.
                or(and(eq(mload(0), 1), gt(returndatasize(), 31)), iszero(returndatasize())),
                // We use 68 because the length of our calldata totals up like so: 4 + 32 * 2.
                // We use 0 and 32 to copy up to 32 bytes of return data into the scratch space.
                // Counterintuitively, this call must be positioned second to the or() call in the
                // surrounding and() call or else returndatasize() will be zero during the computation.
                call(gas(), token, 0, freeMemoryPointer, 68, 0, 32)
            )
        }

        require(success, "TRANSFER_FAILED");
    }

    function safeApprove(
        ERC20 token,
        address to,
        uint256 amount
    ) internal {
        bool success;

        assembly {
            // Get a pointer to some free memory.
            let freeMemoryPointer := mload(0x40)

            // Write the abi-encoded calldata into memory, beginning with the function selector.
            mstore(freeMemoryPointer, 0x095ea7b300000000000000000000000000000000000000000000000000000000)
            mstore(add(freeMemoryPointer, 4), to) // Append the "to" argument.
            mstore(add(freeMemoryPointer, 36), amount) // Append the "amount" argument.

            success := and(
                // Set success to whether the call reverted, if not we check it either
                // returned exactly 1 (can't just be non-zero data), or had no return data.
                or(and(eq(mload(0), 1), gt(returndatasize(), 31)), iszero(returndatasize())),
                // We use 68 because the length of our calldata totals up like so: 4 + 32 * 2.
                // We use 0 and 32 to copy up to 32 bytes of return data into the scratch space.
                // Counterintuitively, this call must be positioned second to the or() call in the
                // surrounding and() call or else returndatasize() will be zero during the computation.
                call(gas(), token, 0, freeMemoryPointer, 68, 0, 32)
            )
        }

        require(success, "APPROVE_FAILED");
    }
}

File 28 of 52 : IERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 standard as defined in the EIP.
 */
interface IERC20 {
    /**
     * @dev Emitted when `value` tokens are moved from one account (`from`) to
     * another (`to`).
     *
     * Note that `value` may be zero.
     */
    event Transfer(address indexed from, address indexed to, uint256 value);

    /**
     * @dev Emitted when the allowance of a `spender` for an `owner` is set by
     * a call to {approve}. `value` is the new allowance.
     */
    event Approval(address indexed owner, address indexed spender, uint256 value);

    /**
     * @dev Returns the amount of tokens in existence.
     */
    function totalSupply() external view returns (uint256);

    /**
     * @dev Returns the amount of tokens owned by `account`.
     */
    function balanceOf(address account) external view returns (uint256);

    /**
     * @dev Moves `amount` tokens from the caller's account to `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 amount) external returns (bool);

    /**
     * @dev Returns the remaining number of tokens that `spender` will be
     * allowed to spend on behalf of `owner` through {transferFrom}. This is
     * zero by default.
     *
     * This value changes when {approve} or {transferFrom} are called.
     */
    function allowance(address owner, address spender) external view returns (uint256);

    /**
     * @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * IMPORTANT: Beware that changing an allowance with this method brings the risk
     * that someone may use both the old and the new allowance by unfortunate
     * transaction ordering. One possible solution to mitigate this race
     * condition is to first reduce the spender's allowance to 0 and set the
     * desired value afterwards:
     * https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
     *
     * Emits an {Approval} event.
     */
    function approve(address spender, uint256 amount) external returns (bool);

    /**
     * @dev Moves `amount` tokens from `from` to `to` using the
     * allowance mechanism. `amount` is then deducted from the caller's
     * allowance.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transferFrom(
        address from,
        address to,
        uint256 amount
    ) external returns (bool);
}

File 29 of 52 : SafeERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (token/ERC20/utils/SafeERC20.sol)

pragma solidity ^0.8.0;

import "../IERC20.sol";
import "../extensions/draft-IERC20Permit.sol";
import "../../../utils/Address.sol";

/**
 * @title SafeERC20
 * @dev Wrappers around ERC20 operations that throw on failure (when the token
 * contract returns false). Tokens that return no value (and instead revert or
 * throw on failure) are also supported, non-reverting calls are assumed to be
 * successful.
 * To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
 * which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
 */
library SafeERC20 {
    using Address for address;

    function safeTransfer(
        IERC20 token,
        address to,
        uint256 value
    ) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
    }

    function safeTransferFrom(
        IERC20 token,
        address from,
        address to,
        uint256 value
    ) internal {
        _callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
    }

    /**
     * @dev Deprecated. This function has issues similar to the ones found in
     * {IERC20-approve}, and its usage is discouraged.
     *
     * Whenever possible, use {safeIncreaseAllowance} and
     * {safeDecreaseAllowance} instead.
     */
    function safeApprove(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        // safeApprove should only be called when setting an initial allowance,
        // or when resetting it to zero. To increase and decrease it, use
        // 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
        require(
            (value == 0) || (token.allowance(address(this), spender) == 0),
            "SafeERC20: approve from non-zero to non-zero allowance"
        );
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
    }

    function safeIncreaseAllowance(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        uint256 newAllowance = token.allowance(address(this), spender) + value;
        _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
    }

    function safeDecreaseAllowance(
        IERC20 token,
        address spender,
        uint256 value
    ) internal {
        unchecked {
            uint256 oldAllowance = token.allowance(address(this), spender);
            require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
            uint256 newAllowance = oldAllowance - value;
            _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
        }
    }

    function safePermit(
        IERC20Permit token,
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) internal {
        uint256 nonceBefore = token.nonces(owner);
        token.permit(owner, spender, value, deadline, v, r, s);
        uint256 nonceAfter = token.nonces(owner);
        require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
    }

    /**
     * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
     * on the return value: the return value is optional (but if data is returned, it must not be false).
     * @param token The token targeted by the call.
     * @param data The call data (encoded using abi.encode or one of its variants).
     */
    function _callOptionalReturn(IERC20 token, bytes memory data) private {
        // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
        // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
        // the target address contains contract code and also asserts for success in the low-level call.

        bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
        if (returndata.length > 0) {
            // Return data is optional
            require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
        }
    }
}

File 30 of 52 : 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 31 of 52 : draft-IERC20Permit.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/draft-IERC20Permit.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
 * https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
 *
 * Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
 * presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
 * need to send a transaction, and thus is not required to hold Ether at all.
 */
interface IERC20Permit {
    /**
     * @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
     * given ``owner``'s signed approval.
     *
     * IMPORTANT: The same issues {IERC20-approve} has related to transaction
     * ordering also apply here.
     *
     * Emits an {Approval} event.
     *
     * Requirements:
     *
     * - `spender` cannot be the zero address.
     * - `deadline` must be a timestamp in the future.
     * - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
     * over the EIP712-formatted function arguments.
     * - the signature must use ``owner``'s current nonce (see {nonces}).
     *
     * For more information on the signature format, see the
     * https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
     * section].
     */
    function permit(
        address owner,
        address spender,
        uint256 value,
        uint256 deadline,
        uint8 v,
        bytes32 r,
        bytes32 s
    ) external;

    /**
     * @dev Returns the current nonce for `owner`. This value must be
     * included whenever a signature is generated for {permit}.
     *
     * Every successful call to {permit} increases ``owner``'s nonce by one. This
     * prevents a signature from being used multiple times.
     */
    function nonces(address owner) external view returns (uint256);

    /**
     * @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
     */
    // solhint-disable-next-line func-name-mixedcase
    function DOMAIN_SEPARATOR() external view returns (bytes32);
}

File 32 of 52 : ITokenIDFilter.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

interface ITokenIDFilter {
    function tokenIDFilterRoot() external view returns (bytes32);
}

File 33 of 52 : IExternalFilter.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {ERC165} from "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import {IERC165} from "@openzeppelin/contracts/interfaces/IERC165.sol";

interface IExternalFilter is IERC165 {
    /**
     * @notice Pools can nominate an external contract to approve whether NFT IDs are accepted.
     * This is typically used to implement some kind of dynamic block list, e.g. stolen NFTs.
     * @param collection NFT contract address
     * @param nftIds List of NFT IDs to check
     * @return allowed True if swap (pool buys) is allowed
     */
    function areNFTsAllowed(address collection, uint256[] calldata nftIds, bytes calldata context)
        external
        returns (bool allowed);
}

File 34 of 52 : 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 35 of 52 : CurveErrorCodes.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

contract CurveErrorCodes {
    error InvalidNumItems(); // The numItem value is 0
    error SpotPriceOverflow(); // The updated spot price doesn't fit into 128 bits
    error TooManyItems(); // The value of numItems passed was too great
}

File 36 of 52 : IERC165.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (interfaces/IERC165.sol)

pragma solidity ^0.8.0;

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

File 37 of 52 : 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 38 of 52 : IPoolActivityMonitor.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {ERC165} from "@openzeppelin/contracts/utils/introspection/ERC165.sol";
import {IERC165} from "@openzeppelin/contracts/interfaces/IERC165.sol";
import {EventType} from "./CollectionStructsAndEnums.sol";

interface IPoolActivityMonitor is IERC165 {
    /**
     * @dev This hook allows pool owners (i.e. owner of the LP token) to observe
     * changes to pools initiated by third-parties, i.e. swaps and deposits.
     *
     * @param amounts If `eventType` is a swap, then `amounts` is an array with
     * 3 elements. The first is the number of nfts swapped. The second is the
     * price of the last NFT swapped (after all fees are applied, i.e. input or
     * output amount if quantity were 1). The third is the total value of the
     * swap with fees included. If `eventType` is not a swap, then amounts is a
     * length 1 array of the amount of token/NFT deposited/withdrawn.
     */
    function onBalancesChanged(address poolAddress, EventType eventType, uint256[] memory amounts) external;
}

File 39 of 52 : TokenIDFilter.sol
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/utils/cryptography/MerkleProof.sol";
import {ITokenIDFilter} from "./ITokenIDFilter.sol";

contract TokenIDFilter is ITokenIDFilter {
    event AcceptsTokenIDs(address indexed _collection, bytes32 indexed _root, bytes _data);

    // Merkle root
    bytes32 public tokenIDFilterRoot;

    function _setRootAndEmitAcceptedIDs(address collection, bytes32 root, bytes calldata data) internal {
        tokenIDFilterRoot = root;
        emit AcceptsTokenIDs(collection, tokenIDFilterRoot, data);
    }

    function _acceptsTokenID(uint256 tokenID, bytes32[] calldata proof) internal view returns (bool) {
        if (tokenIDFilterRoot == 0) {
            return true;
        }

        // double hash to prevent second preimage attack
        bytes32 leaf = keccak256(abi.encodePacked(keccak256(abi.encodePacked((tokenID)))));

        return MerkleProof.verifyCalldata(proof, tokenIDFilterRoot, leaf);
    }

    function _emitTokenIDs(address collection, bytes calldata data) internal {
        emit AcceptsTokenIDs(collection, tokenIDFilterRoot, data);
    }

    function _acceptsTokenIDs(uint256[] calldata tokenIDs, bytes32[] calldata proof, bool[] calldata proofFlags)
        internal
        view
        returns (bool)
    {
        if (tokenIDFilterRoot == 0) {
            return true;
        }

        uint256 length = tokenIDs.length;
        bytes32[] memory leaves = new bytes32[](length);

        for (uint256 i; i < length;) {
            // double hash to prevent second preimage attack
            leaves[i] = keccak256(abi.encodePacked(keccak256(abi.encodePacked((tokenIDs[i])))));
            unchecked {
                ++i;
            }
        }

        return MerkleProof.multiProofVerify(proof, proofFlags, tokenIDFilterRoot, leaves);
    }
}

File 40 of 52 : 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 41 of 52 : 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);
}

File 42 of 52 : ERC1155Holder.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC1155/utils/ERC1155Holder.sol)

pragma solidity ^0.8.0;

import "./ERC1155Receiver.sol";

/**
 * Simple implementation of `ERC1155Receiver` that will allow a contract to hold ERC1155 tokens.
 *
 * IMPORTANT: When inheriting this contract, you must include a way to use the received tokens, otherwise they will be
 * stuck.
 *
 * @dev _Available since v3.1._
 */
contract ERC1155Holder is ERC1155Receiver {
    function onERC1155Received(
        address,
        address,
        uint256,
        uint256,
        bytes memory
    ) public virtual override returns (bytes4) {
        return this.onERC1155Received.selector;
    }

    function onERC1155BatchReceived(
        address,
        address,
        uint256[] memory,
        uint256[] memory,
        bytes memory
    ) public virtual override returns (bytes4) {
        return this.onERC1155BatchReceived.selector;
    }
}

File 43 of 52 : 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 44 of 52 : ERC1155Receiver.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC1155/utils/ERC1155Receiver.sol)

pragma solidity ^0.8.0;

import "../IERC1155Receiver.sol";
import "../../../utils/introspection/ERC165.sol";

/**
 * @dev _Available since v3.1._
 */
abstract contract ERC1155Receiver is ERC165, IERC1155Receiver {
    /**
     * @dev See {IERC165-supportsInterface}.
     */
    function supportsInterface(bytes4 interfaceId) public view virtual override(ERC165, IERC165) returns (bool) {
        return interfaceId == type(IERC1155Receiver).interfaceId || super.supportsInterface(interfaceId);
    }
}

File 45 of 52 : 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 46 of 52 : CollectionPoolEnumerable.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {IERC721Enumerable} from "@openzeppelin/contracts/token/ERC721/extensions/IERC721Enumerable.sol";
import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {BitMaps} from "@openzeppelin/contracts/utils/structs/BitMaps.sol";
import {TransferLib} from "../lib/TransferLib.sol";
import {CollectionRouter} from "../routers/CollectionRouter.sol";
import {ICollectionPool} from "./ICollectionPool.sol";
import {CollectionPool} from "./CollectionPool.sol";
import {ICollectionPoolFactory} from "./ICollectionPoolFactory.sol";

/**
 * @title An NFT/Token pool for an NFT that implements ERC721Enumerable
 * @author Collection
 */
abstract contract CollectionPoolEnumerable is CollectionPool {
    using BitMaps for BitMaps.BitMap;

    // NFT IDs that match our filter are maintained in this BitMap and counted in idLength
    BitMaps.BitMap private idMap;
    uint256 private idLength;

    /// @inheritdoc CollectionPool
    function _selectArbitraryNFTs(IERC721 _nft, uint256 numNFTs)
        internal
        view
        override
        returns (uint256[] memory nftIds)
    {
        uint256 userBalance = _nft.balanceOf(address(this));
        uint256 j;

        if (numNFTs > 0 && numNFTs <= userBalance) {
            nftIds = new uint256[](numNFTs);
            for (uint256 i; i < numNFTs;) {
                // index will be out of bounds if numNFTs > balance
                uint256 nftId = IERC721Enumerable(address(_nft)).tokenOfOwnerByIndex(address(this), j);

                // make sure it's a legal (filtered) ID
                if (idMap.get(nftId)) {
                    nftIds[i] = nftId;
                    unchecked {
                        ++i;
                    }
                }

                unchecked {
                    ++j;
                }
            }
        }
    }

    /// @inheritdoc CollectionPool
    function getAllHeldIds() public view override returns (uint256[] memory nftIds) {
        return _selectArbitraryNFTs(nft(), idLength);
    }

    /// @inheritdoc CollectionPool
    function _depositNFTs(address from, uint256[] calldata nftIds) internal override {
        // transfer NFTs to this pool and update map/size
        IERC721 _nft = nft();
        uint256 length = nftIds.length;
        uint256 _idLength = idLength;

        for (uint256 i; i < length;) {
            uint256 nftId = nftIds[i];
            _nft.safeTransferFrom(from, address(this), nftId);
            if (!idMap.get(nftId)) {
                idMap.set(nftId);
                ++_idLength;
            }

            unchecked {
                ++i;
            }
        }

        idLength = _idLength;
    }

    /// @inheritdoc CollectionPool
    function _depositNFTsNotification(uint256[] calldata nftIds) internal override {
        uint256 length = nftIds.length;
        uint256 _idLength = idLength;

        for (uint256 i; i < length;) {
            uint256 nftId = nftIds[i];
            if (!idMap.get(nftId)) {
                idMap.set(nftId);
                ++_idLength;
            }

            unchecked {
                ++i;
            }
        }

        idLength = _idLength;
    }

    /// @inheritdoc CollectionPool
    function _withdrawNFTs(address to, uint256[] memory nftIds) internal override {
        // Send NFTs to given address, update map and count
        IERC721 _nft = nft();
        uint256 numNFTs = nftIds.length;
        uint256 _idLength = idLength;

        for (uint256 i; i < numNFTs;) {
            uint256 nftId = nftIds[i];
            _nft.safeTransferFrom(address(this), to, nftId);
            // Remove from id map
            if (idMap.get(nftId)) {
                idMap.unset(nftId);
                --_idLength;
            }

            unchecked {
                ++i;
            }
        }

        idLength = _idLength;
    }

    /// @inheritdoc ICollectionPool
    function NFTsCount() external view returns (uint256) {
        return idLength;
    }
}

File 47 of 52 : BitMaps.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/structs/BitMaps.sol)
pragma solidity ^0.8.0;

/**
 * @dev Library for managing uint256 to bool mapping in a compact and efficient way, providing the keys are sequential.
 * Largelly inspired by Uniswap's https://github.com/Uniswap/merkle-distributor/blob/master/contracts/MerkleDistributor.sol[merkle-distributor].
 */
library BitMaps {
    struct BitMap {
        mapping(uint256 => uint256) _data;
    }

    /**
     * @dev Returns whether the bit at `index` is set.
     */
    function get(BitMap storage bitmap, uint256 index) internal view returns (bool) {
        uint256 bucket = index >> 8;
        uint256 mask = 1 << (index & 0xff);
        return bitmap._data[bucket] & mask != 0;
    }

    /**
     * @dev Sets the bit at `index` to the boolean `value`.
     */
    function setTo(
        BitMap storage bitmap,
        uint256 index,
        bool value
    ) internal {
        if (value) {
            set(bitmap, index);
        } else {
            unset(bitmap, index);
        }
    }

    /**
     * @dev Sets the bit at `index`.
     */
    function set(BitMap storage bitmap, uint256 index) internal {
        uint256 bucket = index >> 8;
        uint256 mask = 1 << (index & 0xff);
        bitmap._data[bucket] |= mask;
    }

    /**
     * @dev Unsets the bit at `index`.
     */
    function unset(BitMap storage bitmap, uint256 index) internal {
        uint256 bucket = index >> 8;
        uint256 mask = 1 << (index & 0xff);
        bitmap._data[bucket] &= ~mask;
    }
}

File 48 of 52 : CollectionPoolMissingEnumerable.sol
// SPDX-License-Identifier: AGPL-3.0
pragma solidity ^0.8.0;

import {IERC721} from "@openzeppelin/contracts/token/ERC721/IERC721.sol";
import {EnumerableSet} from "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import {TransferLib} from "../lib/TransferLib.sol";
import {ICollectionPool} from "../pools/ICollectionPool.sol";
import {CollectionPool} from "../pools/CollectionPool.sol";
import {CollectionRouter} from "../routers/CollectionRouter.sol";

/**
 * @title An NFT/Token pool for an NFT that does not implement ERC721Enumerable
 * @author Collection
 */
abstract contract CollectionPoolMissingEnumerable is CollectionPool {
    using EnumerableSet for EnumerableSet.UintSet;

    // Used for internal ID tracking
    EnumerableSet.UintSet private idSet;

    /// @inheritdoc CollectionPool
    function _selectArbitraryNFTs(IERC721, uint256 numNFTs) internal view override returns (uint256[] memory nftIds) {
        uint256 lastIndex = idSet.length();
        if (numNFTs > 0 && numNFTs <= lastIndex) {
            nftIds = new uint256[](numNFTs);
            // NOTE: We start from last index to first index to save on gas when we eventully _withdrawNFTs on results
            lastIndex = lastIndex - 1;

            for (uint256 i; i < numNFTs;) {
                uint256 nftId = idSet.at(lastIndex);
                nftIds[i] = nftId;

                unchecked {
                    --lastIndex;
                    ++i;
                }
            }
        }
    }

    /// @inheritdoc CollectionPool
    function getAllHeldIds() public view override returns (uint256[] memory nftIds) {
        nftIds = idSet.values();
    }

    /// @inheritdoc CollectionPool
    function _depositNFTs(address from, uint256[] calldata nftIds) internal override {
        // transfer NFTs to this pool and update set
        IERC721 _nft = nft();
        uint256 length = nftIds.length;
        for (uint256 i; i < length;) {
            uint256 nftId = nftIds[i];
            _nft.safeTransferFrom(from, address(this), nftId);
            idSet.add(nftId);

            unchecked {
                ++i;
            }
        }
    }

    /// @inheritdoc CollectionPool
    function _depositNFTsNotification(uint256[] calldata nftIds) internal override {
        uint256 length = nftIds.length;
        for (uint256 i; i < length;) {
            idSet.add(nftIds[i]);

            unchecked {
                ++i;
            }
        }
    }

    /// @inheritdoc CollectionPool
    function _withdrawNFTs(address to, uint256[] memory nftIds) internal override {
        IERC721 _nft = nft();

        // Send NFTs to given addres and update valid set
        uint256 numNFTs = nftIds.length;
        for (uint256 i; i < numNFTs;) {
            _nft.safeTransferFrom(address(this), to, nftIds[i]);
            idSet.remove(nftIds[i]);

            unchecked {
                ++i;
            }
        }
    }

    /// @inheritdoc ICollectionPool
    function NFTsCount() external view returns (uint256) {
        return idSet.length();
    }
}

File 49 of 52 : 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 50 of 52 : 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 51 of 52 : IERC721Receiver.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC721/IERC721Receiver.sol)

pragma solidity ^0.8.0;

/**
 * @title ERC721 token receiver interface
 * @dev Interface for any contract that wants to support safeTransfers
 * from ERC721 asset contracts.
 */
interface IERC721Receiver {
    /**
     * @dev Whenever an {IERC721} `tokenId` token is transferred to this contract via {IERC721-safeTransferFrom}
     * by `operator` from `from`, this function is called.
     *
     * It must return its Solidity selector to confirm the token transfer.
     * If any other value is returned or the interface is not implemented by the recipient, the transfer will be reverted.
     *
     * The selector can be obtained in Solidity with `IERC721Receiver.onERC721Received.selector`.
     */
    function onERC721Received(
        address operator,
        address from,
        uint256 tokenId,
        bytes calldata data
    ) external returns (bytes4);
}

File 52 of 52 : IERC721Metadata.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC721/extensions/IERC721Metadata.sol)

pragma solidity ^0.8.0;

import "../IERC721.sol";

/**
 * @title ERC-721 Non-Fungible Token Standard, optional metadata extension
 * @dev See https://eips.ethereum.org/EIPS/eip-721
 */
interface IERC721Metadata is IERC721 {
    /**
     * @dev Returns the token collection name.
     */
    function name() external view returns (string memory);

    /**
     * @dev Returns the token collection symbol.
     */
    function symbol() external view returns (string memory);

    /**
     * @dev Returns the Uniform Resource Identifier (URI) for `tokenId` token.
     */
    function tokenURI(uint256 tokenId) external view returns (string memory);
}

Settings
{
  "optimizer": {
    "enabled": true,
    "runs": 150
  },
  "viaIR": true,
  "outputSelection": {
    "*": {
      "*": [
        "evm.bytecode",
        "evm.deployedBytecode",
        "devdoc",
        "userdoc",
        "metadata",
        "abi"
      ]
    }
  },
  "libraries": {}
}

Contract Security Audit

Contract ABI

[{"inputs":[{"internalType":"contract CollectionPoolEnumerableETH","name":"_enumerableETHTemplate","type":"address"},{"internalType":"contract CollectionPoolMissingEnumerableETH","name":"_missingEnumerableETHTemplate","type":"address"},{"internalType":"contract CollectionPoolEnumerableERC20","name":"_enumerableERC20Template","type":"address"},{"internalType":"contract CollectionPoolMissingEnumerableERC20","name":"_missingEnumerableERC20Template","type":"address"},{"internalType":"address payable","name":"_protocolFeeRecipient","type":"address"},{"internalType":"uint24","name":"_protocolFeeMultiplier","type":"uint24"},{"internalType":"uint24","name":"_carryFeeMultiplier","type":"uint24"}],"stateMutability":"nonpayable","type":"constructor"},{"inputs":[{"internalType":"uint256","name":"msgValue","type":"uint256"},{"internalType":"uint256","name":"amountRequired","type":"uint256"}],"name":"InsufficientValue","type":"error"},{"inputs":[],"name":"Reentrancy","type":"error"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"approved","type":"address"},{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"owner","type":"address"},{"indexed":true,"internalType":"address","name":"operator","type":"address"},{"indexed":false,"internalType":"bool","name":"approved","type":"bool"}],"name":"ApprovalForAll","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract ICurve","name":"bondingCurve","type":"address"},{"indexed":false,"internalType":"bool","name":"isAllowed","type":"bool"}],"name":"BondingCurveStatusUpdate","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"target","type":"address"},{"indexed":false,"internalType":"bool","name":"isAllowed","type":"bool"}],"name":"CallTargetStatusUpdate","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint24","name":"newMultiplier","type":"uint24"}],"name":"CarryFeeMultiplierUpdate","type":"event"},{"anonymous":false,"inputs":[],"name":"CreationPause","type":"event"},{"anonymous":false,"inputs":[],"name":"CreationUnpause","type":"event"},{"anonymous":false,"inputs":[],"name":"DepositPause","type":"event"},{"anonymous":false,"inputs":[],"name":"DepositUnpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"collection","type":"address"},{"indexed":true,"internalType":"address","name":"poolAddress","type":"address"}],"name":"NewPool","type":"event"},{"anonymous":false,"inputs":[],"name":"OthersPause","type":"event"},{"anonymous":false,"inputs":[],"name":"OthersUnpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"internalType":"uint24","name":"newMultiplier","type":"uint24"}],"name":"ProtocolFeeMultiplierUpdate","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"recipientAddress","type":"address"}],"name":"ProtocolFeeRecipientUpdate","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"contract CollectionRouter","name":"router","type":"address"},{"indexed":false,"internalType":"bool","name":"isAllowed","type":"bool"}],"name":"RouterStatusUpdate","type":"event"},{"anonymous":false,"inputs":[],"name":"SwapPause","type":"event"},{"anonymous":false,"inputs":[],"name":"SwapUnpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"poolAddress","type":"address"}],"name":"TokenDeposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"from","type":"address"},{"indexed":true,"internalType":"address","name":"to","type":"address"},{"indexed":true,"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"Transfer","type":"event"},{"inputs":[],"name":"ETH_ADDRESS","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"approve","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"}],"name":"balanceOf","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"baseURI","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract ICurve","name":"","type":"address"}],"name":"bondingCurveAllowed","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"burn","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"}],"name":"callAllowed","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"carryFeeMultiplier","outputs":[{"internalType":"uint24","name":"","type":"uint24"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint24","name":"_carryFeeMultiplier","type":"uint24"}],"name":"changeCarryFeeMultiplier","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint24","name":"_protocolFeeMultiplier","type":"uint24"}],"name":"changeProtocolFeeMultiplier","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address payable","name":"_protocolFeeRecipient","type":"address"}],"name":"changeProtocolFeeRecipient","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"contract ERC20","name":"token","type":"address"},{"internalType":"contract IERC721","name":"nft","type":"address"},{"internalType":"contract ICurve","name":"bondingCurve","type":"address"},{"internalType":"address payable","name":"assetRecipient","type":"address"},{"internalType":"address","name":"receiver","type":"address"},{"internalType":"enum PoolType","name":"poolType","type":"uint8"},{"internalType":"uint128","name":"delta","type":"uint128"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"uint128","name":"spotPrice","type":"uint128"},{"internalType":"bytes","name":"props","type":"bytes"},{"internalType":"bytes","name":"state","type":"bytes"},{"internalType":"uint24","name":"royaltyNumerator","type":"uint24"},{"internalType":"address payable","name":"royaltyRecipientFallback","type":"address"},{"internalType":"uint256[]","name":"initialNFTIDs","type":"uint256[]"},{"internalType":"uint256","name":"initialTokenBalance","type":"uint256"}],"internalType":"struct CreateERC20PoolParams","name":"params","type":"tuple"}],"name":"createPoolERC20","outputs":[{"internalType":"contract ICollectionPool","name":"pool","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"contract ERC20","name":"token","type":"address"},{"internalType":"contract IERC721","name":"nft","type":"address"},{"internalType":"contract ICurve","name":"bondingCurve","type":"address"},{"internalType":"address payable","name":"assetRecipient","type":"address"},{"internalType":"address","name":"receiver","type":"address"},{"internalType":"enum PoolType","name":"poolType","type":"uint8"},{"internalType":"uint128","name":"delta","type":"uint128"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"uint128","name":"spotPrice","type":"uint128"},{"internalType":"bytes","name":"props","type":"bytes"},{"internalType":"bytes","name":"state","type":"bytes"},{"internalType":"uint24","name":"royaltyNumerator","type":"uint24"},{"internalType":"address payable","name":"royaltyRecipientFallback","type":"address"},{"internalType":"uint256[]","name":"initialNFTIDs","type":"uint256[]"},{"internalType":"uint256","name":"initialTokenBalance","type":"uint256"}],"internalType":"struct CreateERC20PoolParams","name":"params","type":"tuple"},{"components":[{"internalType":"bytes32","name":"merkleRoot","type":"bytes32"},{"internalType":"bytes","name":"encodedTokenIDs","type":"bytes"},{"internalType":"bytes32[]","name":"initialProof","type":"bytes32[]"},{"internalType":"bool[]","name":"initialProofFlags","type":"bool[]"},{"internalType":"contract IExternalFilter","name":"externalFilter","type":"address"}],"internalType":"struct NFTFilterParams","name":"filterParams","type":"tuple"}],"name":"createPoolERC20Filtered","outputs":[{"internalType":"contract ICollectionPool","name":"pool","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"components":[{"internalType":"contract IERC721","name":"nft","type":"address"},{"internalType":"contract ICurve","name":"bondingCurve","type":"address"},{"internalType":"address payable","name":"assetRecipient","type":"address"},{"internalType":"address","name":"receiver","type":"address"},{"internalType":"enum PoolType","name":"poolType","type":"uint8"},{"internalType":"uint128","name":"delta","type":"uint128"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"uint128","name":"spotPrice","type":"uint128"},{"internalType":"bytes","name":"props","type":"bytes"},{"internalType":"bytes","name":"state","type":"bytes"},{"internalType":"uint24","name":"royaltyNumerator","type":"uint24"},{"internalType":"address payable","name":"royaltyRecipientFallback","type":"address"},{"internalType":"uint256[]","name":"initialNFTIDs","type":"uint256[]"}],"internalType":"struct CreateETHPoolParams","name":"params","type":"tuple"}],"name":"createPoolETH","outputs":[{"internalType":"contract ICollectionPool","name":"pool","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[{"components":[{"internalType":"contract IERC721","name":"nft","type":"address"},{"internalType":"contract ICurve","name":"bondingCurve","type":"address"},{"internalType":"address payable","name":"assetRecipient","type":"address"},{"internalType":"address","name":"receiver","type":"address"},{"internalType":"enum PoolType","name":"poolType","type":"uint8"},{"internalType":"uint128","name":"delta","type":"uint128"},{"internalType":"uint24","name":"fee","type":"uint24"},{"internalType":"uint128","name":"spotPrice","type":"uint128"},{"internalType":"bytes","name":"props","type":"bytes"},{"internalType":"bytes","name":"state","type":"bytes"},{"internalType":"uint24","name":"royaltyNumerator","type":"uint24"},{"internalType":"address payable","name":"royaltyRecipientFallback","type":"address"},{"internalType":"uint256[]","name":"initialNFTIDs","type":"uint256[]"}],"internalType":"struct CreateETHPoolParams","name":"params","type":"tuple"},{"components":[{"internalType":"bytes32","name":"merkleRoot","type":"bytes32"},{"internalType":"bytes","name":"encodedTokenIDs","type":"bytes"},{"internalType":"bytes32[]","name":"initialProof","type":"bytes32[]"},{"internalType":"bool[]","name":"initialProofFlags","type":"bool[]"},{"internalType":"contract IExternalFilter","name":"externalFilter","type":"address"}],"internalType":"struct NFTFilterParams","name":"filterParams","type":"tuple"}],"name":"createPoolETHFiltered","outputs":[{"internalType":"contract ICollectionPool","name":"pool","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"creationPaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"depositPaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract ERC20","name":"token","type":"address"},{"components":[{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"address","name":"recipient","type":"address"}],"internalType":"struct RoyaltyDue[]","name":"royaltiesDue","type":"tuple[]"},{"internalType":"enum PoolVariant","name":"poolVariant","type":"uint8"}],"name":"depositRoyaltiesNotification","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"enumerableERC20Template","outputs":[{"internalType":"contract CollectionPoolEnumerableERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"enumerableETHTemplate","outputs":[{"internalType":"contract CollectionPoolEnumerableETH","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"getApproved","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"owner","type":"address"},{"internalType":"address","name":"operator","type":"address"}],"name":"isApprovedForAll","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"potentialPool","type":"address"}],"name":"isPool","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"potentialPool","type":"address"},{"internalType":"enum PoolVariant","name":"variant","type":"uint8"}],"name":"isPoolVariant","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"missingEnumerableERC20Template","outputs":[{"internalType":"contract CollectionPoolMissingEnumerableERC20","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"missingEnumerableETHTemplate","outputs":[{"internalType":"contract CollectionPoolMissingEnumerableETH","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"name","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"othersPaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"ownerOf","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"pauseCreation","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"pauseDeposit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"pauseOthers","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"pauseSwap","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"poolOf","outputs":[{"internalType":"contract ICollectionPool","name":"","type":"address"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"protocolFeeMultiplier","outputs":[{"internalType":"uint24","name":"","type":"uint24"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"protocolFeeRecipient","outputs":[{"internalType":"address payable","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"spender","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"requireAuthorizedForToken","outputs":[],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"contract CollectionRouter","name":"","type":"address"}],"name":"routerStatus","outputs":[{"internalType":"bool","name":"allowed","type":"bool"},{"internalType":"bool","name":"wasEverAllowed","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"","type":"address"},{"internalType":"contract ERC20","name":"","type":"address"}],"name":"royaltiesClaimable","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"safeTransferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"},{"internalType":"bytes","name":"data","type":"bytes"}],"name":"safeTransferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"operator","type":"address"},{"internalType":"bool","name":"approved","type":"bool"}],"name":"setApprovalForAll","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"_uri","type":"string"}],"name":"setBaseURI","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract ICurve","name":"bondingCurve","type":"address"},{"internalType":"bool","name":"isAllowed","type":"bool"}],"name":"setBondingCurveAllowed","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address payable","name":"target","type":"address"},{"internalType":"bool","name":"isAllowed","type":"bool"}],"name":"setCallAllowed","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract CollectionRouter","name":"_router","type":"address"},{"internalType":"bool","name":"isAllowed","type":"bool"}],"name":"setRouterAllowed","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"string","name":"_uri","type":"string"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"setTokenURI","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"swapPaused","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"symbol","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"tokenURI","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"from","type":"address"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"tokenId","type":"uint256"}],"name":"transferFrom","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"unpauseCreation","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"unpauseDeposit","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"unpauseOthers","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"unpauseSwap","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract ERC20","name":"token","type":"address"}],"name":"withdrawERC20ProtocolFees","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"withdrawETHProtocolFees","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address payable","name":"recipient","type":"address"},{"internalType":"contract ERC20","name":"token","type":"address"}],"name":"withdrawRoyalties","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address payable","name":"recipient","type":"address"},{"internalType":"contract ERC20[]","name":"tokens","type":"address[]"}],"name":"withdrawRoyaltiesMultipleCurrencies","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address payable[]","name":"recipients","type":"address[]"},{"internalType":"contract ERC20","name":"token","type":"address"}],"name":"withdrawRoyaltiesMultipleRecipients","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address payable[]","name":"recipients","type":"address[]"},{"internalType":"contract ERC20[]","name":"tokens","type":"address[]"}],"name":"withdrawRoyaltiesMultipleRecipientsAndCurrencies","outputs":[],"stateMutability":"nonpayable","type":"function"},{"stateMutability":"payable","type":"receive"}]

610100346200054d57601f906001600160401b03601f196200535f3881900385810183168501919084831186841017620005525780869260e0946040528339810103126200054d5782516001600160a01b039081811681036200054d57602090818601519183831683036200054d5760408701519380851685036200054d5760608801519581871687036200054d576080890151978289168099036200054d57620000bb60c0620000b360a08d0162000588565b9b0162000588565b9a620000c662000568565b90600e82526d0436f6c6c656374696f6e737761760941b86830152620000eb62000568565b600c81526b0436f6c6c656374696f6e4c560a41b8782015260008054336001600160a01b03198216811783559197167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08880a382518481116200053957600254936001948581811c911680156200052e575b8a8210146200051a57908185849311620004c6575b5089908583116001146200046357899262000457575b5050600019600383901b1c191690841b176002555b8051938411620004435760039586548481811c9116801562000438575b898210146200042457838111620003dc575b50879285116001146200037b5793945084929190836200036f575b50501b9160001990841b1c19161790555b62ffffff620186a0818916116200032a576207a12090891611620002e6575060805260a05260c05260e052600a549262ffffff60b81b9060b81b169260018060d01b031916179062ffffff60a01b9060a01b161717600a55604051614dc590816200059a82396080518181816117940152818161240c0152818161359e01526142d3015260a05181818161055e01528181611729015281816135e3015281816142330152614300015260c0518181816116d901528181612221015281816135150152614671015260e0518181816113100152818161175f0152818161355b01528181614608015261469d0152f35b6064906040519062461bcd60e51b82526004820152601360248201527f43617272792066656520746f6f206c61726765000000000000000000000000006044820152fd5b60405162461bcd60e51b815260048101839052601660248201527f50726f746f636f6c2066656520746f6f206c61726765000000000000000000006044820152606490fd5b015192503880620001e7565b929484908116878552888520945b8988838310620003c45750505010620003ab575b505050811b019055620001f8565b015160001983861b60f8161c191690553880806200039d565b86860151885590960195948501948793500162000389565b8782528882208480880160051c8201928b89106200041a575b0160051c019085905b8281106200040e575050620001cc565b838155018590620003fe565b92508192620003f5565b634e487b7160e01b82526022600452602482fd5b90607f1690620001ba565b634e487b7160e01b86526041600452602486fd5b01519050388062000188565b90888794169160028b528b8b20928b5b8d828210620004af575050841162000495575b505050811b016002556200019d565b015160001960f88460031b161c1916905538808062000486565b8385015186558a9790950194938401930162000473565b909150600289528989208580850160051c8201928c861062000510575b918891869594930160051c01915b8281106200050157505062000172565b8b8155859450889101620004f1565b92508192620004e3565b634e487b7160e01b89526022600452602489fd5b90607f16906200015d565b634e487b7160e01b87526041600452602487fd5b600080fd5b634e487b7160e01b600052604160045260246000fd5b60408051919082016001600160401b038111838210176200055257604052565b519062ffffff821682036200054d5756fe6080604052600436101561001b575b361561001957600080fd5b005b60003560e01c80624885ea1461052e57806301ffc9a71461052557806302befd241461051c57806306fdde0314610513578063081812fc1461050a578063095ea7b31461050157806309a3beef146104f857806317d9d113146104ef5780631ce4c78b146104e65780631fba95e8146104dd57806320157d0a146104d457806321c32d6e146104cb57806323b872dd146104c2578063245a4e72146104b957806324cfb7ca146104b057806342842e0e146104a757806342966c681461049e57806349911f4f146104955780634bf107c11461048c5780634c6bc4331461048357806350be75841461047a578063514f0330146104715780635157ced51461046857806355f804b31461045f57806357314e9d146104565780635b16ebb71461044d5780636352211e1461044457806364df049e1461043b57806368bb43691461043257806369026e88146104295780636c0360eb146104205780636cc88edb1461041757806370a082311461040e578063715018a61461040557806380cc3822146103fc57806383966021146103f357806386e4d110146103ea5780638da5cb5b146103e157806395d89b41146103d857806398cb077f146103cf5780639cc2e3ea146103c65780639d2f3228146103bd578063a22cb465146103b4578063a2dbbd04146103ab578063a427f1ad146103a2578063a734f06e14610399578063a7d7fa1f14610390578063a82719c814610387578063a93ec68b1461037e578063aa4eebb814610375578063ad2e57701461036c578063b03b6bdc14610363578063b483eddc1461035a578063b88d4fde14610351578063bd12dd6414610348578063be1ad4ca1461033f578063c87b56dd14610336578063ced9a0211461032d578063d66d0dd414610324578063e985e9c51461031b578063ebd0f69314610312578063ed6b5ad514610309578063f2fde38b14610300578063f887516c146102f75763ff1aed950361000e576102f2612c70565b61000e565b506102f2612c20565b506102f2612b58565b506102f2612b0d565b506102f2612a33565b506102f26129ca565b506102f26128c3565b506102f261285f565b506102f261282b565b506102f26127fe565b506102f26127da565b506102f2612772565b506102f26126ae565b506102f261244a565b506102f26123f5565b506102f261235c565b506102f261230e565b506102f26122cb565b506102f2612280565b506102f2612250565b506102f261220a565b506102f26121be565b506102f26120cd565b506102f2612082565b506102f261205e565b506102f2611fc5565b506102f2611f13565b506102f2611ee9565b506102f2611e9d565b506102f2611e75565b506102f2611e2f565b506102f2611dd3565b506102f2611d2d565b506102f2611c21565b506102f2611b7c565b506102f26119ac565b506102f2611809565b506102f26117df565b506102f26117c0565b506102f26116aa565b506102f2611621565b506102f26114ce565b506102f2611482565b506102f26113bc565b506102f261133f565b506102f26112f9565b506102f2611253565b506102f2611222565b506102f2610f58565b506102f2610f2f565b506102f2610cf6565b506102f2610cb5565b506102f2610c8b565b506102f2610c20565b506102f2610bca565b506102f2610b87565b506102f2610b46565b506102f2610a77565b506102f26108d0565b506102f26107b1565b506102f261076f565b506102f261068e565b506102f261060e565b506102f261059f565b506102f2610547565b600091031261054257565b600080fd5b5034610542576000366003190112610542576040517f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03168152602090f35b6001600160e01b031981160361054257565b50346105425760203660031901126105425760206004356105bf8161058d565b63ffffffff60e01b166380ac58cd60e01b81149081156105fd575b81156105ec575b506040519015158152f35b6301ffc9a760e01b149050386105e1565b635b5e139f60e01b811491506105da565b50346105425760003660031901126105425760206004600954161515604051908152f35b60005b8381106106455750506000910152565b8181015183820152602001610635565b9060209161066e81518092818552858086019101610632565b601f01601f1916010190565b90602061068b928181520190610655565b90565b50346105425760008060031936011261076c57604051816002546106b1816119f7565b8084529060019081811690811561074457506001146106eb575b6106e7846106db81880382611a9e565b6040519182918261067a565b0390f35b60028352602094507f405787fa12a823e0f2b7631cc41b3ba8828b3321ca811111fa75cd3aa3bb5ace5b82841061073157505050816106e7936106db92820101936106cb565b8054858501870152928501928101610715565b6106e796506106db9450602092508593915060ff191682840152151560051b820101936106cb565b80fd5b503461054257602036600319011261054257602061078e600435612e95565b6040516001600160a01b039091168152f35b6001600160a01b0381160361054257565b5034610542576040366003190112610542576004356107cf816107a0565b6024356107db81612e00565b916001600160a01b038084169082168114610854576100199361080891331490811561080d575b50612e23565b613289565b6001600160a01b0316600090815260076020526040902061084e91506108479033905b9060018060a01b0316600052602052604060002090565b5460ff1690565b38610802565b60405162461bcd60e51b815260206004820152602160248201527f4552433732313a20617070726f76616c20746f2063757272656e74206f776e656044820152603960f91b6064820152608490fd5b9181601f84011215610542578235916001600160401b038311610542576020838186019501011161054257565b5034610542576040366003190112610542576001600160401b036004358181116105425761090561091a9136906004016108a3565b919060243592610913612d61565b369161273b565b6000828152600460205260409020549092906001600160a01b031615610a1b5760009182526020906008825260408320918451918211610a0e575b6109698261096385546119f7565b8561404c565b80601f83116001146109a35750839482939492610998575b50508160011b916000199060031b1c191617905580f35b015190503880610981565b90601f198316956109b985600052602060002090565b9286905b8882106109f6575050836001959697106109dd575b505050811b01905580f35b015160001960f88460031b161c191690553880806109d2565b806001859682949686015181550195019301906109bd565b610a16611a31565b610955565b60405162461bcd60e51b815260206004820152602e60248201527f45524337323155524953746f726167653a2055524920736574206f66206e6f6e60448201526d32bc34b9ba32b73a103a37b5b2b760911b6064820152608490fd5b503461054257602036600319011261054257610019600435610a98816107a0565b610aa0612d61565b600a546040516370a0823160e01b81523060048201526001600160a01b0391821691610b0591906020908290602490829088165afa908115610b39575b600091610b0b575b506001600160a01b0384166000908152600d6020526040902054906130d7565b91614d1e565b610b2c915060203d8111610b32575b610b248183611a9e565b810190613f30565b38610ae5565b503d610b1a565b610b4161335e565b610add565b503461054257600036600319011261054257602062ffffff600a5460a01c16604051908152f35b6001600160a01b03166000908152600d6020526040902090565b503461054257602036600319011261054257600435610ba5816107a0565b60018060a01b0316600052600c602052602060ff604060002054166040519015158152f35b50346105425760008060031936011261076c57610be5612d61565b600219600954166009557f25d628909b0e89249ef68fa8607e05a88893ba9b00b6cff609cb914d3b10e3238180a180f35b6004111561054257565b5034610542576040366003190112610542576020610c55600435610c43816107a0565b60243590610c5082610c16565b6134fc565b6040519015158152f35b606090600319011261054257600435610c77816107a0565b90602435610c84816107a0565b9060443590565b503461054257610019610c9d36610c5f565b91610cb0610cab8433612fd9565b612ed3565b6130ff565b50346105425760003660031901126105425760206002600954161515604051908152f35b90816101a09103126105425790565b908160a09103126105425790565b506040806003193601126105425760048035916001600160401b039283811161054257610d269036908401610cd9565b9260243590811161054257610d3e9036908401610ce8565b92610d4e600160095416156139d1565b610d5781614158565b9490936001600160a01b038516610d716020840184613a17565b93823b15610542576080610e2891610e3493895190633f58092760e21b82528180610da460009b8c9487358d8501613a6a565b0381838a5af18015610f22575b610f0f575b50610e22610e0860208b89610dcf6101808e018e613a81565b949092610dde81890189613a81565b610dee60608b949394018b613a81565b939092519889978897638e3fd47560e01b89528801613aef565b03818a5afa908115610f02575b8991610ed4575b50613b5f565b01613b9d565b6001600160a01b031690565b91813b15610ed05785516340a7514160e11b81526001600160a01b039093169083019081526106e79594610e8a94909392909183919082908490829060200103925af18015610ec3575b610eaa575b5084614441565b516001600160a01b03909216825260208201929092529081906040820190565b80610eb7610ebd92611a48565b80610537565b38610e83565b610ecb61335e565b610e7e565b8380fd5b610ef5915060203d8111610efb575b610eed8183611a9e565b810190613ab6565b38610e1c565b503d610ee3565b610f0a61335e565b610e15565b80610eb7610f1c92611a48565b38610db6565b610f2a61335e565b610db1565b503461054257610019610f4136610c5f565b9060405192610f4f84611a68565b60008452612f36565b503461054257602080600319360112610542576004803590600260015414611213576002600155610f91610f8c8333612fd9565b613498565b6040805163b3f29b7d60e01b81529091906001600160a01b0384169085818481855afa908115611206575b6000916111d9575b50610fce816134ea565b801580156111c6575b156111495750803b156105425782516390386bbf60e01b8152600081848183865af1801561113c575b611129575b505b82516323e6650160e11b815285818481855afa95861561111c575b6000966110ed575b50508251632f4fefaf60e01b8152906000828481845afa9182156110e0575b6000926110bd575b50803b1561054257611094956000809461107e9651968795869485936313edab8160e01b85528401613cf4565b03925af180156110b0575b61109d575b50614b4a565b61001960018055565b80610eb76110aa92611a48565b3861108e565b6110b861335e565b611089565b6110d991923d8091833e6110d18183611a9e565b810190613c6b565b9038611051565b6110e861335e565b611049565b61110d929650803d10611115575b6111058183611a9e565b810190613ba7565b93388061102a565b503d6110fb565b61112461335e565b611022565b80610eb761113692611a48565b38611005565b61114461335e565b611000565b611152816134ea565b600281149081156111b2575b501561100757803b15610542578251638a689e8160e01b8152600081848183865af180156111a5575b611192575b50611007565b80610eb761119f92611a48565b3861118c565b6111ad61335e565b611187565b600391506111bf816134ea565b143861115e565b506111d0816134ea565b60018114610fd7565b6111f99150863d88116111ff575b6111f18183611a9e565b810190613c56565b38610fc4565b503d6111e7565b61120e61335e565b610fbc565b60405163558a1e0360e11b8152fd5b503461054257600036600319011261054257602062ffffff600a5460b81c16604051908152f35b8015150361054257565b503461054257604036600319011261054257600435611271816107a0565b6024359061127e82611249565b611286612d61565b6001600160a01b03166000818152600b602052604090205490918015159160ff1615158290036112b257005b7f1da28d127ec72d2dde6a533c98857664b25cd827680fb1f39f57394c2b444d91916112f060209260406000209060ff801983541691151516179055565b604051908152a2005b5034610542576000366003190112610542576040517f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03168152602090f35b50346105425760403660031901126105425760043561135d816107a0565b6024359061136a826107a0565b6001600160a01b03821691826113ad575061138490613d41565b905b600052600d602052604060002080549182039182116113a25755005b6113aa6130a9565b55005b6113b691613d75565b90611386565b5034610542576020366003190112610542576004356113da816107a0565b6113e2612d61565b6001600160a01b038181169190821561145157600a54166001600160a01b031682900361140b57005b600a80546001600160a01b0319166001600160a01b039092169190911790557fceec08a75d1f3b12c14c6cdc16c081aec1c401c2eac1d8c6ea91e9d73b929211600080a2005b60405162461bcd60e51b815260206004820152600960248201526830206164647265737360b81b6044820152606490fd5b50346105425760008060031936011261076c5761149d612d61565b600419600954166009557f0e9c26be0a38592bf8b6a0ba90062ca5a65db29064cc217e2a903c01130bd8568180a180f35b503461054257602080600319360112610542576001600160401b03600435818111610542576115019036906004016108a3565b9161150a612d61565b82116115e4575b611525826115206010546119f7565b614007565b600092601f83116001146115635750918192600092611558575b5050600019600383901b1c191660019190911b17601055005b01359050388061153f565b6010600052601f198316937f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae672929181905b8682106115cc57505083600195106115b2575b505050811b01601055005b0135600019600384901b60f8161c191690553880806115a7565b80600184968294958701358155019501920190611594565b6115ec611a31565b611511565b9181601f84011215610542578235916001600160401b038311610542576020808501948460051b01011161054257565b5034610542576040366003190112610542576001600160401b03600435818111610542576116539036906004016115f1565b916024359081116105425761166c9036906004016115f1565b919060005b83811061167a57005b60018161169f61168e6116a5948887613e28565b35611698816107a0565b8887613e46565b016138b6565b611671565b50346105425760203660031901126105425760206004356116ca816107a0565b6001600160a01b0390611700817f0000000000000000000000000000000000000000000000000000000000000000841630613685565b91821561178b575b8215611756575b8215611722575b50506040519015158152f35b61174f92507f00000000000000000000000000000000000000000000000000000000000000001630613617565b3880611716565b915061178581837f00000000000000000000000000000000000000000000000000000000000000001630613685565b9161170f565b91506117ba81837f00000000000000000000000000000000000000000000000000000000000000001630613617565b91611708565b503461054257602036600319011261054257602061078e600435612e00565b503461054257600036600319011261054257600a546040516001600160a01b039091168152602090f35b50606036600319011261054257600435611822816107a0565b602435906001600160401b038083116105425736602384011215610542578260040135908111610542576024830192602436918360061b0101116105425760443561186c81610c16565b6008600954166119725761188090336134fc565b1561194257600092835b8281106118bc576001600160a01b0384166000908152600d6020526040902085906118b8905b9182546130f2565b9055005b60206118d0610e2882610e22858888613c38565b33036118e0575b5060010161188a565b94906118fa6001926118f3838787613c38565b35906130f2565b9561193a6118b087610830611920611913878b8b613c38565b3595610e22888c8c613c38565b6001600160a01b03166000908152600e6020526040902090565b9055906118d7565b60405162461bcd60e51b8152602060048201526008602482015267139bdd081c1bdbdb60c21b6044820152606490fd5b60405162461bcd60e51b8152602060048201526012602482015271119d5b98dd1a5bdb881a5cc81c185d5cd95960721b6044820152606490fd5b50346105425760008060031936011261076c576119c7612d61565b6004600954176009557f894e6d7139b6bce810a9a2c047c92d53b08e18c97dd6d1167b84e9525e3dd2578180a180f35b90600182811c92168015611a27575b6020831014611a1157565b634e487b7160e01b600052602260045260246000fd5b91607f1691611a06565b50634e487b7160e01b600052604160045260246000fd5b6001600160401b038111611a5b57604052565b611a63611a31565b604052565b602081019081106001600160401b03821117611a5b57604052565b604081019081106001600160401b03821117611a5b57604052565b90601f801991011681019081106001600160401b03821117611a5b57604052565b6040519060008260105491611ad3836119f7565b808352600193808516908115611b5b5750600114611afb575b50611af992500383611a9e565b565b601060009081527f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae67294602093509091905b818310611b43575050611af9935082010138611aec565b85548884018501529485019487945091830191611b2c565b9050611af994506020925060ff191682840152151560051b82010138611aec565b50346105425760008060031936011261076c5760405181601054611b9f816119f7565b808452906001908181169081156107445750600114611bc8576106e7846106db81880382611a9e565b60108352602094507f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae6725b828410611c0e57505050816106e7936106db92820101936106cb565b8054858501870152928501928101611bf2565b503461054257604036600319011261054257600435611c3f816107a0565b60243590611c4c82611249565b81611c55612d61565b611d02575b6001600160a01b0381166000908152600f60205260409020611c7b90610847565b82151590151503611c8857005b611cfd7f24e274cfd23919da24a57044266685f5474338837aecdcca3136b2102f78fee591611ce1611cb8612704565b8515158152600160208201526001600160a01b0383166000908152600f60205260409020613fbb565b60405193151584526001600160a01b0316929081906020820190565b0390a2005b6001600160a01b0381166000908152600c6020526040902054611d289060ff1615613f7b565b611c5a565b503461054257602036600319011261054257600435611d4b816107a0565b6001600160a01b03168015611d7c5760005260056020526106e7604060002054604051918291829190602083019252565b60405162461bcd60e51b815260206004820152602960248201527f4552433732313a2061646472657373207a65726f206973206e6f7420612076616044820152683634b21037bbb732b960b91b6064820152608490fd5b50346105425760008060031936011261076c57611dee612d61565b80546001600160a01b03198116825581906001600160a01b03167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08280a380f35b5034610542576040366003190112610542576004356001600160401b03811161054257611e636100199136906004016115f1565b60243591611e70836107a0565b613e46565b5034610542576020366003190112610542576040516004356001600160a01b03168152602090f35b50346105425760008060031936011261076c57611eb8612d61565b600119600954166009557f15afb7ccbfde73fd4dcc20261272d735c53b391894ea32e4eab3b0bbcc3b68a48180a180f35b5034610542576000366003190112610542576000546040516001600160a01b039091168152602090f35b50346105425760008060031936011261076c5760405181600354611f36816119f7565b808452906001908181169081156107445750600114611f5f576106e7846106db81880382611a9e565b60038352602094507fc2575a0e9e593c00f959f8c92f12db2869c3395a3b0502d05e2516446f71f85b5b828410611fa557505050816106e7936106db92820101936106cb565b8054858501870152928501928101611f89565b62ffffff81160361054257565b503461054257602036600319011261054257600435611fe381611fb8565b611feb612d61565b62ffffff908181166120026207a120821115613f3f565b80600a54938460b81c160361201357005b62ffffff60b81b1990921660b89190911b62ffffff60b81b1617600a556040519081527f1b45b945f2f80d1a9748b57c798253b355141f89518cbe965586e5d20cf62ed090602090a1005b50346105425760003660031901126105425760206008600954161515604051908152f35b50346105425760008060031936011261076c5761209d612d61565b6008600954176009557f2b550821fb732f5939e14503f24dc3832c2d9ebadb16664a9a01f139726ebe428180a180f35b5034610542576040366003190112610542576004356120eb816107a0565b6024356120f781611249565b6001600160a01b0382169133831461217957816121366121479233600052600760205260406000209060018060a01b0316600052602052604060002090565b9060ff801983541691151516179055565b604051901515815233907f17307eab39ab6107e8899845ad3d59bd9653f200f220920489ca2b5937696c3190602090a3005b60405162461bcd60e51b815260206004820152601960248201527f4552433732313a20617070726f766520746f2063616c6c6572000000000000006044820152606490fd5b50346105425760008060031936011261076c576121d9612d61565b600819600954166009557f0b00a41d43ba96cf6fe1e1fdad1d13979e0f6c8aeee0e68f7e419db2c2d910408180a180f35b5034610542576000366003190112610542576040517f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03168152602090f35b503461054257600036600319011261054257602060405173eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee8152f35b50346105425760008060031936011261076c5761229b612d61565b6001600954176009557f169fc5c8e9cdd2e4b8f64a1b71384ab600ca57693c0b9c1c0710e011eace4c948180a180f35b5034610542576020366003190112610542576004356122e9816107a0565b60018060a01b0316600052600b602052602060ff604060002054166040519015158152f35b50346105425760203660031901126105425760043561232c816107a0565b60018060a01b0316600052600f6020526040806000205460ff8251918181161515835260081c1615156020820152f35b50346105425760203660031901126105425760043561237a81611fb8565b612382612d61565b62ffffff90818116612399620186a0821115613f3f565b80600a54938460a01c16036123aa57005b62ffffff60a01b1990921660a09190911b62ffffff60a01b1617600a556040519081527ffd8ee7249e8f6c22d7d3c71093c7e935b7468f133a2a9a807ae4635c70f8cf9b90602090a1005b5034610542576000366003190112610542576040517f00000000000000000000000000000000000000000000000000000000000000006001600160a01b03168152602090f35b90816101e09103126105425790565b503461054257604080600319360112610542576001600160401b03600480358281116105425761247d903690830161243b565b91602435908111610542576124959036908301610ce8565b926124a5600160095416156139d1565b6124d16124b4610e2885613b9d565b9382519463555ddc6560e11b865285806020948593888301613bbc565b0381731820a4b7618bde71dce8cdc73aab6c95905fad245afa9485156126a1575b600095612682575b506001600160a01b039461251090861615613bf5565b61251981614545565b96909586169261252b81830183613a17565b9092853b1561054257610e22610e289360809360006125c4978c6125638d519485938493633f58092760e21b85528a35908501613a6a565b0381838d5af18015612675575b612662575b50806125856101a0890189613a81565b91908c6125a88d612598818a018a613a81565b610dee60608c949394018c613a81565b03818c5afa918215612655575b600092612638575b5050613b5f565b91803b156105425783516340a7514160e11b81526001600160a01b039093169483019485526106e794610e8a939160009183919082908490829060200103925af1801561262b575b612618575b5084614762565b80610eb761262592611a48565b38612611565b61263361335e565b61260c565b61264e9250803d10610efb57610eed8183611a9e565b38806125bd565b61265d61335e565b6125b5565b80610eb761266f92611a48565b38612575565b61267d61335e565b612570565b61269a919550823d8411611115576111058183611a9e565b93386124fa565b6126a961335e565b6124f2565b50346105425760008060031936011261076c576126c9612d61565b6126f460018060a01b03600a54164790838052600d602052604084205482039182116126f757614c57565b80f35b6126ff6130a9565b614c57565b60405190611af982611a83565b6020906001600160401b03811161272e575b601f01601f19160190565b612736611a31565b612723565b92919261274782612711565b916127556040519384611a9e565b829481845281830111610542578281602093846000960137010152565b503461054257608036600319011261054257600435612790816107a0565b60243561279c816107a0565b606435916001600160401b0383116105425736602384011215610542576127d061001993369060248160040135910161273b565b9160443591612f36565b50346105425760003660031901126105425760206001600954161515604051908152f35b503461054257604036600319011261054257610019610f8c600435612822816107a0565b60243590612fd9565b5034610542576020366003190112610542576106e761284b6004356136f3565b604051918291602083526020830190610655565b50346105425760403660031901126105425760206128ba600435612882816107a0565b6024359061288f826107a0565b60018060a01b0316600052600e835260406000209060018060a01b0316600052602052604060002090565b54604051908152f35b5034610542576020366003190112610542576004356001600160401b038111610542576128f761298391369060040161243b565b612906600160095416156139d1565b6129718135612914816107a0565b60405163555ddc6560e11b81526001600160a01b03916020908290819061294090861660048301613bbc565b0381731820a4b7618bde71dce8cdc73aab6c95905fad245afa9081156129bd575b60009161299f575b501615613bf5565b61297a81614545565b92909182614762565b604080516001600160a01b039290921682526020820192909252f35b6129b7915060203d8111611115576111058183611a9e565b38612969565b6129c561335e565b612961565b503461054257604036600319011261054257602060ff612a276004356129ef816107a0565b602435906129fc826107a0565b60018060a01b03166000526007845260406000209060018060a01b0316600052602052604060002090565b54166040519015158152f35b503461054257604036600319011261054257600435612a51816107a0565b60243590612a5e82611249565b81612a67612d61565b612adf575b6001600160a01b0381166000908152600c60205260409020612a8d90610847565b82151590151503612a9a57005b611cfd81611ce1846121367fab2e2e8d21d5efbffb30945e9b6ee1fb43620ef65a228f871f5028bf8a6e004a9560018060a01b0316600052600c602052604060002090565b6001600160a01b0381166000908152600f6020526040902054612b089060081c60ff1615613f7b565b612a6c565b50346105425760008060031936011261076c57612b28612d61565b6002600954176009557fc90e003e0d9f42732e9a57f151adcac590bd64efa6523752de67dd4954bd00238180a180f35b503461054257602036600319011261054257600435612b76816107a0565b612b7e612d61565b6001600160a01b039081168015612bcc57600080546001600160a01b03198116831782559092167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08380a380f35b60405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b6064820152608490fd5b506020366003190112610542576004356001600160401b03811161054257612c4f612983913690600401610cd9565b612c5e600160095416156139d1565b612c6781614158565b92909182614441565b503461054257604036600319011261054257600435612c8e816107a0565b6024356001600160401b03811161054257612cad9036906004016115f1565b91600092835b818110612cbe578480f35b600181612ccf612d50938588613e28565b35612cd9816107a0565b612d48612d40612cff836108308a60018060a01b0316600052600e602052604060002090565b54928a612d22826108308c60018060a01b0316600052600e602052604060002090565b55858060a01b038481831615600014612d5557610b6d918b16614c57565b9182546130d7565b9055016138b6565b612cb3565b610b6d918b1683614d1e565b6000546001600160a01b03163303612d7557565b606460405162461bcd60e51b815260206004820152602060248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e65726044820152fd5b15612dc057565b60405162461bcd60e51b8152602060048201526018602482015277115490cdcc8c4e881a5b9d985b1a59081d1bdad95b88125160421b6044820152606490fd5b6000908152600460205260409020546001600160a01b031661068b811515612db9565b15612e2a57565b60405162461bcd60e51b815260206004820152603e60248201527f4552433732313a20617070726f76652063616c6c6572206973206e6f7420746f60448201527f6b656e206f776e6572206e6f7220617070726f76656420666f7220616c6c00006064820152608490fd5b600081815260046020526040902054612eb8906001600160a01b03161515612db9565b6000908152600660205260409020546001600160a01b031690565b15612eda57565b60405162461bcd60e51b815260206004820152602e60248201527f4552433732313a2063616c6c6572206973206e6f7420746f6b656e206f776e6560448201526d1c881b9bdc88185c1c1c9bdd995960921b6064820152608490fd5b91611af99391612f5d93612f4d610cab8433612fd9565b612f588383836130ff565b61345c565b612fb5565b60809060208152603260208201527f4552433732313a207472616e7366657220746f206e6f6e20455243373231526560408201527131b2b4bb32b91034b6b83632b6b2b73a32b960711b60608201520190565b15612fbc57565b60405162461bcd60e51b815280612fd560048201612f62565b0390fd5b6001600160a01b0380612feb84612e00565b169281831692848414948515613021575b5050831561300b575b50505090565b61301791929350612e95565b1614388080613005565b60009081526007602090815260408083206001600160a01b03949094168352929052205460ff1693503880612ffc565b1561305857565b60405162461bcd60e51b8152602060048201526024808201527f4552433732313a207472616e7366657220746f20746865207a65726f206164646044820152637265737360e01b6064820152608490fd5b50634e487b7160e01b600052601160045260246000fd5b6000198101919082116130cf57565b611af96130a9565b919082039182116130cf57565b90600182018092116130cf57565b919082018092116130cf57565b9061310983612e00565b6001600160a01b0383811692909182168390036131e2576131586131bb92821694613135861515613051565b61313e87613235565b6001600160a01b0316600090815260056020526040902090565b61316281546130c0565b90556001600160a01b038116600090815260056020526040902061318681546130e4565b905561319c856000526004602052604060002090565b80546001600160a01b0319166001600160a01b03909216919091179055565b7fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef600080a4565b60405162461bcd60e51b815260206004820152602560248201527f4552433732313a207472616e736665722066726f6d20696e636f72726563742060448201526437bbb732b960d91b6064820152608490fd5b600081815260066020526040812080546001600160a01b03191690556001600160a01b0361326283612e00565b167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9258280a4565b600082815260066020526040902080546001600160a01b0319166001600160a01b0383161790556001600160a01b03806132c284612e00565b169116907f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925600080a4565b90816020910312610542575161068b8161058d565b61068b939260809260018060a01b031682526000602083015260408201528160608201520190610655565b6001600160a01b03918216815291166020820152604081019190915260806060820181905261068b92910190610655565b506040513d6000823e3d90fd5b3d15613396573d9061337c82612711565b9161338a6040519384611a9e565b82523d6000602084013e565b606090565b909190803b15613454576133cd602091600093604051948580948193630a85bd0160e11b998a84523360048501613302565b03926001600160a01b03165af160009181613424575b50613416576133f061336b565b805190816134115760405162461bcd60e51b815280612fd560048201612f62565b602001fd5b6001600160e01b0319161490565b61344691925060203d811161344d575b61343e8183611a9e565b8101906132ed565b90386133e3565b503d613434565b505050600190565b92909190823b1561348f576133cd926020926000604051809681958294630a85bd0160e11b9a8b8552336004860161332d565b50505050600190565b1561349f57565b60405162461bcd60e51b815260206004820152600c60248201526b139bdd08185c1c1c9bdd995960a21b6044820152606490fd5b50634e487b7160e01b600052602160045260246000fd5b600411156134f457565b611af96134d3565b90613506816134ea565b60028103613543575061068b907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031630613685565b61354c816134ea565b60038103613589575061068b907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031630613685565b613592816134ea565b806135cc575061068b907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031630613617565b806135d86001926134ea565b036136115761068b907f00000000000000000000000000000000000000000000000000000000000000006001600160a01b031630613617565b50600090565b60405191733d3d3d3d363d3d37604160353639366041013d7360601b835260601b60148301526c5af43d3d93803e603357fd5bf360981b602883015260601b60358201526049600081830180943c607281015160298201511460698201516020830151141691519051141690565b60405191733d3d3d3d363d3d37605560353639366055013d7360601b835260601b60148301526c5af43d3d93803e603357fd5bf360981b602883015260601b60358201526049600081830180943c607281015160298201511460698201516020830151141691519051141690565b600081815260046020526040902054613716906001600160a01b03161515612db9565b600090808252602060088152604083206040519384818354613737816119f7565b938484528684019560019288848216918260001461380b5750506001146137cf575b50505061376892500385611a9e565b613770611abf565b9081519485156137c657805161378e575050505061068b915061382d565b61068b94506137ba866137ab604051988996888089019101610632565b84019151809386840190610632565b01038084520182611a9e565b94505050505090565b879350819291528282205b8583106137f35750506137689350820101388080613759565b8054838b0185015289945087939092019181016137da565b93509450505061376894915060ff19168552151560051b820101388080613759565b600081815260046020526040902054613850906001600160a01b03161515612db9565b613858611abf565b8051909190600090156138a15750602061387461068b92613908565b92604051938161388d8693518092868087019101610632565b82016137ba82518093868085019101610632565b915050604051906138b182611a68565b815290565b60019060001981146138c6570190565b6138ce6130a9565b0190565b50634e487b7160e01b600052603260045260246000fd5b9060209180518210156138fb57010190565b6139036138d2565b010190565b80156139b357806000908282935b61399f575061392483612711565b926139326040519485611a9e565b80845281601f1961394283612711565b013660208701375b6139545750505090565b61395d906130c0565b90600a9060308282068101809111613992575b60f81b6001600160f81b031916841a61398984876138e9565b5304908161394a565b61399a6130a9565b613970565b926139ab600a916138b6565b930480613916565b506040516139c081611a83565b60018152600360fc1b602082015290565b156139d857565b60405162461bcd60e51b8152602060048201526017602482015276141bdbdb0818dc99585d1a5bdb881a5cc81c185d5cd959604a1b6044820152606490fd5b903590601e198136030182121561054257018035906001600160401b0382116105425760200191813603831361054257565b908060209392818452848401376000828201840152601f01601f1916010190565b60409061068b949281528160208201520191613a49565b903590601e198136030182121561054257018035906001600160401b03821161054257602001918160051b3603831361054257565b90816020910312610542575161068b81611249565b81835290916001600160fb1b0383116105425760209260051b809284830137010190565b91613b1b91613b0a9197959697606085526060850191613acb565b602095869484830386860152613acb565b90604081830391015282815201929160005b828110613b3b575050505090565b9091929382806001928735613b4f81611249565b1515815201950193929101613b2d565b15613b6657565b60405162461bcd60e51b815260206004820152600f60248201526e139195081b9bdd08185b1b1bddd959608a1b6044820152606490fd5b3561068b816107a0565b90816020910312610542575161068b816107a0565b6001600160a01b0390911681527fac7fbab5f54a3ca8194167523c6753bfeb96a445279294b6125b68cce2177054602082015260400190565b15613bfc57565b60405162461bcd60e51b8152602060048201526014602482015273115490cdcdcdc81b9bdd081cdd5c1c1bdc9d195960621b6044820152606490fd5b9190811015613c49575b60061b0190565b613c516138d2565b613c42565b90816020910312610542575161068b81610c16565b9060209081838203126105425782516001600160401b0393848211610542570181601f82011215610542578051938411613ce7575b8360051b9060405194613cb585840187611a9e565b85528380860192820101928311610542578301905b828210613cd8575050505090565b81518152908301908301613cca565b613cef611a31565b613ca0565b6001600160a01b03909116815260406020808301829052835191830182905260609092019282019160005b828110613d2d575050505090565b835185529381019392810192600101613d1f565b6001600160a01b03166000818152600e60209081526040808320838052909152812080549190559190611af9908390614c57565b9091906001600160a01b039081841615613de45761083093611af99282169081600052600e6020526000613dde82613dc281604085209060018060a01b0316600052602052604060002090565b5498899660018060a01b0316600052600e602052604060002090565b55614d1e565b606460405162461bcd60e51b815260206004820152602060248201527f557365207769746864726177455448526f79616c7469657320696e73746561646044820152fd5b9190811015613e39575b60051b0190565b613e416138d2565b613e32565b600092916001600160a01b038316613ebe57906000915b808310613e8b575050506001600160a01b03166000908152600d60205260409020613e8790612d40565b9055565b909193613eb5600191613eaf613eaa613ea5898789613e28565b613b9d565b613d41565b906130f2565b94019190613e5d565b9083915b808310613eec575050506001600160a01b03166000908152600d60205260409020613e8790612d40565b909193600190613f1085613f01888688613e28565b35613f0b816107a0565b613d75565b8101809111613f23575b94019190613ec2565b613f2b6130a9565b613f1a565b90816020910312610542575190565b15613f4657565b60405162461bcd60e51b815260206004820152600d60248201526c46656520746f6f206c6172676560981b6044820152606490fd5b15613f8257565b60405162461bcd60e51b815260206004820152601160248201527021b0b713ba1031b0b636103937baba32b960791b6044820152606490fd5b90602090613fd881511515849060ff801983541691151516179055565b0151815461ff00191690151560081b61ff0016179055565b818110613ffb575050565b60008155600101613ff0565b90601f8211614014575050565b611af99160106000526020600020906020601f840160051c83019310614042575b601f0160051c0190613ff0565b9091508190614035565b9190601f811161405b57505050565b611af9926000526020600020906020601f840160051c8301931061404257601f0160051c0190613ff0565b1561408d57565b60405162461bcd60e51b815260206004820152601d60248201527f426f6e64696e67206375727665206e6f742077686974656c69737465640000006044820152606490fd5b3561068b81611fb8565b156140e357565b60405162461bcd60e51b815260206004820152603060248201527f4e6f6e7a65726f20726f79616c747920666f72206e6f6e20455243323938312060448201526f776974686f75742066616c6c6261636b60801b6064820152608490fd5b600311156134f457565b3560038110156105425790565b906142986020830161419161418c61084761417284613b9d565b6001600160a01b03166000908152600b6020526040902090565b614086565b61429e610e28610e286142806141aa61014089016140d2565b946141e16141dc6141be6101608c01613b9d565b976141c88c613b9d565b6001600160a01b03999091908a1690614ab4565b6140dc565b60006141f5610e28610e28610e288d613b9d565b6040516301ffc9a760e01b815263780e9d6360e01b60048201529190602090839060249082905afa80928293614325575b506142c7575050614259867f00000000000000000000000000000000000000000000000000000000000000001691613b9d565b6142628a613b9d565b9061426f60808c0161414b565b9261427984614141565b3090614345565b9361428d60608901613b9d565b941694858095614971565b96613b9d565b7f77948cb83ef3caff9ac13dfab1ea1f8a6875c98370287ce587f5dbc74cc5b6b0600080a39190565b50156142fa57614259867f0000000000000000000000000000000000000000000000000000000000000000165b91613b9d565b614259867f0000000000000000000000000000000000000000000000000000000000000000166142f4565b61433e91935060203d8111610efb57610eed8183611a9e565b9138614226565b929091607f9492604051947f60763d8160093d39f33d3d3d3d363d3d37604160353639366041013d73000000865260601b601d8601526c5af43d3d93803e603357fd5bf360981b603186015260601b603e85015260601b605284015260601b6066830152607a8201534360e01b607b8201526000f090565b356001600160801b03811681036105425790565b989a9995946144269197939560e099936144349660018060a01b03809b168d528c60206001600160801b038094169101528c604062ffffff809b169101521660608c01526101008060808d01528b0191613a49565b9188830360a08a0152613a49565b961660c085015216910152565b6001600160a01b0381169161445860408201613b9d565b9261446560a083016143bd565b9061447260c084016140d2565b61447e60e085016143bd565b9561448d610100860186613a17565b94909161449e610120880188613a17565b6144ae6101408a9c939c016140d2565b6144bb6101608b01613b9d565b91883b1561054257611af99c6145079a6000986144ed966040519b8c9a8b9a63f0732b4d60e01b8c5260048c016143d1565b038183865af18015614538575b614525575b503490614c57565b61451f61451382613b9d565b91610180810190613a81565b91614886565b80610eb761453292611a48565b386144ff565b61454061335e565b6144fa565b906142986145a19261429e610e28610e286040840161456c61418c61084761417284613b9d565b61428d608061465e61458161016089016140d2565b936145b56141dc6145956101808c01613b9d565b9660208c019e8f613b9d565b6001600160a01b0398909190891690614ab4565b8b6145ca610e28610e28610e28600094613b9d565b6040516301ffc9a760e01b815263780e9d6360e01b60048201529190602090839060249082905afa809282936146c2575b5061466557505061462e857f00000000000000000000000000000000000000000000000000000000000000001691613b9d565b6146378d613b9d565b61464360a08c0161414b565b9161464d83614141565b6146568c613b9d565b9330906146e2565b9601613b9d565b50156146975761462e857f00000000000000000000000000000000000000000000000000000000000000001691613b9d565b61462e857f0000000000000000000000000000000000000000000000000000000000000000166142f4565b6146db91935060203d8111610efb57610eed8183611a9e565b91386145fb565b93916093959391604051957f608a3d8160093d39f33d3d3d3d363d3d37605560353639366055013d73000000875260601b601d8701526c5af43d3d93803e603357fd5bf360981b603187015260601b603e86015260601b605285015260601b6066840152607a8301534360e01b607b83015260601b607f8201526000f090565b6001600160a01b0381169161477960608201613b9d565b9261478660c083016143bd565b9061479360e084016140d2565b6147a061010085016143bd565b956147af610120860186613a17565b9490916147c0610140880188613a17565b6147d06101608a9c939c016140d2565b6147dd6101808b01613b9d565b91883b1561054257611af99c61483a9a60009861480f966040519b8c9a8b9a63f0732b4d60e01b8c5260048c016143d1565b038183865af18015614868575b614855575b5061482b83613b9d565b906101c0840135913390614ca2565b61451f61484960208301613b9d565b916101a0810190613a81565b80610eb761486292611a48565b38614821565b61487061335e565b61481c565b91602061068b938181520191613acb565b6001600160a01b039384169391929160005b8381106148f957505050823b15610542576148cd9260009283604051809681958294637e24d2ab60e01b845260048401614875565b03925af180156148ec575b6148df5750565b80610eb7611af992611a48565b6148f461335e565b6148d8565b81831690614908818688613e28565b3591803b1561054257600060019360648a836040519586948593632142170760e11b8552336004860152602485015260448401525af18015614964575b614951575b5001614898565b80610eb761495e92611a48565b3861494a565b61496c61335e565b614945565b6040516001600160a01b03928316939290919061498d83611a68565b600083528116918215614a70576000858152600460205260409020546001600160a01b0316614a2b57848281611af9956149dc612f5d9660018060a01b03166000526005602052604060002090565b6149e681546130e4565b9055614a008361319c846000526004602052604060002090565b60007fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef8180a461339b565b60405162461bcd60e51b815260206004820152601c60248201527f4552433732313a20746f6b656e20616c7265616479206d696e746564000000006044820152606490fd5b606460405162461bcd60e51b815260206004820152602060248201527f4552433732313a206d696e7420746f20746865207a65726f20616464726573736044820152fd5b62ffffff161591908215614b37575b508115614ace575090565b6040516301ffc9a760e01b815263152a902d60e11b60048201529150602090829060249082906001600160a01b03165afa908115614b2a575b600091614b12575090565b61068b915060203d8111610efb57610eed8183611a9e565b614b3261335e565b614b07565b6001600160a01b03161515915038614ac3565b614b5381612e00565b614b5c82613235565b6001600160a01b031660008181526005602052604081208054919392839285926000198201918211614c4a575b5582825260046020526040822080546001600160a01b03191690557fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef8280a4614be5614bdf826000526008602052604060002090565b546119f7565b614bed575050565b614c01906000526008602052604060002090565b614c0b81546119f7565b80614c1557505050565b601f8111600114614c24575055565b81835260208320614c4091601f0160051c810190600101613ff0565b8160208120915555565b614c526130a9565b614b89565b600080809381935af115614c6757565b60405162461bcd60e51b815260206004820152601360248201527211551217d514905394d1915497d19052531151606a1b6044820152606490fd5b9060006064926020958295604051946323b872dd60e01b86526004860152602485015260448401525af13d15601f3d1160016000511416171615614ce257565b60405162461bcd60e51b81526020600482015260146024820152731514905394d1915497d19493d357d1905253115160621b6044820152606490fd5b600091826044926020956040519363a9059cbb60e01b8552600485015260248401525af13d15601f3d1160016000511416171615614d5857565b60405162461bcd60e51b815260206004820152600f60248201526e1514905394d1915497d19052531151608a1b6044820152606490fdfea2646970667358221220dcf4512e4081010072bc0555716cd9faf374c10369881ae01fff209cfafb940b64736f6c634300081100330000000000000000000000009f95b3fc933e8d3acda1fca942c0e772c3b3e7d200000000000000000000000071fe31fa53273aeee75678535d992dc1982433c9000000000000000000000000e653a3deb851befedc7a1368b3cc20cc458f9800000000000000000000000000833fd2704cdb5a68d22fcf674b0787cb9cbf63fb0000000000000000000000008c4e0af3829de1e5cf9a29554efd6548028fcd6e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

Deployed Bytecode

0x6080604052600436101561001b575b361561001957600080fd5b005b60003560e01c80624885ea1461052e57806301ffc9a71461052557806302befd241461051c57806306fdde0314610513578063081812fc1461050a578063095ea7b31461050157806309a3beef146104f857806317d9d113146104ef5780631ce4c78b146104e65780631fba95e8146104dd57806320157d0a146104d457806321c32d6e146104cb57806323b872dd146104c2578063245a4e72146104b957806324cfb7ca146104b057806342842e0e146104a757806342966c681461049e57806349911f4f146104955780634bf107c11461048c5780634c6bc4331461048357806350be75841461047a578063514f0330146104715780635157ced51461046857806355f804b31461045f57806357314e9d146104565780635b16ebb71461044d5780636352211e1461044457806364df049e1461043b57806368bb43691461043257806369026e88146104295780636c0360eb146104205780636cc88edb1461041757806370a082311461040e578063715018a61461040557806380cc3822146103fc57806383966021146103f357806386e4d110146103ea5780638da5cb5b146103e157806395d89b41146103d857806398cb077f146103cf5780639cc2e3ea146103c65780639d2f3228146103bd578063a22cb465146103b4578063a2dbbd04146103ab578063a427f1ad146103a2578063a734f06e14610399578063a7d7fa1f14610390578063a82719c814610387578063a93ec68b1461037e578063aa4eebb814610375578063ad2e57701461036c578063b03b6bdc14610363578063b483eddc1461035a578063b88d4fde14610351578063bd12dd6414610348578063be1ad4ca1461033f578063c87b56dd14610336578063ced9a0211461032d578063d66d0dd414610324578063e985e9c51461031b578063ebd0f69314610312578063ed6b5ad514610309578063f2fde38b14610300578063f887516c146102f75763ff1aed950361000e576102f2612c70565b61000e565b506102f2612c20565b506102f2612b58565b506102f2612b0d565b506102f2612a33565b506102f26129ca565b506102f26128c3565b506102f261285f565b506102f261282b565b506102f26127fe565b506102f26127da565b506102f2612772565b506102f26126ae565b506102f261244a565b506102f26123f5565b506102f261235c565b506102f261230e565b506102f26122cb565b506102f2612280565b506102f2612250565b506102f261220a565b506102f26121be565b506102f26120cd565b506102f2612082565b506102f261205e565b506102f2611fc5565b506102f2611f13565b506102f2611ee9565b506102f2611e9d565b506102f2611e75565b506102f2611e2f565b506102f2611dd3565b506102f2611d2d565b506102f2611c21565b506102f2611b7c565b506102f26119ac565b506102f2611809565b506102f26117df565b506102f26117c0565b506102f26116aa565b506102f2611621565b506102f26114ce565b506102f2611482565b506102f26113bc565b506102f261133f565b506102f26112f9565b506102f2611253565b506102f2611222565b506102f2610f58565b506102f2610f2f565b506102f2610cf6565b506102f2610cb5565b506102f2610c8b565b506102f2610c20565b506102f2610bca565b506102f2610b87565b506102f2610b46565b506102f2610a77565b506102f26108d0565b506102f26107b1565b506102f261076f565b506102f261068e565b506102f261060e565b506102f261059f565b506102f2610547565b600091031261054257565b600080fd5b5034610542576000366003190112610542576040517f00000000000000000000000071fe31fa53273aeee75678535d992dc1982433c96001600160a01b03168152602090f35b6001600160e01b031981160361054257565b50346105425760203660031901126105425760206004356105bf8161058d565b63ffffffff60e01b166380ac58cd60e01b81149081156105fd575b81156105ec575b506040519015158152f35b6301ffc9a760e01b149050386105e1565b635b5e139f60e01b811491506105da565b50346105425760003660031901126105425760206004600954161515604051908152f35b60005b8381106106455750506000910152565b8181015183820152602001610635565b9060209161066e81518092818552858086019101610632565b601f01601f1916010190565b90602061068b928181520190610655565b90565b50346105425760008060031936011261076c57604051816002546106b1816119f7565b8084529060019081811690811561074457506001146106eb575b6106e7846106db81880382611a9e565b6040519182918261067a565b0390f35b60028352602094507f405787fa12a823e0f2b7631cc41b3ba8828b3321ca811111fa75cd3aa3bb5ace5b82841061073157505050816106e7936106db92820101936106cb565b8054858501870152928501928101610715565b6106e796506106db9450602092508593915060ff191682840152151560051b820101936106cb565b80fd5b503461054257602036600319011261054257602061078e600435612e95565b6040516001600160a01b039091168152f35b6001600160a01b0381160361054257565b5034610542576040366003190112610542576004356107cf816107a0565b6024356107db81612e00565b916001600160a01b038084169082168114610854576100199361080891331490811561080d575b50612e23565b613289565b6001600160a01b0316600090815260076020526040902061084e91506108479033905b9060018060a01b0316600052602052604060002090565b5460ff1690565b38610802565b60405162461bcd60e51b815260206004820152602160248201527f4552433732313a20617070726f76616c20746f2063757272656e74206f776e656044820152603960f91b6064820152608490fd5b9181601f84011215610542578235916001600160401b038311610542576020838186019501011161054257565b5034610542576040366003190112610542576001600160401b036004358181116105425761090561091a9136906004016108a3565b919060243592610913612d61565b369161273b565b6000828152600460205260409020549092906001600160a01b031615610a1b5760009182526020906008825260408320918451918211610a0e575b6109698261096385546119f7565b8561404c565b80601f83116001146109a35750839482939492610998575b50508160011b916000199060031b1c191617905580f35b015190503880610981565b90601f198316956109b985600052602060002090565b9286905b8882106109f6575050836001959697106109dd575b505050811b01905580f35b015160001960f88460031b161c191690553880806109d2565b806001859682949686015181550195019301906109bd565b610a16611a31565b610955565b60405162461bcd60e51b815260206004820152602e60248201527f45524337323155524953746f726167653a2055524920736574206f66206e6f6e60448201526d32bc34b9ba32b73a103a37b5b2b760911b6064820152608490fd5b503461054257602036600319011261054257610019600435610a98816107a0565b610aa0612d61565b600a546040516370a0823160e01b81523060048201526001600160a01b0391821691610b0591906020908290602490829088165afa908115610b39575b600091610b0b575b506001600160a01b0384166000908152600d6020526040902054906130d7565b91614d1e565b610b2c915060203d8111610b32575b610b248183611a9e565b810190613f30565b38610ae5565b503d610b1a565b610b4161335e565b610add565b503461054257600036600319011261054257602062ffffff600a5460a01c16604051908152f35b6001600160a01b03166000908152600d6020526040902090565b503461054257602036600319011261054257600435610ba5816107a0565b60018060a01b0316600052600c602052602060ff604060002054166040519015158152f35b50346105425760008060031936011261076c57610be5612d61565b600219600954166009557f25d628909b0e89249ef68fa8607e05a88893ba9b00b6cff609cb914d3b10e3238180a180f35b6004111561054257565b5034610542576040366003190112610542576020610c55600435610c43816107a0565b60243590610c5082610c16565b6134fc565b6040519015158152f35b606090600319011261054257600435610c77816107a0565b90602435610c84816107a0565b9060443590565b503461054257610019610c9d36610c5f565b91610cb0610cab8433612fd9565b612ed3565b6130ff565b50346105425760003660031901126105425760206002600954161515604051908152f35b90816101a09103126105425790565b908160a09103126105425790565b506040806003193601126105425760048035916001600160401b039283811161054257610d269036908401610cd9565b9260243590811161054257610d3e9036908401610ce8565b92610d4e600160095416156139d1565b610d5781614158565b9490936001600160a01b038516610d716020840184613a17565b93823b15610542576080610e2891610e3493895190633f58092760e21b82528180610da460009b8c9487358d8501613a6a565b0381838a5af18015610f22575b610f0f575b50610e22610e0860208b89610dcf6101808e018e613a81565b949092610dde81890189613a81565b610dee60608b949394018b613a81565b939092519889978897638e3fd47560e01b89528801613aef565b03818a5afa908115610f02575b8991610ed4575b50613b5f565b01613b9d565b6001600160a01b031690565b91813b15610ed05785516340a7514160e11b81526001600160a01b039093169083019081526106e79594610e8a94909392909183919082908490829060200103925af18015610ec3575b610eaa575b5084614441565b516001600160a01b03909216825260208201929092529081906040820190565b80610eb7610ebd92611a48565b80610537565b38610e83565b610ecb61335e565b610e7e565b8380fd5b610ef5915060203d8111610efb575b610eed8183611a9e565b810190613ab6565b38610e1c565b503d610ee3565b610f0a61335e565b610e15565b80610eb7610f1c92611a48565b38610db6565b610f2a61335e565b610db1565b503461054257610019610f4136610c5f565b9060405192610f4f84611a68565b60008452612f36565b503461054257602080600319360112610542576004803590600260015414611213576002600155610f91610f8c8333612fd9565b613498565b6040805163b3f29b7d60e01b81529091906001600160a01b0384169085818481855afa908115611206575b6000916111d9575b50610fce816134ea565b801580156111c6575b156111495750803b156105425782516390386bbf60e01b8152600081848183865af1801561113c575b611129575b505b82516323e6650160e11b815285818481855afa95861561111c575b6000966110ed575b50508251632f4fefaf60e01b8152906000828481845afa9182156110e0575b6000926110bd575b50803b1561054257611094956000809461107e9651968795869485936313edab8160e01b85528401613cf4565b03925af180156110b0575b61109d575b50614b4a565b61001960018055565b80610eb76110aa92611a48565b3861108e565b6110b861335e565b611089565b6110d991923d8091833e6110d18183611a9e565b810190613c6b565b9038611051565b6110e861335e565b611049565b61110d929650803d10611115575b6111058183611a9e565b810190613ba7565b93388061102a565b503d6110fb565b61112461335e565b611022565b80610eb761113692611a48565b38611005565b61114461335e565b611000565b611152816134ea565b600281149081156111b2575b501561100757803b15610542578251638a689e8160e01b8152600081848183865af180156111a5575b611192575b50611007565b80610eb761119f92611a48565b3861118c565b6111ad61335e565b611187565b600391506111bf816134ea565b143861115e565b506111d0816134ea565b60018114610fd7565b6111f99150863d88116111ff575b6111f18183611a9e565b810190613c56565b38610fc4565b503d6111e7565b61120e61335e565b610fbc565b60405163558a1e0360e11b8152fd5b503461054257600036600319011261054257602062ffffff600a5460b81c16604051908152f35b8015150361054257565b503461054257604036600319011261054257600435611271816107a0565b6024359061127e82611249565b611286612d61565b6001600160a01b03166000818152600b602052604090205490918015159160ff1615158290036112b257005b7f1da28d127ec72d2dde6a533c98857664b25cd827680fb1f39f57394c2b444d91916112f060209260406000209060ff801983541691151516179055565b604051908152a2005b5034610542576000366003190112610542576040517f000000000000000000000000833fd2704cdb5a68d22fcf674b0787cb9cbf63fb6001600160a01b03168152602090f35b50346105425760403660031901126105425760043561135d816107a0565b6024359061136a826107a0565b6001600160a01b03821691826113ad575061138490613d41565b905b600052600d602052604060002080549182039182116113a25755005b6113aa6130a9565b55005b6113b691613d75565b90611386565b5034610542576020366003190112610542576004356113da816107a0565b6113e2612d61565b6001600160a01b038181169190821561145157600a54166001600160a01b031682900361140b57005b600a80546001600160a01b0319166001600160a01b039092169190911790557fceec08a75d1f3b12c14c6cdc16c081aec1c401c2eac1d8c6ea91e9d73b929211600080a2005b60405162461bcd60e51b815260206004820152600960248201526830206164647265737360b81b6044820152606490fd5b50346105425760008060031936011261076c5761149d612d61565b600419600954166009557f0e9c26be0a38592bf8b6a0ba90062ca5a65db29064cc217e2a903c01130bd8568180a180f35b503461054257602080600319360112610542576001600160401b03600435818111610542576115019036906004016108a3565b9161150a612d61565b82116115e4575b611525826115206010546119f7565b614007565b600092601f83116001146115635750918192600092611558575b5050600019600383901b1c191660019190911b17601055005b01359050388061153f565b6010600052601f198316937f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae672929181905b8682106115cc57505083600195106115b2575b505050811b01601055005b0135600019600384901b60f8161c191690553880806115a7565b80600184968294958701358155019501920190611594565b6115ec611a31565b611511565b9181601f84011215610542578235916001600160401b038311610542576020808501948460051b01011161054257565b5034610542576040366003190112610542576001600160401b03600435818111610542576116539036906004016115f1565b916024359081116105425761166c9036906004016115f1565b919060005b83811061167a57005b60018161169f61168e6116a5948887613e28565b35611698816107a0565b8887613e46565b016138b6565b611671565b50346105425760203660031901126105425760206004356116ca816107a0565b6001600160a01b0390611700817f000000000000000000000000e653a3deb851befedc7a1368b3cc20cc458f9800841630613685565b91821561178b575b8215611756575b8215611722575b50506040519015158152f35b61174f92507f00000000000000000000000071fe31fa53273aeee75678535d992dc1982433c91630613617565b3880611716565b915061178581837f000000000000000000000000833fd2704cdb5a68d22fcf674b0787cb9cbf63fb1630613685565b9161170f565b91506117ba81837f0000000000000000000000009f95b3fc933e8d3acda1fca942c0e772c3b3e7d21630613617565b91611708565b503461054257602036600319011261054257602061078e600435612e00565b503461054257600036600319011261054257600a546040516001600160a01b039091168152602090f35b50606036600319011261054257600435611822816107a0565b602435906001600160401b038083116105425736602384011215610542578260040135908111610542576024830192602436918360061b0101116105425760443561186c81610c16565b6008600954166119725761188090336134fc565b1561194257600092835b8281106118bc576001600160a01b0384166000908152600d6020526040902085906118b8905b9182546130f2565b9055005b60206118d0610e2882610e22858888613c38565b33036118e0575b5060010161188a565b94906118fa6001926118f3838787613c38565b35906130f2565b9561193a6118b087610830611920611913878b8b613c38565b3595610e22888c8c613c38565b6001600160a01b03166000908152600e6020526040902090565b9055906118d7565b60405162461bcd60e51b8152602060048201526008602482015267139bdd081c1bdbdb60c21b6044820152606490fd5b60405162461bcd60e51b8152602060048201526012602482015271119d5b98dd1a5bdb881a5cc81c185d5cd95960721b6044820152606490fd5b50346105425760008060031936011261076c576119c7612d61565b6004600954176009557f894e6d7139b6bce810a9a2c047c92d53b08e18c97dd6d1167b84e9525e3dd2578180a180f35b90600182811c92168015611a27575b6020831014611a1157565b634e487b7160e01b600052602260045260246000fd5b91607f1691611a06565b50634e487b7160e01b600052604160045260246000fd5b6001600160401b038111611a5b57604052565b611a63611a31565b604052565b602081019081106001600160401b03821117611a5b57604052565b604081019081106001600160401b03821117611a5b57604052565b90601f801991011681019081106001600160401b03821117611a5b57604052565b6040519060008260105491611ad3836119f7565b808352600193808516908115611b5b5750600114611afb575b50611af992500383611a9e565b565b601060009081527f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae67294602093509091905b818310611b43575050611af9935082010138611aec565b85548884018501529485019487945091830191611b2c565b9050611af994506020925060ff191682840152151560051b82010138611aec565b50346105425760008060031936011261076c5760405181601054611b9f816119f7565b808452906001908181169081156107445750600114611bc8576106e7846106db81880382611a9e565b60108352602094507f1b6847dc741a1b0cd08d278845f9d819d87b734759afb55fe2de5cb82a9ae6725b828410611c0e57505050816106e7936106db92820101936106cb565b8054858501870152928501928101611bf2565b503461054257604036600319011261054257600435611c3f816107a0565b60243590611c4c82611249565b81611c55612d61565b611d02575b6001600160a01b0381166000908152600f60205260409020611c7b90610847565b82151590151503611c8857005b611cfd7f24e274cfd23919da24a57044266685f5474338837aecdcca3136b2102f78fee591611ce1611cb8612704565b8515158152600160208201526001600160a01b0383166000908152600f60205260409020613fbb565b60405193151584526001600160a01b0316929081906020820190565b0390a2005b6001600160a01b0381166000908152600c6020526040902054611d289060ff1615613f7b565b611c5a565b503461054257602036600319011261054257600435611d4b816107a0565b6001600160a01b03168015611d7c5760005260056020526106e7604060002054604051918291829190602083019252565b60405162461bcd60e51b815260206004820152602960248201527f4552433732313a2061646472657373207a65726f206973206e6f7420612076616044820152683634b21037bbb732b960b91b6064820152608490fd5b50346105425760008060031936011261076c57611dee612d61565b80546001600160a01b03198116825581906001600160a01b03167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08280a380f35b5034610542576040366003190112610542576004356001600160401b03811161054257611e636100199136906004016115f1565b60243591611e70836107a0565b613e46565b5034610542576020366003190112610542576040516004356001600160a01b03168152602090f35b50346105425760008060031936011261076c57611eb8612d61565b600119600954166009557f15afb7ccbfde73fd4dcc20261272d735c53b391894ea32e4eab3b0bbcc3b68a48180a180f35b5034610542576000366003190112610542576000546040516001600160a01b039091168152602090f35b50346105425760008060031936011261076c5760405181600354611f36816119f7565b808452906001908181169081156107445750600114611f5f576106e7846106db81880382611a9e565b60038352602094507fc2575a0e9e593c00f959f8c92f12db2869c3395a3b0502d05e2516446f71f85b5b828410611fa557505050816106e7936106db92820101936106cb565b8054858501870152928501928101611f89565b62ffffff81160361054257565b503461054257602036600319011261054257600435611fe381611fb8565b611feb612d61565b62ffffff908181166120026207a120821115613f3f565b80600a54938460b81c160361201357005b62ffffff60b81b1990921660b89190911b62ffffff60b81b1617600a556040519081527f1b45b945f2f80d1a9748b57c798253b355141f89518cbe965586e5d20cf62ed090602090a1005b50346105425760003660031901126105425760206008600954161515604051908152f35b50346105425760008060031936011261076c5761209d612d61565b6008600954176009557f2b550821fb732f5939e14503f24dc3832c2d9ebadb16664a9a01f139726ebe428180a180f35b5034610542576040366003190112610542576004356120eb816107a0565b6024356120f781611249565b6001600160a01b0382169133831461217957816121366121479233600052600760205260406000209060018060a01b0316600052602052604060002090565b9060ff801983541691151516179055565b604051901515815233907f17307eab39ab6107e8899845ad3d59bd9653f200f220920489ca2b5937696c3190602090a3005b60405162461bcd60e51b815260206004820152601960248201527f4552433732313a20617070726f766520746f2063616c6c6572000000000000006044820152606490fd5b50346105425760008060031936011261076c576121d9612d61565b600819600954166009557f0b00a41d43ba96cf6fe1e1fdad1d13979e0f6c8aeee0e68f7e419db2c2d910408180a180f35b5034610542576000366003190112610542576040517f000000000000000000000000e653a3deb851befedc7a1368b3cc20cc458f98006001600160a01b03168152602090f35b503461054257600036600319011261054257602060405173eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee8152f35b50346105425760008060031936011261076c5761229b612d61565b6001600954176009557f169fc5c8e9cdd2e4b8f64a1b71384ab600ca57693c0b9c1c0710e011eace4c948180a180f35b5034610542576020366003190112610542576004356122e9816107a0565b60018060a01b0316600052600b602052602060ff604060002054166040519015158152f35b50346105425760203660031901126105425760043561232c816107a0565b60018060a01b0316600052600f6020526040806000205460ff8251918181161515835260081c1615156020820152f35b50346105425760203660031901126105425760043561237a81611fb8565b612382612d61565b62ffffff90818116612399620186a0821115613f3f565b80600a54938460a01c16036123aa57005b62ffffff60a01b1990921660a09190911b62ffffff60a01b1617600a556040519081527ffd8ee7249e8f6c22d7d3c71093c7e935b7468f133a2a9a807ae4635c70f8cf9b90602090a1005b5034610542576000366003190112610542576040517f0000000000000000000000009f95b3fc933e8d3acda1fca942c0e772c3b3e7d26001600160a01b03168152602090f35b90816101e09103126105425790565b503461054257604080600319360112610542576001600160401b03600480358281116105425761247d903690830161243b565b91602435908111610542576124959036908301610ce8565b926124a5600160095416156139d1565b6124d16124b4610e2885613b9d565b9382519463555ddc6560e11b865285806020948593888301613bbc565b0381731820a4b7618bde71dce8cdc73aab6c95905fad245afa9485156126a1575b600095612682575b506001600160a01b039461251090861615613bf5565b61251981614545565b96909586169261252b81830183613a17565b9092853b1561054257610e22610e289360809360006125c4978c6125638d519485938493633f58092760e21b85528a35908501613a6a565b0381838d5af18015612675575b612662575b50806125856101a0890189613a81565b91908c6125a88d612598818a018a613a81565b610dee60608c949394018c613a81565b03818c5afa918215612655575b600092612638575b5050613b5f565b91803b156105425783516340a7514160e11b81526001600160a01b039093169483019485526106e794610e8a939160009183919082908490829060200103925af1801561262b575b612618575b5084614762565b80610eb761262592611a48565b38612611565b61263361335e565b61260c565b61264e9250803d10610efb57610eed8183611a9e565b38806125bd565b61265d61335e565b6125b5565b80610eb761266f92611a48565b38612575565b61267d61335e565b612570565b61269a919550823d8411611115576111058183611a9e565b93386124fa565b6126a961335e565b6124f2565b50346105425760008060031936011261076c576126c9612d61565b6126f460018060a01b03600a54164790838052600d602052604084205482039182116126f757614c57565b80f35b6126ff6130a9565b614c57565b60405190611af982611a83565b6020906001600160401b03811161272e575b601f01601f19160190565b612736611a31565b612723565b92919261274782612711565b916127556040519384611a9e565b829481845281830111610542578281602093846000960137010152565b503461054257608036600319011261054257600435612790816107a0565b60243561279c816107a0565b606435916001600160401b0383116105425736602384011215610542576127d061001993369060248160040135910161273b565b9160443591612f36565b50346105425760003660031901126105425760206001600954161515604051908152f35b503461054257604036600319011261054257610019610f8c600435612822816107a0565b60243590612fd9565b5034610542576020366003190112610542576106e761284b6004356136f3565b604051918291602083526020830190610655565b50346105425760403660031901126105425760206128ba600435612882816107a0565b6024359061288f826107a0565b60018060a01b0316600052600e835260406000209060018060a01b0316600052602052604060002090565b54604051908152f35b5034610542576020366003190112610542576004356001600160401b038111610542576128f761298391369060040161243b565b612906600160095416156139d1565b6129718135612914816107a0565b60405163555ddc6560e11b81526001600160a01b03916020908290819061294090861660048301613bbc565b0381731820a4b7618bde71dce8cdc73aab6c95905fad245afa9081156129bd575b60009161299f575b501615613bf5565b61297a81614545565b92909182614762565b604080516001600160a01b039290921682526020820192909252f35b6129b7915060203d8111611115576111058183611a9e565b38612969565b6129c561335e565b612961565b503461054257604036600319011261054257602060ff612a276004356129ef816107a0565b602435906129fc826107a0565b60018060a01b03166000526007845260406000209060018060a01b0316600052602052604060002090565b54166040519015158152f35b503461054257604036600319011261054257600435612a51816107a0565b60243590612a5e82611249565b81612a67612d61565b612adf575b6001600160a01b0381166000908152600c60205260409020612a8d90610847565b82151590151503612a9a57005b611cfd81611ce1846121367fab2e2e8d21d5efbffb30945e9b6ee1fb43620ef65a228f871f5028bf8a6e004a9560018060a01b0316600052600c602052604060002090565b6001600160a01b0381166000908152600f6020526040902054612b089060081c60ff1615613f7b565b612a6c565b50346105425760008060031936011261076c57612b28612d61565b6002600954176009557fc90e003e0d9f42732e9a57f151adcac590bd64efa6523752de67dd4954bd00238180a180f35b503461054257602036600319011261054257600435612b76816107a0565b612b7e612d61565b6001600160a01b039081168015612bcc57600080546001600160a01b03198116831782559092167f8be0079c531659141344cd1fd0a4f28419497f9722a3daafe3b4186f6b6457e08380a380f35b60405162461bcd60e51b815260206004820152602660248201527f4f776e61626c653a206e6577206f776e657220697320746865207a65726f206160448201526564647265737360d01b6064820152608490fd5b506020366003190112610542576004356001600160401b03811161054257612c4f612983913690600401610cd9565b612c5e600160095416156139d1565b612c6781614158565b92909182614441565b503461054257604036600319011261054257600435612c8e816107a0565b6024356001600160401b03811161054257612cad9036906004016115f1565b91600092835b818110612cbe578480f35b600181612ccf612d50938588613e28565b35612cd9816107a0565b612d48612d40612cff836108308a60018060a01b0316600052600e602052604060002090565b54928a612d22826108308c60018060a01b0316600052600e602052604060002090565b55858060a01b038481831615600014612d5557610b6d918b16614c57565b9182546130d7565b9055016138b6565b612cb3565b610b6d918b1683614d1e565b6000546001600160a01b03163303612d7557565b606460405162461bcd60e51b815260206004820152602060248201527f4f776e61626c653a2063616c6c6572206973206e6f7420746865206f776e65726044820152fd5b15612dc057565b60405162461bcd60e51b8152602060048201526018602482015277115490cdcc8c4e881a5b9d985b1a59081d1bdad95b88125160421b6044820152606490fd5b6000908152600460205260409020546001600160a01b031661068b811515612db9565b15612e2a57565b60405162461bcd60e51b815260206004820152603e60248201527f4552433732313a20617070726f76652063616c6c6572206973206e6f7420746f60448201527f6b656e206f776e6572206e6f7220617070726f76656420666f7220616c6c00006064820152608490fd5b600081815260046020526040902054612eb8906001600160a01b03161515612db9565b6000908152600660205260409020546001600160a01b031690565b15612eda57565b60405162461bcd60e51b815260206004820152602e60248201527f4552433732313a2063616c6c6572206973206e6f7420746f6b656e206f776e6560448201526d1c881b9bdc88185c1c1c9bdd995960921b6064820152608490fd5b91611af99391612f5d93612f4d610cab8433612fd9565b612f588383836130ff565b61345c565b612fb5565b60809060208152603260208201527f4552433732313a207472616e7366657220746f206e6f6e20455243373231526560408201527131b2b4bb32b91034b6b83632b6b2b73a32b960711b60608201520190565b15612fbc57565b60405162461bcd60e51b815280612fd560048201612f62565b0390fd5b6001600160a01b0380612feb84612e00565b169281831692848414948515613021575b5050831561300b575b50505090565b61301791929350612e95565b1614388080613005565b60009081526007602090815260408083206001600160a01b03949094168352929052205460ff1693503880612ffc565b1561305857565b60405162461bcd60e51b8152602060048201526024808201527f4552433732313a207472616e7366657220746f20746865207a65726f206164646044820152637265737360e01b6064820152608490fd5b50634e487b7160e01b600052601160045260246000fd5b6000198101919082116130cf57565b611af96130a9565b919082039182116130cf57565b90600182018092116130cf57565b919082018092116130cf57565b9061310983612e00565b6001600160a01b0383811692909182168390036131e2576131586131bb92821694613135861515613051565b61313e87613235565b6001600160a01b0316600090815260056020526040902090565b61316281546130c0565b90556001600160a01b038116600090815260056020526040902061318681546130e4565b905561319c856000526004602052604060002090565b80546001600160a01b0319166001600160a01b03909216919091179055565b7fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef600080a4565b60405162461bcd60e51b815260206004820152602560248201527f4552433732313a207472616e736665722066726f6d20696e636f72726563742060448201526437bbb732b960d91b6064820152608490fd5b600081815260066020526040812080546001600160a01b03191690556001600160a01b0361326283612e00565b167f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9258280a4565b600082815260066020526040902080546001600160a01b0319166001600160a01b0383161790556001600160a01b03806132c284612e00565b169116907f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925600080a4565b90816020910312610542575161068b8161058d565b61068b939260809260018060a01b031682526000602083015260408201528160608201520190610655565b6001600160a01b03918216815291166020820152604081019190915260806060820181905261068b92910190610655565b506040513d6000823e3d90fd5b3d15613396573d9061337c82612711565b9161338a6040519384611a9e565b82523d6000602084013e565b606090565b909190803b15613454576133cd602091600093604051948580948193630a85bd0160e11b998a84523360048501613302565b03926001600160a01b03165af160009181613424575b50613416576133f061336b565b805190816134115760405162461bcd60e51b815280612fd560048201612f62565b602001fd5b6001600160e01b0319161490565b61344691925060203d811161344d575b61343e8183611a9e565b8101906132ed565b90386133e3565b503d613434565b505050600190565b92909190823b1561348f576133cd926020926000604051809681958294630a85bd0160e11b9a8b8552336004860161332d565b50505050600190565b1561349f57565b60405162461bcd60e51b815260206004820152600c60248201526b139bdd08185c1c1c9bdd995960a21b6044820152606490fd5b50634e487b7160e01b600052602160045260246000fd5b600411156134f457565b611af96134d3565b90613506816134ea565b60028103613543575061068b907f000000000000000000000000e653a3deb851befedc7a1368b3cc20cc458f98006001600160a01b031630613685565b61354c816134ea565b60038103613589575061068b907f000000000000000000000000833fd2704cdb5a68d22fcf674b0787cb9cbf63fb6001600160a01b031630613685565b613592816134ea565b806135cc575061068b907f0000000000000000000000009f95b3fc933e8d3acda1fca942c0e772c3b3e7d26001600160a01b031630613617565b806135d86001926134ea565b036136115761068b907f00000000000000000000000071fe31fa53273aeee75678535d992dc1982433c96001600160a01b031630613617565b50600090565b60405191733d3d3d3d363d3d37604160353639366041013d7360601b835260601b60148301526c5af43d3d93803e603357fd5bf360981b602883015260601b60358201526049600081830180943c607281015160298201511460698201516020830151141691519051141690565b60405191733d3d3d3d363d3d37605560353639366055013d7360601b835260601b60148301526c5af43d3d93803e603357fd5bf360981b602883015260601b60358201526049600081830180943c607281015160298201511460698201516020830151141691519051141690565b600081815260046020526040902054613716906001600160a01b03161515612db9565b600090808252602060088152604083206040519384818354613737816119f7565b938484528684019560019288848216918260001461380b5750506001146137cf575b50505061376892500385611a9e565b613770611abf565b9081519485156137c657805161378e575050505061068b915061382d565b61068b94506137ba866137ab604051988996888089019101610632565b84019151809386840190610632565b01038084520182611a9e565b94505050505090565b879350819291528282205b8583106137f35750506137689350820101388080613759565b8054838b0185015289945087939092019181016137da565b93509450505061376894915060ff19168552151560051b820101388080613759565b600081815260046020526040902054613850906001600160a01b03161515612db9565b613858611abf565b8051909190600090156138a15750602061387461068b92613908565b92604051938161388d8693518092868087019101610632565b82016137ba82518093868085019101610632565b915050604051906138b182611a68565b815290565b60019060001981146138c6570190565b6138ce6130a9565b0190565b50634e487b7160e01b600052603260045260246000fd5b9060209180518210156138fb57010190565b6139036138d2565b010190565b80156139b357806000908282935b61399f575061392483612711565b926139326040519485611a9e565b80845281601f1961394283612711565b013660208701375b6139545750505090565b61395d906130c0565b90600a9060308282068101809111613992575b60f81b6001600160f81b031916841a61398984876138e9565b5304908161394a565b61399a6130a9565b613970565b926139ab600a916138b6565b930480613916565b506040516139c081611a83565b60018152600360fc1b602082015290565b156139d857565b60405162461bcd60e51b8152602060048201526017602482015276141bdbdb0818dc99585d1a5bdb881a5cc81c185d5cd959604a1b6044820152606490fd5b903590601e198136030182121561054257018035906001600160401b0382116105425760200191813603831361054257565b908060209392818452848401376000828201840152601f01601f1916010190565b60409061068b949281528160208201520191613a49565b903590601e198136030182121561054257018035906001600160401b03821161054257602001918160051b3603831361054257565b90816020910312610542575161068b81611249565b81835290916001600160fb1b0383116105425760209260051b809284830137010190565b91613b1b91613b0a9197959697606085526060850191613acb565b602095869484830386860152613acb565b90604081830391015282815201929160005b828110613b3b575050505090565b9091929382806001928735613b4f81611249565b1515815201950193929101613b2d565b15613b6657565b60405162461bcd60e51b815260206004820152600f60248201526e139195081b9bdd08185b1b1bddd959608a1b6044820152606490fd5b3561068b816107a0565b90816020910312610542575161068b816107a0565b6001600160a01b0390911681527fac7fbab5f54a3ca8194167523c6753bfeb96a445279294b6125b68cce2177054602082015260400190565b15613bfc57565b60405162461bcd60e51b8152602060048201526014602482015273115490cdcdcdc81b9bdd081cdd5c1c1bdc9d195960621b6044820152606490fd5b9190811015613c49575b60061b0190565b613c516138d2565b613c42565b90816020910312610542575161068b81610c16565b9060209081838203126105425782516001600160401b0393848211610542570181601f82011215610542578051938411613ce7575b8360051b9060405194613cb585840187611a9e565b85528380860192820101928311610542578301905b828210613cd8575050505090565b81518152908301908301613cca565b613cef611a31565b613ca0565b6001600160a01b03909116815260406020808301829052835191830182905260609092019282019160005b828110613d2d575050505090565b835185529381019392810192600101613d1f565b6001600160a01b03166000818152600e60209081526040808320838052909152812080549190559190611af9908390614c57565b9091906001600160a01b039081841615613de45761083093611af99282169081600052600e6020526000613dde82613dc281604085209060018060a01b0316600052602052604060002090565b5498899660018060a01b0316600052600e602052604060002090565b55614d1e565b606460405162461bcd60e51b815260206004820152602060248201527f557365207769746864726177455448526f79616c7469657320696e73746561646044820152fd5b9190811015613e39575b60051b0190565b613e416138d2565b613e32565b600092916001600160a01b038316613ebe57906000915b808310613e8b575050506001600160a01b03166000908152600d60205260409020613e8790612d40565b9055565b909193613eb5600191613eaf613eaa613ea5898789613e28565b613b9d565b613d41565b906130f2565b94019190613e5d565b9083915b808310613eec575050506001600160a01b03166000908152600d60205260409020613e8790612d40565b909193600190613f1085613f01888688613e28565b35613f0b816107a0565b613d75565b8101809111613f23575b94019190613ec2565b613f2b6130a9565b613f1a565b90816020910312610542575190565b15613f4657565b60405162461bcd60e51b815260206004820152600d60248201526c46656520746f6f206c6172676560981b6044820152606490fd5b15613f8257565b60405162461bcd60e51b815260206004820152601160248201527021b0b713ba1031b0b636103937baba32b960791b6044820152606490fd5b90602090613fd881511515849060ff801983541691151516179055565b0151815461ff00191690151560081b61ff0016179055565b818110613ffb575050565b60008155600101613ff0565b90601f8211614014575050565b611af99160106000526020600020906020601f840160051c83019310614042575b601f0160051c0190613ff0565b9091508190614035565b9190601f811161405b57505050565b611af9926000526020600020906020601f840160051c8301931061404257601f0160051c0190613ff0565b1561408d57565b60405162461bcd60e51b815260206004820152601d60248201527f426f6e64696e67206375727665206e6f742077686974656c69737465640000006044820152606490fd5b3561068b81611fb8565b156140e357565b60405162461bcd60e51b815260206004820152603060248201527f4e6f6e7a65726f20726f79616c747920666f72206e6f6e20455243323938312060448201526f776974686f75742066616c6c6261636b60801b6064820152608490fd5b600311156134f457565b3560038110156105425790565b906142986020830161419161418c61084761417284613b9d565b6001600160a01b03166000908152600b6020526040902090565b614086565b61429e610e28610e286142806141aa61014089016140d2565b946141e16141dc6141be6101608c01613b9d565b976141c88c613b9d565b6001600160a01b03999091908a1690614ab4565b6140dc565b60006141f5610e28610e28610e288d613b9d565b6040516301ffc9a760e01b815263780e9d6360e01b60048201529190602090839060249082905afa80928293614325575b506142c7575050614259867f00000000000000000000000071fe31fa53273aeee75678535d992dc1982433c91691613b9d565b6142628a613b9d565b9061426f60808c0161414b565b9261427984614141565b3090614345565b9361428d60608901613b9d565b941694858095614971565b96613b9d565b7f77948cb83ef3caff9ac13dfab1ea1f8a6875c98370287ce587f5dbc74cc5b6b0600080a39190565b50156142fa57614259867f0000000000000000000000009f95b3fc933e8d3acda1fca942c0e772c3b3e7d2165b91613b9d565b614259867f00000000000000000000000071fe31fa53273aeee75678535d992dc1982433c9166142f4565b61433e91935060203d8111610efb57610eed8183611a9e565b9138614226565b929091607f9492604051947f60763d8160093d39f33d3d3d3d363d3d37604160353639366041013d73000000865260601b601d8601526c5af43d3d93803e603357fd5bf360981b603186015260601b603e85015260601b605284015260601b6066830152607a8201534360e01b607b8201526000f090565b356001600160801b03811681036105425790565b989a9995946144269197939560e099936144349660018060a01b03809b168d528c60206001600160801b038094169101528c604062ffffff809b169101521660608c01526101008060808d01528b0191613a49565b9188830360a08a0152613a49565b961660c085015216910152565b6001600160a01b0381169161445860408201613b9d565b9261446560a083016143bd565b9061447260c084016140d2565b61447e60e085016143bd565b9561448d610100860186613a17565b94909161449e610120880188613a17565b6144ae6101408a9c939c016140d2565b6144bb6101608b01613b9d565b91883b1561054257611af99c6145079a6000986144ed966040519b8c9a8b9a63f0732b4d60e01b8c5260048c016143d1565b038183865af18015614538575b614525575b503490614c57565b61451f61451382613b9d565b91610180810190613a81565b91614886565b80610eb761453292611a48565b386144ff565b61454061335e565b6144fa565b906142986145a19261429e610e28610e286040840161456c61418c61084761417284613b9d565b61428d608061465e61458161016089016140d2565b936145b56141dc6145956101808c01613b9d565b9660208c019e8f613b9d565b6001600160a01b0398909190891690614ab4565b8b6145ca610e28610e28610e28600094613b9d565b6040516301ffc9a760e01b815263780e9d6360e01b60048201529190602090839060249082905afa809282936146c2575b5061466557505061462e857f000000000000000000000000833fd2704cdb5a68d22fcf674b0787cb9cbf63fb1691613b9d565b6146378d613b9d565b61464360a08c0161414b565b9161464d83614141565b6146568c613b9d565b9330906146e2565b9601613b9d565b50156146975761462e857f000000000000000000000000e653a3deb851befedc7a1368b3cc20cc458f98001691613b9d565b61462e857f000000000000000000000000833fd2704cdb5a68d22fcf674b0787cb9cbf63fb166142f4565b6146db91935060203d8111610efb57610eed8183611a9e565b91386145fb565b93916093959391604051957f608a3d8160093d39f33d3d3d3d363d3d37605560353639366055013d73000000875260601b601d8701526c5af43d3d93803e603357fd5bf360981b603187015260601b603e86015260601b605285015260601b6066840152607a8301534360e01b607b83015260601b607f8201526000f090565b6001600160a01b0381169161477960608201613b9d565b9261478660c083016143bd565b9061479360e084016140d2565b6147a061010085016143bd565b956147af610120860186613a17565b9490916147c0610140880188613a17565b6147d06101608a9c939c016140d2565b6147dd6101808b01613b9d565b91883b1561054257611af99c61483a9a60009861480f966040519b8c9a8b9a63f0732b4d60e01b8c5260048c016143d1565b038183865af18015614868575b614855575b5061482b83613b9d565b906101c0840135913390614ca2565b61451f61484960208301613b9d565b916101a0810190613a81565b80610eb761486292611a48565b38614821565b61487061335e565b61481c565b91602061068b938181520191613acb565b6001600160a01b039384169391929160005b8381106148f957505050823b15610542576148cd9260009283604051809681958294637e24d2ab60e01b845260048401614875565b03925af180156148ec575b6148df5750565b80610eb7611af992611a48565b6148f461335e565b6148d8565b81831690614908818688613e28565b3591803b1561054257600060019360648a836040519586948593632142170760e11b8552336004860152602485015260448401525af18015614964575b614951575b5001614898565b80610eb761495e92611a48565b3861494a565b61496c61335e565b614945565b6040516001600160a01b03928316939290919061498d83611a68565b600083528116918215614a70576000858152600460205260409020546001600160a01b0316614a2b57848281611af9956149dc612f5d9660018060a01b03166000526005602052604060002090565b6149e681546130e4565b9055614a008361319c846000526004602052604060002090565b60007fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef8180a461339b565b60405162461bcd60e51b815260206004820152601c60248201527f4552433732313a20746f6b656e20616c7265616479206d696e746564000000006044820152606490fd5b606460405162461bcd60e51b815260206004820152602060248201527f4552433732313a206d696e7420746f20746865207a65726f20616464726573736044820152fd5b62ffffff161591908215614b37575b508115614ace575090565b6040516301ffc9a760e01b815263152a902d60e11b60048201529150602090829060249082906001600160a01b03165afa908115614b2a575b600091614b12575090565b61068b915060203d8111610efb57610eed8183611a9e565b614b3261335e565b614b07565b6001600160a01b03161515915038614ac3565b614b5381612e00565b614b5c82613235565b6001600160a01b031660008181526005602052604081208054919392839285926000198201918211614c4a575b5582825260046020526040822080546001600160a01b03191690557fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef8280a4614be5614bdf826000526008602052604060002090565b546119f7565b614bed575050565b614c01906000526008602052604060002090565b614c0b81546119f7565b80614c1557505050565b601f8111600114614c24575055565b81835260208320614c4091601f0160051c810190600101613ff0565b8160208120915555565b614c526130a9565b614b89565b600080809381935af115614c6757565b60405162461bcd60e51b815260206004820152601360248201527211551217d514905394d1915497d19052531151606a1b6044820152606490fd5b9060006064926020958295604051946323b872dd60e01b86526004860152602485015260448401525af13d15601f3d1160016000511416171615614ce257565b60405162461bcd60e51b81526020600482015260146024820152731514905394d1915497d19493d357d1905253115160621b6044820152606490fd5b600091826044926020956040519363a9059cbb60e01b8552600485015260248401525af13d15601f3d1160016000511416171615614d5857565b60405162461bcd60e51b815260206004820152600f60248201526e1514905394d1915497d19052531151608a1b6044820152606490fdfea2646970667358221220dcf4512e4081010072bc0555716cd9faf374c10369881ae01fff209cfafb940b64736f6c63430008110033

Constructor Arguments (ABI-Encoded and is the last bytes of the Contract Creation Code above)

0000000000000000000000009f95b3fc933e8d3acda1fca942c0e772c3b3e7d200000000000000000000000071fe31fa53273aeee75678535d992dc1982433c9000000000000000000000000e653a3deb851befedc7a1368b3cc20cc458f9800000000000000000000000000833fd2704cdb5a68d22fcf674b0787cb9cbf63fb0000000000000000000000008c4e0af3829de1e5cf9a29554efd6548028fcd6e00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

-----Decoded View---------------
Arg [0] : _enumerableETHTemplate (address): 0x9f95b3Fc933E8D3acDa1FCa942C0E772C3B3e7D2
Arg [1] : _missingEnumerableETHTemplate (address): 0x71fe31fa53273AeeE75678535d992DC1982433c9
Arg [2] : _enumerableERC20Template (address): 0xE653A3DeB851bEfedC7a1368B3cC20cC458f9800
Arg [3] : _missingEnumerableERC20Template (address): 0x833Fd2704Cdb5a68D22FCf674b0787Cb9cbf63fb
Arg [4] : _protocolFeeRecipient (address): 0x8C4e0af3829De1E5cf9a29554efd6548028Fcd6E
Arg [5] : _protocolFeeMultiplier (uint24): 0
Arg [6] : _carryFeeMultiplier (uint24): 0

-----Encoded View---------------
7 Constructor Arguments found :
Arg [0] : 0000000000000000000000009f95b3fc933e8d3acda1fca942c0e772c3b3e7d2
Arg [1] : 00000000000000000000000071fe31fa53273aeee75678535d992dc1982433c9
Arg [2] : 000000000000000000000000e653a3deb851befedc7a1368b3cc20cc458f9800
Arg [3] : 000000000000000000000000833fd2704cdb5a68d22fcf674b0787cb9cbf63fb
Arg [4] : 0000000000000000000000008c4e0af3829de1e5cf9a29554efd6548028fcd6e
Arg [5] : 0000000000000000000000000000000000000000000000000000000000000000
Arg [6] : 0000000000000000000000000000000000000000000000000000000000000000


Block Transaction Difficulty Gas Used Reward
View All Blocks Produced

Block Uncle Number Difficulty Gas Used Reward
View All Uncles
Loading...
Loading
Loading...
Loading

Validator Index Block Amount
View All Withdrawals

Transaction Hash Block Value Eth2 PubKey Valid
View All Deposits
Loading...
Loading
[ Download: CSV Export  ]
[ Download: CSV Export  ]

A contract address hosts a smart contract, which is a set of code stored on the blockchain that runs when predetermined conditions are met. Learn more about addresses in our Knowledge Base.