TimedCrowdsale.test.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 TimedCrowdsale = 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(TimedCrowdsale.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(TimedCrowdsale.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 TimedCrowdsale.new(this.openingTime, this.closingTime, rate, wallet, this.token.address);
  40. await this.token.transfer(this.crowdsale.address, tokenSupply);
  41. });
  42. it('should be ended only after end', async function () {
  43. (await this.crowdsale.hasClosed()).should.equal(false);
  44. await increaseTimeTo(this.afterClosingTime);
  45. (await this.crowdsale.isOpen()).should.equal(false);
  46. (await this.crowdsale.hasClosed()).should.equal(true);
  47. });
  48. describe('accepting payments', function () {
  49. it('should reject payments before start', async function () {
  50. (await this.crowdsale.isOpen()).should.equal(false);
  51. await expectThrow(this.crowdsale.send(value), EVMRevert);
  52. await expectThrow(this.crowdsale.buyTokens(investor, { from: purchaser, value: value }), EVMRevert);
  53. });
  54. it('should accept payments after start', async function () {
  55. await increaseTimeTo(this.openingTime);
  56. (await this.crowdsale.isOpen()).should.equal(true);
  57. await this.crowdsale.send(value);
  58. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  59. });
  60. it('should reject payments after end', async function () {
  61. await increaseTimeTo(this.afterClosingTime);
  62. await expectThrow(this.crowdsale.send(value), EVMRevert);
  63. await expectThrow(this.crowdsale.buyTokens(investor, { value: value, from: purchaser }), EVMRevert);
  64. });
  65. });
  66. });
  67. });