IncreasingPriceCrowdsale.test.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. const { BN, ether, shouldFail, time } = require('openzeppelin-test-helpers');
  2. const IncreasingPriceCrowdsaleImpl = artifacts.require('IncreasingPriceCrowdsaleImpl');
  3. const SimpleToken = artifacts.require('SimpleToken');
  4. contract('IncreasingPriceCrowdsale', function ([_, investor, wallet, purchaser]) {
  5. const value = ether('1');
  6. const tokenSupply = new BN('10').pow(new BN('22'));
  7. describe('rate during crowdsale should change at a fixed step every block', async function () {
  8. const initialRate = new BN('9166');
  9. const finalRate = new BN('5500');
  10. const rateAtTime150 = new BN('9166');
  11. const rateAtTime300 = new BN('9165');
  12. const rateAtTime1500 = new BN('9157');
  13. const rateAtTime30 = new BN('9166');
  14. const rateAtTime150000 = new BN('8257');
  15. const rateAtTime450000 = new BN('6439');
  16. beforeEach(async function () {
  17. await time.advanceBlock();
  18. this.startTime = (await time.latest()).add(time.duration.weeks(1));
  19. this.closingTime = this.startTime.add(time.duration.weeks(1));
  20. this.afterClosingTime = this.closingTime.add(time.duration.seconds(1));
  21. this.token = await SimpleToken.new();
  22. });
  23. it('reverts with a final rate larger than the initial rate', async function () {
  24. await shouldFail.reverting.withMessage(IncreasingPriceCrowdsaleImpl.new(
  25. this.startTime, this.closingTime, wallet, this.token.address, initialRate, initialRate.addn(1)
  26. ), 'IncreasingPriceCrowdsale: initial rate is not greater than final rate');
  27. });
  28. it('reverts with a final rate equal to the initial rate', async function () {
  29. await shouldFail.reverting.withMessage(IncreasingPriceCrowdsaleImpl.new(
  30. this.startTime, this.closingTime, wallet, this.token.address, initialRate, initialRate
  31. ), 'IncreasingPriceCrowdsale: initial rate is not greater than final rate');
  32. });
  33. it('reverts with a final rate of zero', async function () {
  34. await shouldFail.reverting.withMessage(IncreasingPriceCrowdsaleImpl.new(
  35. this.startTime, this.closingTime, wallet, this.token.address, initialRate, 0
  36. ), 'IncreasingPriceCrowdsale: final rate is 0');
  37. });
  38. context('with crowdsale', function () {
  39. beforeEach(async function () {
  40. this.crowdsale = await IncreasingPriceCrowdsaleImpl.new(
  41. this.startTime, this.closingTime, wallet, this.token.address, initialRate, finalRate
  42. );
  43. await this.token.transfer(this.crowdsale.address, tokenSupply);
  44. });
  45. it('should have initial and final rate', async function () {
  46. (await this.crowdsale.initialRate()).should.be.bignumber.equal(initialRate);
  47. (await this.crowdsale.finalRate()).should.be.bignumber.equal(finalRate);
  48. });
  49. it('reverts when the base Crowdsale\'s rate function is called', async function () {
  50. await shouldFail.reverting.withMessage(this.crowdsale.rate(),
  51. 'IncreasingPriceCrowdsale: rate() called'
  52. );
  53. });
  54. it('returns a rate of 0 before the crowdsale starts', async function () {
  55. (await this.crowdsale.getCurrentRate()).should.be.bignumber.equal('0');
  56. });
  57. it('returns a rate of 0 after the crowdsale ends', async function () {
  58. await time.increaseTo(this.afterClosingTime);
  59. (await this.crowdsale.getCurrentRate()).should.be.bignumber.equal('0');
  60. });
  61. it('at start', async function () {
  62. await time.increaseTo(this.startTime);
  63. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  64. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(initialRate));
  65. });
  66. it('at time 150', async function () {
  67. await time.increaseTo(this.startTime.addn(150));
  68. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  69. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime150));
  70. });
  71. it('at time 300', async function () {
  72. await time.increaseTo(this.startTime.addn(300));
  73. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  74. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime300));
  75. });
  76. it('at time 1500', async function () {
  77. await time.increaseTo(this.startTime.addn(1500));
  78. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  79. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime1500));
  80. });
  81. it('at time 30', async function () {
  82. await time.increaseTo(this.startTime.addn(30));
  83. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  84. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime30));
  85. });
  86. it('at time 150000', async function () {
  87. await time.increaseTo(this.startTime.addn(150000));
  88. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  89. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime150000));
  90. });
  91. it('at time 450000', async function () {
  92. await time.increaseTo(this.startTime.addn(450000));
  93. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  94. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime450000));
  95. });
  96. });
  97. });
  98. });