IncreasingPriceCrowdsale.sol 2.9 KB

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