FinalizableCrowdsale.test.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. const { accounts, contract } = require('@openzeppelin/test-environment');
  2. const { BN, expectEvent, expectRevert, time } = require('@openzeppelin/test-helpers');
  3. const FinalizableCrowdsaleImpl = contract.fromArtifact('FinalizableCrowdsaleImpl');
  4. const ERC20 = contract.fromArtifact('ERC20');
  5. describe('FinalizableCrowdsale', function () {
  6. const [ wallet, other ] = accounts;
  7. const rate = new BN('1000');
  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 ERC20.new();
  17. this.crowdsale = await FinalizableCrowdsaleImpl.new(
  18. this.openingTime, this.closingTime, rate, wallet, this.token.address
  19. );
  20. });
  21. it('cannot be finalized before ending', async function () {
  22. await expectRevert(this.crowdsale.finalize({ from: other }),
  23. 'FinalizableCrowdsale: not closed'
  24. );
  25. });
  26. it('can be finalized by anyone after ending', async function () {
  27. await time.increaseTo(this.afterClosingTime);
  28. await this.crowdsale.finalize({ from: other });
  29. });
  30. it('cannot be finalized twice', async function () {
  31. await time.increaseTo(this.afterClosingTime);
  32. await this.crowdsale.finalize({ from: other });
  33. await expectRevert(this.crowdsale.finalize({ from: other }),
  34. 'FinalizableCrowdsale: already finalized'
  35. );
  36. });
  37. it('logs finalized', async function () {
  38. await time.increaseTo(this.afterClosingTime);
  39. const { logs } = await this.crowdsale.finalize({ from: other });
  40. expectEvent.inLogs(logs, 'CrowdsaleFinalized');
  41. });
  42. });