FinalizableCrowdsale.test.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. const { advanceBlock } = require('../helpers/advanceToBlock');
  2. const { increaseTimeTo, duration } = require('../helpers/increaseTime');
  3. const { latestTime } = require('../helpers/latestTime');
  4. const { expectThrow } = require('../helpers/expectThrow');
  5. const { EVMRevert } = require('../helpers/EVMRevert');
  6. const BigNumber = web3.BigNumber;
  7. const should = require('chai')
  8. .use(require('chai-bignumber')(BigNumber))
  9. .should();
  10. const FinalizableCrowdsale = 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 latestTime()) + duration.weeks(1);
  20. this.closingTime = this.openingTime + duration.weeks(1);
  21. this.afterClosingTime = this.closingTime + duration.seconds(1);
  22. this.token = await ERC20.new();
  23. this.crowdsale = await FinalizableCrowdsale.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 increaseTimeTo(this.afterClosingTime);
  32. await this.crowdsale.finalize({ from: anyone });
  33. });
  34. it('cannot be finalized twice', async function () {
  35. await increaseTimeTo(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 increaseTimeTo(this.afterClosingTime);
  41. const { logs } = await this.crowdsale.finalize({ from: anyone });
  42. const event = logs.find(e => e.event === 'CrowdsaleFinalized');
  43. should.exist(event);
  44. });
  45. });