RefundableCrowdsale.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const { ether } = require('../helpers/ether');
  2. const { balanceDifference } = require('../helpers/balanceDifference');
  3. const shouldFail = require('../helpers/shouldFail');
  4. const time = require('../helpers/time');
  5. const { ethGetBalance } = require('../helpers/web3');
  6. const { BigNumber } = require('../helpers/setup');
  7. const RefundableCrowdsaleImpl = artifacts.require('RefundableCrowdsaleImpl');
  8. const SimpleToken = artifacts.require('SimpleToken');
  9. contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, anyone]) {
  10. const rate = new BigNumber(1);
  11. const goal = ether(50);
  12. const lessThanGoal = ether(45);
  13. const tokenSupply = new BigNumber('1e22');
  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.preWalletBalance = await ethGetBalance(wallet);
  23. this.token = await SimpleToken.new();
  24. });
  25. it('rejects a goal of zero', async function () {
  26. await shouldFail.reverting(
  27. RefundableCrowdsaleImpl.new(this.openingTime, this.closingTime, rate, wallet, this.token.address, 0)
  28. );
  29. });
  30. context('with crowdsale', function () {
  31. beforeEach(async function () {
  32. this.crowdsale = await RefundableCrowdsaleImpl.new(
  33. this.openingTime, this.closingTime, rate, wallet, this.token.address, goal
  34. );
  35. await this.token.transfer(this.crowdsale.address, tokenSupply);
  36. });
  37. context('before opening time', function () {
  38. it('denies refunds', async function () {
  39. await shouldFail.reverting(this.crowdsale.claimRefund(investor));
  40. });
  41. });
  42. context('after opening time', function () {
  43. beforeEach(async function () {
  44. await time.increaseTo(this.openingTime);
  45. });
  46. it('denies refunds', async function () {
  47. await shouldFail.reverting(this.crowdsale.claimRefund(investor));
  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: anyone });
  57. });
  58. it('refunds', async function () {
  59. (await balanceDifference(investor, () =>
  60. this.crowdsale.claimRefund(investor, { gasPrice: 0 }))
  61. ).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: anyone });
  73. });
  74. it('denies refunds', async function () {
  75. await shouldFail.reverting(this.crowdsale.claimRefund(investor));
  76. });
  77. it('forwards funds to wallet', async function () {
  78. const postWalletBalance = await ethGetBalance(wallet);
  79. postWalletBalance.minus(this.preWalletBalance).should.be.bignumber.equal(goal);
  80. });
  81. });
  82. });
  83. });
  84. });
  85. });