RefundablePostDeliveryCrowdsale.test.js 4.1 KB

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