// File: /Users/svenschumacher/git/dloop/platform-registry/truffle/payment/contracts/DloopPayment.sol



/**
Author: dloop ltd
Website: https://www.dloop.ch
Source Code & Licensing Agreement: https://github.com/dloop-ltd/art-payment
*/

// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;

import "./vendor/IUniswapV2Router01.sol";
import "./DloopPaymentGovernance.sol";
import "./DloopPaymentUtil.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";

contract DloopPayment is DloopPaymentGovernance, DloopPaymentUtil {
    address
        private constant UNISWAP_ADDR = 0xf164fC0Ec4E93095b804a4795bBe1e041497b92a;
    address
        private constant DAI_ADDR = 0x6B175474E89094C44Da98b954EedeAC495271d0F;
    address
        private constant WETH_ADDR = 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2;

    IUniswapV2Router01 private _uniswapRouter;
    address private _paymentTokenAddr;
    address private _wethAddr;
    uint256 private _weiToRefund = 0;

    mapping(uint64 => bool) _checkoutIdMap;

    event SwapParamsSet(
        address uniswapRouterAddr,
        address paymentTokenAddr,
        address wethAddr
    );

    event PaymentDone(
        uint64 indexed checkoutId,
        uint256 weiInFromBuyer,
        uint256 weiOutToUniswap,
        uint256 weiOutRefundBuyer,
        uint256 tokenInFromUniswap,
        uint256 tokenOutToDloop,
        uint256 tokenOutToArtist
    );

    event EditionBought(
        uint64 indexed artistId,
        uint64 indexed artworkId,
        uint256 indexed tokenId,
        uint64 checkoutId
    );

    constructor() public {
        setSwapParams(UNISWAP_ADDR, DAI_ADDR, WETH_ADDR);
    }

    function setSwapParams(
        address uniswapRouterAddr,
        address paymentTokenAddr,
        address wethAddr
    ) public onlyOwner {
        require(
            uniswapRouterAddr != address(0x0),
            "uniswapRouterAddr must not be 0x0"
        );
        require(
            paymentTokenAddr != address(0x0),
            "paymentTokenAddr must not be 0x0"
        );
        require(wethAddr != address(0x0), "wethAddr must not be 0x0");

        _uniswapRouter = IUniswapV2Router01(uniswapRouterAddr);
        _paymentTokenAddr = paymentTokenAddr;
        _wethAddr = wethAddr;

        emit SwapParamsSet(uniswapRouterAddr, _paymentTokenAddr, _wethAddr);
    }

    function buyEdition(
        uint64 artistId,
        uint64 artworkId,
        uint256 tokenId,
        uint64 checkoutId,
        address dloopAddress,
        uint256 dloopAmount,
        address artistAddress,
        uint256 artistAmount,
        uint256 maxEthAmount,
        uint256 expiresAt,
        bytes memory sig
    ) external payable nonReentrant whenNotPaused {
        require(artistId > 0, "artistId must be positive");
        require(artworkId > 0, "artworkId must be positive");
        require(tokenId > 0, "tokenId must be positive");
        require(checkoutId > 0, "checkoutId must be positive");
        require(dloopAddress != address(0x0), "dloopAddress must not be 0x0");
        require(dloopAmount > 0, "dloopAmount must be positive");
        require(artistAddress != address(0x0), "artistAddress must not be 0x0");
        require(artistAmount > 0, "artistAmount must be positive");
        require(maxEthAmount > 0, "maxEthAmount must be positive");
        require(maxEthAmount >= msg.value, "sent Ether exceeds maxEthAmount");
        require(msg.value > 0, "no Ether sent");
        require(expiresAt >= block.timestamp, "checkout expired");
        require(sig.length > 0, "signature missing");
        require(!_checkoutIdMap[checkoutId], "edition already bought");
        require(
            dloopAddress != artistAddress,
            "dloopAddress must not equal artistAddress"
        );

        _checkoutIdMap[checkoutId] = true;
        {
            // scope to avoid stack too deep errors

            bytes32 hash = createHash(
                artistId,
                artworkId,
                tokenId,
                checkoutId,
                dloopAddress,
                dloopAmount,
                artistAddress,
                artistAmount,
                maxEthAmount,
                expiresAt
            );

            require(
                validateSignature(hash, sig, getSigningAddress()),
                "signature invalid"
            );
        }

        uint256 totalAmount = SafeMath.add(dloopAmount, artistAmount);
        {
            // scope to avoid stack too deep errors

            exchangeEthToToken(totalAmount);

            IERC20 paymentToken = IERC20(_paymentTokenAddr);
            uint256 contractAmount = paymentToken.balanceOf(address(this));
            require(
                contractAmount >= totalAmount,
                "not enough payment tokens received"
            );

            require(
                paymentToken.transfer(artistAddress, artistAmount),
                "artistAddress payment token transfer failed"
            );
            require(
                paymentToken.transfer(dloopAddress, dloopAmount),
                "dloopAddress payment token transfer failed"
            );

            bool result = msg.sender.send(_weiToRefund);
            require(result, "Ether refund failed");
        }

        {
            // scope to avoid stack too deep errors

            uint256 weiOutToUniswap = SafeMath.sub(msg.value, _weiToRefund);

            emit PaymentDone(
                checkoutId,
                msg.value,
                weiOutToUniswap,
                _weiToRefund,
                totalAmount,
                dloopAmount,
                artistAmount
            );

            emit EditionBought(artistId, artworkId, tokenId, checkoutId);
        }
        _weiToRefund = 0; // reset the state
    }

    function isSold(uint64 checkOutId) external view returns (bool) {
        return _checkoutIdMap[checkOutId];
    }

    function getSwapParams()
        external
        view
        returns (
            address uniswapRouterAddr,
            address paymentTokenAddr,
            address wethAddr
        )
    {
        uniswapRouterAddr = address(_uniswapRouter);
        paymentTokenAddr = _paymentTokenAddr;
        wethAddr = _wethAddr;
    }

    function exchangeEthToToken(uint256 tokenAmount) private {
        _uniswapRouter.swapETHForExactTokens{value: msg.value}(
            tokenAmount,
            getPathForETHtoToken(),
            address(this),
            block.timestamp
        );
    }

    function getPathForETHtoToken() private view returns (address[] memory) {
        address[] memory path = new address[](2);
        path[0] = _wethAddr;
        path[1] = _paymentTokenAddr;

        return path;
    }

    // Fallback function
    // Uniswap will refund not used Ether here.
    receive() external payable {
        _weiToRefund = msg.value;
    }
}

// File: /Users/svenschumacher/git/dloop/platform-registry/truffle/payment/contracts/DloopPaymentGovernance.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/Pausable.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

contract DloopPaymentGovernance is Ownable, Pausable, ReentrancyGuard {
    address private _signingAddress;

    event SigningAddressSet(
        address indexed previousAddress,
        address indexed newAddress
    );

    event ExcessPaymentTokensWithdrawn(
        address indexed tokenAddress,
        address indexed to,
        uint256 amount
    );

    event ExcessEtherWithdrawn(address indexed to, uint256 amount);

    constructor() public {
        _signingAddress = msg.sender;
    }

    function setSigningAddress(address signer) external onlyOwner {
        emit SigningAddressSet(_signingAddress, signer);
        _signingAddress = signer;
    }

    function getSigningAddress() public view returns (address) {
        return _signingAddress;
    }

    function pause() external onlyOwner {
        super._pause();
    }

    function unpause() external onlyOwner {
        super._unpause();
    }

    function withdrawExcessPaymentTokens(address to, address tokenAddr)
        external
        nonReentrant
        onlyOwner
    {
        IERC20 token = IERC20(tokenAddr);
        uint256 totalTokens = token.balanceOf(address(this));

        require(
            token.transfer(to, totalTokens),
            "withdrawExcessPaymentTokens failed"
        );
        emit ExcessPaymentTokensWithdrawn(tokenAddr, to, totalTokens);
    }

    function withdrawExcessEther(address payable to)
        external
        nonReentrant
        onlyOwner
    {
        uint256 totalWei = address(this).balance;
        bool result = to.send(totalWei);

        require(result, "withdrawExcessEther failed");
        emit ExcessEtherWithdrawn(to, totalWei);
    }
}

// File: /Users/svenschumacher/git/dloop/platform-registry/truffle/payment/contracts/DloopPaymentUtil.sol



// SPDX-License-Identifier: MIT
pragma solidity 0.6.12;

import "@openzeppelin/contracts/cryptography/ECDSA.sol";

contract DloopPaymentUtil {
    function validateSignature(
        bytes32 hash,
        bytes memory sig,
        address expectedSigner
    ) public pure returns (bool) {
        bytes32 ethSignedHash = ECDSA.toEthSignedMessageHash(hash);
        address actualSigner = ECDSA.recover(ethSignedHash, sig);
        return actualSigner == expectedSigner;
    }

    function createHash(
        uint256 artistId,
        uint64 artworkId,
        uint256 tokenId,
        uint64 checkoutId,
        address dloopAddress,
        uint256 dloopAmount,
        address artistAddress,
        uint256 artistAmount,
        uint256 maxEthAmount,
        uint256 expiresAt
    ) public pure returns (bytes32) {
        bytes memory encodedParams = abi.encodePacked(
            artistId,
            artworkId,
            tokenId,
            checkoutId,
            dloopAddress,
            dloopAmount,
            artistAddress,
            artistAmount,
            maxEthAmount,
            expiresAt
        );
        return keccak256(encodedParams);
    }
}

// File: /Users/svenschumacher/git/dloop/platform-registry/truffle/payment/contracts/vendor/IUniswapV2Router01.sol



// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.6.2;

interface IUniswapV2Router01 {

    function swapETHForExactTokens(
        uint256 amountOut,
        address[] calldata path,
        address to,
        uint256 deadline
    ) external payable returns (uint256[] memory amounts);
}

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



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

// File: @openzeppelin/contracts/cryptography/ECDSA.sol



// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

/**
 * @dev Elliptic Curve Digital Signature Algorithm (ECDSA) operations.
 *
 * These functions can be used to verify that a message was signed by the holder
 * of the private keys of a given address.
 */
library ECDSA {
    /**
     * @dev Returns the address that signed a hashed message (`hash`) with
     * `signature`. This address can then be used for verification purposes.
     *
     * The `ecrecover` EVM opcode allows for malleable (non-unique) signatures:
     * this function rejects them by requiring the `s` value to be in the lower
     * half order, and the `v` value to be either 27 or 28.
     *
     * IMPORTANT: `hash` _must_ be the result of a hash operation for the
     * verification to be secure: it is possible to craft signatures that
     * recover to arbitrary addresses for non-hashed data. A safe way to ensure
     * this is by receiving a hash of the original message (which may otherwise
     * be too long), and then calling {toEthSignedMessageHash} on it.
     */
    function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
        // Check the signature length
        if (signature.length != 65) {
            revert("ECDSA: invalid signature length");
        }

        // Divide the signature in r, s and v variables
        bytes32 r;
        bytes32 s;
        uint8 v;

        // ecrecover takes the signature parameters, and the only way to get them
        // currently is to use assembly.
        // solhint-disable-next-line no-inline-assembly
        assembly {
            r := mload(add(signature, 0x20))
            s := mload(add(signature, 0x40))
            v := byte(0, mload(add(signature, 0x60)))
        }

        return recover(hash, v, r, s);
    }

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

        // If the signature is valid (and not malleable), return the signer address
        address signer = ecrecover(hash, v, r, s);
        require(signer != address(0), "ECDSA: invalid signature");

        return signer;
    }

    /**
     * @dev Returns an Ethereum Signed Message, created from a `hash`. This
     * replicates the behavior of the
     * https://github.com/ethereum/wiki/wiki/JSON-RPC#eth_sign[`eth_sign`]
     * JSON-RPC method.
     *
     * See {recover}.
     */
    function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) {
        // 32 is the length in bytes of hash,
        // enforced by the type signature above
        return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
    }
}

// File: @openzeppelin/contracts/math/SafeMath.sol



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

// File: @openzeppelin/contracts/token/ERC20/IERC20.sol



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

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



// 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: @openzeppelin/contracts/utils/Pausable.sol



// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

import "./Context.sol";

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

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

    bool private _paused;

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

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

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

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

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

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

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



// SPDX-License-Identifier: MIT

pragma solidity >=0.6.0 <0.8.0;

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

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

    uint256 private _status;

    constructor () internal {
        _status = _NOT_ENTERED;
    }

    /**
     * @dev Prevents a contract from calling itself, directly or indirectly.
     * Calling a `nonReentrant` function from another `nonReentrant`
     * function is not supported. It is possible to prevent this from happening
     * by making the `nonReentrant` function external, and 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;
    }
}