TimedCrowdsale.test.js 6.0 KB

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