TimedCrowdsale.test.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. const { BN, ether, expectEvent, shouldFail, time } = require('openzeppelin-test-helpers');
  2. const TimedCrowdsaleImpl = artifacts.require('TimedCrowdsaleImpl');
  3. const SimpleToken = artifacts.require('SimpleToken');
  4. contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) {
  5. const rate = new BN(1);
  6. const value = ether('42');
  7. const tokenSupply = new BN('10').pow(new BN('22'));
  8. before(async function () {
  9. // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
  10. await time.advanceBlock();
  11. });
  12. beforeEach(async function () {
  13. this.openingTime = (await time.latest()).add(time.duration.weeks(1));
  14. this.closingTime = this.openingTime.add(time.duration.weeks(1));
  15. this.afterClosingTime = this.closingTime.add(time.duration.seconds(1));
  16. this.token = await SimpleToken.new();
  17. });
  18. it('reverts if the opening time is in the past', async function () {
  19. await shouldFail.reverting(TimedCrowdsaleImpl.new(
  20. (await time.latest()).sub(time.duration.days(1)), this.closingTime, rate, wallet, this.token.address
  21. ));
  22. });
  23. it('reverts if the closing time is before the opening time', async function () {
  24. await shouldFail.reverting(TimedCrowdsaleImpl.new(
  25. this.openingTime, this.openingTime.sub(time.duration.seconds(1)), rate, wallet, this.token.address
  26. ));
  27. });
  28. it('reverts if the closing time equals the opening time', async function () {
  29. await shouldFail.reverting(TimedCrowdsaleImpl.new(
  30. this.openingTime, this.openingTime, rate, wallet, this.token.address
  31. ));
  32. });
  33. context('with crowdsale', function () {
  34. beforeEach(async function () {
  35. this.crowdsale = await TimedCrowdsaleImpl.new(
  36. this.openingTime, this.closingTime, rate, wallet, this.token.address
  37. );
  38. await this.token.transfer(this.crowdsale.address, tokenSupply);
  39. });
  40. it('should be ended only after end', async function () {
  41. (await this.crowdsale.hasClosed()).should.equal(false);
  42. await time.increaseTo(this.afterClosingTime);
  43. (await this.crowdsale.isOpen()).should.equal(false);
  44. (await this.crowdsale.hasClosed()).should.equal(true);
  45. });
  46. describe('accepting payments', function () {
  47. it('should reject payments before start', async function () {
  48. (await this.crowdsale.isOpen()).should.equal(false);
  49. await shouldFail.reverting(this.crowdsale.send(value));
  50. await shouldFail.reverting(this.crowdsale.buyTokens(investor, { from: purchaser, value: value }));
  51. });
  52. it('should accept payments after start', async function () {
  53. await time.increaseTo(this.openingTime);
  54. (await this.crowdsale.isOpen()).should.equal(true);
  55. await this.crowdsale.send(value);
  56. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  57. });
  58. it('should reject payments after end', async function () {
  59. await time.increaseTo(this.afterClosingTime);
  60. await shouldFail.reverting(this.crowdsale.send(value));
  61. await shouldFail.reverting(this.crowdsale.buyTokens(investor, { value: value, from: purchaser }));
  62. });
  63. });
  64. describe('extending closing time', function () {
  65. it('should not reduce duration', async function () {
  66. // Same date
  67. await shouldFail.reverting(this.crowdsale.extendTime(this.closingTime));
  68. // Prescending date
  69. const newClosingTime = this.closingTime.sub(time.duration.seconds(1));
  70. await shouldFail.reverting(this.crowdsale.extendTime(newClosingTime));
  71. });
  72. context('before crowdsale start', function () {
  73. beforeEach(async function () {
  74. (await this.crowdsale.isOpen()).should.equal(false);
  75. await shouldFail.reverting(this.crowdsale.send(value));
  76. });
  77. it('it extends end time', async function () {
  78. const newClosingTime = this.closingTime.add(time.duration.days(1));
  79. const { logs } = await this.crowdsale.extendTime(newClosingTime);
  80. expectEvent.inLogs(logs, 'TimedCrowdsaleExtended', {
  81. prevClosingTime: this.closingTime,
  82. newClosingTime: newClosingTime,
  83. });
  84. (await this.crowdsale.closingTime()).should.be.bignumber.equal(newClosingTime);
  85. });
  86. });
  87. context('after crowdsale start', function () {
  88. beforeEach(async function () {
  89. await time.increaseTo(this.openingTime);
  90. (await this.crowdsale.isOpen()).should.equal(true);
  91. await this.crowdsale.send(value);
  92. });
  93. it('it extends end time', async function () {
  94. const newClosingTime = this.closingTime.add(time.duration.days(1));
  95. const { logs } = await this.crowdsale.extendTime(newClosingTime);
  96. expectEvent.inLogs(logs, 'TimedCrowdsaleExtended', {
  97. prevClosingTime: this.closingTime,
  98. newClosingTime: newClosingTime,
  99. });
  100. (await this.crowdsale.closingTime()).should.be.bignumber.equal(newClosingTime);
  101. });
  102. });
  103. context('after crowdsale end', function () {
  104. beforeEach(async function () {
  105. await time.increaseTo(this.afterClosingTime);
  106. });
  107. it('it reverts', async function () {
  108. const newClosingTime = await time.latest();
  109. await shouldFail.reverting(this.crowdsale.extendTime(newClosingTime));
  110. });
  111. });
  112. });
  113. });
  114. });