ETH Price: $1,861.85 (-9.52%)

Transaction Decoder

Block:
16438750 at Jan-19-2023 05:25:23 AM +UTC
Transaction Fee:
0.004347470232304184 ETH $8.09
Gas Used:
252,904 Gas / 17.190199571 Gwei

Emitted Events:

25 SuperRareV2.Transfer( from=[Sender] 0xafafc7c39f09c115e525ff4dd0cd488f17e86c3d, to=0x478bb542...6DFF5b83d, tokenId=39593 )
26 SuperRareBazaar.AcceptOffer( _originContract=SuperRareV2, _bidder=0x478bb542...6DFF5b83d, _seller=[Sender] 0xafafc7c39f09c115e525ff4dd0cd488f17e86c3d, _currencyAddress=0x00000000...000000000, _amount=1000000000000000000, _tokenId=39593, _splitAddresses=[0xAFAFc7c39F09c115e525FF4Dd0Cd488f17e86c3D], _splitRatios=[100] )

Account State Difference:

  Address   Before After State Difference Code
0x1634c3b0...07767B456
(builder0x69)
2.131364492482271921 Eth2.131996752482271921 Eth0.00063226
0x6D7c4477...68E9a7a42
(SuperRare: Bazaar)
464.674295411031070001 Eth463.644295411031070001 Eth1.03
0x860a80d3...D60b48DA9
(SuperRare: Treasury)
2,718.720892982086185999 Eth2,718.900892982086185999 Eth0.18
0xAFAFc7c3...f17e86c3D
0.744126472768299475 Eth
Nonce: 749
1.589779002535995291 Eth
Nonce: 750
0.845652529767695816
0xb932a70A...7f75Fb9e0

Execution Trace

SuperRareBazaar.acceptOffer( _originContract=0xb932a70A57673d89f4acfFBE830E8ed7f75Fb9e0, _tokenId=39593, _currencyAddress=0x0000000000000000000000000000000000000000, _amount=1000000000000000000, _splitAddresses=[0xAFAFc7c39F09c115e525FF4Dd0Cd488f17e86c3D], _splitRatios=[100] )
  • SuperRareMarketplace.acceptOffer( _originContract=0xb932a70A57673d89f4acfFBE830E8ed7f75Fb9e0, _tokenId=39593, _currencyAddress=0x0000000000000000000000000000000000000000, _amount=1000000000000000000, _splitAddresses=[0xAFAFc7c39F09c115e525FF4Dd0Cd488f17e86c3D], _splitRatios=[100] )
    • SuperRareV2.ownerOf( tokenId=39593 ) => ( 0xAFAFc7c39F09c115e525FF4Dd0Cd488f17e86c3D )
    • SuperRareV2.ownerOf( tokenId=39593 ) => ( 0xAFAFc7c39F09c115e525FF4Dd0Cd488f17e86c3D )
    • SuperRareV2.isApprovedForAll( owner=0xAFAFc7c39F09c115e525FF4Dd0Cd488f17e86c3D, operator=0x6D7c44773C52D396F43c2D511B81aa168E9a7a42 ) => ( True )
    • SuperRareV2.safeTransferFrom( from=0xAFAFc7c39F09c115e525FF4Dd0Cd488f17e86c3D, to=0x478bb542f7658d635ABBA67edB987806DFF5b83d, tokenId=39593 )
    • MarketplaceSettingsV2.calculateMarketplaceFee( _amount=1000000000000000000 ) => ( 30000000000000000 )
    • ETH 0.03 Payments.payout( _splits=[0x860a80d33E85e97888F1f0C75c6e5BBD60b48DA9], _amounts=[30000000000000000] )
    • MarketplaceSettingsV2.hasERC721TokenSold( _contractAddress=0xb932a70A57673d89f4acfFBE830E8ed7f75Fb9e0, _tokenId=39593 ) => ( False )
      • 0x1634c3b0b39da13f8724361bdf295b607767b456.511bc84c( )
      • SpaceOperatorRegistry.isApprovedSpaceOperator( _operator=0xAFAFc7c39F09c115e525FF4Dd0Cd488f17e86c3D ) => ( False )
      • MarketplaceSettingsV2.getERC721ContractPrimarySaleFeePercentage( 0xb932a70A57673d89f4acfFBE830E8ed7f75Fb9e0 ) => ( 15 )
      • ETH 0.15 Payments.payout( _splits=[0x860a80d33E85e97888F1f0C75c6e5BBD60b48DA9], _amounts=[150000000000000000] )
      • ETH 0.85 Payments.payout( _splits=[0xAFAFc7c39F09c115e525FF4Dd0Cd488f17e86c3D], _amounts=[850000000000000000] )
        • ETH 0.85 0xafafc7c39f09c115e525ff4dd0cd488f17e86c3d.CALL( )
        • MarketplaceSettingsV2.markERC721Token( _contractAddress=0xb932a70A57673d89f4acfFBE830E8ed7f75Fb9e0, _tokenId=39593, _hasSold=True )
          • 0x1634c3b0b39da13f8724361bdf295b607767b456.5a3c652d( )
            File 1 of 8: SuperRareBazaar
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            import "@openzeppelin/contracts-upgradeable-0.7.2/access/OwnableUpgradeable.sol";
            import "@openzeppelin/contracts-upgradeable-0.7.2/utils/ReentrancyGuardUpgradeable.sol";
            import "@openzeppelin/contracts-0.7.2/token/ERC721/IERC721.sol";
            import "./storage/SuperRareBazaarStorage.sol";
            import "./ISuperRareBazaar.sol";
            /// @author koloz
            /// @title SuperRareBazaar
            /// @notice The unified contract for the bazaar logic (Marketplace and Auction House).
            /// @dev All storage is inherrited and append only (no modifications) to make upgrade compliant.
            contract SuperRareBazaar is
                ISuperRareBazaar,
                OwnableUpgradeable,
                ReentrancyGuardUpgradeable,
                SuperRareBazaarStorage
            {
                /////////////////////////////////////////////////////////////////////////
                // Initializer
                /////////////////////////////////////////////////////////////////////////
                function initialize(
                    address _marketplaceSettings,
                    address _royaltyRegistry,
                    address _royaltyEngine,
                    address _superRareMarketplace,
                    address _superRareAuctionHouse,
                    address _spaceOperatorRegistry,
                    address _approvedTokenRegistry,
                    address _payments,
                    address _stakingRegistry,
                    address _networkBeneficiary
                ) public initializer {
                    require(_marketplaceSettings != address(0));
                    require(_royaltyRegistry != address(0));
                    require(_royaltyEngine != address(0));
                    require(_superRareMarketplace != address(0));
                    require(_superRareAuctionHouse != address(0));
                    require(_spaceOperatorRegistry != address(0));
                    require(_approvedTokenRegistry != address(0));
                    require(_payments != address(0));
                    require(_networkBeneficiary != address(0));
                    marketplaceSettings = IMarketplaceSettings(_marketplaceSettings);
                    royaltyRegistry = IERC721CreatorRoyalty(_royaltyRegistry);
                    royaltyEngine = IRoyaltyEngineV1(_royaltyEngine);
                    superRareMarketplace = _superRareMarketplace;
                    superRareAuctionHouse = _superRareAuctionHouse;
                    spaceOperatorRegistry = ISpaceOperatorRegistry(_spaceOperatorRegistry);
                    approvedTokenRegistry = IApprovedTokenRegistry(_approvedTokenRegistry);
                    payments = IPayments(_payments);
                    stakingRegistry = _stakingRegistry;
                    networkBeneficiary = _networkBeneficiary;
                    minimumBidIncreasePercentage = 10;
                    maxAuctionLength = 7 days;
                    auctionLengthExtension = 15 minutes;
                    offerCancelationDelay = 5 minutes;
                    __Ownable_init();
                    __ReentrancyGuard_init();
                }
                /////////////////////////////////////////////////////////////////////////
                // Admin Functions
                /////////////////////////////////////////////////////////////////////////
                function setMarketplaceSettings(address _marketplaceSettings)
                    external
                    onlyOwner
                {
                    require(_marketplaceSettings != address(0));
                    marketplaceSettings = IMarketplaceSettings(_marketplaceSettings);
                }
                function setRoyaltyRegistry(address _royaltyRegistry) external onlyOwner {
                    require(_royaltyRegistry != address(0));
                    royaltyRegistry = IERC721CreatorRoyalty(_royaltyRegistry);
                }
                function setRoyaltyEngine(address _royaltyEngine) external onlyOwner {
                    require(_royaltyEngine != address(0));
                    royaltyEngine = IRoyaltyEngineV1(_royaltyEngine);
                }
                function setSuperRareMarketplace(address _superRareMarketplace)
                    external
                    onlyOwner
                {
                    require(_superRareMarketplace != address(0));
                    superRareMarketplace = _superRareMarketplace;
                }
                function setSuperRareAuctionHouse(address _superRareAuctionHouse)
                    external
                    onlyOwner
                {
                    require(_superRareAuctionHouse != address(0));
                    superRareAuctionHouse = _superRareAuctionHouse;
                }
                function setSpaceOperatorRegistry(address _spaceOperatorRegistry)
                    external
                    onlyOwner
                {
                    require(_spaceOperatorRegistry != address(0));
                    spaceOperatorRegistry = ISpaceOperatorRegistry(_spaceOperatorRegistry);
                }
                function setApprovedTokenRegistry(address _approvedTokenRegistry)
                    external
                    onlyOwner
                {
                    require(_approvedTokenRegistry != address(0));
                    approvedTokenRegistry = IApprovedTokenRegistry(_approvedTokenRegistry);
                }
                function setPayments(address _payments) external onlyOwner {
                    require(_payments != address(0));
                    payments = IPayments(_payments);
                }
                function setStakingRegistry(address _stakingRegistry) external onlyOwner {
                    require(_stakingRegistry != address(0));
                    stakingRegistry = _stakingRegistry;
                }
                function setNetworkBeneficiary(address _networkBeneficiary)
                    external
                    onlyOwner
                {
                    require(_networkBeneficiary != address(0));
                    networkBeneficiary = _networkBeneficiary;
                }
                function setMinimumBidIncreasePercentage(
                    uint8 _minimumBidIncreasePercentage
                ) external onlyOwner {
                    minimumBidIncreasePercentage = _minimumBidIncreasePercentage;
                }
                function setMaxAuctionLength(uint8 _maxAuctionLength) external onlyOwner {
                    maxAuctionLength = _maxAuctionLength;
                }
                function setAuctionLengthExtension(uint256 _auctionLengthExtension)
                    external
                    onlyOwner
                {
                    auctionLengthExtension = _auctionLengthExtension;
                }
                function setOfferCancelationDelay(uint256 _offerCancelationDelay)
                    external
                    onlyOwner
                {
                    offerCancelationDelay = _offerCancelationDelay;
                }
                /////////////////////////////////////////////////////////////////////////
                // Marketplace Functions
                /////////////////////////////////////////////////////////////////////////
                /// @notice Place an offer for a given asset
                /// @dev Notice we need to verify that the msg sender has approved us to move funds on their behalf.
                /// @dev Covers use of any currency (0 address is eth).
                /// @dev _amount is the amount of the offer excluding the marketplace fee.
                /// @dev There can be multiple offers of different currencies, but only 1 per currency.
                /// @param _originContract Contract address of the asset being listed.
                /// @param _tokenId Token Id of the asset.
                /// @param _currencyAddress Address of the token being offered.
                /// @param _amount Amount being offered.
                /// @param _convertible If the offer can be converted into an auction
                function offer(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount,
                    bool _convertible
                ) external payable override {
                    (bool success, bytes memory data) = superRareMarketplace.delegatecall(
                        abi.encodeWithSelector(
                            this.offer.selector,
                            _originContract,
                            _tokenId,
                            _currencyAddress,
                            _amount,
                            _convertible
                        )
                    );
                    require(success, string(data));
                }
                /// @notice Purchases the token for the current sale price.
                /// @dev Covers use of any currency (0 address is eth).
                /// @dev Need to verify that the buyer (if not using eth) has the marketplace approved for _currencyContract.
                /// @dev Need to verify that the seller has the marketplace approved for _originContract.
                /// @param _originContract Contract address for asset being bought.
                /// @param _tokenId TokenId of asset being bought.
                /// @param _currencyAddress Currency address of asset being used to buy.
                /// @param _amount Amount the piece if being bought for (including marketplace fee).
                function buy(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount
                ) external payable override {
                    (bool success, bytes memory data) = superRareMarketplace.delegatecall(
                        abi.encodeWithSelector(
                            this.buy.selector,
                            _originContract,
                            _tokenId,
                            _currencyAddress,
                            _amount
                        )
                    );
                    require(success, string(data));
                }
                /// @notice Cancels an existing offer the sender has placed on a piece.
                /// @param _originContract Contract address of token.
                /// @param _tokenId TokenId that has an offer.
                /// @param _currencyAddress Currency address of the offer.
                function cancelOffer(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress
                ) external override {
                    (bool success, bytes memory data) = superRareMarketplace.delegatecall(
                        abi.encodeWithSelector(
                            this.cancelOffer.selector,
                            _originContract,
                            _tokenId,
                            _currencyAddress
                        )
                    );
                    require(success, string(data));
                }
                /// @notice Sets a sale price for the given asset(s) directed at the _target address.
                /// @dev Covers use of any currency (0 address is eth).
                /// @dev Sale price for everyone is denoted as the 0 address.
                /// @dev Only 1 currency can be used for the sale price directed at a speicific target.
                /// @dev _listPrice of 0 signifies removing the list price for the provided currency.
                /// @dev This function can be used for counter offers as well.
                /// @param _originContract Contract address of the asset being listed.
                /// @param _tokenId Token Id of the asset.
                /// @param _currencyAddress Contract address of the currency asset is being listed for.
                /// @param _listPrice Amount of the currency the asset is being listed for (including all decimal points).
                /// @param _target Address of the person this sale price is target to.
                /// @param _splitAddresses Addresses to split the sellers commission with.
                /// @param _splitRatios The ratio for the split corresponding to each of the addresses being split with.
                function setSalePrice(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _listPrice,
                    address _target,
                    address payable[] calldata _splitAddresses,
                    uint8[] calldata _splitRatios
                ) external override {
                    (bool success, bytes memory data) = superRareMarketplace.delegatecall(
                        abi.encodeWithSelector(
                            this.setSalePrice.selector,
                            _originContract,
                            _tokenId,
                            _currencyAddress,
                            _listPrice,
                            _target,
                            _splitAddresses,
                            _splitRatios
                        )
                    );
                    require(success, string(data));
                }
                /// @notice Removes the current sale price of an asset for _target for the given currency.
                /// @dev Sale prices could still exist for different currencies.
                /// @dev Sale prices could still exist for different targets.
                /// @dev Zero address for _currency means that its listed in ether.
                /// @dev _target of zero address is the general sale price.
                /// @param _originContract The origin contract of the asset.
                /// @param _tokenId The tokenId of the asset within the _originContract.
                /// @param _target The address of the person
                function removeSalePrice(
                    address _originContract,
                    uint256 _tokenId,
                    address _target
                ) external override {
                    IERC721 erc721 = IERC721(_originContract);
                    address tokenOwner = erc721.ownerOf(_tokenId);
                    require(
                        msg.sender == tokenOwner,
                        "removeSalePrice::Must be tokenOwner."
                    );
                    delete tokenSalePrices[_originContract][_tokenId][_target];
                    emit SetSalePrice(
                        _originContract,
                        address(0),
                        address(0),
                        0,
                        _tokenId,
                        new address payable[](0),
                        new uint8[](0)
                    );
                }
                /// @notice Accept an offer placed on _originContract : _tokenId.
                /// @dev Zero address for _currency means that the offer being accepted is in ether.
                /// @param _originContract Contract of the asset the offer was made on.
                /// @param _tokenId TokenId of the asset.
                /// @param _currencyAddress Address of the currency used for the offer.
                /// @param _amount Amount the offer was for/and is being accepted.
                /// @param _splitAddresses Addresses to split the sellers commission with.
                /// @param _splitRatios The ratio for the split corresponding to each of the addresses being split with.
                function acceptOffer(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount,
                    address payable[] calldata _splitAddresses,
                    uint8[] calldata _splitRatios
                ) external override {
                    (bool success, bytes memory data) = superRareMarketplace.delegatecall(
                        abi.encodeWithSelector(
                            this.acceptOffer.selector,
                            _originContract,
                            _tokenId,
                            _currencyAddress,
                            _amount,
                            _splitAddresses,
                            _splitRatios
                        )
                    );
                    require(success, string(data));
                }
                /////////////////////////////////////////////////////////////////////////
                // Auction House Functions
                /////////////////////////////////////////////////////////////////////////
                /// @notice Configures an Auction for a given asset.
                /// @dev If auction type is coldie (reserve) then _startingAmount cant be 0.
                /// @dev _currencyAddress equal to the zero address denotes eth.
                /// @dev All time related params are unix epoch timestamps.
                /// @param _auctionType The type of auction being configured.
                /// @param _originContract Contract address of the asset being put up for auction.
                /// @param _tokenId Token Id of the asset.
                /// @param _startingAmount The reserve price or min bid of an auction.
                /// @param _currencyAddress The currency the auction is being conducted in.
                /// @param _lengthOfAuction The amount of time in seconds that the auction is configured for.
                /// @param _splitAddresses Addresses to split the sellers commission with.
                /// @param _splitRatios The ratio for the split corresponding to each of the addresses being split with.
                function configureAuction(
                    bytes32 _auctionType,
                    address _originContract,
                    uint256 _tokenId,
                    uint256 _startingAmount,
                    address _currencyAddress,
                    uint256 _lengthOfAuction,
                    uint256 _startTime,
                    address payable[] calldata _splitAddresses,
                    uint8[] calldata _splitRatios
                ) external override {
                    (bool success, bytes memory data) = superRareAuctionHouse.delegatecall(
                        abi.encodeWithSelector(
                            this.configureAuction.selector,
                            _auctionType,
                            _originContract,
                            _tokenId,
                            _startingAmount,
                            _currencyAddress,
                            _lengthOfAuction,
                            _startTime,
                            _splitAddresses,
                            _splitRatios
                        )
                    );
                    require(success, string(data));
                }
                /// @notice Converts an offer into a coldie auction.
                /// @dev Covers use of any currency (0 address is eth).
                /// @dev Only covers converting an offer to a coldie auction.
                /// @dev Cant convert offer if an auction currently exists.
                /// @param _originContract Contract address of the asset.
                /// @param _tokenId Token Id of the asset.
                /// @param _currencyAddress Address of the currency being converted.
                /// @param _amount Amount being converted into an auction.
                /// @param _lengthOfAuction Number of seconds the auction will last.
                /// @param _splitAddresses Addresses that the sellers take in will be split amongst.
                /// @param _splitRatios Ratios that the take in will be split by.
                function convertOfferToAuction(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount,
                    uint256 _lengthOfAuction,
                    address payable[] calldata _splitAddresses,
                    uint8[] calldata _splitRatios
                ) external override {
                    (bool success, bytes memory data) = superRareAuctionHouse.delegatecall(
                        abi.encodeWithSelector(
                            this.convertOfferToAuction.selector,
                            _originContract,
                            _tokenId,
                            _currencyAddress,
                            _amount,
                            _lengthOfAuction,
                            _splitAddresses,
                            _splitRatios
                        )
                    );
                    require(success, string(data));
                }
                /// @notice Cancels a configured Auction that has not started.
                /// @dev Requires the person sending the message to be the auction creator or token owner.
                /// @param _originContract Contract address of the asset pending auction.
                /// @param _tokenId Token Id of the asset.
                function cancelAuction(address _originContract, uint256 _tokenId)
                    external
                    override
                {
                    (bool success, bytes memory data) = superRareAuctionHouse.delegatecall(
                        abi.encodeWithSelector(
                            this.cancelAuction.selector,
                            _originContract,
                            _tokenId
                        )
                    );
                    require(success, string(data));
                }
                /// @notice Places a bid on a valid auction.
                /// @dev Only the configured currency can be used (Zero address for eth)
                /// @param _originContract Contract address of asset being bid on.
                /// @param _tokenId Token Id of the asset.
                /// @param _currencyAddress Address of currency being used to bid.
                /// @param _amount Amount of the currency being used for the bid.
                function bid(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount
                ) external payable override {
                    (bool success, bytes memory data) = superRareAuctionHouse.delegatecall(
                        abi.encodeWithSelector(
                            this.bid.selector,
                            _originContract,
                            _tokenId,
                            _currencyAddress,
                            _amount
                        )
                    );
                    require(success, string(data));
                }
                /// @notice Settles an auction that has ended.
                /// @dev Anyone is able to settle an auction since non-input params are used.
                /// @param _originContract Contract address of asset.
                /// @param _tokenId Token Id of the asset.
                function settleAuction(address _originContract, uint256 _tokenId)
                    external
                    override
                {
                    (bool success, bytes memory data) = superRareAuctionHouse.delegatecall(
                        abi.encodeWithSelector(
                            this.settleAuction.selector,
                            _originContract,
                            _tokenId
                        )
                    );
                    require(success, string(data));
                }
                /// @notice Grabs the current auction details for a token.
                /// @param _originContract Contract address of asset.
                /// @param _tokenId Token Id of the asset.
                /** @return Auction Struct: creatorAddress, creationTime, startingTime, lengthOfAuction,
                            currencyAddress, minimumBid, auctionType, splitRecipients array, and splitRatios array.
                */
                function getAuctionDetails(address _originContract, uint256 _tokenId)
                    external
                    view
                    override
                    returns (
                        address,
                        uint256,
                        uint256,
                        uint256,
                        address,
                        uint256,
                        bytes32,
                        address payable[] memory,
                        uint8[] memory
                    )
                {
                    Auction memory auction = tokenAuctions[_originContract][_tokenId];
                    return (
                        auction.auctionCreator,
                        auction.creationBlock,
                        auction.startingTime,
                        auction.lengthOfAuction,
                        auction.currencyAddress,
                        auction.minimumBid,
                        auction.auctionType,
                        auction.splitRecipients,
                        auction.splitRatios
                    );
                }
                function getSalePrice(
                    address _originContract,
                    uint256 _tokenId,
                    address _target
                )
                    external
                    view
                    override
                    returns (
                        address,
                        address,
                        uint256,
                        address payable[] memory,
                        uint8[] memory
                    )
                {
                    SalePrice memory sp = tokenSalePrices[_originContract][_tokenId][
                        _target
                    ];
                    return (
                        sp.seller,
                        sp.currencyAddress,
                        sp.amount,
                        sp.splitRecipients,
                        sp.splitRatios
                    );
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity ^0.7.0;
            import "../utils/ContextUpgradeable.sol";
            import "../proxy/Initializable.sol";
            /**
             * @dev Contract module which provides a basic access control mechanism, where
             * there is an account (an owner) that can be granted exclusive access to
             * specific functions.
             *
             * By default, the owner account will be the one that deploys the contract. This
             * can later be changed with {transferOwnership}.
             *
             * This module is used through inheritance. It will make available the modifier
             * `onlyOwner`, which can be applied to your functions to restrict their use to
             * the owner.
             */
            abstract contract OwnableUpgradeable is Initializable, ContextUpgradeable {
                address private _owner;
                event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
                /**
                 * @dev Initializes the contract setting the deployer as the initial owner.
                 */
                function __Ownable_init() internal initializer {
                    __Context_init_unchained();
                    __Ownable_init_unchained();
                }
                function __Ownable_init_unchained() internal initializer {
                    address msgSender = _msgSender();
                    _owner = msgSender;
                    emit OwnershipTransferred(address(0), msgSender);
                }
                /**
                 * @dev Returns the address of the current owner.
                 */
                function owner() public view virtual returns (address) {
                    return _owner;
                }
                /**
                 * @dev Throws if called by any account other than the owner.
                 */
                modifier onlyOwner() {
                    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 {
                    emit OwnershipTransferred(_owner, address(0));
                    _owner = 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");
                    emit OwnershipTransferred(_owner, newOwner);
                    _owner = newOwner;
                }
                uint256[49] private __gap;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity ^0.7.0;
            import "../proxy/Initializable.sol";
            /**
             * @dev Contract module that helps prevent reentrant calls to a function.
             *
             * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
             * available, which can be applied to functions to make sure there are no nested
             * (reentrant) calls to them.
             *
             * Note that because there is a single `nonReentrant` guard, functions marked as
             * `nonReentrant` may not call one another. This can be worked around by making
             * those functions `private`, and then adding `external` `nonReentrant` entry
             * points to them.
             *
             * TIP: If you would like to learn more about reentrancy and alternative ways
             * to protect against it, check out our blog post
             * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
             */
            abstract contract ReentrancyGuardUpgradeable is Initializable {
                // Booleans are more expensive than uint256 or any type that takes up a full
                // word because each write operation emits an extra SLOAD to first read the
                // slot's contents, replace the bits taken up by the boolean, and then write
                // back. This is the compiler's defense against contract upgrades and
                // pointer aliasing, and it cannot be disabled.
                // The values being non-zero value makes deployment a bit more expensive,
                // but in exchange the refund on every call to nonReentrant will be lower in
                // amount. Since refunds are capped to a percentage of the total
                // transaction's gas, it is best to keep them low in cases like this one, to
                // increase the likelihood of the full refund coming into effect.
                uint256 private constant _NOT_ENTERED = 1;
                uint256 private constant _ENTERED = 2;
                uint256 private _status;
                function __ReentrancyGuard_init() internal initializer {
                    __ReentrancyGuard_init_unchained();
                }
                function __ReentrancyGuard_init_unchained() internal initializer {
                    _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 make it call a
                 * `private` function that does the actual work.
                 */
                modifier nonReentrant() {
                    // On the first call to nonReentrant, _notEntered will be true
                    require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
                    // Any calls to nonReentrant after this point will fail
                    _status = _ENTERED;
                    _;
                    // By storing the original value once again, a refund is triggered (see
                    // https://eips.ethereum.org/EIPS/eip-2200)
                    _status = _NOT_ENTERED;
                }
                uint256[49] private __gap;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.2 <0.8.0;
            import "../../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`, checking first that contract recipients
                 * are aware of the ERC721 protocol to prevent tokens from being forever locked.
                 *
                 * Requirements:
                 *
                 * - `from` cannot be the zero address.
                 * - `to` cannot be the zero address.
                 * - `tokenId` token must exist and be owned by `from`.
                 * - If the caller is not `from`, it must be have been allowed to move this token by either {approve} or {setApprovalForAll}.
                 * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
                 *
                 * Emits a {Transfer} event.
                 */
                function safeTransferFrom(address from, address to, uint256 tokenId) external;
                /**
                 * @dev Transfers `tokenId` token from `from` to `to`.
                 *
                 * WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
                 *
                 * Requirements:
                 *
                 * - `from` cannot be the zero address.
                 * - `to` cannot be the zero address.
                 * - `tokenId` token must be owned by `from`.
                 * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
                 *
                 * Emits a {Transfer} event.
                 */
                function transferFrom(address from, address to, uint256 tokenId) external;
                /**
                 * @dev Gives permission to `to` to transfer `tokenId` token to another account.
                 * The approval is cleared when the token is transferred.
                 *
                 * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
                 *
                 * Requirements:
                 *
                 * - The caller must own the token or be an approved operator.
                 * - `tokenId` must exist.
                 *
                 * Emits an {Approval} event.
                 */
                function approve(address to, uint256 tokenId) external;
                /**
                 * @dev Returns the account approved for `tokenId` token.
                 *
                 * Requirements:
                 *
                 * - `tokenId` must exist.
                 */
                function getApproved(uint256 tokenId) external view returns (address operator);
                /**
                 * @dev Approve or remove `operator` as an operator for the caller.
                 * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
                 *
                 * Requirements:
                 *
                 * - The `operator` cannot be the caller.
                 *
                 * Emits an {ApprovalForAll} event.
                 */
                function setApprovalForAll(address operator, bool _approved) external;
                /**
                 * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
                 *
                 * See {setApprovalForAll}
                 */
                function isApprovedForAll(address owner, address operator) external view returns (bool);
                /**
                  * @dev Safely transfers `tokenId` token from `from` to `to`.
                  *
                  * Requirements:
                  *
                  * - `from` cannot be the zero address.
                  * - `to` cannot be the zero address.
                  * - `tokenId` token must exist and be owned by `from`.
                  * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
                  * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
                  *
                  * Emits a {Transfer} event.
                  */
                function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            import "../../marketplace/IMarketplaceSettings.sol";
            import "../../royalty/creator/IERC721CreatorRoyalty.sol";
            import "../../payments/IPayments.sol";
            import "../../registry/spaces/ISpaceOperatorRegistry.sol";
            import "../../registry/token/IApprovedTokenRegistry.sol";
            import "../../royalty/creator/IRoyaltyEngine.sol";
            /// @author koloz
            /// @title SuperRareBazaar Storage Contract
            /// @dev STORAGE CAN ONLY BE APPENDED NOT INSERTED OR MODIFIED
            contract SuperRareBazaarStorage {
                /////////////////////////////////////////////////////////////////////////
                // Constants
                /////////////////////////////////////////////////////////////////////////
                // Auction Types
                bytes32 public constant COLDIE_AUCTION = "COLDIE_AUCTION";
                bytes32 public constant SCHEDULED_AUCTION = "SCHEDULED_AUCTION";
                bytes32 public constant NO_AUCTION = bytes32(0);
                /////////////////////////////////////////////////////////////////////////
                // Structs
                /////////////////////////////////////////////////////////////////////////
                // The Offer truct for a given token:
                // buyer - address of person making the offer
                // currencyAddress - address of the erc20 token used for an offer
                //                   or the zero address for eth
                // amount - offer in wei/full erc20 value
                // marketplaceFee - the amount that is taken by the network on offer acceptance.
                struct Offer {
                    address payable buyer;
                    uint256 amount;
                    uint256 timestamp;
                    uint8 marketplaceFee;
                    bool convertible;
                }
                // The Sale Price struct for a given token:
                // seller - address of the person selling the token
                // currencyAddress - address of the erc20 token used for an offer
                //                   or the zero address for eth
                // amount - offer in wei/full erc20 value
                struct SalePrice {
                    address payable seller;
                    address currencyAddress;
                    uint256 amount;
                    address payable[] splitRecipients;
                    uint8[] splitRatios;
                }
                // Structure of an Auction:
                // auctionCreator - creator of the auction
                // creationBlock - time that the auction was created/configured
                // startingBlock - time that the auction starts on
                // lengthOfAuction - how long the auction is
                // currencyAddress - address of the erc20 token used for an offer
                //                   or the zero address for eth
                // minimumBid - min amount a bidder can bid at the start of an auction.
                // auctionType - type of auction, represented as the formatted bytes 32 string
                struct Auction {
                    address payable auctionCreator;
                    uint256 creationBlock;
                    uint256 startingTime;
                    uint256 lengthOfAuction;
                    address currencyAddress;
                    uint256 minimumBid;
                    bytes32 auctionType;
                    address payable[] splitRecipients;
                    uint8[] splitRatios;
                }
                struct Bid {
                    address payable bidder;
                    address currencyAddress;
                    uint256 amount;
                    uint8 marketplaceFee;
                }
                /////////////////////////////////////////////////////////////////////////
                // Events
                /////////////////////////////////////////////////////////////////////////
                event Sold(
                    address indexed _originContract,
                    address indexed _buyer,
                    address indexed _seller,
                    address _currencyAddress,
                    uint256 _amount,
                    uint256 _tokenId
                );
                event SetSalePrice(
                    address indexed _originContract,
                    address indexed _currencyAddress,
                    address _target,
                    uint256 _amount,
                    uint256 _tokenId,
                    address payable[] _splitRecipients,
                    uint8[] _splitRatios
                );
                event OfferPlaced(
                    address indexed _originContract,
                    address indexed _bidder,
                    address indexed _currencyAddress,
                    uint256 _amount,
                    uint256 _tokenId,
                    bool _convertible
                );
                event AcceptOffer(
                    address indexed _originContract,
                    address indexed _bidder,
                    address indexed _seller,
                    address _currencyAddress,
                    uint256 _amount,
                    uint256 _tokenId,
                    address payable[] _splitAddresses,
                    uint8[] _splitRatios
                );
                event CancelOffer(
                    address indexed _originContract,
                    address indexed _bidder,
                    address indexed _currencyAddress,
                    uint256 _amount,
                    uint256 _tokenId
                );
                event NewAuction(
                    address indexed _contractAddress,
                    uint256 indexed _tokenId,
                    address indexed _auctionCreator,
                    address _currencyAddress,
                    uint256 _startingTime,
                    uint256 _minimumBid,
                    uint256 _lengthOfAuction
                );
                event CancelAuction(
                    address indexed _contractAddress,
                    uint256 indexed _tokenId,
                    address indexed _auctionCreator
                );
                event AuctionBid(
                    address indexed _contractAddress,
                    address indexed _bidder,
                    uint256 indexed _tokenId,
                    address _currencyAddress,
                    uint256 _amount,
                    bool _startedAuction,
                    uint256 _newAuctionLength,
                    address _previousBidder
                );
                event AuctionSettled(
                    address indexed _contractAddress,
                    address indexed _bidder,
                    address _seller,
                    uint256 indexed _tokenId,
                    address _currencyAddress,
                    uint256 _amount
                );
                /////////////////////////////////////////////////////////////////////////
                // State Variables
                /////////////////////////////////////////////////////////////////////////
                // Current marketplace settings implementation to be used
                IMarketplaceSettings public marketplaceSettings;
                // Current creator royalty implementation to be used
                IERC721CreatorRoyalty public royaltyRegistry;
                // Address of the global royalty engine being used.
                IRoyaltyEngineV1 public royaltyEngine;
                // Current SuperRareMarketplace implementation to be used
                address public superRareMarketplace;
                // Current SuperRareAuctionHouse implementation to be used
                address public superRareAuctionHouse;
                // Current SpaceOperatorRegistry implementation to be used.
                ISpaceOperatorRegistry public spaceOperatorRegistry;
                // Current ApprovedTokenRegistry implementation being used for currencies.
                IApprovedTokenRegistry public approvedTokenRegistry;
                // Current payments contract to use
                IPayments public payments;
                // Address to be used for staking registry.
                address public stakingRegistry;
                // Address of the network beneficiary
                address public networkBeneficiary;
                // A minimum increase in bid amount when out bidding someone.
                uint8 public minimumBidIncreasePercentage; // 10 = 10%
                // Maximum length that an auction can be.
                uint256 public maxAuctionLength;
                // Extension length for an auction
                uint256 public auctionLengthExtension;
                // Offer cancellation delay
                uint256 public offerCancelationDelay;
                // Mapping from contract to mapping of tokenId to mapping of target to sale price.
                mapping(address => mapping(uint256 => mapping(address => SalePrice)))
                    public tokenSalePrices;
                // Mapping from contract to mapping of tokenId to mapping of currency address to Current Offer.
                mapping(address => mapping(uint256 => mapping(address => Offer)))
                    public tokenCurrentOffers;
                // Mapping from contract to mapping of tokenId to Auction.
                mapping(address => mapping(uint256 => Auction)) public tokenAuctions;
                // Mapping from contract to mapping of tokenId to Bid.
                mapping(address => mapping(uint256 => Bid)) public auctionBids;
                uint256[50] private __gap;
                /// ALL NEW STORAGE MUST COME AFTER THIS
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            /// @author koloz
            /// @title ISuperRareBazaar
            /// @notice Interface for the SuperRareBazaar Contract
            interface ISuperRareBazaar {
                // Marketplace Functions
                // Buyer
                /// @notice Create an offer for a given asset
                /// @param _originContract Contract address of the asset being listed.
                /// @param _tokenId Token Id of the asset.
                /// @param _currencyAddress Address of the token being offered.
                /// @param _amount Amount being offered.
                /// @param _convertible If the offer can be converted into an auction
                function offer(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount,
                    bool _convertible
                ) external payable;
                /// @notice Purchases the token for the current sale price.
                /// @param _originContract Contract address for asset being bought.
                /// @param _tokenId TokenId of asset being bought.
                /// @param _currencyAddress Currency address of asset being used to buy.
                /// @param _amount Amount the piece if being bought for.
                function buy(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount
                ) external payable;
                /// @notice Cancels an existing offer the sender has placed on a piece.
                /// @param _originContract Contract address of token.
                /// @param _tokenId TokenId that has an offer.
                /// @param _currencyAddress Currency address of the offer.
                function cancelOffer(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress
                ) external;
                // Seller
                /// @notice Sets a sale price for the given asset(s).
                /// @param _originContract Contract address of the asset being listed.
                /// @param _tokenId Token Id of the asset.
                /// @param _currencyAddress Contract address of the currency asset is being listed for.
                /// @param _listPrice Amount of the currency the asset is being listed for (including all decimal points).
                /// @param _target Address of the person this sale price is target to.
                /// @param _splitAddresses Addresses to split the sellers commission with.
                /// @param _splitRatios The ratio for the split corresponding to each of the addresses being split with.
                function setSalePrice(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _listPrice,
                    address _target,
                    address payable[] calldata _splitAddresses,
                    uint8[] calldata _splitRatios
                ) external;
                /// @notice Removes the current sale price of an asset for the given currency.
                /// @param _originContract The origin contract of the asset.
                /// @param _tokenId The tokenId of the asset within the _originContract.
                /// @param _target The address of the person
                function removeSalePrice(
                    address _originContract,
                    uint256 _tokenId,
                    address _target
                ) external;
                /// @notice Accept an offer placed on _originContract : _tokenId.
                /// @param _originContract Contract of the asset the offer was made on.
                /// @param _tokenId TokenId of the asset.
                /// @param _currencyAddress Address of the currency used for the offer.
                /// @param _amount Amount the offer was for/and is being accepted.
                /// @param _splitAddresses Addresses to split the sellers commission with.
                /// @param _splitRatios The ratio for the split corresponding to each of the addresses being split with.
                function acceptOffer(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount,
                    address payable[] calldata _splitAddresses,
                    uint8[] calldata _splitRatios
                ) external;
                // Auction House
                // Anyone
                /// @notice Settles an auction that has ended.
                /// @param _originContract Contract address of asset.
                /// @param _tokenId Token Id of the asset.
                function settleAuction(address _originContract, uint256 _tokenId) external;
                // Buyer
                /// @notice Places a bid on a valid auction.
                /// @param _originContract Contract address of asset being bid on.
                /// @param _tokenId Token Id of the asset.
                /// @param _currencyAddress Address of currency being used to bid.
                /// @param _amount Amount of the currency being used for the bid.
                function bid(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount
                ) external payable;
                // Seller
                /// @notice Configures an Auction for a given asset.
                /// @param _auctionType The type of auction being configured.
                /// @param _originContract Contract address of the asset being put up for auction.
                /// @param _tokenId Token Id of the asset.
                /// @param _startingAmount The reserve price or min bid of an auction.
                /// @param _currencyAddress The currency the auction is being conducted in.
                /// @param _lengthOfAuction The amount of time in seconds that the auction is configured for.
                /// @param _splitAddresses Addresses to split the sellers commission with.
                /// @param _splitRatios The ratio for the split corresponding to each of the addresses being split with.
                function configureAuction(
                    bytes32 _auctionType,
                    address _originContract,
                    uint256 _tokenId,
                    uint256 _startingAmount,
                    address _currencyAddress,
                    uint256 _lengthOfAuction,
                    uint256 _startTime,
                    address payable[] calldata _splitAddresses,
                    uint8[] calldata _splitRatios
                ) external;
                /// @notice Cancels a configured Auction that has not started.
                /// @param _originContract Contract address of the asset pending auction.
                /// @param _tokenId Token Id of the asset.
                function cancelAuction(address _originContract, uint256 _tokenId) external;
                /// @notice Converts an offer into a coldie auction.
                /// @param _originContract Contract address of the asset.
                /// @param _tokenId Token Id of the asset.
                /// @param _currencyAddress Address of the currency being converted.
                /// @param _amount Amount being converted into an auction.
                /// @param _lengthOfAuction Number of seconds the auction will last.
                /// @param _splitAddresses Addresses that the sellers take in will be split amongst.
                /// @param _splitRatios Ratios that the take in will be split by.
                function convertOfferToAuction(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount,
                    uint256 _lengthOfAuction,
                    address payable[] calldata _splitAddresses,
                    uint8[] calldata _splitRatios
                ) external;
                /// @notice Grabs the current auction details for a token.
                /// @param _originContract Contract address of asset.
                /// @param _tokenId Token Id of the asset.
                /** @return Auction Struct: creatorAddress, creationTime, startingTime, lengthOfAuction,
                            currencyAddress, minimumBid, auctionType, splitRecipients array, and splitRatios array.
                */
                function getAuctionDetails(address _originContract, uint256 _tokenId)
                    external
                    view
                    returns (
                        address,
                        uint256,
                        uint256,
                        uint256,
                        address,
                        uint256,
                        bytes32,
                        address payable[] calldata,
                        uint8[] calldata
                    );
                function getSalePrice(
                    address _originContract,
                    uint256 _tokenId,
                    address _target
                )
                    external
                    view
                    returns (
                        address,
                        address,
                        uint256,
                        address payable[] memory,
                        uint8[] memory
                    );
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.0 <0.8.0;
            import "../proxy/Initializable.sol";
            /*
             * @dev Provides information about the current execution context, including the
             * sender of the transaction and its data. While these are generally available
             * via msg.sender and msg.data, they should not be accessed in such a direct
             * manner, since when dealing with GSN meta-transactions the account sending and
             * paying for execution may not be the actual sender (as far as an application
             * is concerned).
             *
             * This contract is only required for intermediate, library-like contracts.
             */
            abstract contract ContextUpgradeable is Initializable {
                function __Context_init() internal initializer {
                    __Context_init_unchained();
                }
                function __Context_init_unchained() internal initializer {
                }
                function _msgSender() internal view virtual returns (address payable) {
                    return msg.sender;
                }
                function _msgData() internal view virtual returns (bytes memory) {
                    this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
                    return msg.data;
                }
                uint256[50] private __gap;
            }
            // SPDX-License-Identifier: MIT
            // solhint-disable-next-line compiler-version
            pragma solidity >=0.4.24 <0.8.0;
            import "../utils/AddressUpgradeable.sol";
            /**
             * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
             * behind a proxy. Since a proxied contract can't have a constructor, it's common to move constructor logic to an
             * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
             * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
             *
             * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
             * possible by providing the encoded function call as the `_data` argument to {UpgradeableProxy-constructor}.
             *
             * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
             * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
             */
            abstract contract Initializable {
                /**
                 * @dev Indicates that the contract has been initialized.
                 */
                bool private _initialized;
                /**
                 * @dev Indicates that the contract is in the process of being initialized.
                 */
                bool private _initializing;
                /**
                 * @dev Modifier to protect an initializer function from being invoked twice.
                 */
                modifier initializer() {
                    require(_initializing || _isConstructor() || !_initialized, "Initializable: contract is already initialized");
                    bool isTopLevelCall = !_initializing;
                    if (isTopLevelCall) {
                        _initializing = true;
                        _initialized = true;
                    }
                    _;
                    if (isTopLevelCall) {
                        _initializing = false;
                    }
                }
                /// @dev Returns true if and only if the function is running in the constructor
                function _isConstructor() private view returns (bool) {
                    return !AddressUpgradeable.isContract(address(this));
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity ^0.7.0;
            /**
             * @dev Collection of functions related to the address type
             */
            library AddressUpgradeable {
                /**
                 * @dev Returns true if `account` is a contract.
                 *
                 * [IMPORTANT]
                 * ====
                 * It is unsafe to assume that an address for which this function returns
                 * false is an externally-owned account (EOA) and not a contract.
                 *
                 * Among others, `isContract` will return false for the following
                 * types of addresses:
                 *
                 *  - an externally-owned account
                 *  - a contract in construction
                 *  - an address where a contract will be created
                 *  - an address where a contract lived, but was destroyed
                 * ====
                 */
                function isContract(address account) internal view returns (bool) {
                    // This method relies on extcodesize, which returns 0 for contracts in
                    // construction, since the code is only stored at the end of the
                    // constructor execution.
                    uint256 size;
                    // solhint-disable-next-line no-inline-assembly
                    assembly { size := extcodesize(account) }
                    return size > 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");
                    // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
                    (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");
                    // solhint-disable-next-line avoid-low-level-calls
                    (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");
                    // solhint-disable-next-line avoid-low-level-calls
                    (bool success, bytes memory returndata) = target.staticcall(data);
                    return _verifyCallResult(success, returndata, errorMessage);
                }
                function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private 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
                            // solhint-disable-next-line no-inline-assembly
                            assembly {
                                let returndata_size := mload(returndata)
                                revert(add(32, returndata), returndata_size)
                            }
                        } else {
                            revert(errorMessage);
                        }
                    }
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.0 <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);
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            /**
             * @title IMarketplaceSettings Settings governing a marketplace.
             */
            interface IMarketplaceSettings {
                /////////////////////////////////////////////////////////////////////////
                // Marketplace Min and Max Values
                /////////////////////////////////////////////////////////////////////////
                /**
                 * @dev Get the max value to be used with the marketplace.
                 * @return uint256 wei value.
                 */
                function getMarketplaceMaxValue() external view returns (uint256);
                /**
                 * @dev Get the max value to be used with the marketplace.
                 * @return uint256 wei value.
                 */
                function getMarketplaceMinValue() external view returns (uint256);
                /////////////////////////////////////////////////////////////////////////
                // Marketplace Fee
                /////////////////////////////////////////////////////////////////////////
                /**
                 * @dev Get the marketplace fee percentage.
                 * @return uint8 wei fee.
                 */
                function getMarketplaceFeePercentage() external view returns (uint8);
                /**
                 * @dev Utility function for calculating the marketplace fee for given amount of wei.
                 * @param _amount uint256 wei amount.
                 * @return uint256 wei fee.
                 */
                function calculateMarketplaceFee(uint256 _amount)
                    external
                    view
                    returns (uint256);
                /////////////////////////////////////////////////////////////////////////
                // Primary Sale Fee
                /////////////////////////////////////////////////////////////////////////
                /**
                 * @dev Get the primary sale fee percentage for a specific ERC721 contract.
                 * @param _contractAddress address ERC721Contract address.
                 * @return uint8 wei primary sale fee.
                 */
                function getERC721ContractPrimarySaleFeePercentage(address _contractAddress)
                    external
                    view
                    returns (uint8);
                /**
                 * @dev Utility function for calculating the primary sale fee for given amount of wei
                 * @param _contractAddress address ERC721Contract address.
                 * @param _amount uint256 wei amount.
                 * @return uint256 wei fee.
                 */
                function calculatePrimarySaleFee(address _contractAddress, uint256 _amount)
                    external
                    view
                    returns (uint256);
                /**
                 * @dev Check whether the ERC721 token has sold at least once.
                 * @param _contractAddress address ERC721Contract address.
                 * @param _tokenId uint256 token ID.
                 * @return bool of whether the token has sold.
                 */
                function hasERC721TokenSold(address _contractAddress, uint256 _tokenId)
                    external
                    view
                    returns (bool);
                /**
                 * @dev Mark a token as sold.
                 * Requirements:
                 *
                 * - `_contractAddress` cannot be the zero address.
                 * @param _contractAddress address ERC721Contract address.
                 * @param _tokenId uint256 token ID.
                 * @param _hasSold bool of whether the token should be marked sold or not.
                 */
                function markERC721Token(
                    address _contractAddress,
                    uint256 _tokenId,
                    bool _hasSold
                ) external;
                function setERC721ContractPrimarySaleFeePercentage(
                    address _contractAddress,
                    uint8 _percentage
                ) external;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            import "../../token/ERC721/IERC721TokenCreator.sol";
            /**
             * @title IERC721CreatorRoyalty Token level royalty interface.
             */
            interface IERC721CreatorRoyalty is IERC721TokenCreator {
                /**
                 * @dev Get the royalty fee percentage for a specific ERC721 contract.
                 * @param _contractAddress address ERC721Contract address.
                 * @param _tokenId uint256 token ID.
                 * @return uint8 wei royalty fee.
                 */
                function getERC721TokenRoyaltyPercentage(
                    address _contractAddress,
                    uint256 _tokenId
                ) external view returns (uint8);
                /**
                 * @dev Utililty function to calculate the royalty fee for a token.
                 * @param _contractAddress address ERC721Contract address.
                 * @param _tokenId uint256 token ID.
                 * @param _amount uint256 wei amount.
                 * @return uint256 wei fee.
                 */
                function calculateRoyaltyFee(
                    address _contractAddress,
                    uint256 _tokenId,
                    uint256 _amount
                ) external view returns (uint256);
                /**
                 * @dev Utililty function to set the royalty percentage for a specific ERC721 contract.
                 * @param _contractAddress address ERC721Contract address.
                 * @param _percentage percentage for royalty
                 */
                function setPercentageForSetERC721ContractRoyalty(
                    address _contractAddress,
                    uint8 _percentage
                ) external;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            /// @author koloz
            /// @title IPayments
            /// @notice Interface for the Payments contract used.
            interface IPayments {
                function refund(address _payee, uint256 _amount) external payable;
                function payout(address[] calldata _splits, uint256[] calldata _amounts)
                    external
                    payable;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            /// @author koloz
            /// @title ISpaceOperatorRegistry
            /// @notice The interface for the SpaceOperatorRegistry
            interface ISpaceOperatorRegistry {
                function getPlatformCommission(address _operator)
                    external
                    view
                    returns (uint8);
                function setPlatformCommission(address _operator, uint8 _commission)
                    external;
                function isApprovedSpaceOperator(address _operator)
                    external
                    view
                    returns (bool);
                function setSpaceOperatorApproved(address _operator, bool _approved)
                    external;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            interface IApprovedTokenRegistry {
                /// @notice Returns if a token has been approved or not.
                /// @param _tokenContract Contract of token being checked.
                /// @return True if the token is allowed, false otherwise.
                function isApprovedToken(address _tokenContract)
                    external
                    view
                    returns (bool);
                /// @notice Adds a token to the list of approved tokens.
                /// @param _tokenContract Contract of token being approved.
                function addApprovedToken(address _tokenContract) external;
                /// @notice Removes a token from the approved tokens list.
                /// @param _tokenContract Contract of token being approved.
                function removeApprovedToken(address _tokenContract) external;
                /// @notice Sets whether all token contracts should be approved.
                /// @param _allTokensApproved Bool denoting if all tokens should be approved.
                function setAllTokensApproved(bool _allTokensApproved) external;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            /// @author: manifold.xyz
            /**
             * @dev Lookup engine interface
             */
            interface IRoyaltyEngineV1 {
                /**
                 * Get the royalty for a given token (address, id) and value amount.  Does not cache the bps/amounts.  Caches the spec for a given token address
                 *
                 * @param tokenAddress - The address of the token
                 * @param tokenId      - The id of the token
                 * @param value        - The value you wish to get the royalty of
                 *
                 * returns Two arrays of equal length, royalty recipients and the corresponding amount each recipient should get
                 */
                function getRoyalty(
                    address tokenAddress,
                    uint256 tokenId,
                    uint256 value
                )
                    external
                    returns (address payable[] memory recipients, uint256[] memory amounts);
                /**
                 * View only version of getRoyalty
                 *
                 * @param tokenAddress - The address of the token
                 * @param tokenId      - The id of the token
                 * @param value        - The value you wish to get the royalty of
                 *
                 * returns Two arrays of equal length, royalty recipients and the corresponding amount each recipient should get
                 */
                function getRoyaltyView(
                    address tokenAddress,
                    uint256 tokenId,
                    uint256 value
                )
                    external
                    view
                    returns (address payable[] memory recipients, uint256[] memory amounts);
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            interface IERC721TokenCreator {
                function tokenCreator(address _contractAddress, uint256 _tokenId)
                    external
                    view
                    returns (address payable);
            }
            

            File 2 of 8: SuperRareV2
            pragma solidity ^0.4.24;
            
            /**
             * @title SafeMath
             * @dev Math operations with safety checks that revert on error
             */
            library SafeMath {
            
              /**
              * @dev Multiplies two numbers, reverts on overflow.
              */
              function mul(uint256 a, uint256 b) internal pure returns (uint256) {
                // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
                // benefit is lost if 'b' is also tested.
                // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
                if (a == 0) {
                  return 0;
                }
            
                uint256 c = a * b;
                require(c / a == b);
            
                return c;
              }
            
              /**
              * @dev Integer division of two numbers truncating the quotient, reverts on division by zero.
              */
              function div(uint256 a, uint256 b) internal pure returns (uint256) {
                require(b > 0); // Solidity only automatically asserts when dividing by 0
                uint256 c = a / b;
                // assert(a == b * c + a % b); // There is no case in which this doesn't hold
            
                return c;
              }
            
              /**
              * @dev Subtracts two numbers, reverts on overflow (i.e. if subtrahend is greater than minuend).
              */
              function sub(uint256 a, uint256 b) internal pure returns (uint256) {
                require(b <= a);
                uint256 c = a - b;
            
                return c;
              }
            
              /**
              * @dev Adds two numbers, reverts on overflow.
              */
              function add(uint256 a, uint256 b) internal pure returns (uint256) {
                uint256 c = a + b;
                require(c >= a);
            
                return c;
              }
            
              /**
              * @dev Divides two numbers and returns the remainder (unsigned integer modulo),
              * reverts when dividing by zero.
              */
              function mod(uint256 a, uint256 b) internal pure returns (uint256) {
                require(b != 0);
                return a % b;
              }
            }
            
            interface IERC165 {
            
              /**
               * @notice Query if a contract implements an interface
               * @param interfaceId The interface identifier, as specified in ERC-165
               * @dev Interface identification is specified in ERC-165. This function
               * uses less than 30,000 gas.
               */
              function supportsInterface(bytes4 interfaceId)
                external
                view
                returns (bool);
            }
            
            interface ISuperRare {
              /**
               * @notice A descriptive name for a collection of NFTs in this contract
               */
              function name() external pure returns (string _name);
            
              /**
               * @notice An abbreviated name for NFTs in this contract
               */
              function symbol() external pure returns (string _symbol);
            
              /** 
               * @dev Returns whether the creator is whitelisted
               * @param _creator address to check
               * @return bool 
               */
              function isWhitelisted(address _creator) external view returns (bool);
            
              /** 
               * @notice A distinct Uniform Resource Identifier (URI) for a given asset.
               * @dev Throws if `_tokenId` is not a valid NFT. URIs are defined in RFC
               * 3986. The URI may point to a JSON file that conforms to the "ERC721
               * Metadata JSON Schema".
               */
              function tokenURI(uint256 _tokenId) external view returns (string);
            
              /**
              * @dev Gets the creator of the token
              * @param _tokenId uint256 ID of the token
              * @return address of the creator
              */
              function creatorOfToken(uint256 _tokenId) public view returns (address);
            
              /**
              * @dev Gets the total amount of tokens stored by the contract
              * @return uint256 representing the total amount of tokens
              */
              function totalSupply() public view returns (uint256);
            }
            
            /**
             * @title ERC721 token receiver interface
             * @dev Interface for any contract that wants to support safeTransfers
             * from ERC721 asset contracts.
             */
            contract IERC721Receiver {
              /**
               * @notice Handle the receipt of an NFT
               * @dev The ERC721 smart contract calls this function on the recipient
               * after a `safeTransfer`. This function MUST return the function selector,
               * otherwise the caller will revert the transaction. The selector to be
               * returned can be obtained as `this.onERC721Received.selector`. This
               * function MAY throw to revert and reject the transfer.
               * Note: the ERC721 contract address is always the message sender.
               * @param operator The address which called `safeTransferFrom` function
               * @param from The address which previously owned the token
               * @param tokenId The NFT identifier which is being transferred
               * @param data Additional data with no specified format
               * @return `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
               */
              function onERC721Received(
                address operator,
                address from,
                uint256 tokenId,
                bytes data
              )
                public
                returns(bytes4);
            }
            
            contract IERC721 is IERC165 {
            
              event Transfer(
                address indexed from,
                address indexed to,
                uint256 indexed tokenId
              );
              event Approval(
                address indexed owner,
                address indexed approved,
                uint256 indexed tokenId
              );
              event ApprovalForAll(
                address indexed owner,
                address indexed operator,
                bool approved
              );
            
              function balanceOf(address owner) public view returns (uint256 balance);
              function ownerOf(uint256 tokenId) public view returns (address owner);
            
              function approve(address to, uint256 tokenId) public;
              function getApproved(uint256 tokenId)
                public view returns (address operator);
            
              function setApprovalForAll(address operator, bool _approved) public;
              function isApprovedForAll(address owner, address operator)
                public view returns (bool);
            
              function transferFrom(address from, address to, uint256 tokenId) public;
              function safeTransferFrom(address from, address to, uint256 tokenId)
                public;
            
              function safeTransferFrom(
                address from,
                address to,
                uint256 tokenId,
                bytes data
              )
                public;
            }
            
            contract IERC721Creator is IERC721 {
                /**
               * @dev Gets the creator of the token
               * @param _tokenId uint256 ID of the token
               * @return address of the creator
               */
                function tokenCreator(uint256 _tokenId) public view returns (address);
            }
            
            /**
             * @title ERC-721 Non-Fungible Token Standard, optional metadata extension
             * @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
             */
            contract IERC721Metadata is IERC721 {
              function name() external view returns (string);
              function symbol() external view returns (string);
              function tokenURI(uint256 tokenId) external view returns (string);
            }
            
            /**
             * @title ERC-721 Non-Fungible Token Standard, optional enumeration extension
             * @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
             */
            contract IERC721Enumerable is IERC721 {
              function totalSupply() public view returns (uint256);
              function tokenOfOwnerByIndex(
                address owner,
                uint256 index
              )
                public
                view
                returns (uint256 tokenId);
            
              function tokenByIndex(uint256 index) public view returns (uint256);
            }
            
            /**
             * Utility library of inline functions on addresses
             */
            library Address {
            
              /**
               * Returns whether the target address is a contract
               * @dev This function will return false if invoked during the constructor of a contract,
               * as the code is not actually created until after the constructor finishes.
               * @param account address of the account to check
               * @return whether the target address is a contract
               */
              function isContract(address account) internal view returns (bool) {
                uint256 size;
                // XXX Currently there is no better way to check if there is a contract in an address
                // than to check the size of the code at that address.
                // See https://ethereum.stackexchange.com/a/14016/36603
                // for more details about how this works.
                // TODO Check this again before the Serenity release, because all addresses will be
                // contracts then.
                // solium-disable-next-line security/no-inline-assembly
                assembly { size := extcodesize(account) }
                return size > 0;
              }
            
            }
            
            /**
             * @title ERC165
             * @author Matt Condon (@shrugs)
             * @dev Implements ERC165 using a lookup table.
             */
            contract ERC165 is IERC165 {
            
              bytes4 private constant _InterfaceId_ERC165 = 0x01ffc9a7;
              /**
               * 0x01ffc9a7 ===
               *   bytes4(keccak256('supportsInterface(bytes4)'))
               */
            
              /**
               * @dev a mapping of interface id to whether or not it's supported
               */
              mapping(bytes4 => bool) private _supportedInterfaces;
            
              /**
               * @dev A contract implementing SupportsInterfaceWithLookup
               * implement ERC165 itself
               */
              constructor()
                internal
              {
                _registerInterface(_InterfaceId_ERC165);
              }
            
              /**
               * @dev implement supportsInterface(bytes4) using a lookup table
               */
              function supportsInterface(bytes4 interfaceId)
                external
                view
                returns (bool)
              {
                return _supportedInterfaces[interfaceId];
              }
            
              /**
               * @dev internal method for registering an interface
               */
              function _registerInterface(bytes4 interfaceId)
                internal
              {
                require(interfaceId != 0xffffffff);
                _supportedInterfaces[interfaceId] = true;
              }
            }
            
            /**
             * @title ERC721 Non-Fungible Token Standard basic implementation
             * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
             */
            contract ERC721 is ERC165, IERC721 {
            
              using SafeMath for uint256;
              using Address for address;
            
              // Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
              // which can be also obtained as `IERC721Receiver(0).onERC721Received.selector`
              bytes4 private constant _ERC721_RECEIVED = 0x150b7a02;
            
              // Mapping from token ID to owner
              mapping (uint256 => address) private _tokenOwner;
            
              // Mapping from token ID to approved address
              mapping (uint256 => address) private _tokenApprovals;
            
              // Mapping from owner to number of owned token
              mapping (address => uint256) private _ownedTokensCount;
            
              // Mapping from owner to operator approvals
              mapping (address => mapping (address => bool)) private _operatorApprovals;
            
              bytes4 private constant _InterfaceId_ERC721 = 0x80ac58cd;
              /*
               * 0x80ac58cd ===
               *   bytes4(keccak256('balanceOf(address)')) ^
               *   bytes4(keccak256('ownerOf(uint256)')) ^
               *   bytes4(keccak256('approve(address,uint256)')) ^
               *   bytes4(keccak256('getApproved(uint256)')) ^
               *   bytes4(keccak256('setApprovalForAll(address,bool)')) ^
               *   bytes4(keccak256('isApprovedForAll(address,address)')) ^
               *   bytes4(keccak256('transferFrom(address,address,uint256)')) ^
               *   bytes4(keccak256('safeTransferFrom(address,address,uint256)')) ^
               *   bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)'))
               */
            
              constructor()
                public
              {
                // register the supported interfaces to conform to ERC721 via ERC165
                _registerInterface(_InterfaceId_ERC721);
              }
            
              /**
               * @dev Gets the balance of the specified address
               * @param owner address to query the balance of
               * @return uint256 representing the amount owned by the passed address
               */
              function balanceOf(address owner) public view returns (uint256) {
                require(owner != address(0));
                return _ownedTokensCount[owner];
              }
            
              /**
               * @dev Gets the owner of the specified token ID
               * @param tokenId uint256 ID of the token to query the owner of
               * @return owner address currently marked as the owner of the given token ID
               */
              function ownerOf(uint256 tokenId) public view returns (address) {
                address owner = _tokenOwner[tokenId];
                require(owner != address(0));
                return owner;
              }
            
              /**
               * @dev Approves another address to transfer the given token ID
               * The zero address indicates there is no approved address.
               * There can only be one approved address per token at a given time.
               * Can only be called by the token owner or an approved operator.
               * @param to address to be approved for the given token ID
               * @param tokenId uint256 ID of the token to be approved
               */
              function approve(address to, uint256 tokenId) public {
                address owner = ownerOf(tokenId);
                require(to != owner);
                require(msg.sender == owner || isApprovedForAll(owner, msg.sender));
            
                _tokenApprovals[tokenId] = to;
                emit Approval(owner, to, tokenId);
              }
            
              /**
               * @dev Gets the approved address for a token ID, or zero if no address set
               * Reverts if the token ID does not exist.
               * @param tokenId uint256 ID of the token to query the approval of
               * @return address currently approved for the given token ID
               */
              function getApproved(uint256 tokenId) public view returns (address) {
                require(_exists(tokenId));
                return _tokenApprovals[tokenId];
              }
            
              /**
               * @dev Sets or unsets the approval of a given operator
               * An operator is allowed to transfer all tokens of the sender on their behalf
               * @param to operator address to set the approval
               * @param approved representing the status of the approval to be set
               */
              function setApprovalForAll(address to, bool approved) public {
                require(to != msg.sender);
                _operatorApprovals[msg.sender][to] = approved;
                emit ApprovalForAll(msg.sender, to, approved);
              }
            
              /**
               * @dev Tells whether an operator is approved by a given owner
               * @param owner owner address which you want to query the approval of
               * @param operator operator address which you want to query the approval of
               * @return bool whether the given operator is approved by the given owner
               */
              function isApprovedForAll(
                address owner,
                address operator
              )
                public
                view
                returns (bool)
              {
                return _operatorApprovals[owner][operator];
              }
            
              /**
               * @dev Transfers the ownership of a given token ID to another address
               * Usage of this method is discouraged, use `safeTransferFrom` whenever possible
               * Requires the msg sender to be the owner, approved, or operator
               * @param from current owner of the token
               * @param to address to receive the ownership of the given token ID
               * @param tokenId uint256 ID of the token to be transferred
              */
              function transferFrom(
                address from,
                address to,
                uint256 tokenId
              )
                public
              {
                require(_isApprovedOrOwner(msg.sender, tokenId));
                require(to != address(0));
            
                _clearApproval(from, tokenId);
                _removeTokenFrom(from, tokenId);
                _addTokenTo(to, tokenId);
            
                emit Transfer(from, to, tokenId);
              }
            
              /**
               * @dev Safely transfers the ownership of a given token ID to another address
               * If the target address is a contract, it must implement `onERC721Received`,
               * which is called upon a safe transfer, and return the magic value
               * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
               * the transfer is reverted.
               *
               * Requires the msg sender to be the owner, approved, or operator
               * @param from current owner of the token
               * @param to address to receive the ownership of the given token ID
               * @param tokenId uint256 ID of the token to be transferred
              */
              function safeTransferFrom(
                address from,
                address to,
                uint256 tokenId
              )
                public
              {
                // solium-disable-next-line arg-overflow
                safeTransferFrom(from, to, tokenId, "");
              }
            
              /**
               * @dev Safely transfers the ownership of a given token ID to another address
               * If the target address is a contract, it must implement `onERC721Received`,
               * which is called upon a safe transfer, and return the magic value
               * `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise,
               * the transfer is reverted.
               * Requires the msg sender to be the owner, approved, or operator
               * @param from current owner of the token
               * @param to address to receive the ownership of the given token ID
               * @param tokenId uint256 ID of the token to be transferred
               * @param _data bytes data to send along with a safe transfer check
               */
              function safeTransferFrom(
                address from,
                address to,
                uint256 tokenId,
                bytes _data
              )
                public
              {
                transferFrom(from, to, tokenId);
                // solium-disable-next-line arg-overflow
                require(_checkOnERC721Received(from, to, tokenId, _data));
              }
            
              /**
               * @dev Returns whether the specified token exists
               * @param tokenId uint256 ID of the token to query the existence of
               * @return whether the token exists
               */
              function _exists(uint256 tokenId) internal view returns (bool) {
                address owner = _tokenOwner[tokenId];
                return owner != address(0);
              }
            
              /**
               * @dev Returns whether the given spender can transfer a given token ID
               * @param spender address of the spender to query
               * @param tokenId uint256 ID of the token to be transferred
               * @return bool whether the msg.sender is approved for the given token ID,
               *  is an operator of the owner, or is the owner of the token
               */
              function _isApprovedOrOwner(
                address spender,
                uint256 tokenId
              )
                internal
                view
                returns (bool)
              {
                address owner = ownerOf(tokenId);
                // Disable solium check because of
                // https://github.com/duaraghav8/Solium/issues/175
                // solium-disable-next-line operator-whitespace
                return (
                  spender == owner ||
                  getApproved(tokenId) == spender ||
                  isApprovedForAll(owner, spender)
                );
              }
            
              /**
               * @dev Internal function to mint a new token
               * Reverts if the given token ID already exists
               * @param to The address that will own the minted token
               * @param tokenId uint256 ID of the token to be minted by the msg.sender
               */
              function _mint(address to, uint256 tokenId) internal {
                require(to != address(0));
                _addTokenTo(to, tokenId);
                emit Transfer(address(0), to, tokenId);
              }
            
              /**
               * @dev Internal function to burn a specific token
               * Reverts if the token does not exist
               * @param tokenId uint256 ID of the token being burned by the msg.sender
               */
              function _burn(address owner, uint256 tokenId) internal {
                _clearApproval(owner, tokenId);
                _removeTokenFrom(owner, tokenId);
                emit Transfer(owner, address(0), tokenId);
              }
            
              /**
               * @dev Internal function to add a token ID to the list of a given address
               * Note that this function is left internal to make ERC721Enumerable possible, but is not
               * intended to be called by custom derived contracts: in particular, it emits no Transfer event.
               * @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 _addTokenTo(address to, uint256 tokenId) internal {
                require(_tokenOwner[tokenId] == address(0));
                _tokenOwner[tokenId] = to;
                _ownedTokensCount[to] = _ownedTokensCount[to].add(1);
              }
            
              /**
               * @dev Internal function to remove a token ID from the list of a given address
               * Note that this function is left internal to make ERC721Enumerable possible, but is not
               * intended to be called by custom derived contracts: in particular, it emits no Transfer event,
               * and doesn't clear approvals.
               * @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 _removeTokenFrom(address from, uint256 tokenId) internal {
                require(ownerOf(tokenId) == from);
                _ownedTokensCount[from] = _ownedTokensCount[from].sub(1);
                _tokenOwner[tokenId] = address(0);
              }
            
              /**
               * @dev Internal function to invoke `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 whether the call correctly returned the expected magic value
               */
              function _checkOnERC721Received(
                address from,
                address to,
                uint256 tokenId,
                bytes _data
              )
                internal
                returns (bool)
              {
                if (!to.isContract()) {
                  return true;
                }
                bytes4 retval = IERC721Receiver(to).onERC721Received(
                  msg.sender, from, tokenId, _data);
                return (retval == _ERC721_RECEIVED);
              }
            
              /**
               * @dev Private function to clear current approval of a given token ID
               * Reverts if the given address is not indeed the owner of the token
               * @param owner owner of the token
               * @param tokenId uint256 ID of the token to be transferred
               */
              function _clearApproval(address owner, uint256 tokenId) private {
                require(ownerOf(tokenId) == owner);
                if (_tokenApprovals[tokenId] != address(0)) {
                  _tokenApprovals[tokenId] = address(0);
                }
              }
            }
            
            contract ERC721Enumerable is ERC165, ERC721, IERC721Enumerable {
              // Mapping from owner to list of owned token IDs
              mapping(address => 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;
            
              bytes4 private constant _InterfaceId_ERC721Enumerable = 0x780e9d63;
              /**
               * 0x780e9d63 ===
               *   bytes4(keccak256('totalSupply()')) ^
               *   bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) ^
               *   bytes4(keccak256('tokenByIndex(uint256)'))
               */
            
              /**
               * @dev Constructor function
               */
              constructor() public {
                // register the supported interface to conform to ERC721 via ERC165
                _registerInterface(_InterfaceId_ERC721Enumerable);
              }
            
              /**
               * @dev Gets the token ID at a given index of the tokens list of the requested owner
               * @param owner address owning the tokens list to be accessed
               * @param index uint256 representing the index to be accessed of the requested tokens list
               * @return uint256 token ID at the given index of the tokens list owned by the requested address
               */
              function tokenOfOwnerByIndex(
                address owner,
                uint256 index
              )
                public
                view
                returns (uint256)
              {
                require(index < balanceOf(owner));
                return _ownedTokens[owner][index];
              }
            
              /**
               * @dev Gets the total amount of tokens stored by the contract
               * @return uint256 representing the total amount of tokens
               */
              function totalSupply() public view returns (uint256) {
                return _allTokens.length;
              }
            
              /**
               * @dev Gets the token ID at a given index of all the tokens in this contract
               * Reverts if the index is greater or equal to the total number of tokens
               * @param index uint256 representing the index to be accessed of the tokens list
               * @return uint256 token ID at the given index of the tokens list
               */
              function tokenByIndex(uint256 index) public view returns (uint256) {
                require(index < totalSupply());
                return _allTokens[index];
              }
            
              /**
               * @dev Internal function to add a token ID to the list of a given address
               * This function is internal due to language limitations, see the note in ERC721.sol.
               * It is not intended to be called by custom derived contracts: in particular, it emits no Transfer event.
               * @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 _addTokenTo(address to, uint256 tokenId) internal {
                super._addTokenTo(to, tokenId);
                uint256 length = _ownedTokens[to].length;
                _ownedTokens[to].push(tokenId);
                _ownedTokensIndex[tokenId] = length;
              }
            
              /**
               * @dev Internal function to remove a token ID from the list of a given address
               * This function is internal due to language limitations, see the note in ERC721.sol.
               * It is not intended to be called by custom derived contracts: in particular, it emits no Transfer event,
               * and doesn't clear approvals.
               * @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 _removeTokenFrom(address from, uint256 tokenId) internal {
                super._removeTokenFrom(from, tokenId);
            
                // To prevent a gap in the array, we store the last token in the index of the token to delete, and
                // then delete the last slot.
                uint256 tokenIndex = _ownedTokensIndex[tokenId];
                uint256 lastTokenIndex = _ownedTokens[from].length.sub(1);
                uint256 lastToken = _ownedTokens[from][lastTokenIndex];
            
                _ownedTokens[from][tokenIndex] = lastToken;
                // This also deletes the contents at the last position of the array
                _ownedTokens[from].length--;
            
                // Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to
                // be zero. Then we can make sure that we will remove tokenId from the ownedTokens list since we are first swapping
                // the lastToken to the first position, and then dropping the element placed in the last position of the list
            
                _ownedTokensIndex[tokenId] = 0;
                _ownedTokensIndex[lastToken] = tokenIndex;
              }
            
              /**
               * @dev Internal function to mint a new token
               * Reverts if the given token ID already exists
               * @param to address the beneficiary that will own the minted token
               * @param tokenId uint256 ID of the token to be minted by the msg.sender
               */
              function _mint(address to, uint256 tokenId) internal {
                super._mint(to, tokenId);
            
                _allTokensIndex[tokenId] = _allTokens.length;
                _allTokens.push(tokenId);
              }
            
              /**
               * @dev Internal function to burn a specific token
               * Reverts if the token does not exist
               * @param owner owner of the token to burn
               * @param tokenId uint256 ID of the token being burned by the msg.sender
               */
              function _burn(address owner, uint256 tokenId) internal {
                super._burn(owner, tokenId);
            
                // Reorg all tokens array
                uint256 tokenIndex = _allTokensIndex[tokenId];
                uint256 lastTokenIndex = _allTokens.length.sub(1);
                uint256 lastToken = _allTokens[lastTokenIndex];
            
                _allTokens[tokenIndex] = lastToken;
                _allTokens[lastTokenIndex] = 0;
            
                _allTokens.length--;
                _allTokensIndex[tokenId] = 0;
                _allTokensIndex[lastToken] = tokenIndex;
              }
            }
            
            contract ERC721Metadata is ERC165, ERC721, IERC721Metadata {
              // Token name
              string private _name;
            
              // Token symbol
              string private _symbol;
            
              // Optional mapping for token URIs
              mapping(uint256 => string) private _tokenURIs;
            
              bytes4 private constant InterfaceId_ERC721Metadata = 0x5b5e139f;
              /**
               * 0x5b5e139f ===
               *   bytes4(keccak256('name()')) ^
               *   bytes4(keccak256('symbol()')) ^
               *   bytes4(keccak256('tokenURI(uint256)'))
               */
            
              /**
               * @dev Constructor function
               */
              constructor(string name, string symbol) public {
                _name = name;
                _symbol = symbol;
            
                // register the supported interfaces to conform to ERC721 via ERC165
                _registerInterface(InterfaceId_ERC721Metadata);
              }
            
              /**
               * @dev Gets the token name
               * @return string representing the token name
               */
              function name() external view returns (string) {
                return _name;
              }
            
              /**
               * @dev Gets the token symbol
               * @return string representing the token symbol
               */
              function symbol() external view returns (string) {
                return _symbol;
              }
            
              /**
               * @dev Returns an URI for a given token ID
               * Throws if the token ID does not exist. May return an empty string.
               * @param tokenId uint256 ID of the token to query
               */
              function tokenURI(uint256 tokenId) external view returns (string) {
                require(_exists(tokenId));
                return _tokenURIs[tokenId];
              }
            
              /**
               * @dev Internal function to set the token URI for a given token
               * Reverts if the token ID does not exist
               * @param tokenId uint256 ID of the token to set its URI
               * @param uri string URI to assign
               */
              function _setTokenURI(uint256 tokenId, string uri) internal {
                require(_exists(tokenId));
                _tokenURIs[tokenId] = uri;
              }
            
              /**
               * @dev Internal function to burn a specific token
               * Reverts if the token does not exist
               * @param owner owner of the token to burn
               * @param tokenId uint256 ID of the token being burned by the msg.sender
               */
              function _burn(address owner, uint256 tokenId) internal {
                super._burn(owner, tokenId);
            
                // Clear metadata (if any)
                if (bytes(_tokenURIs[tokenId]).length != 0) {
                  delete _tokenURIs[tokenId];
                }
              }
            }
            
            /**
             * @title Ownable
             * @dev The Ownable contract has an owner address, and provides basic authorization control
             * functions, this simplifies the implementation of "user permissions".
             */
            contract Ownable {
              address private _owner;
            
              event OwnershipTransferred(
                address indexed previousOwner,
                address indexed newOwner
              );
            
              /**
               * @dev The Ownable constructor sets the original `owner` of the contract to the sender
               * account.
               */
              constructor() internal {
                _owner = msg.sender;
                emit OwnershipTransferred(address(0), _owner);
              }
            
              /**
               * @return the address of the owner.
               */
              function owner() public view returns(address) {
                return _owner;
              }
            
              /**
               * @dev Throws if called by any account other than the owner.
               */
              modifier onlyOwner() {
                require(isOwner());
                _;
              }
            
              /**
               * @return true if `msg.sender` is the owner of the contract.
               */
              function isOwner() public view returns(bool) {
                return msg.sender == _owner;
              }
            
              /**
               * @dev Allows the current owner to relinquish control of the contract.
               * @notice Renouncing to ownership will leave the contract without an owner.
               * It will not be possible to call the functions with the `onlyOwner`
               * modifier anymore.
               */
              function renounceOwnership() public onlyOwner {
                emit OwnershipTransferred(_owner, address(0));
                _owner = address(0);
              }
            
              /**
               * @dev Allows the current owner to transfer control of the contract to a newOwner.
               * @param newOwner The address to transfer ownership to.
               */
              function transferOwnership(address newOwner) public onlyOwner {
                _transferOwnership(newOwner);
              }
            
              /**
               * @dev Transfers control of the contract to a newOwner.
               * @param newOwner The address to transfer ownership to.
               */
              function _transferOwnership(address newOwner) internal {
                require(newOwner != address(0));
                emit OwnershipTransferred(_owner, newOwner);
                _owner = newOwner;
              }
            }
            
            contract Whitelist is Ownable {
                // Mapping of address to boolean indicating whether the address is whitelisted
                mapping(address => bool) private whitelistMap;
            
                // flag controlling whether whitelist is enabled.
                bool private whitelistEnabled = true;
            
                event AddToWhitelist(address indexed _newAddress);
                event RemoveFromWhitelist(address indexed _removedAddress);
            
                /**
               * @dev Enable or disable the whitelist
               * @param _enabled bool of whether to enable the whitelist.
               */
                function enableWhitelist(bool _enabled) public onlyOwner {
                    whitelistEnabled = _enabled;
                }
            
                /**
               * @dev Adds the provided address to the whitelist
               * @param _newAddress address to be added to the whitelist
               */
                function addToWhitelist(address _newAddress) public onlyOwner {
                    _whitelist(_newAddress);
                    emit AddToWhitelist(_newAddress);
                }
            
                /**
               * @dev Removes the provided address to the whitelist
               * @param _removedAddress address to be removed from the whitelist
               */
                function removeFromWhitelist(address _removedAddress) public onlyOwner {
                    _unWhitelist(_removedAddress);
                    emit RemoveFromWhitelist(_removedAddress);
                }
            
                /**
               * @dev Returns whether the address is whitelisted
               * @param _address address to check
               * @return bool
               */
                function isWhitelisted(address _address) public view returns (bool) {
                    if (whitelistEnabled) {
                        return whitelistMap[_address];
                    } else {
                        return true;
                    }
                }
            
                /**
               * @dev Internal function for removing an address from the whitelist
               * @param _removedAddress address to unwhitelisted
               */
                function _unWhitelist(address _removedAddress) internal {
                    whitelistMap[_removedAddress] = false;
                }
            
                /**
               * @dev Internal function for adding the provided address to the whitelist
               * @param _newAddress address to be added to the whitelist
               */
                function _whitelist(address _newAddress) internal {
                    whitelistMap[_newAddress] = true;
                }
            }
            
            /**
             * @title Full ERC721 Token
             * This implementation includes all the required and some optional functionality of the ERC721 standard
             * Moreover, it includes approve all functionality using operator terminology
             * @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md
             */
            contract ERC721Full is ERC721, ERC721Enumerable, ERC721Metadata {
              constructor(string name, string symbol) ERC721Metadata(name, symbol)
                public
              {
              }
            }
            
            
            contract SuperRareV2 is ERC721Full, IERC721Creator, Ownable, Whitelist {
                using SafeMath for uint256;
            
                // Mapping from token ID to the creator's address.
                mapping(uint256 => address) private tokenCreators;
            
                // Counter for creating token IDs
                uint256 private idCounter;
            
                // Old SuperRare contract to look up token details.
                ISuperRare private oldSuperRare;
            
                // Event indicating metadata was updated.
                event TokenURIUpdated(uint256 indexed _tokenId, string  _uri);
            
                constructor(
                  string _name,
                  string _symbol,
                  address _oldSuperRare
                )
                ERC721Full(_name, _symbol)
                {
                  // Get reference to old SR contract.
                  oldSuperRare = ISuperRare(_oldSuperRare);
            
                  uint256 oldSupply = oldSuperRare.totalSupply();
                  // Set id counter to be continuous with SuperRare.
                  idCounter = oldSupply + 1;
                }
            
                /**
                 * @dev Whitelists a bunch of addresses.
                 * @param _whitelistees address[] of addresses to whitelist.
                 */
                function initWhitelist(address[] _whitelistees) public onlyOwner {
                  // Add all whitelistees.
                  for (uint256 i = 0; i < _whitelistees.length; i++) {
                    address creator = _whitelistees[i];
                    if (!isWhitelisted(creator)) {
                      _whitelist(creator);
                    }
                  }
                }
            
                /**
                 * @dev Checks that the token is owned by the sender.
                 * @param _tokenId uint256 ID of the token.
                 */
                modifier onlyTokenOwner(uint256 _tokenId) {
                  address owner = ownerOf(_tokenId);
                  require(owner == msg.sender, "must be the owner of the token");
                  _;
                }
            
                /**
                 * @dev Checks that the token was created by the sender.
                 * @param _tokenId uint256 ID of the token.
                 */
                modifier onlyTokenCreator(uint256 _tokenId) {
                  address creator = tokenCreator(_tokenId);
                  require(creator == msg.sender, "must be the creator of the token");
                  _;
                }
            
                /**
                 * @dev Adds a new unique token to the supply.
                 * @param _uri string metadata uri associated with the token.
                 */
                function addNewToken(string _uri) public {
                  require(isWhitelisted(msg.sender), "must be whitelisted to create tokens");
                  _createToken(_uri, msg.sender);
                }
            
                /**
                 * @dev Deletes the token with the provided ID.
                 * @param _tokenId uint256 ID of the token.
                 */
                function deleteToken(uint256 _tokenId) public onlyTokenOwner(_tokenId) {
                  _burn(msg.sender, _tokenId);
                }
            
                /**
                 * @dev Updates the token metadata if the owner is also the
                 *      creator.
                 * @param _tokenId uint256 ID of the token.
                 * @param _uri string metadata URI.
                 */
                function updateTokenMetadata(uint256 _tokenId, string _uri)
                  public
                  onlyTokenOwner(_tokenId)
                  onlyTokenCreator(_tokenId)
                {
                  _setTokenURI(_tokenId, _uri);
                  emit TokenURIUpdated(_tokenId, _uri);
                }
            
                /**
                * @dev Gets the creator of the token.
                * @param _tokenId uint256 ID of the token.
                * @return address of the creator.
                */
                function tokenCreator(uint256 _tokenId) public view returns (address) {
                    return tokenCreators[_tokenId];
                }
            
                /**
                 * @dev Internal function for setting the token's creator.
                 * @param _tokenId uint256 id of the token.
                 * @param _creator address of the creator of the token.
                 */
                function _setTokenCreator(uint256 _tokenId, address _creator) internal {
                  tokenCreators[_tokenId] = _creator;
                }
            
                /**
                 * @dev Internal function creating a new token.
                 * @param _uri string metadata uri associated with the token
                 * @param _creator address of the creator of the token.
                 */
                function _createToken(string _uri, address _creator) internal returns (uint256) {
                  uint256 newId = idCounter;
                  idCounter++;
                  _mint(_creator, newId);
                  _setTokenURI(newId, _uri);
                  _setTokenCreator(newId, _creator);
                  return newId;
                }
            }

            File 3 of 8: SuperRareMarketplace
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            import "@openzeppelin/contracts-upgradeable-0.7.2/access/OwnableUpgradeable.sol";
            import "@openzeppelin/contracts-upgradeable-0.7.2/utils/ReentrancyGuardUpgradeable.sol";
            import "./ISuperRareMarketplace.sol";
            import "../SuperRareBazaarBase.sol";
            /// @author koloz
            /// @title SuperRareMarketplace
            /// @notice The logic for all functions related to the SuperRareMarketplace.
            contract SuperRareMarketplace is
                ISuperMarketplace,
                OwnableUpgradeable,
                ReentrancyGuardUpgradeable,
                SuperRareBazaarBase
            {
                using SafeMath for uint256;
                using SafeERC20 for IERC20;
                /////////////////////////////////////////////////////////////////////////
                // Initializer
                /////////////////////////////////////////////////////////////////////////
                function initialize(
                    address _marketplaceSettings,
                    address _royaltyEngine,
                    address _royaltyRegistry,
                    address _spaceOperatorRegistry,
                    address _approvedTokenRegistry,
                    address _payments,
                    address _stakingRegistry,
                    address _networkBeneficiary
                ) public initializer {
                    require(_marketplaceSettings != address(0));
                    require(_royaltyRegistry != address(0));
                    require(_royaltyEngine != address(0));
                    require(_spaceOperatorRegistry != address(0));
                    require(_approvedTokenRegistry != address(0));
                    require(_payments != address(0));
                    require(_networkBeneficiary != address(0));
                    marketplaceSettings = IMarketplaceSettings(_marketplaceSettings);
                    royaltyRegistry = IERC721CreatorRoyalty(_royaltyRegistry);
                    royaltyEngine = IRoyaltyEngineV1(_royaltyEngine);
                    spaceOperatorRegistry = ISpaceOperatorRegistry(_spaceOperatorRegistry);
                    approvedTokenRegistry = IApprovedTokenRegistry(_approvedTokenRegistry);
                    payments = IPayments(_payments);
                    stakingRegistry = _stakingRegistry;
                    networkBeneficiary = _networkBeneficiary;
                    minimumBidIncreasePercentage = 10;
                    maxAuctionLength = 7 days;
                    auctionLengthExtension = 15 minutes;
                    offerCancelationDelay = 5 minutes;
                    __Ownable_init();
                    __ReentrancyGuard_init();
                }
                /// @notice Place an offer for a given asset
                /// @dev Notice we need to verify that the msg sender has approved us to move funds on their behalf.
                /// @dev Covers use of any currency (0 address is eth).
                /// @dev _amount is the amount of the offer excluding the marketplace fee.
                /// @dev There can be multiple offers of different currencies, but only 1 per currency.
                /// @param _originContract Contract address of the asset being listed.
                /// @param _tokenId Token Id of the asset.
                /// @param _currencyAddress Address of the token being offered.
                /// @param _amount Amount being offered (excluding marketplace fee).
                /// @param _convertible If the offer can be converted into an auction
                function offer(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount,
                    bool _convertible
                ) external payable override nonReentrant {
                    _checkIfCurrencyIsApproved(_currencyAddress);
                    require(_amount > 0, "offer::Amount cannot be 0");
                    Offer memory currOffer = tokenCurrentOffers[_originContract][_tokenId][
                        _currencyAddress
                    ];
                    require(
                        _amount >=
                            currOffer.amount.add(
                                currOffer.amount.mul(minimumBidIncreasePercentage).div(100)
                            ),
                        "offer::Must be greater than prev offer + min increase"
                    );
                    uint256 requiredAmount = _amount.add(
                        marketplaceSettings.calculateMarketplaceFee(_amount)
                    );
                    _senderMustHaveMarketplaceApproved(_currencyAddress, requiredAmount);
                    _checkAmountAndTransfer(_currencyAddress, requiredAmount);
                    IERC721 erc721 = IERC721(_originContract);
                    require(
                        erc721.ownerOf(_tokenId) != msg.sender,
                        "offer::Offer cannot come from owner"
                    );
                    _refund(
                        _currencyAddress,
                        currOffer.amount,
                        currOffer.marketplaceFee,
                        currOffer.buyer
                    );
                    tokenCurrentOffers[_originContract][_tokenId][_currencyAddress] = Offer(
                        msg.sender,
                        _amount,
                        block.timestamp,
                        marketplaceSettings.getMarketplaceFeePercentage(),
                        _convertible
                    );
                    emit OfferPlaced(
                        _originContract,
                        msg.sender,
                        _currencyAddress,
                        _amount,
                        _tokenId,
                        _convertible
                    );
                }
                /// @notice Purchases the token for the current sale price.
                /// @dev Covers use of any currency (0 address is eth).
                /// @dev Need to verify that the buyer (if not using eth) has the marketplace approved for _currencyContract.
                /// @dev Need to verify that the seller has the marketplace approved for _originContract.
                /// @param _originContract Contract address for asset being bought.
                /// @param _tokenId TokenId of asset being bought.
                /// @param _currencyAddress Currency address of asset being used to buy.
                /// @param _amount Amount the piece if being bought for (excluding marketplace fee).
                function buy(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount
                ) external payable override nonReentrant {
                    _ownerMustHaveMarketplaceApprovedForNFT(_originContract, _tokenId);
                    uint256 requiredAmount = _amount.add(
                        marketplaceSettings.calculateMarketplaceFee(_amount)
                    );
                    mapping(address => SalePrice) storage salePrices = tokenSalePrices[
                        _originContract
                    ][_tokenId];
                    SalePrice memory sp = salePrices[msg.sender].amount != 0
                        ? salePrices[msg.sender]
                        : salePrices[address(0)];
                    require(sp.amount > 0, "buy::Token has no buy now price");
                    require(
                        sp.currencyAddress == _currencyAddress,
                        "buy::Currency address mismatch"
                    );
                    IERC721 erc721 = IERC721(_originContract);
                    address tokenOwner = erc721.ownerOf(_tokenId);
                    require(tokenOwner == sp.seller, "buy::Price setter not owner");
                    require(_amount == sp.amount, "buy::Insufficient amount");
                    delete tokenSalePrices[_originContract][_tokenId][msg.sender];
                    delete tokenSalePrices[_originContract][_tokenId][address(0)];
                    _checkAmountAndTransfer(_currencyAddress, requiredAmount);
                    Offer memory currOffer = tokenCurrentOffers[_originContract][_tokenId][
                        _currencyAddress
                    ];
                    if (currOffer.buyer == msg.sender) {
                        delete tokenCurrentOffers[_originContract][_tokenId][
                            _currencyAddress
                        ];
                        _refund(
                            _currencyAddress,
                            currOffer.amount,
                            currOffer.marketplaceFee,
                            msg.sender
                        );
                    }
                    erc721.safeTransferFrom(tokenOwner, msg.sender, _tokenId);
                    _payout(
                        _originContract,
                        _tokenId,
                        _currencyAddress,
                        _amount,
                        sp.seller,
                        sp.splitRecipients,
                        sp.splitRatios
                    );
                    marketplaceSettings.markERC721Token(_originContract, _tokenId, true);
                    emit Sold(
                        _originContract,
                        msg.sender,
                        sp.seller,
                        _currencyAddress,
                        _amount,
                        _tokenId
                    );
                }
                /// @notice Cancels an existing offer the sender has placed on a piece.
                /// @param _originContract Contract address of token.
                /// @param _tokenId TokenId that has an offer.
                /// @param _currencyAddress Currency address of the offer.
                function cancelOffer(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress
                ) external override nonReentrant {
                    Offer memory currOffer = tokenCurrentOffers[_originContract][_tokenId][
                        _currencyAddress
                    ];
                    require(
                        currOffer.amount != 0,
                        "cancelOffer::No offer for currency exists."
                    );
                    require(
                        currOffer.buyer == msg.sender,
                        "cancelOffer::Sender must have placed the offer."
                    );
                    require(
                        block.timestamp - currOffer.timestamp > offerCancelationDelay,
                        "Offer placed too recently."
                    );
                    delete tokenCurrentOffers[_originContract][_tokenId][_currencyAddress];
                    _refund(
                        _currencyAddress,
                        currOffer.amount,
                        currOffer.marketplaceFee,
                        currOffer.buyer
                    );
                    emit CancelOffer(
                        _originContract,
                        msg.sender,
                        _currencyAddress,
                        currOffer.amount,
                        _tokenId
                    );
                }
                /// @notice Sets a sale price for the given asset(s) directed at the _target address.
                /// @dev Covers use of any currency (0 address is eth).
                /// @dev Sale price for everyone is denoted as the 0 address.
                /// @dev Only 1 currency can be used for the sale price directed at a speicific target.
                /// @dev _listPrice of 0 signifies removing the list price for the provided currency.
                /// @dev This function can be used for counter offers as well.
                /// @param _originContract Contract address of the asset being listed.
                /// @param _tokenId Token Id of the asset.
                /// @param _currencyAddress Contract address of the currency asset is being listed for.
                /// @param _listPrice Amount of the currency the asset is being listed for (including all decimal points).
                /// @param _target Address of the person this sale price is target to.
                /// @param _splitAddresses Addresses to split the sellers commission with.
                /// @param _splitRatios The ratio for the split corresponding to each of the addresses being split with.
                function setSalePrice(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _listPrice,
                    address _target,
                    address payable[] calldata _splitAddresses,
                    uint8[] calldata _splitRatios
                ) external override {
                    _checkIfCurrencyIsApproved(_currencyAddress);
                    _senderMustBeTokenOwner(_originContract, _tokenId);
                    _ownerMustHaveMarketplaceApprovedForNFT(_originContract, _tokenId);
                    _checkSplits(_splitAddresses, _splitRatios);
                    tokenSalePrices[_originContract][_tokenId][_target] = SalePrice(
                        payable(msg.sender),
                        _currencyAddress,
                        _listPrice,
                        _splitAddresses,
                        _splitRatios
                    );
                    emit SetSalePrice(
                        _originContract,
                        _currencyAddress,
                        _target,
                        _listPrice,
                        _tokenId,
                        _splitAddresses,
                        _splitRatios
                    );
                }
                /// @notice Removes the current sale price of an asset for _target for the given currency.
                /// @dev Sale prices could still exist for different currencies.
                /// @dev Sale prices could still exist for different targets.
                /// @dev Zero address for _currency means that its listed in ether.
                /// @dev _target of zero address is the general sale price.
                /// @param _originContract The origin contract of the asset.
                /// @param _tokenId The tokenId of the asset within the _originContract.
                /// @param _target The address of the person
                function removeSalePrice(
                    address _originContract,
                    uint256 _tokenId,
                    address _target
                ) external override {
                    IERC721 erc721 = IERC721(_originContract);
                    address tokenOwner = erc721.ownerOf(_tokenId);
                    require(
                        msg.sender == tokenOwner,
                        "removeSalePrice::Must be tokenOwner."
                    );
                    delete tokenSalePrices[_originContract][_tokenId][_target];
                    emit SetSalePrice(
                        _originContract,
                        address(0),
                        address(0),
                        0,
                        _tokenId,
                        new address payable[](0),
                        new uint8[](0)
                    );
                }
                /// @notice Accept an offer placed on _originContract : _tokenId.
                /// @dev Zero address for _currency means that the offer being accepted is in ether.
                /// @param _originContract Contract of the asset the offer was made on.
                /// @param _tokenId TokenId of the asset.
                /// @param _currencyAddress Address of the currency used for the offer.
                /// @param _amount Amount the offer was for/and is being accepted.
                /// @param _splitAddresses Addresses to split the sellers commission with.
                /// @param _splitRatios The ratio for the split corresponding to each of the addresses being split with.
                function acceptOffer(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount,
                    address payable[] calldata _splitAddresses,
                    uint8[] calldata _splitRatios
                ) external override nonReentrant {
                    _senderMustBeTokenOwner(_originContract, _tokenId);
                    _ownerMustHaveMarketplaceApprovedForNFT(_originContract, _tokenId);
                    _checkSplits(_splitAddresses, _splitRatios);
                    Offer memory currOffer = tokenCurrentOffers[_originContract][_tokenId][
                        _currencyAddress
                    ];
                    require(currOffer.amount != 0, "acceptOffer::No offer exists");
                    require(
                        currOffer.amount == _amount,
                        "acceptOffer::Offer amount or currency not equal"
                    );
                    delete tokenSalePrices[_originContract][_tokenId][address(0)];
                    delete tokenCurrentOffers[_originContract][_tokenId][_currencyAddress];
                    IERC721 erc721 = IERC721(_originContract);
                    erc721.safeTransferFrom(msg.sender, currOffer.buyer, _tokenId);
                    _payout(
                        _originContract,
                        _tokenId,
                        _currencyAddress,
                        _amount,
                        msg.sender,
                        _splitAddresses,
                        _splitRatios
                    );
                    marketplaceSettings.markERC721Token(_originContract, _tokenId, true);
                    emit AcceptOffer(
                        _originContract,
                        currOffer.buyer,
                        msg.sender,
                        _currencyAddress,
                        _amount,
                        _tokenId,
                        _splitAddresses,
                        _splitRatios
                    );
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity ^0.7.0;
            import "../utils/ContextUpgradeable.sol";
            import "../proxy/Initializable.sol";
            /**
             * @dev Contract module which provides a basic access control mechanism, where
             * there is an account (an owner) that can be granted exclusive access to
             * specific functions.
             *
             * By default, the owner account will be the one that deploys the contract. This
             * can later be changed with {transferOwnership}.
             *
             * This module is used through inheritance. It will make available the modifier
             * `onlyOwner`, which can be applied to your functions to restrict their use to
             * the owner.
             */
            abstract contract OwnableUpgradeable is Initializable, ContextUpgradeable {
                address private _owner;
                event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
                /**
                 * @dev Initializes the contract setting the deployer as the initial owner.
                 */
                function __Ownable_init() internal initializer {
                    __Context_init_unchained();
                    __Ownable_init_unchained();
                }
                function __Ownable_init_unchained() internal initializer {
                    address msgSender = _msgSender();
                    _owner = msgSender;
                    emit OwnershipTransferred(address(0), msgSender);
                }
                /**
                 * @dev Returns the address of the current owner.
                 */
                function owner() public view virtual returns (address) {
                    return _owner;
                }
                /**
                 * @dev Throws if called by any account other than the owner.
                 */
                modifier onlyOwner() {
                    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 {
                    emit OwnershipTransferred(_owner, address(0));
                    _owner = 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");
                    emit OwnershipTransferred(_owner, newOwner);
                    _owner = newOwner;
                }
                uint256[49] private __gap;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity ^0.7.0;
            import "../proxy/Initializable.sol";
            /**
             * @dev Contract module that helps prevent reentrant calls to a function.
             *
             * Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
             * available, which can be applied to functions to make sure there are no nested
             * (reentrant) calls to them.
             *
             * Note that because there is a single `nonReentrant` guard, functions marked as
             * `nonReentrant` may not call one another. This can be worked around by making
             * those functions `private`, and then adding `external` `nonReentrant` entry
             * points to them.
             *
             * TIP: If you would like to learn more about reentrancy and alternative ways
             * to protect against it, check out our blog post
             * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
             */
            abstract contract ReentrancyGuardUpgradeable is Initializable {
                // Booleans are more expensive than uint256 or any type that takes up a full
                // word because each write operation emits an extra SLOAD to first read the
                // slot's contents, replace the bits taken up by the boolean, and then write
                // back. This is the compiler's defense against contract upgrades and
                // pointer aliasing, and it cannot be disabled.
                // The values being non-zero value makes deployment a bit more expensive,
                // but in exchange the refund on every call to nonReentrant will be lower in
                // amount. Since refunds are capped to a percentage of the total
                // transaction's gas, it is best to keep them low in cases like this one, to
                // increase the likelihood of the full refund coming into effect.
                uint256 private constant _NOT_ENTERED = 1;
                uint256 private constant _ENTERED = 2;
                uint256 private _status;
                function __ReentrancyGuard_init() internal initializer {
                    __ReentrancyGuard_init_unchained();
                }
                function __ReentrancyGuard_init_unchained() internal initializer {
                    _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 make it call a
                 * `private` function that does the actual work.
                 */
                modifier nonReentrant() {
                    // On the first call to nonReentrant, _notEntered will be true
                    require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
                    // Any calls to nonReentrant after this point will fail
                    _status = _ENTERED;
                    _;
                    // By storing the original value once again, a refund is triggered (see
                    // https://eips.ethereum.org/EIPS/eip-2200)
                    _status = _NOT_ENTERED;
                }
                uint256[49] private __gap;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            /// @author koloz
            /// @title ISuperRareMarketplace
            /// @notice The interface for the SuperRareMarketplace Functions.
            interface ISuperMarketplace {
                /// @notice Place an offer for a given asset
                /// @param _originContract Contract address of the asset being listed.
                /// @param _tokenId Token Id of the asset.
                /// @param _currencyAddress Address of the token being offered.
                /// @param _amount Amount being offered (excluding marketplace fee).
                /// @param _convertible If the offer can be converted into an auction
                function offer(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount,
                    bool _convertible
                ) external payable;
                /// @notice Purchases the token for the current sale price.
                /// @param _originContract Contract address for asset being bought.
                /// @param _tokenId TokenId of asset being bought.
                /// @param _currencyAddress Currency address of asset being used to buy.
                /// @param _amount Amount the piece if being bought for (excluding marketplace fee).
                function buy(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount
                ) external payable;
                /// @notice Cancels an existing offer the sender has placed on a piece.
                /// @param _originContract Contract address of token.
                /// @param _tokenId TokenId that has an offer.
                /// @param _currencyAddress Currency address of the offer.
                function cancelOffer(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress
                ) external;
                /// @notice Sets a sale price for the given asset(s) directed at the _target address.
                /// @param _originContract Contract address of the asset being listed.
                /// @param _tokenId Token Id of the asset.
                /// @param _currencyAddress Contract address of the currency asset is being listed for.
                /// @param _listPrice Amount of the currency the asset is being listed for (including all decimal points).
                /// @param _target Address of the person this sale price is target to.
                /// @param _splitAddresses Addresses to split the sellers commission with.
                /// @param _splitRatios The ratio for the split corresponding to each of the addresses being split with.
                function setSalePrice(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _listPrice,
                    address _target,
                    address payable[] calldata _splitAddresses,
                    uint8[] calldata _splitRatios
                ) external;
                /// @notice Removes the current sale price of an asset for _target for the given currency.
                /// @param _originContract The origin contract of the asset.
                /// @param _tokenId The tokenId of the asset within the _originContract.
                /// @param _target The address of the person
                function removeSalePrice(
                    address _originContract,
                    uint256 _tokenId,
                    address _target
                ) external;
                /// @notice Accept an offer placed on _originContract : _tokenId.
                /// @param _originContract Contract of the asset the offer was made on.
                /// @param _tokenId TokenId of the asset.
                /// @param _currencyAddress Address of the currency used for the offer.
                /// @param _amount Amount the offer was for/and is being accepted.
                /// @param _splitAddresses Addresses to split the sellers commission with.
                /// @param _splitRatios The ratio for the split corresponding to each of the addresses being split with.
                function acceptOffer(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount,
                    address payable[] calldata _splitAddresses,
                    uint8[] calldata _splitRatios
                ) external;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            import "@openzeppelin/contracts-0.7.2/token/ERC721/IERC721.sol";
            import "@openzeppelin/contracts-0.7.2/token/ERC20/IERC20.sol";
            import "@openzeppelin/contracts-0.7.2/token/ERC20/SafeERC20.sol";
            import "@openzeppelin/contracts-0.7.2/math/SafeMath.sol";
            import "./storage/SuperRareBazaarStorage.sol";
            /// @author koloz
            /// @title SuperRareBazaarBase
            /// @notice Base contract containing the internal functions for the SuperRareBazaar.
            abstract contract SuperRareBazaarBase is SuperRareBazaarStorage {
                using SafeMath for uint256;
                using SafeMath for uint8;
                using SafeERC20 for IERC20;
                /////////////////////////////////////////////////////////////////////////
                // Internal Functions
                /////////////////////////////////////////////////////////////////////////
                /// @notice Checks to see if the currenccy address is eth or an approved erc20 token.
                /// @param _currencyAddress Address of currency (Zero address if eth).
                function _checkIfCurrencyIsApproved(address _currencyAddress)
                    internal
                    view
                {
                    require(
                        _currencyAddress == address(0) ||
                            approvedTokenRegistry.isApprovedToken(_currencyAddress),
                        "Not approved currency"
                    );
                }
                /// @notice Checks to see if the owner of the token has the marketplace approved.
                /// @param _originContract Contract address of the token being checked.
                /// @param _tokenId Token Id of the asset.
                function _ownerMustHaveMarketplaceApprovedForNFT(
                    address _originContract,
                    uint256 _tokenId
                ) internal view {
                    IERC721 erc721 = IERC721(_originContract);
                    address owner = erc721.ownerOf(_tokenId);
                    require(
                        erc721.isApprovedForAll(owner, address(this)),
                        "owner must have approved contract"
                    );
                }
                /// @notice Checks to see if the msg sender owns the token.
                /// @param _originContract Contract address of the token being checked.
                /// @param _tokenId Token Id of the asset.
                function _senderMustBeTokenOwner(address _originContract, uint256 _tokenId)
                    internal
                    view
                {
                    IERC721 erc721 = IERC721(_originContract);
                    require(
                        erc721.ownerOf(_tokenId) == msg.sender,
                        "sender must be the token owner"
                    );
                }
                /// @notice Verifies that the splits supplied are valid.
                /// @dev A valid split has the same number of splits and ratios.
                /// @dev There can only be a max of 5 parties split with.
                /// @dev Total of the ratios should be 100 which is relative.
                /// @param _splits The addresses the amount is being split with.
                /// @param _ratios The ratios each address in _splits is getting.
                function _checkSplits(
                    address payable[] calldata _splits,
                    uint8[] calldata _ratios
                ) internal pure {
                    require(_splits.length > 0, "checkSplits::Must have at least 1 split");
                    require(_splits.length <= 5, "checkSplits::Split exceeded max size");
                    require(
                        _splits.length == _ratios.length,
                        "checkSplits::Splits and ratios must be equal"
                    );
                    uint256 totalRatio = 0;
                    for (uint256 i = 0; i < _ratios.length; i++) {
                        totalRatio += _ratios[i];
                    }
                    require(totalRatio == 100, "checkSplits::Total must be equal to 100");
                }
                /// @notice Checks to see if the sender has approved the marketplace to move tokens.
                /// @dev This is for offers/buys/bids and the allowance of erc20 tokens.
                /// @dev Returns on zero address because no allowance is needed for eth.
                /// @param _contract The address of the currency being checked.
                /// @param _amount The total amount being checked.
                function _senderMustHaveMarketplaceApproved(
                    address _contract,
                    uint256 _amount
                ) internal view {
                    if (_contract == address(0)) {
                        return;
                    }
                    IERC20 erc20 = IERC20(_contract);
                    require(
                        erc20.allowance(msg.sender, address(this)) >= _amount,
                        "sender needs to approve marketplace for currency"
                    );
                }
                /// @notice Checks the user has the correct amount and transfers to the marketplace.
                /// @dev If the currency used is eth (zero address) the msg value is checked.
                /// @dev If eth isnt used and eth is sent we revert the txn.
                /// @dev We need to check this contracts balance before and after the transfer to ensure no fee.
                /// @param _currencyAddress Currency address being checked and transfered.
                /// @param _amount Total amount of currency.
                function _checkAmountAndTransfer(address _currencyAddress, uint256 _amount)
                    internal
                {
                    if (_currencyAddress == address(0)) {
                        require(msg.value == _amount, "not enough eth sent");
                        return;
                    }
                    require(msg.value == 0, "msg.value should be 0 when not using eth");
                    IERC20 erc20 = IERC20(_currencyAddress);
                    uint256 balanceBefore = erc20.balanceOf(address(this));
                    erc20.safeTransferFrom(msg.sender, address(this), _amount);
                    uint256 balanceAfter = erc20.balanceOf(address(this));
                    require(
                        balanceAfter.sub(balanceBefore) == _amount,
                        "not enough tokens transfered"
                    );
                }
                /// @notice Refunds an address the designated amount.
                /// @dev Return if amount being refunded is zero.
                /// @dev Forwards to payment contract if eth is being refunded.
                /// @param _currencyAddress Address of currency being refunded.
                /// @param _amount Amount being refunded.
                /// @param _marketplaceFee Marketplace Fee (percentage) paid by _recipient.
                /// @param _recipient Address amount is being refunded to.
                function _refund(
                    address _currencyAddress,
                    uint256 _amount,
                    uint256 _marketplaceFee,
                    address _recipient
                ) internal {
                    if (_amount == 0) {
                        return;
                    }
                    uint256 requiredAmount = _amount.add(
                        _amount.mul(_marketplaceFee).div(100)
                    );
                    if (_currencyAddress == address(0)) {
                        (bool success, bytes memory data) = address(payments).call{
                            value: requiredAmount
                        }(
                            abi.encodeWithSignature(
                                "refund(address,uint256)",
                                _recipient,
                                requiredAmount
                            )
                        );
                        require(success, string(data));
                        return;
                    }
                    IERC20 erc20 = IERC20(_currencyAddress);
                    erc20.safeTransfer(_recipient, requiredAmount);
                }
                /// @notice Sends a payout to all the necessary parties.
                /// @dev Sends payments to the network, royalty if applicable, and splits for the rest.
                /// @dev Forwards payments to the payment contract if payout is happening in eth.
                /// @dev Total amount of ratios should be 100 and is relative to the total ratio left.
                /// @param _originContract Contract address of asset triggering a payout.
                /// @param _tokenId Token Id of the asset.
                /// @param _currencyAddress Address of currency being paid out.
                /// @param _amount Total amount to be paid out.
                /// @param _seller Address of the person selling the asset.
                /// @param _splitAddrs Addresses that funds need to be split against.
                /// @param _splitRatios Ratios for split pertaining to each address.
                function _payout(
                    address _originContract,
                    uint256 _tokenId,
                    address _currencyAddress,
                    uint256 _amount,
                    address _seller,
                    address payable[] memory _splitAddrs,
                    uint8[] memory _splitRatios
                ) internal {
                    require(
                        _splitAddrs.length == _splitRatios.length,
                        "Number of split addresses and ratios must be equal."
                    );
                    /*
                    The overall flow for payouts is:
                        1. Payout marketplace fee
                        2. Primary/Secondary Payouts
                            a. Primary -> If space sale, query space operator registry for platform comission and payout
                                          Else query marketplace setting for primary sale comission and payout
                            b. Secondary -> Query global royalty registry for recipients and amounts and payout
                        3. Calculate the amount for each _splitAddr based on remaining amount and payout
                     */
                    uint256 remainingAmount = _amount;
                    // Marketplace fee
                    uint256 marketplaceFee = marketplaceSettings.calculateMarketplaceFee(
                        _amount
                    );
                    address payable[] memory mktFeeRecip = new address payable[](1);
                    mktFeeRecip[0] = payable(networkBeneficiary);
                    uint256[] memory mktFee = new uint256[](1);
                    mktFee[0] = marketplaceFee;
                    _performPayouts(_currencyAddress, marketplaceFee, mktFeeRecip, mktFee);
                    if (
                        !marketplaceSettings.hasERC721TokenSold(_originContract, _tokenId)
                    ) {
                        uint256[] memory platformFee = new uint256[](1);
                        if (spaceOperatorRegistry.isApprovedSpaceOperator(_seller)) {
                            uint256 platformCommission = spaceOperatorRegistry
                                .getPlatformCommission(_seller);
                            remainingAmount = remainingAmount.sub(
                                _amount.mul(platformCommission).div(100)
                            );
                            platformFee[0] = _amount.mul(platformCommission).div(100);
                            _performPayouts(
                                _currencyAddress,
                                platformFee[0],
                                mktFeeRecip,
                                platformFee
                            );
                        } else {
                            uint256 platformCommission = marketplaceSettings
                                .getERC721ContractPrimarySaleFeePercentage(_originContract);
                            remainingAmount = remainingAmount.sub(
                                _amount.mul(platformCommission).div(100)
                            );
                            platformFee[0] = _amount.mul(platformCommission).div(100);
                            _performPayouts(
                                _currencyAddress,
                                platformFee[0],
                                mktFeeRecip,
                                platformFee
                            );
                        }
                    } else {
                        (
                            address payable[] memory receivers,
                            uint256[] memory royalties
                        ) = royaltyEngine.getRoyalty(_originContract, _tokenId, _amount);
                        uint256 totalRoyalties = 0;
                        for (uint256 i = 0; i < royalties.length; i++) {
                            totalRoyalties = totalRoyalties.add(royalties[i]);
                        }
                        remainingAmount = remainingAmount.sub(totalRoyalties);
                        _performPayouts(
                            _currencyAddress,
                            totalRoyalties,
                            receivers,
                            royalties
                        );
                    }
                    uint256[] memory remainingAmts = new uint256[](_splitAddrs.length);
                    uint256 totalSplit = 0;
                    for (uint256 i = 0; i < _splitAddrs.length; i++) {
                        remainingAmts[i] = remainingAmount.mul(_splitRatios[i]).div(100);
                        totalSplit = totalSplit.add(
                            remainingAmount.mul(_splitRatios[i]).div(100)
                        );
                    }
                    _performPayouts(
                        _currencyAddress,
                        totalSplit,
                        _splitAddrs,
                        remainingAmts
                    );
                }
                function _performPayouts(
                    address _currencyAddress,
                    uint256 _amount,
                    address payable[] memory _recipients,
                    uint256[] memory _amounts
                ) internal {
                    if (_currencyAddress == address(0)) {
                        (bool success, bytes memory data) = address(payments).call{
                            value: _amount
                        }(
                            abi.encodeWithSelector(
                                IPayments.payout.selector,
                                _recipients,
                                _amounts
                            )
                        );
                        require(success, string(data));
                    } else {
                        IERC20 erc20 = IERC20(_currencyAddress);
                        for (uint256 i = 0; i < _recipients.length; i++) {
                            erc20.safeTransfer(_recipients[i], _amounts[i]);
                        }
                    }
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.0 <0.8.0;
            import "../proxy/Initializable.sol";
            /*
             * @dev Provides information about the current execution context, including the
             * sender of the transaction and its data. While these are generally available
             * via msg.sender and msg.data, they should not be accessed in such a direct
             * manner, since when dealing with GSN meta-transactions the account sending and
             * paying for execution may not be the actual sender (as far as an application
             * is concerned).
             *
             * This contract is only required for intermediate, library-like contracts.
             */
            abstract contract ContextUpgradeable is Initializable {
                function __Context_init() internal initializer {
                    __Context_init_unchained();
                }
                function __Context_init_unchained() internal initializer {
                }
                function _msgSender() internal view virtual returns (address payable) {
                    return msg.sender;
                }
                function _msgData() internal view virtual returns (bytes memory) {
                    this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
                    return msg.data;
                }
                uint256[50] private __gap;
            }
            // SPDX-License-Identifier: MIT
            // solhint-disable-next-line compiler-version
            pragma solidity >=0.4.24 <0.8.0;
            import "../utils/AddressUpgradeable.sol";
            /**
             * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
             * behind a proxy. Since a proxied contract can't have a constructor, it's common to move constructor logic to an
             * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
             * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
             *
             * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
             * possible by providing the encoded function call as the `_data` argument to {UpgradeableProxy-constructor}.
             *
             * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
             * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
             */
            abstract contract Initializable {
                /**
                 * @dev Indicates that the contract has been initialized.
                 */
                bool private _initialized;
                /**
                 * @dev Indicates that the contract is in the process of being initialized.
                 */
                bool private _initializing;
                /**
                 * @dev Modifier to protect an initializer function from being invoked twice.
                 */
                modifier initializer() {
                    require(_initializing || _isConstructor() || !_initialized, "Initializable: contract is already initialized");
                    bool isTopLevelCall = !_initializing;
                    if (isTopLevelCall) {
                        _initializing = true;
                        _initialized = true;
                    }
                    _;
                    if (isTopLevelCall) {
                        _initializing = false;
                    }
                }
                /// @dev Returns true if and only if the function is running in the constructor
                function _isConstructor() private view returns (bool) {
                    return !AddressUpgradeable.isContract(address(this));
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity ^0.7.0;
            /**
             * @dev Collection of functions related to the address type
             */
            library AddressUpgradeable {
                /**
                 * @dev Returns true if `account` is a contract.
                 *
                 * [IMPORTANT]
                 * ====
                 * It is unsafe to assume that an address for which this function returns
                 * false is an externally-owned account (EOA) and not a contract.
                 *
                 * Among others, `isContract` will return false for the following
                 * types of addresses:
                 *
                 *  - an externally-owned account
                 *  - a contract in construction
                 *  - an address where a contract will be created
                 *  - an address where a contract lived, but was destroyed
                 * ====
                 */
                function isContract(address account) internal view returns (bool) {
                    // This method relies on extcodesize, which returns 0 for contracts in
                    // construction, since the code is only stored at the end of the
                    // constructor execution.
                    uint256 size;
                    // solhint-disable-next-line no-inline-assembly
                    assembly { size := extcodesize(account) }
                    return size > 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");
                    // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
                    (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");
                    // solhint-disable-next-line avoid-low-level-calls
                    (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");
                    // solhint-disable-next-line avoid-low-level-calls
                    (bool success, bytes memory returndata) = target.staticcall(data);
                    return _verifyCallResult(success, returndata, errorMessage);
                }
                function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private 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
                            // solhint-disable-next-line no-inline-assembly
                            assembly {
                                let returndata_size := mload(returndata)
                                revert(add(32, returndata), returndata_size)
                            }
                        } else {
                            revert(errorMessage);
                        }
                    }
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.2 <0.8.0;
            import "../../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`, checking first that contract recipients
                 * are aware of the ERC721 protocol to prevent tokens from being forever locked.
                 *
                 * Requirements:
                 *
                 * - `from` cannot be the zero address.
                 * - `to` cannot be the zero address.
                 * - `tokenId` token must exist and be owned by `from`.
                 * - If the caller is not `from`, it must be have been allowed to move this token by either {approve} or {setApprovalForAll}.
                 * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
                 *
                 * Emits a {Transfer} event.
                 */
                function safeTransferFrom(address from, address to, uint256 tokenId) external;
                /**
                 * @dev Transfers `tokenId` token from `from` to `to`.
                 *
                 * WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
                 *
                 * Requirements:
                 *
                 * - `from` cannot be the zero address.
                 * - `to` cannot be the zero address.
                 * - `tokenId` token must be owned by `from`.
                 * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
                 *
                 * Emits a {Transfer} event.
                 */
                function transferFrom(address from, address to, uint256 tokenId) external;
                /**
                 * @dev Gives permission to `to` to transfer `tokenId` token to another account.
                 * The approval is cleared when the token is transferred.
                 *
                 * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
                 *
                 * Requirements:
                 *
                 * - The caller must own the token or be an approved operator.
                 * - `tokenId` must exist.
                 *
                 * Emits an {Approval} event.
                 */
                function approve(address to, uint256 tokenId) external;
                /**
                 * @dev Returns the account approved for `tokenId` token.
                 *
                 * Requirements:
                 *
                 * - `tokenId` must exist.
                 */
                function getApproved(uint256 tokenId) external view returns (address operator);
                /**
                 * @dev Approve or remove `operator` as an operator for the caller.
                 * Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
                 *
                 * Requirements:
                 *
                 * - The `operator` cannot be the caller.
                 *
                 * Emits an {ApprovalForAll} event.
                 */
                function setApprovalForAll(address operator, bool _approved) external;
                /**
                 * @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
                 *
                 * See {setApprovalForAll}
                 */
                function isApprovedForAll(address owner, address operator) external view returns (bool);
                /**
                  * @dev Safely transfers `tokenId` token from `from` to `to`.
                  *
                  * Requirements:
                  *
                  * - `from` cannot be the zero address.
                  * - `to` cannot be the zero address.
                  * - `tokenId` token must exist and be owned by `from`.
                  * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
                  * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
                  *
                  * Emits a {Transfer} event.
                  */
                function safeTransferFrom(address from, address to, uint256 tokenId, bytes calldata data) external;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.0 <0.8.0;
            /**
             * @dev Interface of the ERC20 standard as defined in the EIP.
             */
            interface IERC20 {
                /**
                 * @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 `recipient`.
                 *
                 * Returns a boolean value indicating whether the operation succeeded.
                 *
                 * Emits a {Transfer} event.
                 */
                function transfer(address recipient, 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 `sender` to `recipient` 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 sender, address recipient, uint256 amount) external returns (bool);
                /**
                 * @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);
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.0 <0.8.0;
            import "./IERC20.sol";
            import "../../math/SafeMath.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 SafeMath for uint256;
                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'
                    // solhint-disable-next-line max-line-length
                    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).add(value);
                    _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
                }
                function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
                    uint256 newAllowance = token.allowance(address(this), spender).sub(value, "SafeERC20: decreased allowance below zero");
                    _callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
                }
                /**
                 * @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
                 * on the return value: the return value is optional (but if data is returned, it must not be false).
                 * @param token The token targeted by the call.
                 * @param data The call data (encoded using abi.encode or one of its variants).
                 */
                function _callOptionalReturn(IERC20 token, bytes memory data) private {
                    // We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
                    // we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
                    // the target address contains contract code and also asserts for success in the low-level call.
                    bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
                    if (returndata.length > 0) { // Return data is optional
                        // solhint-disable-next-line max-line-length
                        require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
                    }
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.0 <0.8.0;
            /**
             * @dev Wrappers over Solidity's arithmetic operations with added overflow
             * checks.
             *
             * Arithmetic operations in Solidity wrap on overflow. This can easily result
             * in bugs, because programmers usually assume that an overflow raises an
             * error, which is the standard behavior in high level programming languages.
             * `SafeMath` restores this intuition by reverting the transaction when an
             * operation overflows.
             *
             * Using this library instead of the unchecked operations eliminates an entire
             * class of bugs, so it's recommended to use it always.
             */
            library SafeMath {
                /**
                 * @dev Returns the addition of two unsigned integers, with an overflow flag.
                 *
                 * _Available since v3.4._
                 */
                function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
                    uint256 c = a + b;
                    if (c < a) return (false, 0);
                    return (true, c);
                }
                /**
                 * @dev Returns the substraction of two unsigned integers, with an overflow flag.
                 *
                 * _Available since v3.4._
                 */
                function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
                    if (b > a) return (false, 0);
                    return (true, a - b);
                }
                /**
                 * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
                 *
                 * _Available since v3.4._
                 */
                function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
                    // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
                    // benefit is lost if 'b' is also tested.
                    // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
                    if (a == 0) return (true, 0);
                    uint256 c = a * b;
                    if (c / a != b) return (false, 0);
                    return (true, c);
                }
                /**
                 * @dev Returns the division of two unsigned integers, with a division by zero flag.
                 *
                 * _Available since v3.4._
                 */
                function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
                    if (b == 0) return (false, 0);
                    return (true, a / b);
                }
                /**
                 * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
                 *
                 * _Available since v3.4._
                 */
                function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
                    if (b == 0) return (false, 0);
                    return (true, a % b);
                }
                /**
                 * @dev Returns the addition of two unsigned integers, reverting on
                 * overflow.
                 *
                 * Counterpart to Solidity's `+` operator.
                 *
                 * Requirements:
                 *
                 * - Addition cannot overflow.
                 */
                function add(uint256 a, uint256 b) internal pure returns (uint256) {
                    uint256 c = a + b;
                    require(c >= a, "SafeMath: addition overflow");
                    return c;
                }
                /**
                 * @dev Returns the subtraction of two unsigned integers, reverting on
                 * overflow (when the result is negative).
                 *
                 * Counterpart to Solidity's `-` operator.
                 *
                 * Requirements:
                 *
                 * - Subtraction cannot overflow.
                 */
                function sub(uint256 a, uint256 b) internal pure returns (uint256) {
                    require(b <= a, "SafeMath: subtraction overflow");
                    return a - b;
                }
                /**
                 * @dev Returns the multiplication of two unsigned integers, reverting on
                 * overflow.
                 *
                 * Counterpart to Solidity's `*` operator.
                 *
                 * Requirements:
                 *
                 * - Multiplication cannot overflow.
                 */
                function mul(uint256 a, uint256 b) internal pure returns (uint256) {
                    if (a == 0) return 0;
                    uint256 c = a * b;
                    require(c / a == b, "SafeMath: multiplication overflow");
                    return c;
                }
                /**
                 * @dev Returns the integer division of two unsigned integers, reverting on
                 * division by zero. The result is rounded towards zero.
                 *
                 * Counterpart to Solidity's `/` operator. Note: this function uses a
                 * `revert` opcode (which leaves remaining gas untouched) while Solidity
                 * uses an invalid opcode to revert (consuming all remaining gas).
                 *
                 * Requirements:
                 *
                 * - The divisor cannot be zero.
                 */
                function div(uint256 a, uint256 b) internal pure returns (uint256) {
                    require(b > 0, "SafeMath: division by zero");
                    return a / b;
                }
                /**
                 * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
                 * reverting when dividing by zero.
                 *
                 * Counterpart to Solidity's `%` operator. This function uses a `revert`
                 * opcode (which leaves remaining gas untouched) while Solidity uses an
                 * invalid opcode to revert (consuming all remaining gas).
                 *
                 * Requirements:
                 *
                 * - The divisor cannot be zero.
                 */
                function mod(uint256 a, uint256 b) internal pure returns (uint256) {
                    require(b > 0, "SafeMath: modulo by zero");
                    return a % b;
                }
                /**
                 * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
                 * overflow (when the result is negative).
                 *
                 * CAUTION: This function is deprecated because it requires allocating memory for the error
                 * message unnecessarily. For custom revert reasons use {trySub}.
                 *
                 * Counterpart to Solidity's `-` operator.
                 *
                 * Requirements:
                 *
                 * - Subtraction cannot overflow.
                 */
                function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
                    require(b <= a, errorMessage);
                    return a - b;
                }
                /**
                 * @dev Returns the integer division of two unsigned integers, reverting with custom message on
                 * division by zero. The result is rounded towards zero.
                 *
                 * CAUTION: This function is deprecated because it requires allocating memory for the error
                 * message unnecessarily. For custom revert reasons use {tryDiv}.
                 *
                 * Counterpart to Solidity's `/` operator. Note: this function uses a
                 * `revert` opcode (which leaves remaining gas untouched) while Solidity
                 * uses an invalid opcode to revert (consuming all remaining gas).
                 *
                 * Requirements:
                 *
                 * - The divisor cannot be zero.
                 */
                function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
                    require(b > 0, errorMessage);
                    return a / b;
                }
                /**
                 * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
                 * reverting with custom message when dividing by zero.
                 *
                 * CAUTION: This function is deprecated because it requires allocating memory for the error
                 * message unnecessarily. For custom revert reasons use {tryMod}.
                 *
                 * Counterpart to Solidity's `%` operator. This function uses a `revert`
                 * opcode (which leaves remaining gas untouched) while Solidity uses an
                 * invalid opcode to revert (consuming all remaining gas).
                 *
                 * Requirements:
                 *
                 * - The divisor cannot be zero.
                 */
                function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
                    require(b > 0, errorMessage);
                    return a % b;
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            import "../../marketplace/IMarketplaceSettings.sol";
            import "../../royalty/creator/IERC721CreatorRoyalty.sol";
            import "../../payments/IPayments.sol";
            import "../../registry/spaces/ISpaceOperatorRegistry.sol";
            import "../../registry/token/IApprovedTokenRegistry.sol";
            import "../../royalty/creator/IRoyaltyEngine.sol";
            /// @author koloz
            /// @title SuperRareBazaar Storage Contract
            /// @dev STORAGE CAN ONLY BE APPENDED NOT INSERTED OR MODIFIED
            contract SuperRareBazaarStorage {
                /////////////////////////////////////////////////////////////////////////
                // Constants
                /////////////////////////////////////////////////////////////////////////
                // Auction Types
                bytes32 public constant COLDIE_AUCTION = "COLDIE_AUCTION";
                bytes32 public constant SCHEDULED_AUCTION = "SCHEDULED_AUCTION";
                bytes32 public constant NO_AUCTION = bytes32(0);
                /////////////////////////////////////////////////////////////////////////
                // Structs
                /////////////////////////////////////////////////////////////////////////
                // The Offer truct for a given token:
                // buyer - address of person making the offer
                // currencyAddress - address of the erc20 token used for an offer
                //                   or the zero address for eth
                // amount - offer in wei/full erc20 value
                // marketplaceFee - the amount that is taken by the network on offer acceptance.
                struct Offer {
                    address payable buyer;
                    uint256 amount;
                    uint256 timestamp;
                    uint8 marketplaceFee;
                    bool convertible;
                }
                // The Sale Price struct for a given token:
                // seller - address of the person selling the token
                // currencyAddress - address of the erc20 token used for an offer
                //                   or the zero address for eth
                // amount - offer in wei/full erc20 value
                struct SalePrice {
                    address payable seller;
                    address currencyAddress;
                    uint256 amount;
                    address payable[] splitRecipients;
                    uint8[] splitRatios;
                }
                // Structure of an Auction:
                // auctionCreator - creator of the auction
                // creationBlock - time that the auction was created/configured
                // startingBlock - time that the auction starts on
                // lengthOfAuction - how long the auction is
                // currencyAddress - address of the erc20 token used for an offer
                //                   or the zero address for eth
                // minimumBid - min amount a bidder can bid at the start of an auction.
                // auctionType - type of auction, represented as the formatted bytes 32 string
                struct Auction {
                    address payable auctionCreator;
                    uint256 creationBlock;
                    uint256 startingTime;
                    uint256 lengthOfAuction;
                    address currencyAddress;
                    uint256 minimumBid;
                    bytes32 auctionType;
                    address payable[] splitRecipients;
                    uint8[] splitRatios;
                }
                struct Bid {
                    address payable bidder;
                    address currencyAddress;
                    uint256 amount;
                    uint8 marketplaceFee;
                }
                /////////////////////////////////////////////////////////////////////////
                // Events
                /////////////////////////////////////////////////////////////////////////
                event Sold(
                    address indexed _originContract,
                    address indexed _buyer,
                    address indexed _seller,
                    address _currencyAddress,
                    uint256 _amount,
                    uint256 _tokenId
                );
                event SetSalePrice(
                    address indexed _originContract,
                    address indexed _currencyAddress,
                    address _target,
                    uint256 _amount,
                    uint256 _tokenId,
                    address payable[] _splitRecipients,
                    uint8[] _splitRatios
                );
                event OfferPlaced(
                    address indexed _originContract,
                    address indexed _bidder,
                    address indexed _currencyAddress,
                    uint256 _amount,
                    uint256 _tokenId,
                    bool _convertible
                );
                event AcceptOffer(
                    address indexed _originContract,
                    address indexed _bidder,
                    address indexed _seller,
                    address _currencyAddress,
                    uint256 _amount,
                    uint256 _tokenId,
                    address payable[] _splitAddresses,
                    uint8[] _splitRatios
                );
                event CancelOffer(
                    address indexed _originContract,
                    address indexed _bidder,
                    address indexed _currencyAddress,
                    uint256 _amount,
                    uint256 _tokenId
                );
                event NewAuction(
                    address indexed _contractAddress,
                    uint256 indexed _tokenId,
                    address indexed _auctionCreator,
                    address _currencyAddress,
                    uint256 _startingTime,
                    uint256 _minimumBid,
                    uint256 _lengthOfAuction
                );
                event CancelAuction(
                    address indexed _contractAddress,
                    uint256 indexed _tokenId,
                    address indexed _auctionCreator
                );
                event AuctionBid(
                    address indexed _contractAddress,
                    address indexed _bidder,
                    uint256 indexed _tokenId,
                    address _currencyAddress,
                    uint256 _amount,
                    bool _startedAuction,
                    uint256 _newAuctionLength,
                    address _previousBidder
                );
                event AuctionSettled(
                    address indexed _contractAddress,
                    address indexed _bidder,
                    address _seller,
                    uint256 indexed _tokenId,
                    address _currencyAddress,
                    uint256 _amount
                );
                /////////////////////////////////////////////////////////////////////////
                // State Variables
                /////////////////////////////////////////////////////////////////////////
                // Current marketplace settings implementation to be used
                IMarketplaceSettings public marketplaceSettings;
                // Current creator royalty implementation to be used
                IERC721CreatorRoyalty public royaltyRegistry;
                // Address of the global royalty engine being used.
                IRoyaltyEngineV1 public royaltyEngine;
                // Current SuperRareMarketplace implementation to be used
                address public superRareMarketplace;
                // Current SuperRareAuctionHouse implementation to be used
                address public superRareAuctionHouse;
                // Current SpaceOperatorRegistry implementation to be used.
                ISpaceOperatorRegistry public spaceOperatorRegistry;
                // Current ApprovedTokenRegistry implementation being used for currencies.
                IApprovedTokenRegistry public approvedTokenRegistry;
                // Current payments contract to use
                IPayments public payments;
                // Address to be used for staking registry.
                address public stakingRegistry;
                // Address of the network beneficiary
                address public networkBeneficiary;
                // A minimum increase in bid amount when out bidding someone.
                uint8 public minimumBidIncreasePercentage; // 10 = 10%
                // Maximum length that an auction can be.
                uint256 public maxAuctionLength;
                // Extension length for an auction
                uint256 public auctionLengthExtension;
                // Offer cancellation delay
                uint256 public offerCancelationDelay;
                // Mapping from contract to mapping of tokenId to mapping of target to sale price.
                mapping(address => mapping(uint256 => mapping(address => SalePrice)))
                    public tokenSalePrices;
                // Mapping from contract to mapping of tokenId to mapping of currency address to Current Offer.
                mapping(address => mapping(uint256 => mapping(address => Offer)))
                    public tokenCurrentOffers;
                // Mapping from contract to mapping of tokenId to Auction.
                mapping(address => mapping(uint256 => Auction)) public tokenAuctions;
                // Mapping from contract to mapping of tokenId to Bid.
                mapping(address => mapping(uint256 => Bid)) public auctionBids;
                uint256[50] private __gap;
                /// ALL NEW STORAGE MUST COME AFTER THIS
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.0 <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);
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.2 <0.8.0;
            /**
             * @dev Collection of functions related to the address type
             */
            library Address {
                /**
                 * @dev Returns true if `account` is a contract.
                 *
                 * [IMPORTANT]
                 * ====
                 * It is unsafe to assume that an address for which this function returns
                 * false is an externally-owned account (EOA) and not a contract.
                 *
                 * Among others, `isContract` will return false for the following
                 * types of addresses:
                 *
                 *  - an externally-owned account
                 *  - a contract in construction
                 *  - an address where a contract will be created
                 *  - an address where a contract lived, but was destroyed
                 * ====
                 */
                function isContract(address account) internal view returns (bool) {
                    // This method relies on extcodesize, which returns 0 for contracts in
                    // construction, since the code is only stored at the end of the
                    // constructor execution.
                    uint256 size;
                    // solhint-disable-next-line no-inline-assembly
                    assembly { size := extcodesize(account) }
                    return size > 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");
                    // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
                    (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");
                    // solhint-disable-next-line avoid-low-level-calls
                    (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");
                    // solhint-disable-next-line avoid-low-level-calls
                    (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");
                    // solhint-disable-next-line avoid-low-level-calls
                    (bool success, bytes memory returndata) = target.delegatecall(data);
                    return _verifyCallResult(success, returndata, errorMessage);
                }
                function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private 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
                            // solhint-disable-next-line no-inline-assembly
                            assembly {
                                let returndata_size := mload(returndata)
                                revert(add(32, returndata), returndata_size)
                            }
                        } else {
                            revert(errorMessage);
                        }
                    }
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            /**
             * @title IMarketplaceSettings Settings governing a marketplace.
             */
            interface IMarketplaceSettings {
                /////////////////////////////////////////////////////////////////////////
                // Marketplace Min and Max Values
                /////////////////////////////////////////////////////////////////////////
                /**
                 * @dev Get the max value to be used with the marketplace.
                 * @return uint256 wei value.
                 */
                function getMarketplaceMaxValue() external view returns (uint256);
                /**
                 * @dev Get the max value to be used with the marketplace.
                 * @return uint256 wei value.
                 */
                function getMarketplaceMinValue() external view returns (uint256);
                /////////////////////////////////////////////////////////////////////////
                // Marketplace Fee
                /////////////////////////////////////////////////////////////////////////
                /**
                 * @dev Get the marketplace fee percentage.
                 * @return uint8 wei fee.
                 */
                function getMarketplaceFeePercentage() external view returns (uint8);
                /**
                 * @dev Utility function for calculating the marketplace fee for given amount of wei.
                 * @param _amount uint256 wei amount.
                 * @return uint256 wei fee.
                 */
                function calculateMarketplaceFee(uint256 _amount)
                    external
                    view
                    returns (uint256);
                /////////////////////////////////////////////////////////////////////////
                // Primary Sale Fee
                /////////////////////////////////////////////////////////////////////////
                /**
                 * @dev Get the primary sale fee percentage for a specific ERC721 contract.
                 * @param _contractAddress address ERC721Contract address.
                 * @return uint8 wei primary sale fee.
                 */
                function getERC721ContractPrimarySaleFeePercentage(address _contractAddress)
                    external
                    view
                    returns (uint8);
                /**
                 * @dev Utility function for calculating the primary sale fee for given amount of wei
                 * @param _contractAddress address ERC721Contract address.
                 * @param _amount uint256 wei amount.
                 * @return uint256 wei fee.
                 */
                function calculatePrimarySaleFee(address _contractAddress, uint256 _amount)
                    external
                    view
                    returns (uint256);
                /**
                 * @dev Check whether the ERC721 token has sold at least once.
                 * @param _contractAddress address ERC721Contract address.
                 * @param _tokenId uint256 token ID.
                 * @return bool of whether the token has sold.
                 */
                function hasERC721TokenSold(address _contractAddress, uint256 _tokenId)
                    external
                    view
                    returns (bool);
                /**
                 * @dev Mark a token as sold.
                 * Requirements:
                 *
                 * - `_contractAddress` cannot be the zero address.
                 * @param _contractAddress address ERC721Contract address.
                 * @param _tokenId uint256 token ID.
                 * @param _hasSold bool of whether the token should be marked sold or not.
                 */
                function markERC721Token(
                    address _contractAddress,
                    uint256 _tokenId,
                    bool _hasSold
                ) external;
                function setERC721ContractPrimarySaleFeePercentage(
                    address _contractAddress,
                    uint8 _percentage
                ) external;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            import "../../token/ERC721/IERC721TokenCreator.sol";
            /**
             * @title IERC721CreatorRoyalty Token level royalty interface.
             */
            interface IERC721CreatorRoyalty is IERC721TokenCreator {
                /**
                 * @dev Get the royalty fee percentage for a specific ERC721 contract.
                 * @param _contractAddress address ERC721Contract address.
                 * @param _tokenId uint256 token ID.
                 * @return uint8 wei royalty fee.
                 */
                function getERC721TokenRoyaltyPercentage(
                    address _contractAddress,
                    uint256 _tokenId
                ) external view returns (uint8);
                /**
                 * @dev Utililty function to calculate the royalty fee for a token.
                 * @param _contractAddress address ERC721Contract address.
                 * @param _tokenId uint256 token ID.
                 * @param _amount uint256 wei amount.
                 * @return uint256 wei fee.
                 */
                function calculateRoyaltyFee(
                    address _contractAddress,
                    uint256 _tokenId,
                    uint256 _amount
                ) external view returns (uint256);
                /**
                 * @dev Utililty function to set the royalty percentage for a specific ERC721 contract.
                 * @param _contractAddress address ERC721Contract address.
                 * @param _percentage percentage for royalty
                 */
                function setPercentageForSetERC721ContractRoyalty(
                    address _contractAddress,
                    uint8 _percentage
                ) external;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            /// @author koloz
            /// @title IPayments
            /// @notice Interface for the Payments contract used.
            interface IPayments {
                function refund(address _payee, uint256 _amount) external payable;
                function payout(address[] calldata _splits, uint256[] calldata _amounts)
                    external
                    payable;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            /// @author koloz
            /// @title ISpaceOperatorRegistry
            /// @notice The interface for the SpaceOperatorRegistry
            interface ISpaceOperatorRegistry {
                function getPlatformCommission(address _operator)
                    external
                    view
                    returns (uint8);
                function setPlatformCommission(address _operator, uint8 _commission)
                    external;
                function isApprovedSpaceOperator(address _operator)
                    external
                    view
                    returns (bool);
                function setSpaceOperatorApproved(address _operator, bool _approved)
                    external;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            interface IApprovedTokenRegistry {
                /// @notice Returns if a token has been approved or not.
                /// @param _tokenContract Contract of token being checked.
                /// @return True if the token is allowed, false otherwise.
                function isApprovedToken(address _tokenContract)
                    external
                    view
                    returns (bool);
                /// @notice Adds a token to the list of approved tokens.
                /// @param _tokenContract Contract of token being approved.
                function addApprovedToken(address _tokenContract) external;
                /// @notice Removes a token from the approved tokens list.
                /// @param _tokenContract Contract of token being approved.
                function removeApprovedToken(address _tokenContract) external;
                /// @notice Sets whether all token contracts should be approved.
                /// @param _allTokensApproved Bool denoting if all tokens should be approved.
                function setAllTokensApproved(bool _allTokensApproved) external;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            /// @author: manifold.xyz
            /**
             * @dev Lookup engine interface
             */
            interface IRoyaltyEngineV1 {
                /**
                 * Get the royalty for a given token (address, id) and value amount.  Does not cache the bps/amounts.  Caches the spec for a given token address
                 *
                 * @param tokenAddress - The address of the token
                 * @param tokenId      - The id of the token
                 * @param value        - The value you wish to get the royalty of
                 *
                 * returns Two arrays of equal length, royalty recipients and the corresponding amount each recipient should get
                 */
                function getRoyalty(
                    address tokenAddress,
                    uint256 tokenId,
                    uint256 value
                )
                    external
                    returns (address payable[] memory recipients, uint256[] memory amounts);
                /**
                 * View only version of getRoyalty
                 *
                 * @param tokenAddress - The address of the token
                 * @param tokenId      - The id of the token
                 * @param value        - The value you wish to get the royalty of
                 *
                 * returns Two arrays of equal length, royalty recipients and the corresponding amount each recipient should get
                 */
                function getRoyaltyView(
                    address tokenAddress,
                    uint256 tokenId,
                    uint256 value
                )
                    external
                    view
                    returns (address payable[] memory recipients, uint256[] memory amounts);
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            interface IERC721TokenCreator {
                function tokenCreator(address _contractAddress, uint256 _tokenId)
                    external
                    view
                    returns (address payable);
            }
            

            File 4 of 8: MarketplaceSettingsV2
            // SPDX-License-Identifier: MIT
            pragma solidity 0.8.10;
            import {IMarketplaceSettings} from "./IMarketplaceSettings.sol";
            import {Ownable} from "openzeppelin-contracts/access/Ownable.sol";
            import {AccessControl} from "openzeppelin-contracts/access/AccessControl.sol";
            import {ITokenCreator} from "../token/ITokenCreator.sol";
            import {IERC721} from "openzeppelin-contracts/token/ERC721/IERC721.sol";
            import {EnumerableSet} from "openzeppelin-contracts/utils/structs/EnumerableSet.sol";
            contract MarketplaceSettingsV2 is Ownable, AccessControl, IMarketplaceSettings {
                bytes32 public constant TOKEN_MARK_ROLE = keccak256("TOKEN_MARK_ROLE");
                // This is meant to be the MarketplaceSettings contract located in the V1 folder
                IMarketplaceSettings private oldMarketplaceSettings;
                // EnumerableSet library method
                using EnumerableSet for EnumerableSet.AddressSet;
                // EnumerableSet of contracts marked sold
                EnumerableSet.AddressSet private contractSold;
                uint256 private maxValue;
                uint256 private minValue;
                uint8 private marketplaceFeePercentage;
                uint8 private primarySaleFeePercentage;
                constructor(address newOwner, address oldSettings) {
                    maxValue = 2**254;
                    minValue = 1000;
                    marketplaceFeePercentage = 3;
                    primarySaleFeePercentage = 15;
                    require(
                        newOwner != address(0),
                        "constructor::New owner address cannot be null"
                    );
                    require(
                        oldSettings != address(0),
                        "constructor::Old Marketplace Settings address cannot be null"
                    );
                    _setupRole(AccessControl.DEFAULT_ADMIN_ROLE, newOwner);
                    _setupRole(TOKEN_MARK_ROLE, newOwner);
                    transferOwnership(newOwner);
                    oldMarketplaceSettings = IMarketplaceSettings(oldSettings);
                }
                function grantMarketplaceAccess(address _account) external {
                    require(
                        hasRole(AccessControl.DEFAULT_ADMIN_ROLE, _msgSender()),
                        "grantMarketplaceAccess::Must be admin to call method"
                    );
                    grantRole(TOKEN_MARK_ROLE, _account);
                }
                function getMarketplaceMaxValue() external view override returns (uint256) {
                    return maxValue;
                }
                function setPrimarySaleFeePercentage(uint8 _primarySaleFeePercentage)
                    external
                    onlyOwner
                {
                    primarySaleFeePercentage = _primarySaleFeePercentage;
                }
                function setMarketplaceMaxValue(uint256 _maxValue) external onlyOwner {
                    maxValue = _maxValue;
                }
                function getMarketplaceMinValue() external view override returns (uint256) {
                    return minValue;
                }
                function setMarketplaceMinValue(uint256 _minValue) external onlyOwner {
                    minValue = _minValue;
                }
                function getMarketplaceFeePercentage()
                    external
                    view
                    override
                    returns (uint8)
                {
                    return marketplaceFeePercentage;
                }
                function setMarketplaceFeePercentage(uint8 _percentage) external onlyOwner {
                    require(
                        _percentage <= 100,
                        "setMarketplaceFeePercentage::_percentage must be <= 100"
                    );
                    marketplaceFeePercentage = _percentage;
                }
                function calculateMarketplaceFee(uint256 _amount)
                    external
                    view
                    override
                    returns (uint256)
                {
                    return (_amount * marketplaceFeePercentage) / 100;
                }
                function getERC721ContractPrimarySaleFeePercentage(address)
                    external
                    view
                    override
                    returns (uint8)
                {
                    return primarySaleFeePercentage;
                }
                function setERC721ContractPrimarySaleFeePercentage(
                    address _contractAddress,
                    uint8 _percentage
                ) external override {}
                function calculatePrimarySaleFee(address, uint256 _amount)
                    external
                    view
                    override
                    returns (uint256)
                {
                    return (_amount * primarySaleFeePercentage) / 100;
                }
                function hasERC721TokenSold(address _contractAddress, uint256 _tokenId)
                    external
                    view
                    override
                    returns (bool)
                {
                    bool contractHasSold = contractSold.contains(_contractAddress);
                    if (contractHasSold) return true;
                    return
                        oldMarketplaceSettings.hasERC721TokenSold(
                            _contractAddress,
                            _tokenId
                        );
                }
                function markERC721Token(
                    address _contractAddress,
                    uint256 _tokenId,
                    bool _hasSold
                ) public override {
                    require(
                        hasRole(TOKEN_MARK_ROLE, msg.sender),
                        "markERC721Token::Must have TOKEN_MARK_ROLE role to call method"
                    );
                    oldMarketplaceSettings.markERC721Token(
                        _contractAddress,
                        _tokenId,
                        _hasSold
                    );
                }
                function markTokensAsSold(
                    address _originContract,
                    uint256[] calldata _tokenIds
                ) external {
                    require(
                        hasRole(TOKEN_MARK_ROLE, msg.sender),
                        "markERC721Token::Must have TOKEN_MARK_ROLE role to call method"
                    );
                    // limit to batches of 2000
                    require(
                        _tokenIds.length <= 2000,
                        "markTokensAsSold::Attempted to mark more than 2000 tokens as sold"
                    );
                    // Mark provided tokens as sold.
                    for (uint256 i = 0; i < _tokenIds.length; i++) {
                        markERC721Token(_originContract, _tokenIds[i], true);
                    }
                }
                function markContractAsSold(address _contractAddress)
                    external
                    returns (bool)
                {
                    require(
                        hasRole(TOKEN_MARK_ROLE, msg.sender),
                        "markContract::Must have TOKEN_MARK_ROLE role to call method"
                    );
                    // Prevents contract address from being marked multiple times
                    require(
                        !contractSold.contains(_contractAddress),
                        "markContract::Contract already marked as sold"
                    );
                    // Adds contract address to set
                    return contractSold.add(_contractAddress);
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.8.10;
            /**
             * @title IMarketplaceSettings Settings governing a marketplace.
             */
            interface IMarketplaceSettings {
                /////////////////////////////////////////////////////////////////////////
                // Marketplace Min and Max Values
                /////////////////////////////////////////////////////////////////////////
                /**
                 * @dev Get the max value to be used with the marketplace.
                 * @return uint256 wei value.
                 */
                function getMarketplaceMaxValue() external view returns (uint256);
                /**
                 * @dev Get the max value to be used with the marketplace.
                 * @return uint256 wei value.
                 */
                function getMarketplaceMinValue() external view returns (uint256);
                /////////////////////////////////////////////////////////////////////////
                // Marketplace Fee
                /////////////////////////////////////////////////////////////////////////
                /**
                 * @dev Get the marketplace fee percentage.
                 * @return uint8 wei fee.
                 */
                function getMarketplaceFeePercentage() external view returns (uint8);
                /**
                 * @dev Utility function for calculating the marketplace fee for given amount of wei.
                 * @param _amount uint256 wei amount.
                 * @return uint256 wei fee.
                 */
                function calculateMarketplaceFee(uint256 _amount)
                    external
                    view
                    returns (uint256);
                /////////////////////////////////////////////////////////////////////////
                // Primary Sale Fee
                /////////////////////////////////////////////////////////////////////////
                /**
                 * @dev Get the primary sale fee percentage for a specific ERC721 contract.
                 * @param _contractAddress address ERC721Contract address.
                 * @return uint8 wei primary sale fee.
                 */
                function getERC721ContractPrimarySaleFeePercentage(address _contractAddress)
                    external
                    view
                    returns (uint8);
                /**
                 * @dev Utility function for calculating the primary sale fee for given amount of wei
                 * @param _contractAddress address ERC721Contract address.
                 * @param _amount uint256 wei amount.
                 * @return uint256 wei fee.
                 */
                function calculatePrimarySaleFee(address _contractAddress, uint256 _amount)
                    external
                    view
                    returns (uint256);
                /**
                 * @dev Check whether the ERC721 token has sold at least once.
                 * @param _contractAddress address ERC721Contract address.
                 * @param _tokenId uint256 token ID.
                 * @return bool of whether the token has sold.
                 */
                function hasERC721TokenSold(address _contractAddress, uint256 _tokenId)
                    external
                    view
                    returns (bool);
                /**
                 * @dev Mark a token as sold.
                 * Requirements:
                 *
                 * - `_contractAddress` cannot be the zero address.
                 * @param _contractAddress address ERC721Contract address.
                 * @param _tokenId uint256 token ID.
                 * @param _hasSold bool of whether the token should be marked sold or not.
                 */
                function markERC721Token(
                    address _contractAddress,
                    uint256 _tokenId,
                    bool _hasSold
                ) external;
                function setERC721ContractPrimarySaleFeePercentage(
                    address _contractAddress,
                    uint8 _percentage
                ) external;
            }
            // SPDX-License-Identifier: MIT
            // OpenZeppelin Contracts v4.4.1 (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 Returns the address of the current owner.
                 */
                function owner() public view virtual returns (address) {
                    return _owner;
                }
                /**
                 * @dev Throws if called by any account other than the owner.
                 */
                modifier onlyOwner() {
                    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);
                }
            }
            // SPDX-License-Identifier: MIT
            // OpenZeppelin Contracts (last updated v4.6.0) (access/AccessControl.sol)
            pragma solidity ^0.8.0;
            import "./IAccessControl.sol";
            import "../utils/Context.sol";
            import "../utils/Strings.sol";
            import "../utils/introspection/ERC165.sol";
            /**
             * @dev Contract module that allows children to implement role-based access
             * control mechanisms. This is a lightweight version that doesn't allow enumerating role
             * members except through off-chain means by accessing the contract event logs. Some
             * applications may benefit from on-chain enumerability, for those cases see
             * {AccessControlEnumerable}.
             *
             * Roles are referred to by their `bytes32` identifier. These should be exposed
             * in the external API and be unique. The best way to achieve this is by
             * using `public constant` hash digests:
             *
             * ```
             * bytes32 public constant MY_ROLE = keccak256("MY_ROLE");
             * ```
             *
             * Roles can be used to represent a set of permissions. To restrict access to a
             * function call, use {hasRole}:
             *
             * ```
             * function foo() public {
             *     require(hasRole(MY_ROLE, msg.sender));
             *     ...
             * }
             * ```
             *
             * Roles can be granted and revoked dynamically via the {grantRole} and
             * {revokeRole} functions. Each role has an associated admin role, and only
             * accounts that have a role's admin role can call {grantRole} and {revokeRole}.
             *
             * By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means
             * that only accounts with this role will be able to grant or revoke other
             * roles. More complex role relationships can be created by using
             * {_setRoleAdmin}.
             *
             * WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to
             * grant and revoke this role. Extra precautions should be taken to secure
             * accounts that have been granted it.
             */
            abstract contract AccessControl is Context, IAccessControl, ERC165 {
                struct RoleData {
                    mapping(address => bool) members;
                    bytes32 adminRole;
                }
                mapping(bytes32 => RoleData) private _roles;
                bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
                /**
                 * @dev Modifier that checks that an account has a specific role. Reverts
                 * with a standardized message including the required role.
                 *
                 * The format of the revert reason is given by the following regular expression:
                 *
                 *  /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/
                 *
                 * _Available since v4.1._
                 */
                modifier onlyRole(bytes32 role) {
                    _checkRole(role);
                    _;
                }
                /**
                 * @dev See {IERC165-supportsInterface}.
                 */
                function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
                    return interfaceId == type(IAccessControl).interfaceId || super.supportsInterface(interfaceId);
                }
                /**
                 * @dev Returns `true` if `account` has been granted `role`.
                 */
                function hasRole(bytes32 role, address account) public view virtual override returns (bool) {
                    return _roles[role].members[account];
                }
                /**
                 * @dev Revert with a standard message if `_msgSender()` is missing `role`.
                 * Overriding this function changes the behavior of the {onlyRole} modifier.
                 *
                 * Format of the revert message is described in {_checkRole}.
                 *
                 * _Available since v4.6._
                 */
                function _checkRole(bytes32 role) internal view virtual {
                    _checkRole(role, _msgSender());
                }
                /**
                 * @dev Revert with a standard message if `account` is missing `role`.
                 *
                 * The format of the revert reason is given by the following regular expression:
                 *
                 *  /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/
                 */
                function _checkRole(bytes32 role, address account) internal view virtual {
                    if (!hasRole(role, account)) {
                        revert(
                            string(
                                abi.encodePacked(
                                    "AccessControl: account ",
                                    Strings.toHexString(uint160(account), 20),
                                    " is missing role ",
                                    Strings.toHexString(uint256(role), 32)
                                )
                            )
                        );
                    }
                }
                /**
                 * @dev Returns the admin role that controls `role`. See {grantRole} and
                 * {revokeRole}.
                 *
                 * To change a role's admin, use {_setRoleAdmin}.
                 */
                function getRoleAdmin(bytes32 role) public view virtual override returns (bytes32) {
                    return _roles[role].adminRole;
                }
                /**
                 * @dev Grants `role` to `account`.
                 *
                 * If `account` had not been already granted `role`, emits a {RoleGranted}
                 * event.
                 *
                 * Requirements:
                 *
                 * - the caller must have ``role``'s admin role.
                 */
                function grantRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
                    _grantRole(role, account);
                }
                /**
                 * @dev Revokes `role` from `account`.
                 *
                 * If `account` had been granted `role`, emits a {RoleRevoked} event.
                 *
                 * Requirements:
                 *
                 * - the caller must have ``role``'s admin role.
                 */
                function revokeRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
                    _revokeRole(role, account);
                }
                /**
                 * @dev Revokes `role` from the calling account.
                 *
                 * Roles are often managed via {grantRole} and {revokeRole}: this function's
                 * purpose is to provide a mechanism for accounts to lose their privileges
                 * if they are compromised (such as when a trusted device is misplaced).
                 *
                 * If the calling account had been revoked `role`, emits a {RoleRevoked}
                 * event.
                 *
                 * Requirements:
                 *
                 * - the caller must be `account`.
                 */
                function renounceRole(bytes32 role, address account) public virtual override {
                    require(account == _msgSender(), "AccessControl: can only renounce roles for self");
                    _revokeRole(role, account);
                }
                /**
                 * @dev Grants `role` to `account`.
                 *
                 * If `account` had not been already granted `role`, emits a {RoleGranted}
                 * event. Note that unlike {grantRole}, this function doesn't perform any
                 * checks on the calling account.
                 *
                 * [WARNING]
                 * ====
                 * This function should only be called from the constructor when setting
                 * up the initial roles for the system.
                 *
                 * Using this function in any other way is effectively circumventing the admin
                 * system imposed by {AccessControl}.
                 * ====
                 *
                 * NOTE: This function is deprecated in favor of {_grantRole}.
                 */
                function _setupRole(bytes32 role, address account) internal virtual {
                    _grantRole(role, account);
                }
                /**
                 * @dev Sets `adminRole` as ``role``'s admin role.
                 *
                 * Emits a {RoleAdminChanged} event.
                 */
                function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
                    bytes32 previousAdminRole = getRoleAdmin(role);
                    _roles[role].adminRole = adminRole;
                    emit RoleAdminChanged(role, previousAdminRole, adminRole);
                }
                /**
                 * @dev Grants `role` to `account`.
                 *
                 * Internal function without access restriction.
                 */
                function _grantRole(bytes32 role, address account) internal virtual {
                    if (!hasRole(role, account)) {
                        _roles[role].members[account] = true;
                        emit RoleGranted(role, account, _msgSender());
                    }
                }
                /**
                 * @dev Revokes `role` from `account`.
                 *
                 * Internal function without access restriction.
                 */
                function _revokeRole(bytes32 role, address account) internal virtual {
                    if (hasRole(role, account)) {
                        _roles[role].members[account] = false;
                        emit RoleRevoked(role, account, _msgSender());
                    }
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.8.10;
            interface ITokenCreator {
                // bytes4(keccak256(tokenCreator(uint256))) == 0x40c1a064
                function tokenCreator(uint256 _tokenId)
                    external
                    view
                    returns (address payable);
            }
            // SPDX-License-Identifier: MIT
            // OpenZeppelin Contracts (last updated v4.6.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 be have been allowed to move this token by either {approve} or {setApprovalForAll}.
                 * - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
                 *
                 * Emits a {Transfer} event.
                 */
                function safeTransferFrom(
                    address from,
                    address to,
                    uint256 tokenId
                ) external;
                /**
                 * @dev Transfers `tokenId` token from `from` to `to`.
                 *
                 * WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
                 *
                 * Requirements:
                 *
                 * - `from` cannot be the zero address.
                 * - `to` cannot be the zero address.
                 * - `tokenId` token must be owned by `from`.
                 * - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
                 *
                 * Emits a {Transfer} event.
                 */
                function transferFrom(
                    address from,
                    address to,
                    uint256 tokenId
                ) external;
                /**
                 * @dev Gives permission to `to` to transfer `tokenId` token to another account.
                 * The approval is cleared when the token is transferred.
                 *
                 * Only a single account can be approved at a time, so approving the zero address clears previous approvals.
                 *
                 * Requirements:
                 *
                 * - The caller must own the token or be an approved operator.
                 * - `tokenId` must exist.
                 *
                 * Emits an {Approval} event.
                 */
                function approve(address to, uint256 tokenId) external;
                /**
                 * @dev 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);
            }
            // SPDX-License-Identifier: MIT
            // OpenZeppelin Contracts (last updated v4.6.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.
             */
            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;
                    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;
                    assembly {
                        result := store
                    }
                    return result;
                }
            }
            // 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;
                }
            }
            // SPDX-License-Identifier: MIT
            // OpenZeppelin Contracts v4.4.1 (access/IAccessControl.sol)
            pragma solidity ^0.8.0;
            /**
             * @dev External interface of AccessControl declared to support ERC165 detection.
             */
            interface IAccessControl {
                /**
                 * @dev Emitted when `newAdminRole` is set as ``role``'s admin role, replacing `previousAdminRole`
                 *
                 * `DEFAULT_ADMIN_ROLE` is the starting admin for all roles, despite
                 * {RoleAdminChanged} not being emitted signaling this.
                 *
                 * _Available since v3.1._
                 */
                event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole);
                /**
                 * @dev Emitted when `account` is granted `role`.
                 *
                 * `sender` is the account that originated the contract call, an admin role
                 * bearer except when using {AccessControl-_setupRole}.
                 */
                event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender);
                /**
                 * @dev Emitted when `account` is revoked `role`.
                 *
                 * `sender` is the account that originated the contract call:
                 *   - if using `revokeRole`, it is the admin role bearer
                 *   - if using `renounceRole`, it is the role bearer (i.e. `account`)
                 */
                event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender);
                /**
                 * @dev Returns `true` if `account` has been granted `role`.
                 */
                function hasRole(bytes32 role, address account) external view returns (bool);
                /**
                 * @dev Returns the admin role that controls `role`. See {grantRole} and
                 * {revokeRole}.
                 *
                 * To change a role's admin, use {AccessControl-_setRoleAdmin}.
                 */
                function getRoleAdmin(bytes32 role) external view returns (bytes32);
                /**
                 * @dev Grants `role` to `account`.
                 *
                 * If `account` had not been already granted `role`, emits a {RoleGranted}
                 * event.
                 *
                 * Requirements:
                 *
                 * - the caller must have ``role``'s admin role.
                 */
                function grantRole(bytes32 role, address account) external;
                /**
                 * @dev Revokes `role` from `account`.
                 *
                 * If `account` had been granted `role`, emits a {RoleRevoked} event.
                 *
                 * Requirements:
                 *
                 * - the caller must have ``role``'s admin role.
                 */
                function revokeRole(bytes32 role, address account) external;
                /**
                 * @dev Revokes `role` from the calling account.
                 *
                 * Roles are often managed via {grantRole} and {revokeRole}: this function's
                 * purpose is to provide a mechanism for accounts to lose their privileges
                 * if they are compromised (such as when a trusted device is misplaced).
                 *
                 * If the calling account had been granted `role`, emits a {RoleRevoked}
                 * event.
                 *
                 * Requirements:
                 *
                 * - the caller must be `account`.
                 */
                function renounceRole(bytes32 role, address account) external;
            }
            // SPDX-License-Identifier: MIT
            // OpenZeppelin Contracts v4.4.1 (utils/Strings.sol)
            pragma solidity ^0.8.0;
            /**
             * @dev String operations.
             */
            library Strings {
                bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";
                /**
                 * @dev Converts a `uint256` to its ASCII `string` decimal representation.
                 */
                function toString(uint256 value) internal pure returns (string memory) {
                    // Inspired by OraclizeAPI's implementation - MIT licence
                    // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
                    if (value == 0) {
                        return "0";
                    }
                    uint256 temp = value;
                    uint256 digits;
                    while (temp != 0) {
                        digits++;
                        temp /= 10;
                    }
                    bytes memory buffer = new bytes(digits);
                    while (value != 0) {
                        digits -= 1;
                        buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
                        value /= 10;
                    }
                    return string(buffer);
                }
                /**
                 * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
                 */
                function toHexString(uint256 value) internal pure returns (string memory) {
                    if (value == 0) {
                        return "0x00";
                    }
                    uint256 temp = value;
                    uint256 length = 0;
                    while (temp != 0) {
                        length++;
                        temp >>= 8;
                    }
                    return toHexString(value, length);
                }
                /**
                 * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
                 */
                function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
                    bytes memory buffer = new bytes(2 * length + 2);
                    buffer[0] = "0";
                    buffer[1] = "x";
                    for (uint256 i = 2 * length + 1; i > 1; --i) {
                        buffer[i] = _HEX_SYMBOLS[value & 0xf];
                        value >>= 4;
                    }
                    require(value == 0, "Strings: hex length insufficient");
                    return string(buffer);
                }
            }
            // 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;
                }
            }
            // 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 5 of 8: Payments
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            import "@openzeppelin/contracts-0.7.2/payment/PullPayment.sol";
            import "@openzeppelin/contracts-0.7.2/math/SafeMath.sol";
            import "./IPayments.sol";
            /**
             * @title Payments contract for SuperRare Marketplaces.
             */
            contract Payments is IPayments, PullPayment {
                using SafeMath for uint256;
                /////////////////////////////////////////////////////////////////////////
                // refund
                /////////////////////////////////////////////////////////////////////////
                /**
                 * @dev Internal function to refund an address. Typically for canceled bids or offers.
                 * Requirements:
                 *
                 *  - _payee cannot be the zero address
                 *
                 * @param _amount uint256 value to be split.
                 * @param _payee address seller of the token.
                 */
                function refund(address _payee, uint256 _amount) external payable override {
                    require(
                        _payee != address(0),
                        "refund::no payees can be the zero address"
                    );
                    require(msg.value == _amount);
                    if (_amount > 0) {
                        (bool success, ) = address(_payee).call{value: _amount}("");
                        if (!success) {
                            _asyncTransfer(_payee, _amount);
                        }
                    }
                }
                /////////////////////////////////////////////////////////////////////////
                // payout
                /////////////////////////////////////////////////////////////////////////
                function payout(address[] calldata _splits, uint256[] calldata _amounts)
                    external
                    payable
                    override
                {
                    uint256 totalAmount = 0;
                    for (uint256 i = 0; i < _splits.length; i++) {
                        totalAmount = totalAmount.add(_amounts[i]);
                        if (_splits[i] != address(0)) {
                            (bool success, ) = address(_splits[i]).call{value: _amounts[i]}(
                                ""
                            );
                            if (!success) {
                                _asyncTransfer(_splits[i], _amounts[i]);
                            }
                        }
                    }
                    require(msg.value == totalAmount, "payout::not enough sent");
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.2 <0.8.0;
            import "./escrow/Escrow.sol";
            /**
             * @dev Simple implementation of a
             * https://consensys.github.io/smart-contract-best-practices/recommendations/#favor-pull-over-push-for-external-calls[pull-payment]
             * strategy, where the paying contract doesn't interact directly with the
             * receiver account, which must withdraw its payments itself.
             *
             * Pull-payments are often considered the best practice when it comes to sending
             * Ether, security-wise. It prevents recipients from blocking execution, and
             * eliminates reentrancy concerns.
             *
             * TIP: If you would like to learn more about reentrancy and alternative ways
             * to protect against it, check out our blog post
             * https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
             *
             * To use, derive from the `PullPayment` contract, and use {_asyncTransfer}
             * instead of Solidity's `transfer` function. Payees can query their due
             * payments with {payments}, and retrieve them with {withdrawPayments}.
             */
            abstract contract PullPayment {
                Escrow private _escrow;
                constructor () internal {
                    _escrow = new Escrow();
                }
                /**
                 * @dev Withdraw accumulated payments, forwarding all gas to the recipient.
                 *
                 * Note that _any_ account can call this function, not just the `payee`.
                 * This means that contracts unaware of the `PullPayment` protocol can still
                 * receive funds this way, by having a separate account call
                 * {withdrawPayments}.
                 *
                 * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
                 * Make sure you trust the recipient, or are either following the
                 * checks-effects-interactions pattern or using {ReentrancyGuard}.
                 *
                 * @param payee Whose payments will be withdrawn.
                 */
                function withdrawPayments(address payable payee) public virtual {
                    _escrow.withdraw(payee);
                }
                /**
                 * @dev Returns the payments owed to an address.
                 * @param dest The creditor's address.
                 */
                function payments(address dest) public view returns (uint256) {
                    return _escrow.depositsOf(dest);
                }
                /**
                 * @dev Called by the payer to store the sent amount as credit to be pulled.
                 * Funds sent in this way are stored in an intermediate {Escrow} contract, so
                 * there is no danger of them being spent before withdrawal.
                 *
                 * @param dest The destination address of the funds.
                 * @param amount The amount to transfer.
                 */
                function _asyncTransfer(address dest, uint256 amount) internal virtual {
                    _escrow.deposit{ value: amount }(dest);
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.0 <0.8.0;
            /**
             * @dev Wrappers over Solidity's arithmetic operations with added overflow
             * checks.
             *
             * Arithmetic operations in Solidity wrap on overflow. This can easily result
             * in bugs, because programmers usually assume that an overflow raises an
             * error, which is the standard behavior in high level programming languages.
             * `SafeMath` restores this intuition by reverting the transaction when an
             * operation overflows.
             *
             * Using this library instead of the unchecked operations eliminates an entire
             * class of bugs, so it's recommended to use it always.
             */
            library SafeMath {
                /**
                 * @dev Returns the addition of two unsigned integers, with an overflow flag.
                 *
                 * _Available since v3.4._
                 */
                function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
                    uint256 c = a + b;
                    if (c < a) return (false, 0);
                    return (true, c);
                }
                /**
                 * @dev Returns the substraction of two unsigned integers, with an overflow flag.
                 *
                 * _Available since v3.4._
                 */
                function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
                    if (b > a) return (false, 0);
                    return (true, a - b);
                }
                /**
                 * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
                 *
                 * _Available since v3.4._
                 */
                function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
                    // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
                    // benefit is lost if 'b' is also tested.
                    // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
                    if (a == 0) return (true, 0);
                    uint256 c = a * b;
                    if (c / a != b) return (false, 0);
                    return (true, c);
                }
                /**
                 * @dev Returns the division of two unsigned integers, with a division by zero flag.
                 *
                 * _Available since v3.4._
                 */
                function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
                    if (b == 0) return (false, 0);
                    return (true, a / b);
                }
                /**
                 * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
                 *
                 * _Available since v3.4._
                 */
                function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
                    if (b == 0) return (false, 0);
                    return (true, a % b);
                }
                /**
                 * @dev Returns the addition of two unsigned integers, reverting on
                 * overflow.
                 *
                 * Counterpart to Solidity's `+` operator.
                 *
                 * Requirements:
                 *
                 * - Addition cannot overflow.
                 */
                function add(uint256 a, uint256 b) internal pure returns (uint256) {
                    uint256 c = a + b;
                    require(c >= a, "SafeMath: addition overflow");
                    return c;
                }
                /**
                 * @dev Returns the subtraction of two unsigned integers, reverting on
                 * overflow (when the result is negative).
                 *
                 * Counterpart to Solidity's `-` operator.
                 *
                 * Requirements:
                 *
                 * - Subtraction cannot overflow.
                 */
                function sub(uint256 a, uint256 b) internal pure returns (uint256) {
                    require(b <= a, "SafeMath: subtraction overflow");
                    return a - b;
                }
                /**
                 * @dev Returns the multiplication of two unsigned integers, reverting on
                 * overflow.
                 *
                 * Counterpart to Solidity's `*` operator.
                 *
                 * Requirements:
                 *
                 * - Multiplication cannot overflow.
                 */
                function mul(uint256 a, uint256 b) internal pure returns (uint256) {
                    if (a == 0) return 0;
                    uint256 c = a * b;
                    require(c / a == b, "SafeMath: multiplication overflow");
                    return c;
                }
                /**
                 * @dev Returns the integer division of two unsigned integers, reverting on
                 * division by zero. The result is rounded towards zero.
                 *
                 * Counterpart to Solidity's `/` operator. Note: this function uses a
                 * `revert` opcode (which leaves remaining gas untouched) while Solidity
                 * uses an invalid opcode to revert (consuming all remaining gas).
                 *
                 * Requirements:
                 *
                 * - The divisor cannot be zero.
                 */
                function div(uint256 a, uint256 b) internal pure returns (uint256) {
                    require(b > 0, "SafeMath: division by zero");
                    return a / b;
                }
                /**
                 * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
                 * reverting when dividing by zero.
                 *
                 * Counterpart to Solidity's `%` operator. This function uses a `revert`
                 * opcode (which leaves remaining gas untouched) while Solidity uses an
                 * invalid opcode to revert (consuming all remaining gas).
                 *
                 * Requirements:
                 *
                 * - The divisor cannot be zero.
                 */
                function mod(uint256 a, uint256 b) internal pure returns (uint256) {
                    require(b > 0, "SafeMath: modulo by zero");
                    return a % b;
                }
                /**
                 * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
                 * overflow (when the result is negative).
                 *
                 * CAUTION: This function is deprecated because it requires allocating memory for the error
                 * message unnecessarily. For custom revert reasons use {trySub}.
                 *
                 * Counterpart to Solidity's `-` operator.
                 *
                 * Requirements:
                 *
                 * - Subtraction cannot overflow.
                 */
                function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
                    require(b <= a, errorMessage);
                    return a - b;
                }
                /**
                 * @dev Returns the integer division of two unsigned integers, reverting with custom message on
                 * division by zero. The result is rounded towards zero.
                 *
                 * CAUTION: This function is deprecated because it requires allocating memory for the error
                 * message unnecessarily. For custom revert reasons use {tryDiv}.
                 *
                 * Counterpart to Solidity's `/` operator. Note: this function uses a
                 * `revert` opcode (which leaves remaining gas untouched) while Solidity
                 * uses an invalid opcode to revert (consuming all remaining gas).
                 *
                 * Requirements:
                 *
                 * - The divisor cannot be zero.
                 */
                function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
                    require(b > 0, errorMessage);
                    return a / b;
                }
                /**
                 * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
                 * reverting with custom message when dividing by zero.
                 *
                 * CAUTION: This function is deprecated because it requires allocating memory for the error
                 * message unnecessarily. For custom revert reasons use {tryMod}.
                 *
                 * Counterpart to Solidity's `%` operator. This function uses a `revert`
                 * opcode (which leaves remaining gas untouched) while Solidity uses an
                 * invalid opcode to revert (consuming all remaining gas).
                 *
                 * Requirements:
                 *
                 * - The divisor cannot be zero.
                 */
                function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
                    require(b > 0, errorMessage);
                    return a % b;
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.7.3;
            /// @author koloz
            /// @title IPayments
            /// @notice Interface for the Payments contract used.
            interface IPayments {
                function refund(address _payee, uint256 _amount) external payable;
                function payout(address[] calldata _splits, uint256[] calldata _amounts)
                    external
                    payable;
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.0 <0.8.0;
            import "../../math/SafeMath.sol";
            import "../../access/Ownable.sol";
            import "../../utils/Address.sol";
             /**
              * @title Escrow
              * @dev Base escrow contract, holds funds designated for a payee until they
              * withdraw them.
              *
              * Intended usage: This contract (and derived escrow contracts) should be a
              * standalone contract, that only interacts with the contract that instantiated
              * it. That way, it is guaranteed that all Ether will be handled according to
              * the `Escrow` rules, and there is no need to check for payable functions or
              * transfers in the inheritance tree. The contract that uses the escrow as its
              * payment method should be its owner, and provide public methods redirecting
              * to the escrow's deposit and withdraw.
              */
            contract Escrow is Ownable {
                using SafeMath for uint256;
                using Address for address payable;
                event Deposited(address indexed payee, uint256 weiAmount);
                event Withdrawn(address indexed payee, uint256 weiAmount);
                mapping(address => uint256) private _deposits;
                function depositsOf(address payee) public view returns (uint256) {
                    return _deposits[payee];
                }
                /**
                 * @dev Stores the sent amount as credit to be withdrawn.
                 * @param payee The destination address of the funds.
                 */
                function deposit(address payee) public payable virtual onlyOwner {
                    uint256 amount = msg.value;
                    _deposits[payee] = _deposits[payee].add(amount);
                    emit Deposited(payee, amount);
                }
                /**
                 * @dev Withdraw accumulated balance for a payee, forwarding all gas to the
                 * recipient.
                 *
                 * WARNING: Forwarding all gas opens the door to reentrancy vulnerabilities.
                 * Make sure you trust the recipient, or are either following the
                 * checks-effects-interactions pattern or using {ReentrancyGuard}.
                 *
                 * @param payee The address whose funds will be withdrawn and transferred to.
                 */
                function withdraw(address payable payee) public virtual onlyOwner {
                    uint256 payment = _deposits[payee];
                    _deposits[payee] = 0;
                    payee.sendValue(payment);
                    emit Withdrawn(payee, payment);
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.0 <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 () internal {
                    address msgSender = _msgSender();
                    _owner = msgSender;
                    emit OwnershipTransferred(address(0), msgSender);
                }
                /**
                 * @dev Returns the address of the current owner.
                 */
                function owner() public view virtual returns (address) {
                    return _owner;
                }
                /**
                 * @dev Throws if called by any account other than the owner.
                 */
                modifier onlyOwner() {
                    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 {
                    emit OwnershipTransferred(_owner, address(0));
                    _owner = 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");
                    emit OwnershipTransferred(_owner, newOwner);
                    _owner = newOwner;
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.2 <0.8.0;
            /**
             * @dev Collection of functions related to the address type
             */
            library Address {
                /**
                 * @dev Returns true if `account` is a contract.
                 *
                 * [IMPORTANT]
                 * ====
                 * It is unsafe to assume that an address for which this function returns
                 * false is an externally-owned account (EOA) and not a contract.
                 *
                 * Among others, `isContract` will return false for the following
                 * types of addresses:
                 *
                 *  - an externally-owned account
                 *  - a contract in construction
                 *  - an address where a contract will be created
                 *  - an address where a contract lived, but was destroyed
                 * ====
                 */
                function isContract(address account) internal view returns (bool) {
                    // This method relies on extcodesize, which returns 0 for contracts in
                    // construction, since the code is only stored at the end of the
                    // constructor execution.
                    uint256 size;
                    // solhint-disable-next-line no-inline-assembly
                    assembly { size := extcodesize(account) }
                    return size > 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");
                    // solhint-disable-next-line avoid-low-level-calls, avoid-call-value
                    (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");
                    // solhint-disable-next-line avoid-low-level-calls
                    (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");
                    // solhint-disable-next-line avoid-low-level-calls
                    (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");
                    // solhint-disable-next-line avoid-low-level-calls
                    (bool success, bytes memory returndata) = target.delegatecall(data);
                    return _verifyCallResult(success, returndata, errorMessage);
                }
                function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private 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
                            // solhint-disable-next-line no-inline-assembly
                            assembly {
                                let returndata_size := mload(returndata)
                                revert(add(32, returndata), returndata_size)
                            }
                        } else {
                            revert(errorMessage);
                        }
                    }
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity >=0.6.0 <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 GSN 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 payable) {
                    return msg.sender;
                }
                function _msgData() internal view virtual returns (bytes memory) {
                    this; // silence state mutability warning without generating bytecode - see https://github.com/ethereum/solidity/issues/2691
                    return msg.data;
                }
            }
            

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

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

            File 8 of 8: SpaceOperatorRegistry
            // SPDX-License-Identifier: MIT
            // OpenZeppelin Contracts (last updated v4.6.0) (access/AccessControl.sol)
            pragma solidity ^0.8.0;
            import "./IAccessControlUpgradeable.sol";
            import "../utils/ContextUpgradeable.sol";
            import "../utils/StringsUpgradeable.sol";
            import "../utils/introspection/ERC165Upgradeable.sol";
            import "../proxy/utils/Initializable.sol";
            /**
             * @dev Contract module that allows children to implement role-based access
             * control mechanisms. This is a lightweight version that doesn't allow enumerating role
             * members except through off-chain means by accessing the contract event logs. Some
             * applications may benefit from on-chain enumerability, for those cases see
             * {AccessControlEnumerable}.
             *
             * Roles are referred to by their `bytes32` identifier. These should be exposed
             * in the external API and be unique. The best way to achieve this is by
             * using `public constant` hash digests:
             *
             * ```
             * bytes32 public constant MY_ROLE = keccak256("MY_ROLE");
             * ```
             *
             * Roles can be used to represent a set of permissions. To restrict access to a
             * function call, use {hasRole}:
             *
             * ```
             * function foo() public {
             *     require(hasRole(MY_ROLE, msg.sender));
             *     ...
             * }
             * ```
             *
             * Roles can be granted and revoked dynamically via the {grantRole} and
             * {revokeRole} functions. Each role has an associated admin role, and only
             * accounts that have a role's admin role can call {grantRole} and {revokeRole}.
             *
             * By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means
             * that only accounts with this role will be able to grant or revoke other
             * roles. More complex role relationships can be created by using
             * {_setRoleAdmin}.
             *
             * WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to
             * grant and revoke this role. Extra precautions should be taken to secure
             * accounts that have been granted it.
             */
            abstract contract AccessControlUpgradeable is Initializable, ContextUpgradeable, IAccessControlUpgradeable, ERC165Upgradeable {
                function __AccessControl_init() internal onlyInitializing {
                }
                function __AccessControl_init_unchained() internal onlyInitializing {
                }
                struct RoleData {
                    mapping(address => bool) members;
                    bytes32 adminRole;
                }
                mapping(bytes32 => RoleData) private _roles;
                bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
                /**
                 * @dev Modifier that checks that an account has a specific role. Reverts
                 * with a standardized message including the required role.
                 *
                 * The format of the revert reason is given by the following regular expression:
                 *
                 *  /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/
                 *
                 * _Available since v4.1._
                 */
                modifier onlyRole(bytes32 role) {
                    _checkRole(role);
                    _;
                }
                /**
                 * @dev See {IERC165-supportsInterface}.
                 */
                function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
                    return interfaceId == type(IAccessControlUpgradeable).interfaceId || super.supportsInterface(interfaceId);
                }
                /**
                 * @dev Returns `true` if `account` has been granted `role`.
                 */
                function hasRole(bytes32 role, address account) public view virtual override returns (bool) {
                    return _roles[role].members[account];
                }
                /**
                 * @dev Revert with a standard message if `_msgSender()` is missing `role`.
                 * Overriding this function changes the behavior of the {onlyRole} modifier.
                 *
                 * Format of the revert message is described in {_checkRole}.
                 *
                 * _Available since v4.6._
                 */
                function _checkRole(bytes32 role) internal view virtual {
                    _checkRole(role, _msgSender());
                }
                /**
                 * @dev Revert with a standard message if `account` is missing `role`.
                 *
                 * The format of the revert reason is given by the following regular expression:
                 *
                 *  /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/
                 */
                function _checkRole(bytes32 role, address account) internal view virtual {
                    if (!hasRole(role, account)) {
                        revert(
                            string(
                                abi.encodePacked(
                                    "AccessControl: account ",
                                    StringsUpgradeable.toHexString(uint160(account), 20),
                                    " is missing role ",
                                    StringsUpgradeable.toHexString(uint256(role), 32)
                                )
                            )
                        );
                    }
                }
                /**
                 * @dev Returns the admin role that controls `role`. See {grantRole} and
                 * {revokeRole}.
                 *
                 * To change a role's admin, use {_setRoleAdmin}.
                 */
                function getRoleAdmin(bytes32 role) public view virtual override returns (bytes32) {
                    return _roles[role].adminRole;
                }
                /**
                 * @dev Grants `role` to `account`.
                 *
                 * If `account` had not been already granted `role`, emits a {RoleGranted}
                 * event.
                 *
                 * Requirements:
                 *
                 * - the caller must have ``role``'s admin role.
                 */
                function grantRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
                    _grantRole(role, account);
                }
                /**
                 * @dev Revokes `role` from `account`.
                 *
                 * If `account` had been granted `role`, emits a {RoleRevoked} event.
                 *
                 * Requirements:
                 *
                 * - the caller must have ``role``'s admin role.
                 */
                function revokeRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
                    _revokeRole(role, account);
                }
                /**
                 * @dev Revokes `role` from the calling account.
                 *
                 * Roles are often managed via {grantRole} and {revokeRole}: this function's
                 * purpose is to provide a mechanism for accounts to lose their privileges
                 * if they are compromised (such as when a trusted device is misplaced).
                 *
                 * If the calling account had been revoked `role`, emits a {RoleRevoked}
                 * event.
                 *
                 * Requirements:
                 *
                 * - the caller must be `account`.
                 */
                function renounceRole(bytes32 role, address account) public virtual override {
                    require(account == _msgSender(), "AccessControl: can only renounce roles for self");
                    _revokeRole(role, account);
                }
                /**
                 * @dev Grants `role` to `account`.
                 *
                 * If `account` had not been already granted `role`, emits a {RoleGranted}
                 * event. Note that unlike {grantRole}, this function doesn't perform any
                 * checks on the calling account.
                 *
                 * [WARNING]
                 * ====
                 * This function should only be called from the constructor when setting
                 * up the initial roles for the system.
                 *
                 * Using this function in any other way is effectively circumventing the admin
                 * system imposed by {AccessControl}.
                 * ====
                 *
                 * NOTE: This function is deprecated in favor of {_grantRole}.
                 */
                function _setupRole(bytes32 role, address account) internal virtual {
                    _grantRole(role, account);
                }
                /**
                 * @dev Sets `adminRole` as ``role``'s admin role.
                 *
                 * Emits a {RoleAdminChanged} event.
                 */
                function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
                    bytes32 previousAdminRole = getRoleAdmin(role);
                    _roles[role].adminRole = adminRole;
                    emit RoleAdminChanged(role, previousAdminRole, adminRole);
                }
                /**
                 * @dev Grants `role` to `account`.
                 *
                 * Internal function without access restriction.
                 */
                function _grantRole(bytes32 role, address account) internal virtual {
                    if (!hasRole(role, account)) {
                        _roles[role].members[account] = true;
                        emit RoleGranted(role, account, _msgSender());
                    }
                }
                /**
                 * @dev Revokes `role` from `account`.
                 *
                 * Internal function without access restriction.
                 */
                function _revokeRole(bytes32 role, address account) internal virtual {
                    if (hasRole(role, account)) {
                        _roles[role].members[account] = false;
                        emit RoleRevoked(role, account, _msgSender());
                    }
                }
                /**
                 * @dev This empty reserved space is put in place to allow future versions to add new
                 * variables without shifting down storage in the inheritance chain.
                 * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
                 */
                uint256[49] private __gap;
            }
            // SPDX-License-Identifier: MIT
            // OpenZeppelin Contracts v4.4.1 (access/IAccessControl.sol)
            pragma solidity ^0.8.0;
            /**
             * @dev External interface of AccessControl declared to support ERC165 detection.
             */
            interface IAccessControlUpgradeable {
                /**
                 * @dev Emitted when `newAdminRole` is set as ``role``'s admin role, replacing `previousAdminRole`
                 *
                 * `DEFAULT_ADMIN_ROLE` is the starting admin for all roles, despite
                 * {RoleAdminChanged} not being emitted signaling this.
                 *
                 * _Available since v3.1._
                 */
                event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole);
                /**
                 * @dev Emitted when `account` is granted `role`.
                 *
                 * `sender` is the account that originated the contract call, an admin role
                 * bearer except when using {AccessControl-_setupRole}.
                 */
                event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender);
                /**
                 * @dev Emitted when `account` is revoked `role`.
                 *
                 * `sender` is the account that originated the contract call:
                 *   - if using `revokeRole`, it is the admin role bearer
                 *   - if using `renounceRole`, it is the role bearer (i.e. `account`)
                 */
                event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender);
                /**
                 * @dev Returns `true` if `account` has been granted `role`.
                 */
                function hasRole(bytes32 role, address account) external view returns (bool);
                /**
                 * @dev Returns the admin role that controls `role`. See {grantRole} and
                 * {revokeRole}.
                 *
                 * To change a role's admin, use {AccessControl-_setRoleAdmin}.
                 */
                function getRoleAdmin(bytes32 role) external view returns (bytes32);
                /**
                 * @dev Grants `role` to `account`.
                 *
                 * If `account` had not been already granted `role`, emits a {RoleGranted}
                 * event.
                 *
                 * Requirements:
                 *
                 * - the caller must have ``role``'s admin role.
                 */
                function grantRole(bytes32 role, address account) external;
                /**
                 * @dev Revokes `role` from `account`.
                 *
                 * If `account` had been granted `role`, emits a {RoleRevoked} event.
                 *
                 * Requirements:
                 *
                 * - the caller must have ``role``'s admin role.
                 */
                function revokeRole(bytes32 role, address account) external;
                /**
                 * @dev Revokes `role` from the calling account.
                 *
                 * Roles are often managed via {grantRole} and {revokeRole}: this function's
                 * purpose is to provide a mechanism for accounts to lose their privileges
                 * if they are compromised (such as when a trusted device is misplaced).
                 *
                 * If the calling account had been granted `role`, emits a {RoleRevoked}
                 * event.
                 *
                 * Requirements:
                 *
                 * - the caller must be `account`.
                 */
                function renounceRole(bytes32 role, address account) external;
            }
            // SPDX-License-Identifier: MIT
            // OpenZeppelin Contracts (last updated v4.6.0) (proxy/utils/Initializable.sol)
            pragma solidity ^0.8.2;
            import "../../utils/AddressUpgradeable.sol";
            /**
             * @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
             * behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
             * external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
             * function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
             *
             * The initialization functions use a version number. Once a version number is used, it is consumed and cannot be
             * reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in
             * case an upgrade adds a module that needs to be initialized.
             *
             * For example:
             *
             * [.hljs-theme-light.nopadding]
             * ```
             * contract MyToken is ERC20Upgradeable {
             *     function initialize() initializer public {
             *         __ERC20_init("MyToken", "MTK");
             *     }
             * }
             * contract MyTokenV2 is MyToken, ERC20PermitUpgradeable {
             *     function initializeV2() reinitializer(2) public {
             *         __ERC20Permit_init("MyToken");
             *     }
             * }
             * ```
             *
             * TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
             * possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
             *
             * CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
             * that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
             *
             * [CAUTION]
             * ====
             * Avoid leaving a contract uninitialized.
             *
             * An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
             * contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke
             * the {_disableInitializers} function in the constructor to automatically lock it when it is deployed:
             *
             * [.hljs-theme-light.nopadding]
             * ```
             * /// @custom:oz-upgrades-unsafe-allow constructor
             * constructor() {
             *     _disableInitializers();
             * }
             * ```
             * ====
             */
            abstract contract Initializable {
                /**
                 * @dev Indicates that the contract has been initialized.
                 * @custom:oz-retyped-from bool
                 */
                uint8 private _initialized;
                /**
                 * @dev Indicates that the contract is in the process of being initialized.
                 */
                bool private _initializing;
                /**
                 * @dev Triggered when the contract has been initialized or reinitialized.
                 */
                event Initialized(uint8 version);
                /**
                 * @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope,
                 * `onlyInitializing` functions can be used to initialize parent contracts. Equivalent to `reinitializer(1)`.
                 */
                modifier initializer() {
                    bool isTopLevelCall = _setInitializedVersion(1);
                    if (isTopLevelCall) {
                        _initializing = true;
                    }
                    _;
                    if (isTopLevelCall) {
                        _initializing = false;
                        emit Initialized(1);
                    }
                }
                /**
                 * @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the
                 * contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be
                 * used to initialize parent contracts.
                 *
                 * `initializer` is equivalent to `reinitializer(1)`, so a reinitializer may be used after the original
                 * initialization step. This is essential to configure modules that are added through upgrades and that require
                 * initialization.
                 *
                 * Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in
                 * a contract, executing them in the right order is up to the developer or operator.
                 */
                modifier reinitializer(uint8 version) {
                    bool isTopLevelCall = _setInitializedVersion(version);
                    if (isTopLevelCall) {
                        _initializing = true;
                    }
                    _;
                    if (isTopLevelCall) {
                        _initializing = false;
                        emit Initialized(version);
                    }
                }
                /**
                 * @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
                 * {initializer} and {reinitializer} modifiers, directly or indirectly.
                 */
                modifier onlyInitializing() {
                    require(_initializing, "Initializable: contract is not initializing");
                    _;
                }
                /**
                 * @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call.
                 * Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized
                 * to any version. It is recommended to use this to lock implementation contracts that are designed to be called
                 * through proxies.
                 */
                function _disableInitializers() internal virtual {
                    _setInitializedVersion(type(uint8).max);
                }
                function _setInitializedVersion(uint8 version) private returns (bool) {
                    // If the contract is initializing we ignore whether _initialized is set in order to support multiple
                    // inheritance patterns, but we only do this in the context of a constructor, and for the lowest level
                    // of initializers, because in other contexts the contract may have been reentered.
                    if (_initializing) {
                        require(
                            version == 1 && !AddressUpgradeable.isContract(address(this)),
                            "Initializable: contract is already initialized"
                        );
                        return false;
                    } else {
                        require(_initialized < version, "Initializable: contract is already initialized");
                        _initialized = version;
                        return true;
                    }
                }
            }
            // SPDX-License-Identifier: MIT
            // OpenZeppelin Contracts (last updated v4.5.0) (utils/Address.sol)
            pragma solidity ^0.8.1;
            /**
             * @dev Collection of functions related to the address type
             */
            library AddressUpgradeable {
                /**
                 * @dev Returns true if `account` is a contract.
                 *
                 * [IMPORTANT]
                 * ====
                 * It is unsafe to assume that an address for which this function returns
                 * false is an externally-owned account (EOA) and not a contract.
                 *
                 * Among others, `isContract` will return false for the following
                 * types of addresses:
                 *
                 *  - an externally-owned account
                 *  - a contract in construction
                 *  - an address where a contract will be created
                 *  - an address where a contract lived, but was destroyed
                 * ====
                 *
                 * [IMPORTANT]
                 * ====
                 * You shouldn't rely on `isContract` to protect against flash loan attacks!
                 *
                 * Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
                 * like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
                 * constructor.
                 * ====
                 */
                function isContract(address account) internal view returns (bool) {
                    // This method relies on extcodesize/address.code.length, which returns 0
                    // for contracts in construction, since the code is only stored at the end
                    // of the constructor execution.
                    return account.code.length > 0;
                }
                /**
                 * @dev Replacement for Solidity's `transfer`: sends `amount` wei to
                 * `recipient`, forwarding all available gas and reverting on errors.
                 *
                 * https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
                 * of certain opcodes, possibly making contracts go over the 2300 gas limit
                 * imposed by `transfer`, making them unable to receive funds via
                 * `transfer`. {sendValue} removes this limitation.
                 *
                 * https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
                 *
                 * IMPORTANT: because control is transferred to `recipient`, care must be
                 * taken to not create reentrancy vulnerabilities. Consider using
                 * {ReentrancyGuard} or the
                 * https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
                 */
                function sendValue(address payable recipient, uint256 amount) internal {
                    require(address(this).balance >= amount, "Address: insufficient balance");
                    (bool success, ) = recipient.call{value: amount}("");
                    require(success, "Address: unable to send value, recipient may have reverted");
                }
                /**
                 * @dev Performs a Solidity function call using a low level `call`. A
                 * plain `call` is an unsafe replacement for a function call: use this
                 * function instead.
                 *
                 * If `target` reverts with a revert reason, it is bubbled up by this
                 * function (like regular Solidity function calls).
                 *
                 * Returns the raw returned data. To convert to the expected return value,
                 * use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
                 *
                 * Requirements:
                 *
                 * - `target` must be a contract.
                 * - calling `target` with `data` must not revert.
                 *
                 * _Available since v3.1._
                 */
                function functionCall(address target, bytes memory data) internal returns (bytes memory) {
                    return functionCall(target, data, "Address: low-level call failed");
                }
                /**
                 * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
                 * `errorMessage` as a fallback revert reason when `target` reverts.
                 *
                 * _Available since v3.1._
                 */
                function functionCall(
                    address target,
                    bytes memory data,
                    string memory errorMessage
                ) internal returns (bytes memory) {
                    return functionCallWithValue(target, data, 0, errorMessage);
                }
                /**
                 * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
                 * but also transferring `value` wei to `target`.
                 *
                 * Requirements:
                 *
                 * - the calling contract must have an ETH balance of at least `value`.
                 * - the called Solidity function must be `payable`.
                 *
                 * _Available since v3.1._
                 */
                function functionCallWithValue(
                    address target,
                    bytes memory data,
                    uint256 value
                ) internal returns (bytes memory) {
                    return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
                }
                /**
                 * @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
                 * with `errorMessage` as a fallback revert reason when `target` reverts.
                 *
                 * _Available since v3.1._
                 */
                function functionCallWithValue(
                    address target,
                    bytes memory data,
                    uint256 value,
                    string memory errorMessage
                ) internal returns (bytes memory) {
                    require(address(this).balance >= value, "Address: insufficient balance for call");
                    require(isContract(target), "Address: call to non-contract");
                    (bool success, bytes memory returndata) = target.call{value: value}(data);
                    return verifyCallResult(success, returndata, errorMessage);
                }
                /**
                 * @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
                 * but performing a static call.
                 *
                 * _Available since v3.3._
                 */
                function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
                    return functionStaticCall(target, data, "Address: low-level static call failed");
                }
                /**
                 * @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
                 * but performing a static call.
                 *
                 * _Available since v3.3._
                 */
                function functionStaticCall(
                    address target,
                    bytes memory data,
                    string memory errorMessage
                ) internal view returns (bytes memory) {
                    require(isContract(target), "Address: static call to non-contract");
                    (bool success, bytes memory returndata) = target.staticcall(data);
                    return verifyCallResult(success, returndata, errorMessage);
                }
                /**
                 * @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
                 * revert reason using the provided one.
                 *
                 * _Available since v4.3._
                 */
                function verifyCallResult(
                    bool success,
                    bytes memory returndata,
                    string memory errorMessage
                ) internal pure returns (bytes memory) {
                    if (success) {
                        return returndata;
                    } else {
                        // Look for revert reason and bubble it up if present
                        if (returndata.length > 0) {
                            // The easiest way to bubble the revert reason is using memory via assembly
                            assembly {
                                let returndata_size := mload(returndata)
                                revert(add(32, returndata), returndata_size)
                            }
                        } else {
                            revert(errorMessage);
                        }
                    }
                }
            }
            // SPDX-License-Identifier: MIT
            // OpenZeppelin Contracts v4.4.1 (utils/Context.sol)
            pragma solidity ^0.8.0;
            import "../proxy/utils/Initializable.sol";
            /**
             * @dev Provides information about the current execution context, including the
             * sender of the transaction and its data. While these are generally available
             * via msg.sender and msg.data, they should not be accessed in such a direct
             * manner, since when dealing with meta-transactions the account sending and
             * paying for execution may not be the actual sender (as far as an application
             * is concerned).
             *
             * This contract is only required for intermediate, library-like contracts.
             */
            abstract contract ContextUpgradeable is Initializable {
                function __Context_init() internal onlyInitializing {
                }
                function __Context_init_unchained() internal onlyInitializing {
                }
                function _msgSender() internal view virtual returns (address) {
                    return msg.sender;
                }
                function _msgData() internal view virtual returns (bytes calldata) {
                    return msg.data;
                }
                /**
                 * @dev This empty reserved space is put in place to allow future versions to add new
                 * variables without shifting down storage in the inheritance chain.
                 * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
                 */
                uint256[50] private __gap;
            }
            // SPDX-License-Identifier: MIT
            // OpenZeppelin Contracts v4.4.1 (utils/Strings.sol)
            pragma solidity ^0.8.0;
            /**
             * @dev String operations.
             */
            library StringsUpgradeable {
                bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";
                /**
                 * @dev Converts a `uint256` to its ASCII `string` decimal representation.
                 */
                function toString(uint256 value) internal pure returns (string memory) {
                    // Inspired by OraclizeAPI's implementation - MIT licence
                    // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
                    if (value == 0) {
                        return "0";
                    }
                    uint256 temp = value;
                    uint256 digits;
                    while (temp != 0) {
                        digits++;
                        temp /= 10;
                    }
                    bytes memory buffer = new bytes(digits);
                    while (value != 0) {
                        digits -= 1;
                        buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
                        value /= 10;
                    }
                    return string(buffer);
                }
                /**
                 * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
                 */
                function toHexString(uint256 value) internal pure returns (string memory) {
                    if (value == 0) {
                        return "0x00";
                    }
                    uint256 temp = value;
                    uint256 length = 0;
                    while (temp != 0) {
                        length++;
                        temp >>= 8;
                    }
                    return toHexString(value, length);
                }
                /**
                 * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
                 */
                function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
                    bytes memory buffer = new bytes(2 * length + 2);
                    buffer[0] = "0";
                    buffer[1] = "x";
                    for (uint256 i = 2 * length + 1; i > 1; --i) {
                        buffer[i] = _HEX_SYMBOLS[value & 0xf];
                        value >>= 4;
                    }
                    require(value == 0, "Strings: hex length insufficient");
                    return string(buffer);
                }
            }
            // SPDX-License-Identifier: MIT
            // OpenZeppelin Contracts v4.4.1 (utils/introspection/ERC165.sol)
            pragma solidity ^0.8.0;
            import "./IERC165Upgradeable.sol";
            import "../../proxy/utils/Initializable.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 ERC165Upgradeable is Initializable, IERC165Upgradeable {
                function __ERC165_init() internal onlyInitializing {
                }
                function __ERC165_init_unchained() internal onlyInitializing {
                }
                /**
                 * @dev See {IERC165-supportsInterface}.
                 */
                function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
                    return interfaceId == type(IERC165Upgradeable).interfaceId;
                }
                /**
                 * @dev This empty reserved space is put in place to allow future versions to add new
                 * variables without shifting down storage in the inheritance chain.
                 * See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
                 */
                uint256[50] private __gap;
            }
            // SPDX-License-Identifier: MIT
            // OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)
            pragma solidity ^0.8.0;
            /**
             * @dev Interface of the ERC165 standard, as defined in the
             * https://eips.ethereum.org/EIPS/eip-165[EIP].
             *
             * Implementers can declare support of contract interfaces, which can then be
             * queried by others ({ERC165Checker}).
             *
             * For an implementation, see {ERC165}.
             */
            interface IERC165Upgradeable {
                /**
                 * @dev Returns true if this contract implements the interface defined by
                 * `interfaceId`. See the corresponding
                 * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
                 * to learn more about how these ids are created.
                 *
                 * This function call must use less than 30 000 gas.
                 */
                function supportsInterface(bytes4 interfaceId) external view returns (bool);
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.8.10;
            import {AccessControlUpgradeable} from "openzeppelin-contracts-upgradeable/access/AccessControlUpgradeable.sol";
            import {ISpaceOperatorRegistry} from "./interfaces/ISpaceOperatorRegistry.sol";
            contract SpaceOperatorRegistry is
                ISpaceOperatorRegistry,
                AccessControlUpgradeable
            {
                bytes32 public constant SPACE_OPERATOR_REGISTER_ROLE =
                    keccak256("SPACE_OPERATOR_REGISTER_ROLE");
                event SpaceOperatorApproved(
                    address indexed _operator,
                    address indexed _approver,
                    bool _approved
                );
                mapping(address => bool) public isApprovedOperator;
                function initialize() public initializer {
                    __AccessControl_init();
                    _setupRole(DEFAULT_ADMIN_ROLE, msg.sender);
                    _setupRole(SPACE_OPERATOR_REGISTER_ROLE, msg.sender);
                }
                function getPlatformCommission(address)
                    external
                    pure
                    override
                    returns (uint8)
                {
                    return 5;
                }
                function setPlatformCommission(address, uint8) external override {}
                function isApprovedSpaceOperator(address _operator)
                    external
                    view
                    override
                    returns (bool)
                {
                    return isApprovedOperator[_operator];
                }
                function setSpaceOperatorApproved(address _operator, bool _approved)
                    external
                    override
                {
                    require(hasRole(SPACE_OPERATOR_REGISTER_ROLE, msg.sender));
                    isApprovedOperator[_operator] = _approved;
                    emit SpaceOperatorApproved(_operator, msg.sender, _approved);
                }
                function batchSetSpaceOperatorApproved(
                    address[] calldata _operators,
                    bool _approved
                ) external {
                    for (uint256 i = 0; i < _operators.length; i++) {
                        require(hasRole(SPACE_OPERATOR_REGISTER_ROLE, msg.sender));
                        isApprovedOperator[_operators[i]] = _approved;
                        emit SpaceOperatorApproved(_operators[i], msg.sender, _approved);
                    }
                }
            }
            // SPDX-License-Identifier: MIT
            pragma solidity 0.8.10;
            /// @author koloz
            /// @title ISpaceOperatorRegistry
            /// @notice The interface for the SpaceOperatorRegistry
            interface ISpaceOperatorRegistry {
                function getPlatformCommission(address _operator)
                    external
                    view
                    returns (uint8);
                function setPlatformCommission(address _operator, uint8 _commission)
                    external;
                function isApprovedSpaceOperator(address _operator)
                    external
                    view
                    returns (bool);
                function setSpaceOperatorApproved(address _operator, bool _approved)
                    external;
            }