TimedCrowdsale.test.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const { ether } = require('../helpers/ether');
  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 TimedCrowdsale = artifacts.require('TimedCrowdsaleImpl');
  12. const SimpleToken = artifacts.require('SimpleToken');
  13. contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) {
  14. const rate = new BigNumber(1);
  15. const value = ether(42);
  16. const tokenSupply = new BigNumber('1e22');
  17. before(async function () {
  18. // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
  19. await advanceBlock();
  20. });
  21. beforeEach(async function () {
  22. this.openingTime = (await latestTime()) + duration.weeks(1);
  23. this.closingTime = this.openingTime + duration.weeks(1);
  24. this.afterClosingTime = this.closingTime + duration.seconds(1);
  25. this.token = await SimpleToken.new();
  26. this.crowdsale = await TimedCrowdsale.new(this.openingTime, this.closingTime, rate, wallet, this.token.address);
  27. await this.token.transfer(this.crowdsale.address, tokenSupply);
  28. });
  29. it('should be ended only after end', async function () {
  30. (await this.crowdsale.hasClosed()).should.be.false;
  31. await increaseTimeTo(this.afterClosingTime);
  32. (await this.crowdsale.hasClosed()).should.be.true;
  33. });
  34. describe('accepting payments', function () {
  35. it('should reject payments before start', async function () {
  36. await expectThrow(this.crowdsale.send(value), EVMRevert);
  37. await expectThrow(this.crowdsale.buyTokens(investor, { from: purchaser, value: value }), EVMRevert);
  38. });
  39. it('should accept payments after start', async function () {
  40. await increaseTimeTo(this.openingTime);
  41. await this.crowdsale.send(value);
  42. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  43. });
  44. it('should reject payments after end', async function () {
  45. await increaseTimeTo(this.afterClosingTime);
  46. await expectThrow(this.crowdsale.send(value), EVMRevert);
  47. await expectThrow(this.crowdsale.buyTokens(investor, { value: value, from: purchaser }), EVMRevert);
  48. });
  49. });
  50. });