FinalizableCrowdsale.test.js 1.9 KB

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