IncreasingPriceCrowdsale.sol 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. pragma solidity ^0.4.24;
  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. constructor(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. // solium-disable-next-line security/no-block-members
  32. uint256 elapsedTime = block.timestamp.sub(openingTime);
  33. uint256 timeRange = closingTime.sub(openingTime);
  34. uint256 rateRange = initialRate.sub(finalRate);
  35. return initialRate.sub(elapsedTime.mul(rateRange).div(timeRange));
  36. }
  37. /**
  38. * @dev Overrides parent method taking into account variable rate.
  39. * @param _weiAmount The value in wei to be converted into tokens
  40. * @return The number of tokens _weiAmount wei will buy at present time
  41. */
  42. function _getTokenAmount(uint256 _weiAmount)
  43. internal view returns (uint256)
  44. {
  45. uint256 currentRate = getCurrentRate();
  46. return currentRate.mul(_weiAmount);
  47. }
  48. }