TimedCrowdsale.test.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const { ether } = require('../helpers/ether');
  2. const shouldFail = require('../helpers/shouldFail');
  3. const time = require('../helpers/time');
  4. const { BigNumber } = require('../helpers/setup');
  5. const TimedCrowdsaleImpl = artifacts.require('TimedCrowdsaleImpl');
  6. const SimpleToken = artifacts.require('SimpleToken');
  7. contract('TimedCrowdsale', function ([_, investor, wallet, purchaser]) {
  8. const rate = new BigNumber(1);
  9. const value = ether(42);
  10. const tokenSupply = new BigNumber('1e22');
  11. before(async function () {
  12. // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
  13. await time.advanceBlock();
  14. });
  15. beforeEach(async function () {
  16. this.openingTime = (await time.latest()) + time.duration.weeks(1);
  17. this.closingTime = this.openingTime + time.duration.weeks(1);
  18. this.afterClosingTime = this.closingTime + time.duration.seconds(1);
  19. this.token = await SimpleToken.new();
  20. });
  21. it('reverts if the opening time is in the past', async function () {
  22. await shouldFail.reverting(TimedCrowdsaleImpl.new(
  23. (await time.latest()) - time.duration.days(1), this.closingTime, rate, wallet, this.token.address
  24. ));
  25. });
  26. it('reverts if the closing time is before the opening time', async function () {
  27. await shouldFail.reverting(TimedCrowdsaleImpl.new(
  28. this.openingTime, this.openingTime - time.duration.seconds(1), rate, wallet, this.token.address
  29. ));
  30. });
  31. it('reverts if the closing time equals the opening time', async function () {
  32. await shouldFail.reverting(TimedCrowdsaleImpl.new(
  33. this.openingTime, this.openingTime, rate, wallet, this.token.address
  34. ));
  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 shouldFail.reverting(this.crowdsale.send(value));
  53. await shouldFail.reverting(this.crowdsale.buyTokens(investor, { from: purchaser, value: value }));
  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 shouldFail.reverting(this.crowdsale.send(value));
  64. await shouldFail.reverting(this.crowdsale.buyTokens(investor, { value: value, from: purchaser }));
  65. });
  66. });
  67. });
  68. });