FinalizableCrowdsale.test.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. const { BN, expectEvent, shouldFail, time } = require('openzeppelin-test-helpers');
  2. const FinalizableCrowdsaleImpl = artifacts.require('FinalizableCrowdsaleImpl');
  3. const ERC20 = artifacts.require('ERC20');
  4. contract('FinalizableCrowdsale', function ([_, wallet, other]) {
  5. const rate = new BN('1000');
  6. before(async function () {
  7. // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
  8. await time.advanceBlock();
  9. });
  10. beforeEach(async function () {
  11. this.openingTime = (await time.latest()).add(time.duration.weeks(1));
  12. this.closingTime = this.openingTime.add(time.duration.weeks(1));
  13. this.afterClosingTime = this.closingTime.add(time.duration.seconds(1));
  14. this.token = await ERC20.new();
  15. this.crowdsale = await FinalizableCrowdsaleImpl.new(
  16. this.openingTime, this.closingTime, rate, wallet, this.token.address
  17. );
  18. });
  19. it('cannot be finalized before ending', async function () {
  20. await shouldFail.reverting.withMessage(this.crowdsale.finalize({ from: other }),
  21. 'FinalizableCrowdsale: not closed'
  22. );
  23. });
  24. it('can be finalized by anyone after ending', async function () {
  25. await time.increaseTo(this.afterClosingTime);
  26. await this.crowdsale.finalize({ from: other });
  27. });
  28. it('cannot be finalized twice', async function () {
  29. await time.increaseTo(this.afterClosingTime);
  30. await this.crowdsale.finalize({ from: other });
  31. await shouldFail.reverting.withMessage(this.crowdsale.finalize({ from: other }),
  32. 'FinalizableCrowdsale: already finalized'
  33. );
  34. });
  35. it('logs finalized', async function () {
  36. await time.increaseTo(this.afterClosingTime);
  37. const { logs } = await this.crowdsale.finalize({ from: other });
  38. expectEvent.inLogs(logs, 'CrowdsaleFinalized');
  39. });
  40. });