TimedCrowdsale.test.js 5.9 KB

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