contract ChgUsdConverter is Ownable{
    address public contractAddress = 0xC4A86561cb0b7EA1214904f26E6D50FD357C7986;
    address public dashboardAddress = 0x482EFd447bE88748e7625e2b7c522c388970B790;
    uint public ETHUSDPRICE = 810;
    uint public CHGUSDPRICE = 4; // $0.4

    function setETHUSDPrice(uint newPrice) public {
        if (msg.sender != dashboardAddress) revert();
        
        ETHUSDPRICE = newPrice;
    }

    function setCHGUSDPrice(uint newPrice) public {
        if (msg.sender != dashboardAddress) revert();

        CHGUSDPRICE = newPrice;
    }

    function calculateCHGAmountToEther(uint etherAmount) view public returns (uint){
        return ((etherAmount * ETHUSDPRICE) / CHGUSDPRICE) * 10;
    }

    function balances(address a) view public returns (uint) {
        ChargCoinContract c = ChargCoinContract(contractAddress);
        return c.balances(a);
    }

    function currentBalance() view public returns (uint) {
        ChargCoinContract c = ChargCoinContract(contractAddress);
        return c.balances(address(this));
    }

    function() payable public {
        uint calculatedAmount = calculateCHGAmountToEther(msg.value);

        ChargCoinContract c = ChargCoinContract(contractAddress);

        if (currentBalance() < calculatedAmount) {
            revert();
        }

        if (!c.transfer(msg.sender, calculatedAmount)) {
            revert();
        }

    }
}
function setETHUSDPrice(uint newPrice) public {
    if (msg.sender != dashboardAddress) revert();
        
    ETHUSDPRICE = newPrice;
}

// $0.4