FinalizableCrowdsale.test.js 2.0 KB

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