TimedCrowdsale.test.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. const { BN, ether, shouldFail, time } = require('openzeppelin-test-helpers');
  2. const TimedCrowdsaleImpl = artifacts.require('TimedCrowdsaleImpl');
  3. const SimpleToken = artifacts.require('SimpleToken');
  4. contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) {
  5. const rate = new BN(1);
  6. const value = ether('42');
  7. const tokenSupply = new BN('10').pow(new BN('22'));
  8. before(async function () {
  9. // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
  10. await time.advanceBlock();
  11. });
  12. beforeEach(async function () {
  13. this.openingTime = (await time.latest()).add(time.duration.weeks(1));
  14. this.closingTime = this.openingTime.add(time.duration.weeks(1));
  15. this.afterClosingTime = this.closingTime.add(time.duration.seconds(1));
  16. this.token = await SimpleToken.new();
  17. });
  18. it('reverts if the opening time is in the past', async function () {
  19. await shouldFail.reverting(TimedCrowdsaleImpl.new(
  20. (await time.latest()).sub(time.duration.days(1)), this.closingTime, rate, wallet, this.token.address
  21. ));
  22. });
  23. it('reverts if the closing time is before the opening time', async function () {
  24. await shouldFail.reverting(TimedCrowdsaleImpl.new(
  25. this.openingTime, this.openingTime.sub(time.duration.seconds(1)), rate, wallet, this.token.address
  26. ));
  27. });
  28. it('reverts if the closing time equals the opening time', async function () {
  29. await shouldFail.reverting(TimedCrowdsaleImpl.new(
  30. this.openingTime, this.openingTime, rate, wallet, this.token.address
  31. ));
  32. });
  33. context('with crowdsale', function () {
  34. beforeEach(async function () {
  35. this.crowdsale = await TimedCrowdsaleImpl.new(
  36. this.openingTime, this.closingTime, rate, wallet, this.token.address
  37. );
  38. await this.token.transfer(this.crowdsale.address, tokenSupply);
  39. });
  40. it('should be ended only after end', async function () {
  41. (await this.crowdsale.hasClosed()).should.equal(false);
  42. await time.increaseTo(this.afterClosingTime);
  43. (await this.crowdsale.isOpen()).should.equal(false);
  44. (await this.crowdsale.hasClosed()).should.equal(true);
  45. });
  46. describe('accepting payments', function () {
  47. it('should reject payments before start', async function () {
  48. (await this.crowdsale.isOpen()).should.equal(false);
  49. await shouldFail.reverting(this.crowdsale.send(value));
  50. await shouldFail.reverting(this.crowdsale.buyTokens(investor, { from: purchaser, value: value }));
  51. });
  52. it('should accept payments after start', async function () {
  53. await time.increaseTo(this.openingTime);
  54. (await this.crowdsale.isOpen()).should.equal(true);
  55. await this.crowdsale.send(value);
  56. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  57. });
  58. it('should reject payments after end', async function () {
  59. await time.increaseTo(this.afterClosingTime);
  60. await shouldFail.reverting(this.crowdsale.send(value));
  61. await shouldFail.reverting(this.crowdsale.buyTokens(investor, { value: value, from: purchaser }));
  62. });
  63. });
  64. });
  65. });