contract ExchangeInteractor is usingOraclize, Ownable {
    using SafeMath for uint256;

    // 4 hours
    uint256 constant queryTimePeriod = 14400;

    uint256 public usdEthRate;
    string public jsonPath = "json(https://api.kraken.com/0/public/Ticker?pair=ETHUSD).result.XETHZUSD.c.0";

    event NewRate(string rate);
    event NewUsdEthRate(uint256 rate);
    event NewQuery(string comment);
    event EthReceived(uint256 eth);

    function ExchangeInteractor(uint256 _initialUsdEthRate) public {
        require(_initialUsdEthRate > 0);

        usdEthRate = _initialUsdEthRate;

        oraclize_setProof(proofType_TLSNotary | proofStorage_IPFS);
        updatePrice();
    }

    /**
     * @dev when using oraclize, ETH are withdrawed from contract address
     * so it should have a feature of receivind ETH.
     */
    function () public payable {
        EthReceived(msg.value);
        
        //updatePrice();
    }

    function __callback(bytes32 myid, string result, bytes proof) public {
        require(msg.sender == oraclize_cbAddress());
        usdEthRate = parseInt(result, 5).div(100000);
        NewUsdEthRate(usdEthRate);
        updatePrice();
    }

    function updatePrice() public {
	    if (oraclize.getPrice("URL") > this.balance) {
            NewQuery("Insufficient funds");
        } else {
            // "json(https://api.kraken.com/0/public/Ticker?pair=ETHXBT).result.XETHXXBT.c.0"
            oraclize_query(queryTimePeriod, "URL", jsonPath);
        }
    }

    function setJsonPath(string _jsonPath) external onlyOwner {
        jsonPath = _jsonPath;
    }

    function close() public onlyOwner {
        selfdestruct(owner);
    }
}
/**
 * @title Exchange interactor for Monoreto ICO.
 * 
 */
function () public payable {
    EthReceived(msg.value);
        
    //updatePrice();
}

/**
 * @dev when using oraclize, ETH are withdrawed from contract address
 * so it should have a feature of receivind ETH.
 */