TimedCrowdsale.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const { ether } = require('../helpers/ether');
  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 TimedCrowdsaleImpl = artifacts.require('TimedCrowdsaleImpl');
  11. const SimpleToken = artifacts.require('SimpleToken');
  12. contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) {
  13. const rate = new BigNumber(1);
  14. const value = ether(42);
  15. const tokenSupply = new BigNumber('1e22');
  16. before(async function () {
  17. // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
  18. await advanceBlock();
  19. });
  20. beforeEach(async function () {
  21. this.openingTime = (await time.latest()) + time.duration.weeks(1);
  22. this.closingTime = this.openingTime + time.duration.weeks(1);
  23. this.afterClosingTime = this.closingTime + time.duration.seconds(1);
  24. this.token = await SimpleToken.new();
  25. });
  26. it('rejects an opening time in the past', async function () {
  27. await expectThrow(TimedCrowdsaleImpl.new(
  28. (await time.latest()) - time.duration.days(1), this.closingTime, rate, wallet, this.token.address
  29. ), EVMRevert);
  30. });
  31. it('rejects a closing time before the opening time', async function () {
  32. await expectThrow(TimedCrowdsaleImpl.new(
  33. this.openingTime, this.openingTime - time.duration.seconds(1), rate, wallet, this.token.address
  34. ), EVMRevert);
  35. });
  36. context('with crowdsale', function () {
  37. beforeEach(async function () {
  38. this.crowdsale = await TimedCrowdsaleImpl.new(
  39. this.openingTime, this.closingTime, rate, wallet, this.token.address
  40. );
  41. await this.token.transfer(this.crowdsale.address, tokenSupply);
  42. });
  43. it('should be ended only after end', async function () {
  44. (await this.crowdsale.hasClosed()).should.equal(false);
  45. await time.increaseTo(this.afterClosingTime);
  46. (await this.crowdsale.isOpen()).should.equal(false);
  47. (await this.crowdsale.hasClosed()).should.equal(true);
  48. });
  49. describe('accepting payments', function () {
  50. it('should reject payments before start', async function () {
  51. (await this.crowdsale.isOpen()).should.equal(false);
  52. await expectThrow(this.crowdsale.send(value), EVMRevert);
  53. await expectThrow(this.crowdsale.buyTokens(investor, { from: purchaser, value: value }), EVMRevert);
  54. });
  55. it('should accept payments after start', async function () {
  56. await time.increaseTo(this.openingTime);
  57. (await this.crowdsale.isOpen()).should.equal(true);
  58. await this.crowdsale.send(value);
  59. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  60. });
  61. it('should reject payments after end', async function () {
  62. await time.increaseTo(this.afterClosingTime);
  63. await expectThrow(this.crowdsale.send(value), EVMRevert);
  64. await expectThrow(this.crowdsale.buyTokens(investor, { value: value, from: purchaser }), EVMRevert);
  65. });
  66. });
  67. });
  68. });