ETH Price: $3,481.22 (-1.04%)
Gas: 2 Gwei

Contract

0x039D6707B4cDC66D6C6b383ECe2Dd42dD13d48Ef
 

Overview

ETH Balance

0 ETH

Eth Value

$0.00

Multichain Info

No addresses found
Transaction Hash
Method
Block
From
To
0x60806040169724852023-04-04 2:02:59476 days ago1680573779IN
 Create: PaymentHandlerFacet
0 ETH0.0146555620.58582484

View more zero value Internal Transactions in Advanced View mode

Advanced mode:
Loading...
Loading

Contract Source Code Verified (Exact Match)

Contract Name:
PaymentHandlerFacet

Compiler Version
v0.8.1+commit.df193b15

Optimization Enabled:
Yes with 200 runs

Other Settings:
default evmVersion
File 1 of 12 : PaymentHandlerFacet.sol
/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "../../../diamond/IDiamondFacet.sol";
import "./PaymentHandlerInternal.sol";

/// @author Kam Amini <[email protected]>
///
/// @notice Use at your own risk
contract PaymentHandlerFacet is IDiamondFacet {

    function getFacetName()
      external pure override returns (string memory) {
        return "payment-handler";
    }

    // CAUTION: Don't forget to update the version when adding new functionality
    function getFacetVersion()
      external pure override returns (string memory) {
        return "4.0.0";
    }

    function getFacetPI()
      external pure override returns (string[] memory) {
        string[] memory pi = new string[](3);
        pi[0] = "getPaymentHandlerSettings()";
        pi[1] = "setPaymentHandlerSettings(address)";
        pi[2] = "transferTo(string,address,uint256,string)";
        return pi;
    }

    function getFacetProtectedPI()
      external pure override returns (string[] memory) {
        string[] memory pi = new string[](2);
        pi[0] = "setPaymentHandlerSettings(address)";
        pi[1] = "transferTo(string,address,uint256,string)";
        return pi;
    }

    function supportsInterface(bytes4 interfaceId)
      external pure override returns (bool) {
        return interfaceId == type(IDiamondFacet).interfaceId;
    }

    function getPaymentHandlerSettings() external view returns (address) {
        return PaymentHandlerInternal._getPaymentHandlerSettings();
    }

    function setPaymentHandlerSettings(
        address payoutAddress
    ) external {
        PaymentHandlerInternal._setPaymentHandlerSettings(
            payoutAddress
        );
    }

    function transferTo(
        string memory paymentMethodName,
        address to,
        uint256 amount,
        string memory data
    ) external {
        PaymentHandlerInternal._transferTo(
            paymentMethodName,
            to,
            amount,
            data
        );
    }
}

File 2 of 12 : IDiamondFacet.sol
/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "@openzeppelin/contracts/interfaces/IERC165.sol";

/// @author Kam Amini <[email protected]>
///
/// @notice Use at your own risk
interface IDiamondFacet is IERC165 {

    // NOTE: The override MUST remain 'pure'.
    function getFacetName() external pure returns (string memory);

    // NOTE: The override MUST remain 'pure'.
    function getFacetVersion() external pure returns (string memory);

    // NOTE: The override MUST remain 'pure'.
    function getFacetPI() external pure returns (string[] memory);

    // NOTE: The override MUST remain 'pure'.
    function getFacetProtectedPI() external pure returns (string[] memory);
}

File 3 of 12 : PaymentHandlerInternal.sol
/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "@openzeppelin/contracts/interfaces/IERC20.sol";
import "../../IUniswapV2Pair.sol";
import "../payment-method-manager/PaymentMethodManagerLib.sol";
import "./PaymentHandlerStorage.sol";

/// @author Kam Amini <[email protected]>
///
/// @notice Use at your own risk
library PaymentHandlerInternal {

    bytes32 constant public WEI_PAYMENT_METHOD_HASH = keccak256(abi.encode("WEI"));

    event TransferTo(
        address to,
        uint256 amount,
        string data
    );
    event TransferETH20To(
        string paymentMethodName,
        address to,
        uint256 amount,
        string data
    );

    function _getPaymentHandlerSettings() internal view returns (address) {
        return __s().payoutAddress;
    }

    function _setPaymentHandlerSettings(
        address payoutAddress
    ) internal {
        __s().payoutAddress = payoutAddress;
    }

    function _transferTo(
        string memory paymentMethodName,
        address to,
        uint256 amount,
        string memory data
    ) internal {
        require(to != address(0), "PH:TTZ");
        require(amount > 0, "PH:ZAM");
        bytes32 nameHash = keccak256(abi.encode(paymentMethodName));
        require(nameHash == WEI_PAYMENT_METHOD_HASH ||
                PaymentMethodManagerLib._paymentMethodExists(nameHash), "PH:MNS");
        if (nameHash == WEI_PAYMENT_METHOD_HASH) {
            require(amount <= address(this).balance, "PH:MTB");
            /* solhint-disable avoid-low-level-calls */
            (bool success, ) = to.call{value: amount}(new bytes(0));
            /* solhint-enable avoid-low-level-calls */
            require(success, "PH:TF");
            emit TransferTo(to, amount, data);
        } else {
            address erc20 =
                PaymentMethodManagerLib._getERC20PaymentMethodAddress(nameHash);
            require(amount <= IERC20(erc20).balanceOf(address(this)), "PH:MTB");
            IERC20(erc20).transfer(to, amount);
            emit TransferETH20To(paymentMethodName, to, amount, data);
        }
    }

    function _handlePayment(
        uint256 nrOfItems1, uint256 priceWeiPerItem1,
        uint256 nrOfItems2, uint256 priceWeiPerItem2,
        string memory paymentMethodName
    ) internal {
        uint256 totalWei =
            nrOfItems1 * priceWeiPerItem1 +
            nrOfItems2 * priceWeiPerItem2;
        if (totalWei == 0) {
            return;
        }
        bytes32 nameHash = keccak256(abi.encode(paymentMethodName));
        require(nameHash == WEI_PAYMENT_METHOD_HASH ||
                PaymentMethodManagerLib._paymentMethodExists(nameHash), "PH:MNS");
        if (nameHash == WEI_PAYMENT_METHOD_HASH) {
            PaymentMethodManagerLib._handleWeiPayment(
                msg.sender, __s().payoutAddress, msg.value, totalWei, "");
        } else {
            PaymentMethodManagerLib.
                _handleERC20Payment(
                    paymentMethodName, msg.sender, __s().payoutAddress, totalWei, "");
        }
    }

    function __s() private pure returns (PaymentHandlerStorage.Layout storage) {
        return PaymentHandlerStorage.layout();
    }
}

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

pragma solidity ^0.8.0;

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

File 5 of 12 : IERC165.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)

pragma solidity ^0.8.0;

/**
 * @dev Interface of the ERC165 standard, as defined in the
 * https://eips.ethereum.org/EIPS/eip-165[EIP].
 *
 * Implementers can declare support of contract interfaces, which can then be
 * queried by others ({ERC165Checker}).
 *
 * For an implementation, see {ERC165}.
 */
interface IERC165 {
    /**
     * @dev Returns true if this contract implements the interface defined by
     * `interfaceId`. See the corresponding
     * https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
     * to learn more about how these ids are created.
     *
     * This function call must use less than 30 000 gas.
     */
    function supportsInterface(bytes4 interfaceId) external view returns (bool);
}

File 6 of 12 : IERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (interfaces/IERC20.sol)

pragma solidity ^0.8.0;

import "../token/ERC20/IERC20.sol";

File 7 of 12 : IUniswapV2Pair.sol
/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

interface IUniswapV2Pair {
    event Approval(address indexed owner, address indexed spender, uint value);
    event Transfer(address indexed from, address indexed to, uint value);

    function name() external pure returns (string memory);
    function symbol() external pure returns (string memory);
    function decimals() external pure returns (uint8);
    function totalSupply() external view returns (uint);
    function balanceOf(address owner) external view returns (uint);
    function allowance(address owner, address spender) external view returns (uint);

    function approve(address spender, uint value) external returns (bool);
    function transfer(address to, uint value) external returns (bool);
    function transferFrom(address from, address to, uint value) external returns (bool);

    function DOMAIN_SEPARATOR() external view returns (bytes32);
    function PERMIT_TYPEHASH() external pure returns (bytes32);
    function nonces(address owner) external view returns (uint);

    function permit(address owner, address spender, uint value, uint deadline, uint8 v, bytes32 r, bytes32 s) external;

    event Mint(address indexed sender, uint amount0, uint amount1);
    event Burn(address indexed sender, uint amount0, uint amount1, address indexed to);
    event Swap(
        address indexed sender,
        uint amount0In,
        uint amount1In,
        uint amount0Out,
        uint amount1Out,
        address indexed to
    );
    event Sync(uint112 reserve0, uint112 reserve1);

    function MINIMUM_LIQUIDITY() external pure returns (uint);
    function factory() external view returns (address);
    function token0() external view returns (address);
    function token1() external view returns (address);
    function getReserves() external view returns (uint112 reserve0, uint112 reserve1, uint32 blockTimestampLast);
    function price0CumulativeLast() external view returns (uint);
    function price1CumulativeLast() external view returns (uint);
    function kLast() external view returns (uint);

    function mint(address to) external returns (uint liquidity);
    function burn(address to) external returns (uint amount0, uint amount1);
    function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external;
    function skim(address to) external;
    function sync() external;

    function initialize(address, address) external;
}

File 8 of 12 : PaymentMethodManagerLib.sol
/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "./PaymentMethodManagerInternal.sol";

/// @author Kam Amini <[email protected]>
///
/// @notice Use at your own risk
library PaymentMethodManagerLib {

    function _handleWeiPayment(
        address payer,
        address dest,
        uint256 paidPriceWei, // could be the msg.value
        uint256 priceWeiToPay,
        string memory data
    ) internal {
        PaymentMethodManagerInternal._handleWeiPayment(
            payer,
            dest,
            paidPriceWei,
            priceWeiToPay,
            data
        );
    }

    function _handleERC20Payment(
        string memory paymentMethodName,
        address payer,
        address dest,
        uint256 priceWeiToPay,
        string memory data
    ) internal {
        PaymentMethodManagerInternal._handleERC20Payment(
            paymentMethodName,
            payer,
            dest,
            priceWeiToPay,
            data
        );
    }

    function _paymentMethodExists(
        bytes32 paymentMethodNameHash
    ) internal view returns (bool) {
        return PaymentMethodManagerLib._paymentMethodExists(paymentMethodNameHash);
    }

    function _paymentMethodEnabled(
        bytes32 paymentMethodNameHash
    ) internal view returns (bool) {
        return PaymentMethodManagerLib._paymentMethodEnabled(paymentMethodNameHash);
    }

    function _getERC20PaymentMethodAddress(
        bytes32 paymentMethodNameHash
    ) internal view returns (address) {
        return PaymentMethodManagerLib._getERC20PaymentMethodAddress(paymentMethodNameHash);
    }
}

File 9 of 12 : PaymentHandlerStorage.sol
/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

/// @author Kam Amini <[email protected]>
///
/// @notice Use at your own risk. Just got the basic
///         idea from: https://github.com/solidstate-network/solidstate-solidity
library PaymentHandlerStorage {

    struct Layout {
        address payoutAddress;
        mapping(bytes32 => bytes) extra;
    }

    bytes32 internal constant STORAGE_SLOT =
        keccak256("qomet-tech.contracts.facets.collection.payment-handler.storage");

    function layout() internal pure returns (Layout storage s) {
        bytes32 slot = STORAGE_SLOT;
        /* solhint-disable no-inline-assembly */
        assembly {
            s.slot := slot
        }
        /* solhint-enable no-inline-assembly */
    }
}

File 10 of 12 : IERC20.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.5.0) (token/ERC20/IERC20.sol)

pragma solidity ^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 `to`.
     *
     * Returns a boolean value indicating whether the operation succeeded.
     *
     * Emits a {Transfer} event.
     */
    function transfer(address to, uint256 amount) external returns (bool);

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

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

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

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

File 11 of 12 : PaymentMethodManagerInternal.sol
/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

import "@openzeppelin/contracts/interfaces/IERC20.sol";
import "../../IUniswapV2Pair.sol";
import "./PaymentMethodManagerStorage.sol";

/// @author Kam Amini <[email protected]>
///
/// @notice Use at your own risk
library PaymentMethodManagerInternal {

    event ERC20PaymentMethodUpdate(
        string paymentMethodName,
        address erc20,
        address wethPair,
        bool enabled,
        string data
    );
    event WeiPayment(
        address payer,
        address dest,
        uint256 paidPriceWei,
        uint256 priceWeiToPay,
        string data
    );
    event ERC20Payment(
        string paymentMethodName,
        address payer,
        address dest,
        uint256 amountWei,
        uint256 amountTokens,
        string data
    );

    function _getPaymentMethodManagerSettings() internal view returns (address) {
        return __s().wethAddress;
    }

    function _setPaymentMethodManagerSettings(
        address wethAddress
    ) internal {
        __s().wethAddress = wethAddress;
    }

    function _getERC20PaymentMethods() internal view returns (string[] memory) {
        return __s().erc20PaymentMethodNames;
    }

    function _getERC20PaymentMethod(
        string memory paymentMethodName
    ) internal view returns (address, address, bool) {
        bytes32 nameHash = keccak256(abi.encode(paymentMethodName));
        require(_paymentMethodExists(nameHash), "PMM:NEM");
        return (
            __s().erc20PaymentMethods[nameHash].erc20,
            __s().erc20PaymentMethods[nameHash].wethPair,
            __s().erc20PaymentMethods[nameHash].enabled
        );
    }

    function _addOrUpdateERC20PaymentMethod(
        string memory paymentMethodName,
        address erc20,
        address wethPair,
        bool enabled,
        string memory data
    ) internal {
        bytes32 nameHash = keccak256(abi.encode(paymentMethodName));
        __s().erc20PaymentMethods[nameHash].erc20 = erc20;
        __s().erc20PaymentMethods[nameHash].wethPair = wethPair;
        __s().erc20PaymentMethods[nameHash].enabled = enabled;
        address token0 = IUniswapV2Pair(wethPair).token0();
        address token1 = IUniswapV2Pair(wethPair).token1();
        require(token0 == __s().wethAddress || token1 == __s().wethAddress, "PMM:IPC");
        bool reverseIndices = (token1 == __s().wethAddress);
        __s().erc20PaymentMethods[nameHash].reverseIndices = reverseIndices;
        if (__s().erc20PaymentMethodNamesIndex[paymentMethodName] == 0) {
            __s().erc20PaymentMethodNames.push(paymentMethodName);
            __s().erc20PaymentMethodNamesIndex[paymentMethodName] =
                __s().erc20PaymentMethodNames.length;
        }
        emit ERC20PaymentMethodUpdate(
            paymentMethodName, erc20, wethPair, enabled, data);
    }

    function _enableERC20TokenPayment(
        string memory paymentMethodName,
        bool enabled
    ) internal {
        bytes32 nameHash = keccak256(abi.encode(paymentMethodName));
        require(_paymentMethodExists(nameHash), "PMM:NEM");
        __s().erc20PaymentMethods[nameHash].enabled = enabled;
        emit ERC20PaymentMethodUpdate(
            paymentMethodName,
            __s().erc20PaymentMethods[nameHash].erc20,
            __s().erc20PaymentMethods[nameHash].wethPair,
            enabled,
            ""
        );
    }

    function _handleWeiPayment(
        address payer,
        address dest,
        uint256 paidPriceWei, // could be the msg.value
        uint256 priceWeiToPay,
        string memory data
    ) internal {
        require(paidPriceWei >= priceWeiToPay, "PMM:IF");
        uint256 remainder = paidPriceWei - priceWeiToPay;
        if (dest != address(0)) {
            /* solhint-disable avoid-low-level-calls */
            (bool success, ) = dest.call{value: priceWeiToPay}(new bytes(0));
            /* solhint-enable avoid-low-level-calls */
            require(success, "PMM:TF");
            emit WeiPayment(payer, dest, paidPriceWei, priceWeiToPay, data);
        } else {
            emit WeiPayment(
                payer, address(this), paidPriceWei, priceWeiToPay, data);
        }
        if (remainder > 0) {
            /* solhint-disable avoid-low-level-calls */
            (bool success, ) = payer.call{value: remainder}(new bytes(0));
            /* solhint-enable avoid-low-level-calls */
            require(success, "PMM:RTF");
        }
    }

    function _handleERC20Payment(
        string memory paymentMethodName,
        address payer,
        address dest,
        uint256 priceWeiToPay,
        string memory data
    ) internal {
        bytes32 nameHash = keccak256(abi.encode(paymentMethodName));
        require(_paymentMethodExists(nameHash), "PMM:NEM");
        require(_paymentMethodEnabled(nameHash), "PMM:NENM");
        PaymentMethodManagerStorage.ERC20PaymentMethod memory paymentMethod =
            __s().erc20PaymentMethods[nameHash];
        (uint112 amount0, uint112 amount1,) = IUniswapV2Pair(paymentMethod.wethPair).getReserves();
        uint256 reserveWei = amount0;
        uint256 reserveTokens = amount1;
        if (paymentMethod.reverseIndices) {
            reserveWei = amount1;
            reserveTokens = amount0;
        }
        require(reserveWei > 0, "PMM:NWR");
        // TODO(kam): check if this is OK
        uint256 amountTokens = (priceWeiToPay * reserveTokens) / reserveWei;
        if (dest == address(0)) {
            dest = address(this);
        }
        // this contract must have already been approved by the msg.sender
        IERC20(paymentMethod.erc20).transferFrom(payer, dest, amountTokens);
        emit ERC20Payment(
            paymentMethodName, payer, dest, priceWeiToPay, amountTokens, data);
    }

    function _paymentMethodExists(
        bytes32 paymentMethodNameHash
    ) internal view returns (bool) {
        return __s().erc20PaymentMethods[paymentMethodNameHash].erc20 != address(0) &&
               __s().erc20PaymentMethods[paymentMethodNameHash].wethPair != address(0);
    }

    function _paymentMethodEnabled(
        bytes32 paymentMethodNameHash
    ) internal view returns (bool) {
        return __s().erc20PaymentMethods[paymentMethodNameHash].enabled;
    }

    function _getERC20PaymentMethodAddress(
        bytes32 paymentMethodNameHash
    ) internal view returns (address) {
        require(_paymentMethodExists(paymentMethodNameHash), "PMM:NEM");
        return __s().erc20PaymentMethods[paymentMethodNameHash].erc20;
    }

    function __s() private pure returns (PaymentMethodManagerStorage.Layout storage) {
        return PaymentMethodManagerStorage.layout();
    }
}

File 12 of 12 : PaymentMethodManagerStorage.sol
/*
 * This file is part of the Qomet Technologies contracts (https://github.com/qomet-tech/contracts).
 * Copyright (c) 2022 Qomet Technologies (https://qomet.tech)
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
// SPDX-License-Identifier: GNU General Public License v3.0

pragma solidity 0.8.1;

/// @author Kam Amini <[email protected]>
///
/// @notice Use at your own risk. Just got the basic
///         idea from: https://github.com/solidstate-network/solidstate-solidity
library PaymentMethodManagerStorage {

    struct ERC20PaymentMethod {
        // The internal unique name of the ERC-20 payment method
        string name;
        // The ERC-20 contract
        address erc20;
        // Uniswap V2 Pair with WETH
        address wethPair;
        // True if the read pair from Uniswap has a reverse ordering
        // for contract addresses
        bool reverseIndices;
        // If the payment method is enabled
        bool enabled;
    }

    struct Layout {
        // The WETH ERC-20 contract address.
        //   On mainnet, it is: 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
        address wethAddress;
        // The list of the existing ERC20 payment method names
        string[] erc20PaymentMethodNames;
        mapping(string => uint256)  erc20PaymentMethodNamesIndex;
        // name > erc20 payment method
        mapping(bytes32 => ERC20PaymentMethod) erc20PaymentMethods;
        // Reserved for future upgrades
        mapping(bytes32 => bytes) extra;
    }

    bytes32 internal constant STORAGE_SLOT =
        keccak256("qomet-tech.contracts.facets.collection.payment-method-manager.storage");

    function layout() internal pure returns (Layout storage s) {
        bytes32 slot = STORAGE_SLOT;
        /* solhint-disable no-inline-assembly */
        assembly {
            s.slot := slot
        }
        /* solhint-enable no-inline-assembly */
    }
}

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

Contract Security Audit

Contract ABI

[{"inputs":[],"name":"getFacetName","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getFacetPI","outputs":[{"internalType":"string[]","name":"","type":"string[]"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getFacetProtectedPI","outputs":[{"internalType":"string[]","name":"","type":"string[]"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getFacetVersion","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"pure","type":"function"},{"inputs":[],"name":"getPaymentHandlerSettings","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"payoutAddress","type":"address"}],"name":"setPaymentHandlerSettings","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"bytes4","name":"interfaceId","type":"bytes4"}],"name":"supportsInterface","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"pure","type":"function"},{"inputs":[{"internalType":"string","name":"paymentMethodName","type":"string"},{"internalType":"address","name":"to","type":"address"},{"internalType":"uint256","name":"amount","type":"uint256"},{"internalType":"string","name":"data","type":"string"}],"name":"transferTo","outputs":[],"stateMutability":"nonpayable","type":"function"}]

608060405234801561001057600080fd5b50610bea806100206000396000f3fe608060405234801561001057600080fd5b50600436106100885760003560e01c80633c0f5faf1161005b5780633c0f5faf146100f557806368498a331461010a578063afbc802514610112578063f8c2be4f1461012557610088565b806301ffc9a71461008d5780631078fade146100b6578063320dcd97146100cb57806339ab8b40146100e0575b600080fd5b6100a061009b366004610847565b61012d565b6040516100ad9190610a07565b60405180910390f35b6100be610147565b6040516100ad91906109a7565b6100d361026b565b6040516100ad9190610a12565b6100e8610294565b6040516100ad919061094a565b610108610103366004610806565b6102a3565b005b6100be6102af565b61010861012036600461086f565b61035f565b6100d3610371565b6001600160e01b0319811663b2fe033560e01b145b919050565b6040805160038082526080820190925260609160009190816020015b60608152602001906001900390816101635790505090506040518060400160405280601b81526020017f6765745061796d656e7448616e646c657253657474696e677328290000000000815250816000815181106101d157634e487b7160e01b600052603260045260246000fd5b6020026020010181905250604051806060016040528060228152602001610b6a602291398160018151811061021657634e487b7160e01b600052603260045260246000fd5b6020026020010181905250604051806060016040528060298152602001610b8c602991398160028151811061025b57634e487b7160e01b600052603260045260246000fd5b6020908102919091010152905090565b60408051808201909152600f81526e3830bcb6b2b73a16b430b7323632b960891b602082015290565b600061029e610390565b905090565b6102ac816103a9565b50565b604080516002808252606082810190935260009190816020015b60608152602001906001900390816102c9579050509050604051806060016040528060228152602001610b6a602291398160008151811061031a57634e487b7160e01b600052603260045260246000fd5b6020026020010181905250604051806060016040528060298152602001610b8c602991398160018151811061025b57634e487b7160e01b600052603260045260246000fd5b61036b848484846103d3565b50505050565b6040805180820190915260058152640342e302e360dc1b602082015290565b600061039a610729565b546001600160a01b0316905090565b806103b2610729565b80546001600160a01b0319166001600160a01b039290921691909117905550565b6001600160a01b0383166104025760405162461bcd60e51b81526004016103f990610ae7565b60405180910390fd5b600082116104225760405162461bcd60e51b81526004016103f990610b07565b6000846040516020016104359190610a12565b60405160208183030381529060405280519060200120905060405160200161045c90610aca565b60405160208183030381529060405280519060200120811480610483575061048381610733565b61049f5760405162461bcd60e51b81526004016103f990610aaa565b6040516020016104ae90610aca565b604051602081830303815290604052805190602001208114156105b957478311156104eb5760405162461bcd60e51b81526004016103f990610a8a565b604080516000808252602082019092526001600160a01b038616908590604051610515919061092e565b60006040518083038185875af1925050503d8060008114610552576040519150601f19603f3d011682016040523d82523d6000602084013e610557565b606091505b50509050806105785760405162461bcd60e51b81526004016103f990610a6b565b7f7e3fdce76c995e7fd7ebbd5177d6ef82b4ea364b1a078e501b91a3de4a3378038585856040516105ab93929190610977565b60405180910390a150610722565b60006105c482610733565b6040516370a0823160e01b81529091506001600160a01b038216906370a08231906105f390309060040161094a565b60206040518083038186803b15801561060b57600080fd5b505afa15801561061f573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061064391906108ea565b8411156106625760405162461bcd60e51b81526004016103f990610a8a565b60405163a9059cbb60e01b81526001600160a01b0382169063a9059cbb90610690908890889060040161095e565b602060405180830381600087803b1580156106aa57600080fd5b505af11580156106be573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106e29190610827565b507fa3598163b612c857c72dadeaa15f50613e4d837547d69aec1e045e84a54ab2ee868686866040516107189493929190610a25565b60405180910390a1505b5050505050565b600061029e610744565b600061073e82610733565b92915050565b7fbbdc48a4c38ee68aae7b7a4d695913f62d41dea3b689e93b0a6ca9163997770990565b80356001600160a01b038116811461014257600080fd5b600082601f83011261078f578081fd5b813567ffffffffffffffff808211156107aa576107aa610b53565b604051601f8301601f19908116603f011681019082821181831017156107d2576107d2610b53565b816040528381528660208588010111156107ea578485fd5b8360208701602083013792830160200193909352509392505050565b600060208284031215610817578081fd5b61082082610768565b9392505050565b600060208284031215610838578081fd5b81518015158114610820578182fd5b600060208284031215610858578081fd5b81356001600160e01b031981168114610820578182fd5b60008060008060808587031215610884578283fd5b843567ffffffffffffffff8082111561089b578485fd5b6108a78883890161077f565b95506108b560208801610768565b94506040870135935060608701359150808211156108d1578283fd5b506108de8782880161077f565b91505092959194509250565b6000602082840312156108fb578081fd5b5051919050565b6000815180845261091a816020860160208601610b27565b601f01601f19169290920160200192915050565b60008251610940818460208701610b27565b9190910192915050565b6001600160a01b0391909116815260200190565b6001600160a01b03929092168252602082015260400190565b600060018060a01b03851682528360208301526060604083015261099e6060830184610902565b95945050505050565b6000602080830181845280855180835260408601915060408482028701019250838701855b828110156109fa57603f198886030184526109e8858351610902565b945092850192908501906001016109cc565b5092979650505050505050565b901515815260200190565b6000602082526108206020830184610902565b600060808252610a386080830187610902565b6001600160a01b0386166020840152604083018590528281036060840152610a608185610902565b979650505050505050565b60208082526005908201526428241d2a2360d91b604082015260600190565b60208082526006908201526528241d26aa2160d11b604082015260600190565b60208082526006908201526550483a4d4e5360d01b604082015260600190565b60208082526003908201526257454960e81b604082015260600190565b60208082526006908201526528241d2a2a2d60d11b604082015260600190565b60208082526006908201526550483a5a414d60d01b604082015260600190565b60005b83811015610b42578181015183820152602001610b2a565b8381111561036b5750506000910152565b634e487b7160e01b600052604160045260246000fdfe7365745061796d656e7448616e646c657253657474696e67732861646472657373297472616e73666572546f28737472696e672c616464726573732c75696e743235362c737472696e6729a2646970667358221220029bd6fba166c78dfbe47b46cd75ff7cc28c7d99dc133b16b2bfbb0186e626fb64736f6c63430008010033

Deployed Bytecode

0x608060405234801561001057600080fd5b50600436106100885760003560e01c80633c0f5faf1161005b5780633c0f5faf146100f557806368498a331461010a578063afbc802514610112578063f8c2be4f1461012557610088565b806301ffc9a71461008d5780631078fade146100b6578063320dcd97146100cb57806339ab8b40146100e0575b600080fd5b6100a061009b366004610847565b61012d565b6040516100ad9190610a07565b60405180910390f35b6100be610147565b6040516100ad91906109a7565b6100d361026b565b6040516100ad9190610a12565b6100e8610294565b6040516100ad919061094a565b610108610103366004610806565b6102a3565b005b6100be6102af565b61010861012036600461086f565b61035f565b6100d3610371565b6001600160e01b0319811663b2fe033560e01b145b919050565b6040805160038082526080820190925260609160009190816020015b60608152602001906001900390816101635790505090506040518060400160405280601b81526020017f6765745061796d656e7448616e646c657253657474696e677328290000000000815250816000815181106101d157634e487b7160e01b600052603260045260246000fd5b6020026020010181905250604051806060016040528060228152602001610b6a602291398160018151811061021657634e487b7160e01b600052603260045260246000fd5b6020026020010181905250604051806060016040528060298152602001610b8c602991398160028151811061025b57634e487b7160e01b600052603260045260246000fd5b6020908102919091010152905090565b60408051808201909152600f81526e3830bcb6b2b73a16b430b7323632b960891b602082015290565b600061029e610390565b905090565b6102ac816103a9565b50565b604080516002808252606082810190935260009190816020015b60608152602001906001900390816102c9579050509050604051806060016040528060228152602001610b6a602291398160008151811061031a57634e487b7160e01b600052603260045260246000fd5b6020026020010181905250604051806060016040528060298152602001610b8c602991398160018151811061025b57634e487b7160e01b600052603260045260246000fd5b61036b848484846103d3565b50505050565b6040805180820190915260058152640342e302e360dc1b602082015290565b600061039a610729565b546001600160a01b0316905090565b806103b2610729565b80546001600160a01b0319166001600160a01b039290921691909117905550565b6001600160a01b0383166104025760405162461bcd60e51b81526004016103f990610ae7565b60405180910390fd5b600082116104225760405162461bcd60e51b81526004016103f990610b07565b6000846040516020016104359190610a12565b60405160208183030381529060405280519060200120905060405160200161045c90610aca565b60405160208183030381529060405280519060200120811480610483575061048381610733565b61049f5760405162461bcd60e51b81526004016103f990610aaa565b6040516020016104ae90610aca565b604051602081830303815290604052805190602001208114156105b957478311156104eb5760405162461bcd60e51b81526004016103f990610a8a565b604080516000808252602082019092526001600160a01b038616908590604051610515919061092e565b60006040518083038185875af1925050503d8060008114610552576040519150601f19603f3d011682016040523d82523d6000602084013e610557565b606091505b50509050806105785760405162461bcd60e51b81526004016103f990610a6b565b7f7e3fdce76c995e7fd7ebbd5177d6ef82b4ea364b1a078e501b91a3de4a3378038585856040516105ab93929190610977565b60405180910390a150610722565b60006105c482610733565b6040516370a0823160e01b81529091506001600160a01b038216906370a08231906105f390309060040161094a565b60206040518083038186803b15801561060b57600080fd5b505afa15801561061f573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061064391906108ea565b8411156106625760405162461bcd60e51b81526004016103f990610a8a565b60405163a9059cbb60e01b81526001600160a01b0382169063a9059cbb90610690908890889060040161095e565b602060405180830381600087803b1580156106aa57600080fd5b505af11580156106be573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906106e29190610827565b507fa3598163b612c857c72dadeaa15f50613e4d837547d69aec1e045e84a54ab2ee868686866040516107189493929190610a25565b60405180910390a1505b5050505050565b600061029e610744565b600061073e82610733565b92915050565b7fbbdc48a4c38ee68aae7b7a4d695913f62d41dea3b689e93b0a6ca9163997770990565b80356001600160a01b038116811461014257600080fd5b600082601f83011261078f578081fd5b813567ffffffffffffffff808211156107aa576107aa610b53565b604051601f8301601f19908116603f011681019082821181831017156107d2576107d2610b53565b816040528381528660208588010111156107ea578485fd5b8360208701602083013792830160200193909352509392505050565b600060208284031215610817578081fd5b61082082610768565b9392505050565b600060208284031215610838578081fd5b81518015158114610820578182fd5b600060208284031215610858578081fd5b81356001600160e01b031981168114610820578182fd5b60008060008060808587031215610884578283fd5b843567ffffffffffffffff8082111561089b578485fd5b6108a78883890161077f565b95506108b560208801610768565b94506040870135935060608701359150808211156108d1578283fd5b506108de8782880161077f565b91505092959194509250565b6000602082840312156108fb578081fd5b5051919050565b6000815180845261091a816020860160208601610b27565b601f01601f19169290920160200192915050565b60008251610940818460208701610b27565b9190910192915050565b6001600160a01b0391909116815260200190565b6001600160a01b03929092168252602082015260400190565b600060018060a01b03851682528360208301526060604083015261099e6060830184610902565b95945050505050565b6000602080830181845280855180835260408601915060408482028701019250838701855b828110156109fa57603f198886030184526109e8858351610902565b945092850192908501906001016109cc565b5092979650505050505050565b901515815260200190565b6000602082526108206020830184610902565b600060808252610a386080830187610902565b6001600160a01b0386166020840152604083018590528281036060840152610a608185610902565b979650505050505050565b60208082526005908201526428241d2a2360d91b604082015260600190565b60208082526006908201526528241d26aa2160d11b604082015260600190565b60208082526006908201526550483a4d4e5360d01b604082015260600190565b60208082526003908201526257454960e81b604082015260600190565b60208082526006908201526528241d2a2a2d60d11b604082015260600190565b60208082526006908201526550483a5a414d60d01b604082015260600190565b60005b83811015610b42578181015183820152602001610b2a565b8381111561036b5750506000910152565b634e487b7160e01b600052604160045260246000fdfe7365745061796d656e7448616e646c657253657474696e67732861646472657373297472616e73666572546f28737472696e672c616464726573732c75696e743235362c737472696e6729a2646970667358221220029bd6fba166c78dfbe47b46cd75ff7cc28c7d99dc133b16b2bfbb0186e626fb64736f6c63430008010033

Block Transaction Difficulty Gas Used Reward
View All Blocks Produced

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

Validator Index Block Amount
View All Withdrawals

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

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