RefundableCrowdsale.test.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 ([_, owner, wallet, investor, purchaser]) {
  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.token = await SimpleToken.new();
  28. this.crowdsale = await RefundableCrowdsale.new(
  29. this.openingTime, this.closingTime, rate, wallet, this.token.address, goal, { from: owner }
  30. );
  31. await this.token.transfer(this.crowdsale.address, tokenSupply);
  32. });
  33. describe('creating a valid crowdsale', function () {
  34. it('should fail with zero goal', async function () {
  35. await expectThrow(
  36. RefundableCrowdsale.new(
  37. this.openingTime, this.closingTime, rate, wallet, this.token.address, 0, { from: owner }
  38. ),
  39. EVMRevert,
  40. );
  41. });
  42. });
  43. it('should deny refunds before end', async function () {
  44. await expectThrow(this.crowdsale.claimRefund({ from: investor }), EVMRevert);
  45. await increaseTimeTo(this.openingTime);
  46. await expectThrow(this.crowdsale.claimRefund({ from: investor }), EVMRevert);
  47. });
  48. it('should deny refunds after end if goal was reached', async function () {
  49. await increaseTimeTo(this.openingTime);
  50. await this.crowdsale.sendTransaction({ value: goal, from: investor });
  51. await increaseTimeTo(this.afterClosingTime);
  52. await expectThrow(this.crowdsale.claimRefund({ from: investor }), EVMRevert);
  53. });
  54. it('should allow refunds after end if goal was not reached', async function () {
  55. await increaseTimeTo(this.openingTime);
  56. await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor });
  57. await increaseTimeTo(this.afterClosingTime);
  58. await this.crowdsale.finalize({ from: owner });
  59. const pre = await ethGetBalance(investor);
  60. await this.crowdsale.claimRefund({ from: investor, gasPrice: 0 });
  61. const post = await ethGetBalance(investor);
  62. post.minus(pre).should.be.bignumber.equal(lessThanGoal);
  63. });
  64. it('should forward funds to wallet after end if goal was reached', async function () {
  65. await increaseTimeTo(this.openingTime);
  66. await this.crowdsale.sendTransaction({ value: goal, from: investor });
  67. await increaseTimeTo(this.afterClosingTime);
  68. const pre = await ethGetBalance(wallet);
  69. await this.crowdsale.finalize({ from: owner });
  70. const post = await ethGetBalance(wallet);
  71. post.minus(pre).should.be.bignumber.equal(goal);
  72. });
  73. });