IncreasingPriceCrowdsale.test.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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('rejects 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('rejects a final rate of zero', async function () {
  36. await shouldFail.reverting(IncreasingPriceCrowdsaleImpl.new(
  37. this.startTime, this.closingTime, wallet, this.token.address, initialRate, 0
  38. ));
  39. });
  40. context('with crowdsale', function () {
  41. beforeEach(async function () {
  42. this.crowdsale = await IncreasingPriceCrowdsaleImpl.new(
  43. this.startTime, this.closingTime, wallet, this.token.address, initialRate, finalRate
  44. );
  45. await this.token.transfer(this.crowdsale.address, tokenSupply);
  46. });
  47. it('should have initial and final rate', async function () {
  48. (await this.crowdsale.initialRate()).should.be.bignumber.equal(initialRate);
  49. (await this.crowdsale.finalRate()).should.be.bignumber.equal(finalRate);
  50. });
  51. it('returns a rate of 0 before the crowdsale starts', async function () {
  52. (await this.crowdsale.getCurrentRate()).should.be.bignumber.equal(0);
  53. });
  54. it('returns a rate of 0 after the crowdsale ends', async function () {
  55. await time.increaseTo(this.afterClosingTime);
  56. (await this.crowdsale.getCurrentRate()).should.be.bignumber.equal(0);
  57. });
  58. it('at start', async function () {
  59. await time.increaseTo(this.startTime);
  60. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  61. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(initialRate));
  62. });
  63. it('at time 150', async function () {
  64. await time.increaseTo(this.startTime + 150);
  65. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  66. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime150));
  67. });
  68. it('at time 300', async function () {
  69. await time.increaseTo(this.startTime + 300);
  70. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  71. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime300));
  72. });
  73. it('at time 1500', async function () {
  74. await time.increaseTo(this.startTime + 1500);
  75. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  76. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime1500));
  77. });
  78. it('at time 30', async function () {
  79. await time.increaseTo(this.startTime + 30);
  80. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  81. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime30));
  82. });
  83. it('at time 150000', async function () {
  84. await time.increaseTo(this.startTime + 150000);
  85. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  86. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime150000));
  87. });
  88. it('at time 450000', async function () {
  89. await time.increaseTo(this.startTime + 450000);
  90. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  91. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value.mul(rateAtTime450000));
  92. });
  93. });
  94. });
  95. });