FinalizableCrowdsale.test.js 2.0 KB

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