contract ArbitrageETHStaking is Ownable {

    using SafeMath for uint256;

    /*==============================
     =            EVENTS            =
     ==============================*/

    event onPurchase(
       address indexed customerAddress,
       uint256 etherIn,
       uint256 contractBal,
       uint256 poolFee,
       uint timestamp
    );

    event onWithdraw(
         address indexed customerAddress,
         uint256 etherOut,
         uint256 contractBal,
         uint timestamp
    );


    /*** STORAGE ***/

    mapping(address => uint256) internal personalFactorLedger_; // personal factor ledger
    mapping(address => uint256) internal balanceLedger_; // users balance ledger

    // Configurations
    uint256 minBuyIn = 0.001 ether; // can't buy less then 0.0001 ETH
    uint256 stakingPrecent = 2;
    uint256 internal globalFactor = 10e21; // global factor
    uint256 constant internal constantFactor = 10e21 * 10e21; // constant factor

    /// @dev Forward all Ether in buy() function
    function() external payable {
        buy();
    }

    // @dev Buy in staking pool, transfer ethereum in the contract, pay 2% fee
    function buy()
        public
        payable
    {
        address _customerAddress = msg.sender;

        require(msg.value >= minBuyIn, "should be more the 0.0001 ether sent");

        uint256 _etherBeforeBuyIn = getBalance().sub(msg.value);

        uint256 poolFee;
        // Check is not a first buy in
        if (_etherBeforeBuyIn != 0) {

            // Add 2% fee of the buy to the staking pool
            poolFee = msg.value.mul(stakingPrecent).div(100);

            // Increase amount of eth everyone else owns
            uint256 globalIncrease = globalFactor.mul(poolFee) / _etherBeforeBuyIn;
            globalFactor = globalFactor.add(globalIncrease);
        }


        balanceLedger_[_customerAddress] = ethBalanceOf(_customerAddress).add(msg.value).sub(poolFee);
        personalFactorLedger_[_customerAddress] = constantFactor / globalFactor;

        emit onPurchase(_customerAddress, msg.value, getBalance(), poolFee, now);
    }

    /**
     * @dev Withdraw selected amount of ethereum from the contract back to user,
     *      update the balance.
     * @param _sellEth - Amount of ethereum to withdraw from contract
     */
    function withdraw(uint256 _sellEth)
        public
    {
        address _customerAddress = msg.sender;
        // User must have enough eth and cannot sell 0
        require(_sellEth > 0, "user cant spam transactions with 0 value");
        require(_sellEth <= ethBalanceOf(_customerAddress), "user cant withdraw more then he holds ");


        // Transfer balance and update user ledgers
        _customerAddress.transfer(_sellEth);
        balanceLedger_[_customerAddress] = ethBalanceOf(_customerAddress).sub(_sellEth);
        personalFactorLedger_[_customerAddress] = constantFactor / globalFactor;

        emit onWithdraw(_customerAddress, _sellEth, getBalance(), now);
    }

    // @dev Withdraw all the ethereum user holds in the contract, set balance to 0
    function withdrawAll()
        public
    {
        address _customerAddress = msg.sender;
        // Set the sell amount to the user's full balance, don't sell if empty
        uint256 _sellEth = ethBalanceOf(_customerAddress);
        require(_sellEth > 0, "user cant call withdraw, when holds nothing");
        // Transfer balance and update user ledgers
        _customerAddress.transfer(_sellEth);
        balanceLedger_[_customerAddress] = 0;
        personalFactorLedger_[_customerAddress] = constantFactor / globalFactor;

        emit onWithdraw(_customerAddress, _sellEth, getBalance(), now);
    }

    /**
    * UI Logic - View Functions
    */

    // @dev Returns contract ETH balance
    function getBalance()
        public
        view
        returns (uint256)
    {
        return address(this).balance;
    }

    // @dev Returns user ETH tokens balance in contract
    function ethBalanceOf(address _customerAddress)
        public
        view
        returns (uint256)
    {
        // Balance ledger * personal factor * globalFactor / constantFactor
        return balanceLedger_[_customerAddress].mul(personalFactorLedger_[_customerAddress]).mul(globalFactor) / constantFactor;
    }
}
/**
 * @title ArbitrageETHStaking
 * @dev The ArbitrageETHStaking contract staking Ether(ETH) tokens.
 *      Here is stored all function and data of user stakes in contract.
 *      Staking is configured for 2%.
 */
function getBalance()
    public
    view
    returns (uint256)
{
    return address(this).balance;
}

// @dev Returns contract ETH balance