IncreasingPriceCrowdsale.sol 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. pragma solidity ^0.4.18;
  2. import "../validation/TimedCrowdsale.sol";
  3. import "../../math/SafeMath.sol";
  4. /**
  5. * @title IncreasingPriceCrowdsale
  6. * @dev Extension of Crowdsale contract that increases the price of tokens linearly in time.
  7. * Note that what should be provided to the constructor is the initial and final _rates_, that is,
  8. * the amount of tokens per wei contributed. Thus, the initial rate must be greater than the final rate.
  9. */
  10. contract IncreasingPriceCrowdsale is TimedCrowdsale {
  11. using SafeMath for uint256;
  12. uint256 public initialRate;
  13. uint256 public finalRate;
  14. /**
  15. * @dev Constructor, takes intial and final rates of tokens received per wei contributed.
  16. * @param _initialRate Number of tokens a buyer gets per wei at the start of the crowdsale
  17. * @param _finalRate Number of tokens a buyer gets per wei at the end of the crowdsale
  18. */
  19. function IncreasingPriceCrowdsale(uint256 _initialRate, uint256 _finalRate) public {
  20. require(_initialRate >= _finalRate);
  21. require(_finalRate > 0);
  22. initialRate = _initialRate;
  23. finalRate = _finalRate;
  24. }
  25. /**
  26. * @dev Returns the rate of tokens per wei at the present time.
  27. * Note that, as price _increases_ with time, the rate _decreases_.
  28. * @return The number of tokens a buyer gets per wei at a given time
  29. */
  30. function getCurrentRate() public view returns (uint256) {
  31. uint256 elapsedTime = now.sub(openingTime);
  32. uint256 timeRange = closingTime.sub(openingTime);
  33. uint256 rateRange = initialRate.sub(finalRate);
  34. return initialRate.sub(elapsedTime.mul(rateRange).div(timeRange));
  35. }
  36. /**
  37. * @dev Overrides parent method taking into account variable rate.
  38. * @param _weiAmount The value in wei to be converted into tokens
  39. * @return The number of tokens _weiAmount wei will buy at present time
  40. */
  41. function _getTokenAmount(uint256 _weiAmount) internal view returns (uint256) {
  42. uint256 currentRate = getCurrentRate();
  43. return currentRate.mul(_weiAmount);
  44. }
  45. }