TimedCrowdsale.test.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import ether from '../helpers/ether';
  2. import { advanceBlock } from '../helpers/advanceToBlock';
  3. import { increaseTimeTo, duration } from '../helpers/increaseTime';
  4. import latestTime from '../helpers/latestTime';
  5. import EVMRevert from '../helpers/EVMRevert';
  6. const BigNumber = web3.BigNumber;
  7. require('chai')
  8. .use(require('chai-as-promised'))
  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 = 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. this.crowdsale = await TimedCrowdsale.new(this.openingTime, this.closingTime, rate, wallet, this.token.address);
  27. await this.token.transfer(this.crowdsale.address, tokenSupply);
  28. });
  29. it('should be ended only after end', async function () {
  30. let ended = await this.crowdsale.hasClosed();
  31. ended.should.equal(false);
  32. await increaseTimeTo(this.afterClosingTime);
  33. ended = await this.crowdsale.hasClosed();
  34. ended.should.equal(true);
  35. });
  36. describe('accepting payments', function () {
  37. it('should reject payments before start', async function () {
  38. await this.crowdsale.send(value).should.be.rejectedWith(EVMRevert);
  39. await this.crowdsale.buyTokens(investor, { from: purchaser, value: value }).should.be.rejectedWith(EVMRevert);
  40. });
  41. it('should accept payments after start', async function () {
  42. await increaseTimeTo(this.openingTime);
  43. await this.crowdsale.send(value).should.be.fulfilled;
  44. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser }).should.be.fulfilled;
  45. });
  46. it('should reject payments after end', async function () {
  47. await increaseTimeTo(this.afterClosingTime);
  48. await this.crowdsale.send(value).should.be.rejectedWith(EVMRevert);
  49. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser }).should.be.rejectedWith(EVMRevert);
  50. });
  51. });
  52. });