contract BFEXMini is Ownable {

  using SafeMath for uint256;

  // Start and end timestamps
  uint public startTime;
  /* uint public endTime; */

  // BFEX Address where funds are collected
  address public wallet;
  address public feeWallet;

  // Whitelist Enable
  bool public whitelistEnable;

  // timeLimitEnable Enable
  bool public timeLimitEnable;

  mapping (address => bool) public whitelist;
  mapping (address => uint256) public bfexAmount; // 18 digits
  mapping (address => uint256) public weiParticipate;
  mapping (address => uint256) public balances;

  // Amount of wei raised
  uint256 public weiRaised = 0;

  // BFEX price pair with ETH
  uint256 public rate;
  uint256 public rateSecondTier;

  // Minimum ETH to participate
  uint256 public minimum;

  // number of contributor
  uint256 public contributor;

  // Maximun number of contributor
  uint256 public maxContributor;

  event BFEXParticipate(
    address sender,
    uint256 amount
  );

  event WhitelistState(
    address beneficiary,
    bool whitelistState
  );

  event LogWithdrawal(
    address receiver,
    uint amount
  );

  /* solhint-disable */
  constructor(address _wallet, address _feeWallet, uint256 _rate, uint256 _rateSecondTier, uint256 _minimum) public {

    require(_wallet != address(0));

    wallet = _wallet;
    feeWallet = _feeWallet;
    rate = _rate;
    rateSecondTier = _rateSecondTier;
    minimum = _minimum;
    whitelistEnable = true;
    timeLimitEnable = true;
    contributor = 0;
    maxContributor = 10001;
    startTime = 1528625400; // 06/10/2018 @ 10:10am (UTC)
  }
  /* solhint-enable */

  /**
   * @dev Fallback function that can be used to participate in token generation event.
   */
  function() external payable {
    getBFEX(msg.sender);
  }

  /**
   * @dev set rate of Token per 1 ETH
   * @param _rate of Token per 1 ETH
   */
  function setRate(uint _rate) public onlyOwner {
    rate = _rate;
  }

  /**
   * @dev setMinimum amount to participate
   * @param _minimum minimum amount in wei
   */
  function setMinimum(uint256 _minimum) public onlyOwner {
    minimum = _minimum;
  }

  /**
   * @dev setMinimum amount to participate
   * @param _max Maximum contributor allowed
   */
  function setMaxContributor(uint256 _max) public onlyOwner {
    maxContributor = _max;
  }

  /**
   * @dev Add single address to whitelist.
   * @param _beneficiary Address to be added to the whitelist
   */
  function addToWhitelist(address _beneficiary) external onlyOwner {
    whitelist[_beneficiary] = true;
    emit WhitelistState(_beneficiary, true);
  }

  /**
   * @dev Adds list of addresses to whitelist. Not overloaded due to limitations with truffle testing.
   * @param _beneficiaries Addresses to be added to the whitelist
   */
  function addManyToWhitelist(address[] _beneficiaries) external onlyOwner {
    for (uint256 i = 0; i < _beneficiaries.length; i++) {
      whitelist[_beneficiaries[i]] = true;
    }
  }

  /**
   * @dev Remove single address from whitelist.
   * @param _beneficiary Address to be removed from the whitelist
   */
  function removeFromWhiteList(address _beneficiary) external onlyOwner {
    whitelist[_beneficiary] = false;
    emit WhitelistState(_beneficiary, false);
  }

  function isWhitelist(address _beneficiary) public view returns (bool whitelisted) {
    return whitelist[_beneficiary];
  }

  function checkBenefit(address _beneficiary) public view returns (uint256 bfex) {
    return bfexAmount[_beneficiary];
  }

  function checkContribution(address _beneficiary) public view returns (uint256 weiContribute) {
    return weiParticipate[_beneficiary];
  }
  /**
  * @dev getBfex function
  * @param _participant Address performing the bfex token participate
  */
  function getBFEX(address _participant) public payable {

    uint256 weiAmount = msg.value;

    _preApprove(_participant);
    require(_participant != address(0));
    require(weiAmount >= minimum);

    // calculate bfex token _participant will recieve
    uint256 bfexToken = _getTokenAmount(weiAmount);

    // update state
    weiRaised = weiRaised.add(weiAmount);
    // update ETH balance
    uint256 raise = weiAmount.div(1000).mul(955);
    uint256 fee = weiAmount.div(1000).mul(45);
    // update contributor count
    contributor += 1;

    balances[wallet] = balances[wallet].add(raise);
    balances[feeWallet] = balances[feeWallet].add(fee);

    bfexAmount[_participant] = bfexAmount[_participant].add(bfexToken);
    weiParticipate[_participant] = weiParticipate[_participant].add(weiAmount);

    emit BFEXParticipate(_participant, weiAmount);
  }

  /**
  * @dev calculate token amont
  * @param _weiAmount wei amont user are participate
  */
  function _getTokenAmount(uint256 _weiAmount) internal view returns (uint256) {
    uint256 _rate;
    if (_weiAmount >= 0.1 ether && _weiAmount < 1 ether ) {
      _rate = rate;
    } else if (_weiAmount >= 1 ether ) {
      _rate = rateSecondTier;
    }
    uint256 bfex = _weiAmount.mul(_rate);
    /* bfex = bfex.div(1 ether); */
    return bfex;
  }

  /**
  * @dev check if address is on the whitelist
  * @param _participant address
  */
  function _preApprove(address _participant) internal view {
    require (maxContributor >= contributor);
    if (timeLimitEnable == true) {
      require (now >= startTime && now <= startTime + 1 days);
    }
    if (whitelistEnable == true) {
      require(isWhitelist(_participant));
      return;
    } else {
      return;
    }
  }

  /**
  * @dev disable whitelist state
  *
  */
  function disableWhitelist() public onlyOwner returns (bool whitelistState) {
    whitelistEnable = false;
    emit WhitelistState(msg.sender, whitelistEnable);
    return whitelistEnable;
  }

  /**
  * @dev enable whitelist state
  *
  */
  function enableWhitelist() public onlyOwner returns (bool whitelistState) {
    whitelistEnable = true;
    emit WhitelistState(msg.sender, whitelistEnable);
    return whitelistEnable;
  }

  function withdraw(uint _value) public returns (bool success) {
    require(balances[msg.sender] <= _value);

    balances[msg.sender] = balances[msg.sender].sub(_value);
    msg.sender.transfer(_value);
    emit LogWithdrawal(msg.sender, _value);

    return true;
  }

  function checkBalance(address _account) public view returns (uint256 balance)  {
    return balances[_account];
  }
}
function _getTokenAmount(uint256 _weiAmount) internal view returns (uint256) {
  uint256 _rate;
  if (_weiAmount >= 0.1 ether && _weiAmount < 1 ether ) {
    _rate = rate;
  } else if (_weiAmount >= 1 ether ) {
    _rate = rateSecondTier;
  }
  uint256 bfex = _weiAmount.mul(_rate);
  /* bfex = bfex.div(1 ether); */
  return bfex;
}

/**
 * @dev calculate token amont
 * @param _weiAmount wei amont user are participate
 */