RefundablePostDeliveryCrowdsale.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. const time = require('../helpers/time');
  2. const shouldFail = require('../helpers/shouldFail');
  3. const { ether } = require('../helpers/ether');
  4. const BigNumber = web3.BigNumber;
  5. require('chai')
  6. .use(require('chai-bignumber')(BigNumber))
  7. .should();
  8. const RefundablePostDeliveryCrowdsaleImpl = artifacts.require('RefundablePostDeliveryCrowdsaleImpl');
  9. const SimpleToken = artifacts.require('SimpleToken');
  10. contract('RefundablePostDeliveryCrowdsale', function ([_, investor, wallet, purchaser]) {
  11. const rate = new BigNumber(1);
  12. const tokenSupply = new BigNumber('1e22');
  13. const goal = ether(100);
  14. before(async function () {
  15. // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
  16. await time.advanceBlock();
  17. });
  18. beforeEach(async function () {
  19. this.openingTime = (await time.latest()) + time.duration.weeks(1);
  20. this.closingTime = this.openingTime + time.duration.weeks(1);
  21. this.afterClosingTime = this.closingTime + time.duration.seconds(1);
  22. this.token = await SimpleToken.new();
  23. this.crowdsale = await RefundablePostDeliveryCrowdsaleImpl.new(
  24. this.openingTime, this.closingTime, rate, wallet, this.token.address, goal
  25. );
  26. await this.token.transfer(this.crowdsale.address, tokenSupply);
  27. });
  28. context('after opening time', function () {
  29. beforeEach(async function () {
  30. await time.increaseTo(this.openingTime);
  31. });
  32. context('with bought tokens below the goal', function () {
  33. const value = goal.sub(1);
  34. beforeEach(async function () {
  35. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  36. });
  37. it('does not immediately deliver tokens to beneficiaries', async function () {
  38. (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal(value);
  39. (await this.token.balanceOf(investor)).should.be.bignumber.equal(0);
  40. });
  41. it('does not allow beneficiaries to withdraw tokens before crowdsale ends', async function () {
  42. await shouldFail.reverting(this.crowdsale.withdrawTokens(investor));
  43. });
  44. context('after closing time and finalization', function () {
  45. beforeEach(async function () {
  46. await time.increaseTo(this.afterClosingTime);
  47. await this.crowdsale.finalize();
  48. });
  49. it('rejects token withdrawals', async function () {
  50. await shouldFail.reverting(this.crowdsale.withdrawTokens(investor));
  51. });
  52. });
  53. });
  54. context('with bought tokens matching the goal', function () {
  55. const value = goal;
  56. beforeEach(async function () {
  57. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  58. });
  59. it('does not immediately deliver tokens to beneficiaries', async function () {
  60. (await this.crowdsale.balanceOf(investor)).should.be.bignumber.equal(value);
  61. (await this.token.balanceOf(investor)).should.be.bignumber.equal(0);
  62. });
  63. it('does not allow beneficiaries to withdraw tokens before crowdsale ends', async function () {
  64. await shouldFail.reverting(this.crowdsale.withdrawTokens(investor));
  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(this.crowdsale.withdrawTokens(investor));
  79. });
  80. });
  81. });
  82. });
  83. });