PostDeliveryCrowdsale.test.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. (await this.token.balanceOf(investor)).should.be.bignumber.equal(0);
  36. });
  37. it('should not allow beneficiaries to withdraw tokens before crowdsale ends', async function () {
  38. await increaseTimeTo(this.beforeEndTime);
  39. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  40. await expectThrow(this.crowdsale.withdrawTokens({ from: investor }), EVMRevert);
  41. });
  42. it('should allow beneficiaries to withdraw tokens after crowdsale ends', async function () {
  43. await increaseTimeTo(this.openingTime);
  44. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  45. await increaseTimeTo(this.afterClosingTime);
  46. await this.crowdsale.withdrawTokens({ from: investor });
  47. });
  48. it('should return the amount of tokens bought', async function () {
  49. await increaseTimeTo(this.openingTime);
  50. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  51. await increaseTimeTo(this.afterClosingTime);
  52. await this.crowdsale.withdrawTokens({ from: investor });
  53. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value);
  54. });
  55. });