RefundablePostDeliveryCrowdsale.test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const { BN, ether, shouldFail, time } = require('openzeppelin-test-helpers');
  2. const RefundablePostDeliveryCrowdsaleImpl = artifacts.require('RefundablePostDeliveryCrowdsaleImpl');
  3. const SimpleToken = artifacts.require('SimpleToken');
  4. contract('RefundablePostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) {
  5. const rate = new BN(1);
  6. const tokenSupply = new BN('10').pow(new BN('22'));
  7. const goal = ether('100');
  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 RefundablePostDeliveryCrowdsaleImpl.new(
  18. this.openingTime, this.closingTime, rate, wallet, this.token.address, goal
  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 below the goal', function () {
  27. const value = goal.subn(1);
  28. beforeEach(async function () {
  29. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  30. });
  31. it('does not immediately deliver tokens to beneficiaries', async function () {
  32. (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal(value);
  33. (await this.token.balanceOf(investor)).should.be.bignumber.equal('0');
  34. });
  35. it('does not allow beneficiaries to withdraw tokens before crowdsale ends', async function () {
  36. await shouldFail.reverting.withMessage(this.crowdsale.withdrawTokens(investor),
  37. 'RefundablePostDeliveryCrowdsale: not finalized'
  38. );
  39. });
  40. context('after closing time and finalization', function () {
  41. beforeEach(async function () {
  42. await time.increaseTo(this.afterClosingTime);
  43. await this.crowdsale.finalize();
  44. });
  45. it('rejects token withdrawals', async function () {
  46. await shouldFail.reverting.withMessage(this.crowdsale.withdrawTokens(investor),
  47. 'RefundablePostDeliveryCrowdsale: goal not reached'
  48. );
  49. });
  50. });
  51. });
  52. context('with bought tokens matching the goal', function () {
  53. const value = goal;
  54. beforeEach(async function () {
  55. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  56. });
  57. it('does not immediately deliver tokens to beneficiaries', async function () {
  58. (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal(value);
  59. (await this.token.balanceOf(investor)).should.be.bignumber.equal('0');
  60. });
  61. it('does not allow beneficiaries to withdraw tokens before crowdsale ends', async function () {
  62. await shouldFail.reverting.withMessage(this.crowdsale.withdrawTokens(investor),
  63. 'RefundablePostDeliveryCrowdsale: not finalized'
  64. );
  65. });
  66. context('after closing time and finalization', function () {
  67. beforeEach(async function () {
  68. await time.increaseTo(this.afterClosingTime);
  69. await this.crowdsale.finalize();
  70. });
  71. it('allows beneficiaries to withdraw tokens', async function () {
  72. await this.crowdsale.withdrawTokens(investor);
  73. (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal('0');
  74. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value);
  75. });
  76. it('rejects multiple withdrawals', async function () {
  77. await this.crowdsale.withdrawTokens(investor);
  78. await shouldFail.reverting.withMessage(this.crowdsale.withdrawTokens(investor),
  79. 'PostDeliveryCrowdsale: beneficiary is not due any tokens'
  80. );
  81. });
  82. });
  83. });
  84. });
  85. });