RefundableCrowdsale.test.js 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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, anyone]) {
  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(
  22. RefundableCrowdsaleImpl.new(this.openingTime, this.closingTime, rate, wallet, this.token.address, 0)
  23. );
  24. });
  25. context('with crowdsale', function () {
  26. beforeEach(async function () {
  27. this.crowdsale = await RefundableCrowdsaleImpl.new(
  28. this.openingTime, this.closingTime, rate, wallet, this.token.address, goal
  29. );
  30. await this.token.transfer(this.crowdsale.address, tokenSupply);
  31. });
  32. context('before opening time', function () {
  33. it('denies refunds', async function () {
  34. await shouldFail.reverting(this.crowdsale.claimRefund(investor));
  35. });
  36. });
  37. context('after opening time', function () {
  38. beforeEach(async function () {
  39. await time.increaseTo(this.openingTime);
  40. });
  41. it('denies refunds', async function () {
  42. await shouldFail.reverting(this.crowdsale.claimRefund(investor));
  43. });
  44. context('with unreached goal', function () {
  45. beforeEach(async function () {
  46. await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor });
  47. });
  48. context('after closing time and finalization', function () {
  49. beforeEach(async function () {
  50. await time.increaseTo(this.afterClosingTime);
  51. await this.crowdsale.finalize({ from: anyone });
  52. });
  53. it('refunds', async function () {
  54. const balanceTracker = await balance.tracker(investor);
  55. await this.crowdsale.claimRefund(investor, { gasPrice: 0 });
  56. (await balanceTracker.delta()).should.be.bignumber.equal(lessThanGoal);
  57. });
  58. });
  59. });
  60. context('with reached goal', function () {
  61. beforeEach(async function () {
  62. await this.crowdsale.sendTransaction({ value: goal, from: investor });
  63. });
  64. context('after closing time and finalization', function () {
  65. beforeEach(async function () {
  66. await time.increaseTo(this.afterClosingTime);
  67. await this.crowdsale.finalize({ from: anyone });
  68. });
  69. it('denies refunds', async function () {
  70. await shouldFail.reverting(this.crowdsale.claimRefund(investor));
  71. });
  72. it('forwards funds to wallet', async function () {
  73. const postWalletBalance = await balance.current(wallet);
  74. postWalletBalance.sub(this.preWalletBalance).should.be.bignumber.equal(goal);
  75. });
  76. });
  77. });
  78. });
  79. });
  80. });