TimedCrowdsale.test.js 3.3 KB

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