PostDeliveryCrowdsale.test.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. const { BN, ether, expectRevert, time } = require('openzeppelin-test-helpers');
  2. const { expect } = require('chai');
  3. const PostDeliveryCrowdsaleImpl = artifacts.require('PostDeliveryCrowdsaleImpl');
  4. const SimpleToken = artifacts.require('SimpleToken');
  5. contract('PostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) {
  6. const rate = new BN(1);
  7. const tokenSupply = new BN('10').pow(new BN('22'));
  8. before(async function () {
  9. // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
  10. await time.advanceBlock();
  11. });
  12. beforeEach(async function () {
  13. this.openingTime = (await time.latest()).add(time.duration.weeks(1));
  14. this.closingTime = this.openingTime.add(time.duration.weeks(1));
  15. this.afterClosingTime = this.closingTime.add(time.duration.seconds(1));
  16. this.token = await SimpleToken.new();
  17. this.crowdsale = await PostDeliveryCrowdsaleImpl.new(
  18. this.openingTime, this.closingTime, rate, wallet, this.token.address
  19. );
  20. await this.token.transfer(this.crowdsale.address, tokenSupply);
  21. });
  22. context('after opening time', function () {
  23. beforeEach(async function () {
  24. await time.increaseTo(this.openingTime);
  25. });
  26. context('with bought tokens', function () {
  27. const value = ether('42');
  28. beforeEach(async function () {
  29. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  30. });
  31. it('does not immediately assign tokens to beneficiaries', async function () {
  32. expect(await this.crowdsale.balanceOf(investor)).to.be.bignumber.equal(value);
  33. expect(await this.token.balanceOf(investor)).to.be.bignumber.equal('0');
  34. });
  35. it('does not allow beneficiaries to withdraw tokens before crowdsale ends', async function () {
  36. await expectRevert(this.crowdsale.withdrawTokens(investor),
  37. 'PostDeliveryCrowdsale: not closed'
  38. );
  39. });
  40. context('after closing time', function () {
  41. beforeEach(async function () {
  42. await time.increaseTo(this.afterClosingTime);
  43. });
  44. it('allows beneficiaries to withdraw tokens', async function () {
  45. await this.crowdsale.withdrawTokens(investor);
  46. expect(await this.crowdsale.balanceOf(investor)).to.be.bignumber.equal('0');
  47. expect(await this.token.balanceOf(investor)).to.be.bignumber.equal(value);
  48. });
  49. it('rejects multiple withdrawals', async function () {
  50. await this.crowdsale.withdrawTokens(investor);
  51. await expectRevert(this.crowdsale.withdrawTokens(investor),
  52. 'PostDeliveryCrowdsale: beneficiary is not due any tokens'
  53. );
  54. });
  55. });
  56. });
  57. });
  58. });