RefundablePostDeliveryCrowdsale.test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const { BN, ether, expectRevert, time } = require('openzeppelin-test-helpers');
  2. const { expect } = require('chai');
  3. const RefundablePostDeliveryCrowdsaleImpl = artifacts.require('RefundablePostDeliveryCrowdsaleImpl');
  4. const SimpleToken = artifacts.require('SimpleToken');
  5. contract('RefundablePostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) {
  6. const rate = new BN(1);
  7. const tokenSupply = new BN('10').pow(new BN('22'));
  8. const goal = ether('100');
  9. before(async function () {
  10. // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
  11. await time.advanceBlock();
  12. });
  13. beforeEach(async function () {
  14. this.openingTime = (await time.latest()).add(time.duration.weeks(1));
  15. this.closingTime = this.openingTime.add(time.duration.weeks(1));
  16. this.afterClosingTime = this.closingTime.add(time.duration.seconds(1));
  17. this.token = await SimpleToken.new();
  18. this.crowdsale = await RefundablePostDeliveryCrowdsaleImpl.new(
  19. this.openingTime, this.closingTime, rate, wallet, this.token.address, goal
  20. );
  21. await this.token.transfer(this.crowdsale.address, tokenSupply);
  22. });
  23. context('after opening time', function () {
  24. beforeEach(async function () {
  25. await time.increaseTo(this.openingTime);
  26. });
  27. context('with bought tokens below the goal', function () {
  28. const value = goal.subn(1);
  29. beforeEach(async function () {
  30. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  31. });
  32. it('does not immediately deliver tokens to beneficiaries', async function () {
  33. expect(await this.crowdsale.balanceOf(investor)).to.be.bignumber.equal(value);
  34. expect(await this.token.balanceOf(investor)).to.be.bignumber.equal('0');
  35. });
  36. it('does not allow beneficiaries to withdraw tokens before crowdsale ends', async function () {
  37. await expectRevert(this.crowdsale.withdrawTokens(investor),
  38. 'RefundablePostDeliveryCrowdsale: not finalized'
  39. );
  40. });
  41. context('after closing time and finalization', function () {
  42. beforeEach(async function () {
  43. await time.increaseTo(this.afterClosingTime);
  44. await this.crowdsale.finalize();
  45. });
  46. it('rejects token withdrawals', async function () {
  47. await expectRevert(this.crowdsale.withdrawTokens(investor),
  48. 'RefundablePostDeliveryCrowdsale: goal not reached'
  49. );
  50. });
  51. });
  52. });
  53. context('with bought tokens matching the goal', function () {
  54. const value = goal;
  55. beforeEach(async function () {
  56. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  57. });
  58. it('does not immediately deliver tokens to beneficiaries', async function () {
  59. expect(await this.crowdsale.balanceOf(investor)).to.be.bignumber.equal(value);
  60. expect(await this.token.balanceOf(investor)).to.be.bignumber.equal('0');
  61. });
  62. it('does not allow beneficiaries to withdraw tokens before crowdsale ends', async function () {
  63. await expectRevert(this.crowdsale.withdrawTokens(investor),
  64. 'RefundablePostDeliveryCrowdsale: not finalized'
  65. );
  66. });
  67. context('after closing time and finalization', function () {
  68. beforeEach(async function () {
  69. await time.increaseTo(this.afterClosingTime);
  70. await this.crowdsale.finalize();
  71. });
  72. it('allows beneficiaries to withdraw tokens', async function () {
  73. await this.crowdsale.withdrawTokens(investor);
  74. expect(await this.crowdsale.balanceOf(investor)).to.be.bignumber.equal('0');
  75. expect(await this.token.balanceOf(investor)).to.be.bignumber.equal(value);
  76. });
  77. it('rejects multiple withdrawals', async function () {
  78. await this.crowdsale.withdrawTokens(investor);
  79. await expectRevert(this.crowdsale.withdrawTokens(investor),
  80. 'PostDeliveryCrowdsale: beneficiary is not due any tokens'
  81. );
  82. });
  83. });
  84. });
  85. });
  86. });