RefundableCrowdsale.test.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const { balance, BN, ether, expectRevert, time } = require('openzeppelin-test-helpers');
  2. const { expect } = require('chai');
  3. const RefundableCrowdsaleImpl = artifacts.require('RefundableCrowdsaleImpl');
  4. const SimpleToken = artifacts.require('SimpleToken');
  5. contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, other]) {
  6. const rate = new BN(1);
  7. const goal = ether('50');
  8. const lessThanGoal = ether('45');
  9. const tokenSupply = new BN('10').pow(new BN('22'));
  10. before(async function () {
  11. // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
  12. await time.advanceBlock();
  13. });
  14. beforeEach(async function () {
  15. this.openingTime = (await time.latest()).add(time.duration.weeks(1));
  16. this.closingTime = this.openingTime.add(time.duration.weeks(1));
  17. this.afterClosingTime = this.closingTime.add(time.duration.seconds(1));
  18. this.preWalletBalance = await balance.current(wallet);
  19. this.token = await SimpleToken.new();
  20. });
  21. it('rejects a goal of zero', async function () {
  22. await expectRevert(
  23. RefundableCrowdsaleImpl.new(this.openingTime, this.closingTime, rate, wallet, this.token.address, 0),
  24. 'RefundableCrowdsale: goal is 0'
  25. );
  26. });
  27. context('with crowdsale', function () {
  28. beforeEach(async function () {
  29. this.crowdsale = await RefundableCrowdsaleImpl.new(
  30. this.openingTime, this.closingTime, rate, wallet, this.token.address, goal
  31. );
  32. await this.token.transfer(this.crowdsale.address, tokenSupply);
  33. });
  34. context('before opening time', function () {
  35. it('denies refunds', async function () {
  36. await expectRevert(this.crowdsale.claimRefund(investor),
  37. 'RefundableCrowdsale: not finalized'
  38. );
  39. });
  40. });
  41. context('after opening time', function () {
  42. beforeEach(async function () {
  43. await time.increaseTo(this.openingTime);
  44. });
  45. it('denies refunds', async function () {
  46. await expectRevert(this.crowdsale.claimRefund(investor),
  47. 'RefundableCrowdsale: not finalized'
  48. );
  49. });
  50. context('with unreached goal', function () {
  51. beforeEach(async function () {
  52. await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor });
  53. });
  54. context('after closing time and finalization', function () {
  55. beforeEach(async function () {
  56. await time.increaseTo(this.afterClosingTime);
  57. await this.crowdsale.finalize({ from: other });
  58. });
  59. it('refunds', async function () {
  60. const balanceTracker = await balance.tracker(investor);
  61. await this.crowdsale.claimRefund(investor, { gasPrice: 0 });
  62. expect(await balanceTracker.delta()).to.be.bignumber.equal(lessThanGoal);
  63. });
  64. });
  65. });
  66. context('with reached goal', function () {
  67. beforeEach(async function () {
  68. await this.crowdsale.sendTransaction({ value: goal, from: investor });
  69. });
  70. context('after closing time and finalization', function () {
  71. beforeEach(async function () {
  72. await time.increaseTo(this.afterClosingTime);
  73. await this.crowdsale.finalize({ from: other });
  74. });
  75. it('denies refunds', async function () {
  76. await expectRevert(this.crowdsale.claimRefund(investor),
  77. 'RefundableCrowdsale: goal reached'
  78. );
  79. });
  80. it('forwards funds to wallet', async function () {
  81. const postWalletBalance = await balance.current(wallet);
  82. expect(postWalletBalance.sub(this.preWalletBalance)).to.be.bignumber.equal(goal);
  83. });
  84. });
  85. });
  86. });
  87. });
  88. });