PostDeliveryCrowdsale.test.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const { advanceBlock } = require('../helpers/advanceToBlock');
  2. const { increaseTimeTo, duration } = require('../helpers/increaseTime');
  3. const { latestTime } = require('../helpers/latestTime');
  4. const { expectThrow } = require('../helpers/expectThrow');
  5. const { EVMRevert } = require('../helpers/EVMRevert');
  6. const { ether } = require('../helpers/ether');
  7. const BigNumber = web3.BigNumber;
  8. require('chai')
  9. .use(require('chai-bignumber')(BigNumber))
  10. .should();
  11. const PostDeliveryCrowdsale = artifacts.require('PostDeliveryCrowdsaleImpl');
  12. const SimpleToken = artifacts.require('SimpleToken');
  13. contract('PostDeliveryCrowdsale', 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.beforeEndTime = this.closingTime - duration.hours(1);
  25. this.afterClosingTime = this.closingTime + duration.seconds(1);
  26. this.token = await SimpleToken.new();
  27. this.crowdsale = await PostDeliveryCrowdsale.new(
  28. this.openingTime, this.closingTime, rate, wallet, this.token.address
  29. );
  30. await this.token.transfer(this.crowdsale.address, tokenSupply);
  31. });
  32. it('should not immediately assign tokens to beneficiary', async function () {
  33. await increaseTimeTo(this.openingTime);
  34. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  35. const balance = await this.token.balanceOf(investor);
  36. balance.should.be.bignumber.equal(0);
  37. });
  38. it('should not allow beneficiaries to withdraw tokens before crowdsale ends', async function () {
  39. await increaseTimeTo(this.beforeEndTime);
  40. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  41. await expectThrow(this.crowdsale.withdrawTokens({ from: investor }), EVMRevert);
  42. });
  43. it('should allow beneficiaries to withdraw tokens after crowdsale ends', async function () {
  44. await increaseTimeTo(this.openingTime);
  45. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  46. await increaseTimeTo(this.afterClosingTime);
  47. await this.crowdsale.withdrawTokens({ from: investor });
  48. });
  49. it('should return the amount of tokens bought', async function () {
  50. await increaseTimeTo(this.openingTime);
  51. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  52. await increaseTimeTo(this.afterClosingTime);
  53. await this.crowdsale.withdrawTokens({ from: investor });
  54. const balance = await this.token.balanceOf(investor);
  55. balance.should.be.bignumber.equal(value);
  56. });
  57. });