IncreasingPriceCrowdsale.test.js 5.3 KB

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