RefundableCrowdsale.test.js 3.8 KB

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