RefundableCrowdsale.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. const { ether } = require('../helpers/ether');
  2. const { advanceBlock } = require('../helpers/advanceToBlock');
  3. const shouldFail = require('../helpers/shouldFail');
  4. const time = require('../helpers/time');
  5. const { ethGetBalance } = require('../helpers/web3');
  6. const BigNumber = web3.BigNumber;
  7. require('chai')
  8. .use(require('chai-bignumber')(BigNumber))
  9. .should();
  10. const RefundableCrowdsaleImpl = artifacts.require('RefundableCrowdsaleImpl');
  11. const SimpleToken = artifacts.require('SimpleToken');
  12. contract('RefundableCrowdsale', function ([_, wallet, investor, purchaser, anyone]) {
  13. const rate = new BigNumber(1);
  14. const goal = ether(50);
  15. const lessThanGoal = ether(45);
  16. const tokenSupply = new BigNumber('1e22');
  17. before(async function () {
  18. // Advance to the next block to correctly read time in the solidity "now" function interpreted by ganache
  19. await advanceBlock();
  20. });
  21. beforeEach(async function () {
  22. this.openingTime = (await time.latest()) + time.duration.weeks(1);
  23. this.closingTime = this.openingTime + time.duration.weeks(1);
  24. this.afterClosingTime = this.closingTime + time.duration.seconds(1);
  25. this.preWalletBalance = await ethGetBalance(wallet);
  26. this.token = await SimpleToken.new();
  27. });
  28. it('rejects a goal of zero', async function () {
  29. await shouldFail.reverting(
  30. RefundableCrowdsaleImpl.new(this.openingTime, this.closingTime, rate, wallet, this.token.address, 0)
  31. );
  32. });
  33. context('with crowdsale', function () {
  34. beforeEach(async function () {
  35. this.crowdsale = await RefundableCrowdsaleImpl.new(
  36. this.openingTime, this.closingTime, rate, wallet, this.token.address, goal
  37. );
  38. await this.token.transfer(this.crowdsale.address, tokenSupply);
  39. });
  40. context('before opening time', function () {
  41. it('denies refunds', async function () {
  42. await shouldFail.reverting(this.crowdsale.claimRefund(investor));
  43. });
  44. });
  45. context('after opening time', function () {
  46. beforeEach(async function () {
  47. await time.increaseTo(this.openingTime);
  48. });
  49. it('denies refunds', async function () {
  50. await shouldFail.reverting(this.crowdsale.claimRefund(investor));
  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: anyone });
  60. });
  61. it('refunds', async function () {
  62. const pre = await ethGetBalance(investor);
  63. await this.crowdsale.claimRefund(investor, { gasPrice: 0 });
  64. const post = await ethGetBalance(investor);
  65. post.minus(pre).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. });