IncreasingPriceCrowdsale.test.js 5.2 KB

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