TimedCrowdsale.test.js 3.2 KB

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