RefundableCrowdsale.test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. const { ether } = require('../helpers/ether');
  2. const { advanceBlock } = require('../helpers/advanceToBlock');
  3. const time = require('../helpers/time');
  4. const { expectThrow } = require('../helpers/expectThrow');
  5. const { EVMRevert } = require('../helpers/EVMRevert');
  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 expectThrow(
  31. RefundableCrowdsaleImpl.new(
  32. this.openingTime, this.closingTime, rate, wallet, this.token.address, 0,
  33. ),
  34. EVMRevert,
  35. );
  36. });
  37. context('with crowdsale', function () {
  38. beforeEach(async function () {
  39. this.crowdsale = await RefundableCrowdsaleImpl.new(
  40. this.openingTime, this.closingTime, rate, wallet, this.token.address, goal
  41. );
  42. await this.token.transfer(this.crowdsale.address, tokenSupply);
  43. });
  44. context('before opening time', function () {
  45. it('denies refunds', async function () {
  46. await expectThrow(this.crowdsale.claimRefund(investor), EVMRevert);
  47. });
  48. });
  49. context('after opening time', function () {
  50. beforeEach(async function () {
  51. await time.increaseTo(this.openingTime);
  52. });
  53. it('denies refunds', async function () {
  54. await expectThrow(this.crowdsale.claimRefund(investor), EVMRevert);
  55. });
  56. context('with unreached goal', function () {
  57. beforeEach(async function () {
  58. await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor });
  59. });
  60. context('after closing time and finalization', function () {
  61. beforeEach(async function () {
  62. await time.increaseTo(this.afterClosingTime);
  63. await this.crowdsale.finalize({ from: anyone });
  64. });
  65. it('refunds', async function () {
  66. const pre = await ethGetBalance(investor);
  67. await this.crowdsale.claimRefund(investor, { gasPrice: 0 });
  68. const post = await ethGetBalance(investor);
  69. post.minus(pre).should.be.bignumber.equal(lessThanGoal);
  70. });
  71. });
  72. });
  73. context('with reached goal', function () {
  74. beforeEach(async function () {
  75. await this.crowdsale.sendTransaction({ value: goal, from: investor });
  76. });
  77. context('after closing time and finalization', function () {
  78. beforeEach(async function () {
  79. await time.increaseTo(this.afterClosingTime);
  80. await this.crowdsale.finalize({ from: anyone });
  81. });
  82. it('denies refunds', async function () {
  83. await expectThrow(this.crowdsale.claimRefund(investor), EVMRevert);
  84. });
  85. it('forwards funds to wallet', async function () {
  86. const postWalletBalance = await ethGetBalance(wallet);
  87. postWalletBalance.minus(this.preWalletBalance).should.be.bignumber.equal(goal);
  88. });
  89. });
  90. });
  91. });
  92. });
  93. });