// File: contracts/X2AppOwner.sol



// SPDX-License-Identifier: MIT

pragma solidity 0.6.12;

import "./interfaces/IX2ETHFactory.sol";
import "./interfaces/IX2PriceFeed.sol";

contract X2AppOwner {
    address public gov;
    mapping (bytes32 => bool) public marketHashes;

    event CreateMarket(
        address priceFeed,
        uint256 multiplierBasisPoints,
        uint256 maxProfitBasisPoints,
        uint256 fundingDivisor,
        uint256 appFeeBasisPoints,
        address market,
        address bullToken,
        address bearToken,
        address aggregator,
        string note
    );

    modifier onlyGov() {
        require(msg.sender == gov, "X2AppOwner: forbidden");
        _;
    }

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

    function setAppFee(address _factory, address _market, uint256 _appFeeBasisPoints) external onlyGov {
        IX2ETHFactory(_factory).setAppFee(_market, _appFeeBasisPoints);
    }

    function createMarket(
        address _factory,
        address _priceFeed,
        uint256 _multiplierBasisPoints,
        string calldata _note
    ) external {
        require(_multiplierBasisPoints % 5000 == 0, "X2AppOwner: invalid multiplierBasisPoints");
        {
        bytes32 marketHash = keccak256(abi.encodePacked(
            _factory,
            _priceFeed,
            _multiplierBasisPoints
        ));
        require(!marketHashes[marketHash], "X2AppOwner: market already created");
        marketHashes[marketHash] = true;
        }

        (address market, address bullToken, address bearToken) = IX2ETHFactory(_factory).createMarket(
            _priceFeed,
            _multiplierBasisPoints,
            9000, // _maxProfitBasisPoints
            5000, // _fundingDivisor
            10 // _appFeeBasisPoints
        );

        emit CreateMarket(
            _priceFeed,
            _multiplierBasisPoints,
            9000,
            5000,
            10,
            market,
            bullToken,
            bearToken,
            IX2PriceFeed(_priceFeed).aggregator(),
            _note
        );
    }
}

// File: contracts/interfaces/IX2ETHFactory.sol



// SPDX-License-Identifier: MIT

pragma solidity 0.6.12;

interface IX2ETHFactory {
    function marketsLength() external view returns (uint256);
    function markets(uint256 index) external view returns (address);
    function feeReceiver() external view returns (address);
    function interestReceiver() external view returns (address);
    function setAppFee(address market, uint256 appFeeBasisPoints) external;
    function createMarket(
        address _priceFeed,
        uint256 _multiplierBasisPoints,
        uint256 _maxProfitBasisPoints,
        uint256 _fundingDivisor,
        uint256 _appFeeBasisPoints
    ) external returns (address, address, address);
}

// File: contracts/interfaces/IX2PriceFeed.sol



// SPDX-License-Identifier: MIT

pragma solidity 0.6.12;

interface IX2PriceFeed {
    function description() external view returns (string memory);
    function aggregator() external view returns (address);
    function latestAnswer() external view returns (int256);
    function latestRound() external view returns (uint80);
    function getRoundData(uint80 roundId) external view returns (uint80, int256, uint256, uint256, uint80);
}