IncreasingPriceCrowdsale.test.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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(IncreasingPriceCrowdsaleImpl.new(
  25. this.startTime, this.closingTime, wallet, this.token.address, initialRate, initialRate.addn(1)
  26. ));
  27. });
  28. it('reverts with a final rate equal to the initial rate', async function () {
  29. await shouldFail.reverting(IncreasingPriceCrowdsaleImpl.new(
  30. this.startTime, this.closingTime, wallet, this.token.address, initialRate, initialRate
  31. ));
  32. });
  33. it('reverts with a final rate of zero', async function () {
  34. await shouldFail.reverting(IncreasingPriceCrowdsaleImpl.new(
  35. this.startTime, this.closingTime, wallet, this.token.address, initialRate, 0
  36. ));
  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(this.crowdsale.rate());
  51. });
  52. it('returns a rate of 0 before the crowdsale starts', async function () {
  53. (await this.crowdsale.getCurrentRate()).should.be.bignumber.equal('0');
  54. });
  55. it('returns a rate of 0 after the crowdsale ends', async function () {
  56. await time.increaseTo(this.afterClosingTime);
  57. (await this.crowdsale.getCurrentRate()).should.be.bignumber.equal('0');
  58. });
  59. it('at start', async function () {
  60. await time.increaseTo(this.startTime);
  61. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  62. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(initialRate));
  63. });
  64. it('at time 150', async function () {
  65. await time.increaseTo(this.startTime.addn(150));
  66. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  67. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime150));
  68. });
  69. it('at time 300', async function () {
  70. await time.increaseTo(this.startTime.addn(300));
  71. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  72. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime300));
  73. });
  74. it('at time 1500', async function () {
  75. await time.increaseTo(this.startTime.addn(1500));
  76. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  77. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime1500));
  78. });
  79. it('at time 30', async function () {
  80. await time.increaseTo(this.startTime.addn(30));
  81. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  82. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime30));
  83. });
  84. it('at time 150000', async function () {
  85. await time.increaseTo(this.startTime.addn(150000));
  86. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  87. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime150000));
  88. });
  89. it('at time 450000', async function () {
  90. await time.increaseTo(this.startTime.addn(450000));
  91. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  92. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime450000));
  93. });
  94. });
  95. });
  96. });