TimedCrowdsale.test.js 3.3 KB

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