IncreasingPriceCrowdsale.test.js 5.4 KB

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