contract TokenExchange is Owned {

    uint256 internal  constant initialBuyPrice = 1400000000000000;


    uint256 internal  priceIncrement =         550000000;
    uint256 internal  buyPrice;

    StandardToken internal  token;

    event BuyEvent(
        address wallet,
        uint256 amount
    );

    /* Initializes contract with initial supply tokens to the creator of the contract */
    constructor() public {
        buyPrice = initialBuyPrice;
    }

    function setCurrency(address _tokenContract) public onlyOwner {
        token = StandardToken(_tokenContract);
    }

    function withdraw() onlyOwner public {
        owner.transfer(address(this).balance);
    }

    /// @notice Allow users to buy tokens for `newBuyPrice` eth
    /// @param newBuyPrice Price users can buy from the contract
    function setBuyPrice(uint256 newBuyPrice) onlyOwner public {
        buyPrice = newBuyPrice;
    }

    function setPriceIncrement(uint256 _newPriceIncrement) onlyOwner public{
        priceIncrement=_newPriceIncrement;
    }

    /// @notice Buy tokens from contract by sending ether
    function buy(uint256 amount) payable public {
        require(msg.value == getTotalCost(amount));
        token.increaseApproval(this, amount);
        token.transferFrom(this, msg.sender, amount);
        // makes the transfers
        buyPrice = SafeMath.add(buyPrice, SafeMath.mul(priceIncrement, amount));
        emit BuyEvent(msg.sender, amount);
    }


    // calculate the amount of ETH needed to buy "amount" of token
    function getTotalCost(uint256 amount) public view returns (uint256){
        return SafeMath.add(
            SafeMath.mul(buyPrice, amount),
            SafeMath.div(SafeMath.mul(priceIncrement, SafeMath.mul(SafeMath.add(amount, 1), amount)), 2));
    }

    // calculate the amount of ETH received after selling "amount" of token

    function getBuyPrice() view public returns (uint){
        return buyPrice;
    }


    function getBalance() view public returns (uint ethBalance, uint tokenBalance){
        return (address(this).balance, token.balanceOf(this));
    }
}
function setBuyPrice(uint256 newBuyPrice) onlyOwner public {
    buyPrice = newBuyPrice;
}

/// @notice Allow users to buy tokens for `newBuyPrice` eth
/// @param newBuyPrice Price users can buy from the contract