IncreasingPriceCrowdsale.sol 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 private _initialRate;
  13. uint256 private _finalRate;
  14. /**
  15. * @dev Constructor, takes initial 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(finalRate > 0);
  21. require(initialRate >= finalRate);
  22. _initialRate = initialRate;
  23. _finalRate = finalRate;
  24. }
  25. /**
  26. * @return the initial rate of the crowdsale.
  27. */
  28. function initialRate() public view returns(uint256) {
  29. return _initialRate;
  30. }
  31. /**
  32. * @return the final rate of the crowdsale.
  33. */
  34. function finalRate() public view returns (uint256) {
  35. return _finalRate;
  36. }
  37. /**
  38. * @dev Returns the rate of tokens per wei at the present time.
  39. * Note that, as price _increases_ with time, the rate _decreases_.
  40. * @return The number of tokens a buyer gets per wei at a given time
  41. */
  42. function getCurrentRate() public view returns (uint256) {
  43. // solium-disable-next-line security/no-block-members
  44. uint256 elapsedTime = block.timestamp.sub(openingTime());
  45. uint256 timeRange = closingTime().sub(openingTime());
  46. uint256 rateRange = _initialRate.sub(_finalRate);
  47. return _initialRate.sub(elapsedTime.mul(rateRange).div(timeRange));
  48. }
  49. /**
  50. * @dev Overrides parent method taking into account variable rate.
  51. * @param weiAmount The value in wei to be converted into tokens
  52. * @return The number of tokens _weiAmount wei will buy at present time
  53. */
  54. function _getTokenAmount(uint256 weiAmount)
  55. internal view returns (uint256)
  56. {
  57. uint256 currentRate = getCurrentRate();
  58. return currentRate.mul(weiAmount);
  59. }
  60. }