IncreasingPriceCrowdsale.sol 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. pragma solidity ^0.5.7;
  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. * The base rate function is overridden to revert, since this crowdsale doesn't use it, and
  27. * all calls to it are a mistake.
  28. */
  29. function rate() public view returns (uint256) {
  30. revert();
  31. }
  32. /**
  33. * @return the initial rate of the crowdsale.
  34. */
  35. function initialRate() public view returns (uint256) {
  36. return _initialRate;
  37. }
  38. /**
  39. * @return the final rate of the crowdsale.
  40. */
  41. function finalRate() public view returns (uint256) {
  42. return _finalRate;
  43. }
  44. /**
  45. * @dev Returns the rate of tokens per wei at the present time.
  46. * Note that, as price _increases_ with time, the rate _decreases_.
  47. * @return The number of tokens a buyer gets per wei at a given time
  48. */
  49. function getCurrentRate() public view returns (uint256) {
  50. if (!isOpen()) {
  51. return 0;
  52. }
  53. // solhint-disable-next-line not-rely-on-time
  54. uint256 elapsedTime = block.timestamp.sub(openingTime());
  55. uint256 timeRange = closingTime().sub(openingTime());
  56. uint256 rateRange = _initialRate.sub(_finalRate);
  57. return _initialRate.sub(elapsedTime.mul(rateRange).div(timeRange));
  58. }
  59. /**
  60. * @dev Overrides parent method taking into account variable rate.
  61. * @param weiAmount The value in wei to be converted into tokens
  62. * @return The number of tokens _weiAmount wei will buy at present time
  63. */
  64. function _getTokenAmount(uint256 weiAmount) internal view returns (uint256) {
  65. uint256 currentRate = getCurrentRate();
  66. return currentRate.mul(weiAmount);
  67. }
  68. }