RefundableCrowdsale.test.js 3.7 KB

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