RefundableCrowdsale.test.js 3.9 KB

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