RefundableCrowdsale.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const { balance, BN, ether, shouldFail, time } = require('openzeppelin-test-helpers');
  2. const RefundableCrowdsaleImpl = artifacts.require('RefundableCrowdsaleImpl');
  3. const SimpleToken = artifacts.require('SimpleToken');
  4. contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, other]) {
  5. const rate = new BN(1);
  6. const goal = ether('50');
  7. const lessThanGoal = ether('45');
  8. const tokenSupply = new BN('10').pow(new BN('22'));
  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.preWalletBalance = await balance.current(wallet);
  18. this.token = await SimpleToken.new();
  19. });
  20. it('rejects a goal of zero', async function () {
  21. await shouldFail.reverting.withMessage(
  22. RefundableCrowdsaleImpl.new(this.openingTime, this.closingTime, rate, wallet, this.token.address, 0),
  23. 'RefundableCrowdsale: goal is 0'
  24. );
  25. });
  26. context('with crowdsale', function () {
  27. beforeEach(async function () {
  28. this.crowdsale = await RefundableCrowdsaleImpl.new(
  29. this.openingTime, this.closingTime, rate, wallet, this.token.address, goal
  30. );
  31. await this.token.transfer(this.crowdsale.address, tokenSupply);
  32. });
  33. context('before opening time', function () {
  34. it('denies refunds', async function () {
  35. await shouldFail.reverting.withMessage(this.crowdsale.claimRefund(investor),
  36. 'RefundableCrowdsale: not finalized'
  37. );
  38. });
  39. });
  40. context('after opening time', function () {
  41. beforeEach(async function () {
  42. await time.increaseTo(this.openingTime);
  43. });
  44. it('denies refunds', async function () {
  45. await shouldFail.reverting.withMessage(this.crowdsale.claimRefund(investor),
  46. 'RefundableCrowdsale: not finalized'
  47. );
  48. });
  49. context('with unreached goal', function () {
  50. beforeEach(async function () {
  51. await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor });
  52. });
  53. context('after closing time and finalization', function () {
  54. beforeEach(async function () {
  55. await time.increaseTo(this.afterClosingTime);
  56. await this.crowdsale.finalize({ from: other });
  57. });
  58. it('refunds', async function () {
  59. const balanceTracker = await balance.tracker(investor);
  60. await this.crowdsale.claimRefund(investor, { gasPrice: 0 });
  61. (await balanceTracker.delta()).should.be.bignumber.equal(lessThanGoal);
  62. });
  63. });
  64. });
  65. context('with reached goal', function () {
  66. beforeEach(async function () {
  67. await this.crowdsale.sendTransaction({ value: goal, from: investor });
  68. });
  69. context('after closing time and finalization', function () {
  70. beforeEach(async function () {
  71. await time.increaseTo(this.afterClosingTime);
  72. await this.crowdsale.finalize({ from: other });
  73. });
  74. it('denies refunds', async function () {
  75. await shouldFail.reverting.withMessage(this.crowdsale.claimRefund(investor),
  76. 'RefundableCrowdsale: goal reached'
  77. );
  78. });
  79. it('forwards funds to wallet', async function () {
  80. const postWalletBalance = await balance.current(wallet);
  81. postWalletBalance.sub(this.preWalletBalance).should.be.bignumber.equal(goal);
  82. });
  83. });
  84. });
  85. });
  86. });
  87. });