RefundablePostDeliveryCrowdsale.test.js 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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(this.crowdsale.withdrawTokens(investor));
  37. });
  38. context('after closing time and finalization', function () {
  39. beforeEach(async function () {
  40. await time.increaseTo(this.afterClosingTime);
  41. await this.crowdsale.finalize();
  42. });
  43. it('rejects token withdrawals', async function () {
  44. await shouldFail.reverting(this.crowdsale.withdrawTokens(investor));
  45. });
  46. });
  47. });
  48. context('with bought tokens matching the goal', function () {
  49. const value = goal;
  50. beforeEach(async function () {
  51. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  52. });
  53. it('does not immediately deliver tokens to beneficiaries', async function () {
  54. (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal(value);
  55. (await this.token.balanceOf(investor)).should.be.bignumber.equal('0');
  56. });
  57. it('does not allow beneficiaries to withdraw tokens before crowdsale ends', async function () {
  58. await shouldFail.reverting(this.crowdsale.withdrawTokens(investor));
  59. });
  60. context('after closing time and finalization', function () {
  61. beforeEach(async function () {
  62. await time.increaseTo(this.afterClosingTime);
  63. await this.crowdsale.finalize();
  64. });
  65. it('allows beneficiaries to withdraw tokens', async function () {
  66. await this.crowdsale.withdrawTokens(investor);
  67. (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal('0');
  68. (await this.token.balanceOf(investor)).should.be.bignumber.equal(value);
  69. });
  70. it('rejects multiple withdrawals', async function () {
  71. await this.crowdsale.withdrawTokens(investor);
  72. await shouldFail.reverting(this.crowdsale.withdrawTokens(investor));
  73. });
  74. });
  75. });
  76. });
  77. });