TimedCrowdsale.test.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.withMessage(TimedCrowdsaleImpl.new(
  20. (await time.latest()).sub(time.duration.days(1)), this.closingTime, rate, wallet, this.token.address
  21. ), 'TimedCrowdsale: opening time is before current time');
  22. });
  23. it('reverts if the closing time is before the opening time', async function () {
  24. await shouldFail.reverting.withMessage(TimedCrowdsaleImpl.new(
  25. this.openingTime, this.openingTime.sub(time.duration.seconds(1)), rate, wallet, this.token.address
  26. ), 'TimedCrowdsale: opening time is not before closing time');
  27. });
  28. it('reverts if the closing time equals the opening time', async function () {
  29. await shouldFail.reverting.withMessage(TimedCrowdsaleImpl.new(
  30. this.openingTime, this.openingTime, rate, wallet, this.token.address
  31. ), 'TimedCrowdsale: opening time is not before closing time');
  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.withMessage(this.crowdsale.send(value), 'TimedCrowdsale: not open');
  50. await shouldFail.reverting.withMessage(this.crowdsale.buyTokens(investor, { from: purchaser, value: value }),
  51. 'TimedCrowdsale: not open'
  52. );
  53. });
  54. it('should accept payments after start', async function () {
  55. await time.increaseTo(this.openingTime);
  56. (await this.crowdsale.isOpen()).should.equal(true);
  57. await this.crowdsale.send(value);
  58. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  59. });
  60. it('should reject payments after end', async function () {
  61. await time.increaseTo(this.afterClosingTime);
  62. await shouldFail.reverting.withMessage(this.crowdsale.send(value), 'TimedCrowdsale: not open');
  63. await shouldFail.reverting.withMessage(this.crowdsale.buyTokens(investor, { value: value, from: purchaser }),
  64. 'TimedCrowdsale: not open'
  65. );
  66. });
  67. });
  68. describe('extending closing time', function () {
  69. it('should not reduce duration', async function () {
  70. // Same date
  71. await shouldFail.reverting.withMessage(this.crowdsale.extendTime(this.closingTime),
  72. 'TimedCrowdsale: new closing time is before current closing time'
  73. );
  74. // Prescending date
  75. const newClosingTime = this.closingTime.sub(time.duration.seconds(1));
  76. await shouldFail.reverting.withMessage(this.crowdsale.extendTime(newClosingTime),
  77. 'TimedCrowdsale: new closing time is before current closing time'
  78. );
  79. });
  80. context('before crowdsale start', function () {
  81. beforeEach(async function () {
  82. (await this.crowdsale.isOpen()).should.equal(false);
  83. await shouldFail.reverting.withMessage(this.crowdsale.send(value), 'TimedCrowdsale: not open');
  84. });
  85. it('it extends end time', async function () {
  86. const newClosingTime = this.closingTime.add(time.duration.days(1));
  87. const { logs } = await this.crowdsale.extendTime(newClosingTime);
  88. expectEvent.inLogs(logs, 'TimedCrowdsaleExtended', {
  89. prevClosingTime: this.closingTime,
  90. newClosingTime: newClosingTime,
  91. });
  92. (await this.crowdsale.closingTime()).should.be.bignumber.equal(newClosingTime);
  93. });
  94. });
  95. context('after crowdsale start', function () {
  96. beforeEach(async function () {
  97. await time.increaseTo(this.openingTime);
  98. (await this.crowdsale.isOpen()).should.equal(true);
  99. await this.crowdsale.send(value);
  100. });
  101. it('it extends end time', async function () {
  102. const newClosingTime = this.closingTime.add(time.duration.days(1));
  103. const { logs } = await this.crowdsale.extendTime(newClosingTime);
  104. expectEvent.inLogs(logs, 'TimedCrowdsaleExtended', {
  105. prevClosingTime: this.closingTime,
  106. newClosingTime: newClosingTime,
  107. });
  108. (await this.crowdsale.closingTime()).should.be.bignumber.equal(newClosingTime);
  109. });
  110. });
  111. context('after crowdsale end', function () {
  112. beforeEach(async function () {
  113. await time.increaseTo(this.afterClosingTime);
  114. });
  115. it('it reverts', async function () {
  116. const newClosingTime = await time.latest();
  117. await shouldFail.reverting.withMessage(this.crowdsale.extendTime(newClosingTime),
  118. 'TimedCrowdsale: already closed'
  119. );
  120. });
  121. });
  122. });
  123. });
  124. });