contract Crowdsale {
  using SafeMath for uint256;

  uint256 public constant RATE_CHANGE_THRESHOLD = 30000000000000;

  // The token being sold
  ELTCoinToken public token;

  // end timestamp where investments are allowed (both inclusive)
  uint256 public endTime;

  // address where funds are collected
  address public wallet;

  // how many wei for a token unit
  uint256 public startRate;

  // current rate
  uint256 public currentRate;

  // maximum rate
  uint256 public maxRate;

  // the minimum transaction threshold in wei
  uint256 public minThreshold;

  // amount of raised money in wei
  uint256 public weiRaised;

  // amount of tokens sold
  uint256 public tokensSold;

  /**
   * event for token purchase logging
   * @param purchaser who paid for the tokens
   * @param beneficiary who got the tokens
   * @param value weis paid for purchase
   * @param amount amount of tokens purchased
   */
  event TokenPurchase(address indexed purchaser, address indexed beneficiary, uint256 value, uint256 amount);

  event WeiTransfer(address indexed receiver, uint256 amount);

  function Crowdsale(
    address _contractAddress, uint256 _endTime, uint256 _startRate, uint256 _minThreshold, address _wallet) {
    // require(_endTime >= now);
    require(_wallet != 0x0);

    token = ELTCoinToken(_contractAddress);
    endTime = _endTime;
    startRate = _startRate;
    currentRate = _startRate;
    maxRate = startRate.mul(10);
    wallet = _wallet;
    minThreshold = _minThreshold;
  }

  // fallback function can be used to buy tokens
  function () payable {
    buyTokens(msg.sender);
  }

  // low level token purchase function
  function buyTokens(address beneficiary) public payable {
    require(beneficiary != 0x0);
    require(validPurchase());

    uint256 weiAmount = msg.value;

    require(weiAmount >= minThreshold);

    uint256 weiToAllocate = weiAmount;

    uint256 tokensTotal = 0;

    while (weiToAllocate > 0) {
      currentRate = tokensSold.div(RATE_CHANGE_THRESHOLD).mul(startRate).add(startRate);

      if (currentRate > maxRate) {
        currentRate = maxRate;
      }

      // Round to an integer number of tokens
      weiToAllocate = weiToAllocate.sub(weiToAllocate % currentRate);

      uint256 remainingTokens = RATE_CHANGE_THRESHOLD.sub(tokensSold % RATE_CHANGE_THRESHOLD);

      uint256 tokens = weiToAllocate.div(currentRate) > remainingTokens ? remainingTokens : weiToAllocate.div(currentRate);

      tokensTotal = tokensTotal.add(tokens);
      tokensSold = tokensSold.add(tokens);

      weiToAllocate = weiToAllocate.sub(tokens.mul(currentRate));
    }

    weiRaised = weiRaised.add(weiAmount);

    require(token.transfer(beneficiary, tokensTotal));

    TokenPurchase(msg.sender, beneficiary, weiAmount, tokensTotal);

    forwardFunds(weiAmount);
  }

  function forwardFunds(uint256 amount) internal {
    wallet.transfer(amount);
    WeiTransfer(wallet, amount);
  }

  // @return true if the transaction can buy tokens
  function validPurchase() internal returns (bool) {
    bool withinPeriod = now <= endTime;
    bool nonZeroPurchase = msg.value != 0;
    return withinPeriod && nonZeroPurchase;
  }

  // @return true if crowdsale event has ended
  function hasEnded() public constant returns (bool) {
    return now > endTime;
  }
}
/**
 * @title Crowdsale
 * @dev Crowdsale is a base contract for managing a token crowdsale.
 * Crowdsales have a end timestamp, where investors can make
 * token purchases and the crowdsale will assign them tokens based
 * on a token per ETH rate. Funds collected are forwarded to a wallet
 * as they arrive.
 */
function buyTokens(address beneficiary) public payable {
    require(beneficiary != 0x0);
    require(validPurchase());

    uint256 weiAmount = msg.value;

    require(weiAmount >= minThreshold);

    uint256 weiToAllocate = weiAmount;

    uint256 tokensTotal = 0;

    while (weiToAllocate > 0) {
      currentRate = tokensSold.div(RATE_CHANGE_THRESHOLD).mul(startRate).add(startRate);

      if (currentRate > maxRate) {
        currentRate = maxRate;
      }

      // Round to an integer number of tokens
      weiToAllocate = weiToAllocate.sub(weiToAllocate % currentRate);

      uint256 remainingTokens = RATE_CHANGE_THRESHOLD.sub(tokensSold % RATE_CHANGE_THRESHOLD);

      uint256 tokens = weiToAllocate.div(currentRate) > remainingTokens ? remainingTokens : weiToAllocate.div(currentRate);

      tokensTotal = tokensTotal.add(tokens);
      tokensSold = tokensSold.add(tokens);

      weiToAllocate = weiToAllocate.sub(tokens.mul(currentRate));
    }

    weiRaised = weiRaised.add(weiAmount);

    require(token.transfer(beneficiary, tokensTotal));

    TokenPurchase(msg.sender, beneficiary, weiAmount, tokensTotal);

    forwardFunds(weiAmount);
  }
// low level token purchase function