FinalizableCrowdsale.test.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 MintableToken = artifacts.require('MintableToken');
  12. contract('FinalizableCrowdsale', function ([_, owner, wallet, thirdparty]) {
  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 MintableToken.new();
  23. this.crowdsale = await FinalizableCrowdsale.new(
  24. this.openingTime, this.closingTime, rate, wallet, this.token.address, { from: owner }
  25. );
  26. await this.token.transferOwnership(this.crowdsale.address);
  27. });
  28. it('cannot be finalized before ending', async function () {
  29. await expectThrow(this.crowdsale.finalize({ from: owner }), EVMRevert);
  30. });
  31. it('cannot be finalized by third party after ending', async function () {
  32. await increaseTimeTo(this.afterClosingTime);
  33. await expectThrow(this.crowdsale.finalize({ from: thirdparty }), EVMRevert);
  34. });
  35. it('can be finalized by owner after ending', async function () {
  36. await increaseTimeTo(this.afterClosingTime);
  37. await this.crowdsale.finalize({ from: owner });
  38. });
  39. it('cannot be finalized twice', async function () {
  40. await increaseTimeTo(this.afterClosingTime);
  41. await this.crowdsale.finalize({ from: owner });
  42. await expectThrow(this.crowdsale.finalize({ from: owner }), EVMRevert);
  43. });
  44. it('logs finalized', async function () {
  45. await increaseTimeTo(this.afterClosingTime);
  46. const { logs } = await this.crowdsale.finalize({ from: owner });
  47. const event = logs.find(e => e.event === 'Finalized');
  48. should.exist(event);
  49. });
  50. });