RefundableCrowdsale.test.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 { EVMRevert } = require('../helpers/EVMRevert');
  6. const { ethGetBalance } = require('../helpers/web3');
  7. const BigNumber = web3.BigNumber;
  8. require('chai')
  9. .use(require('chai-as-promised'))
  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 RefundableCrowdsale.new(
  36. this.openingTime, this.closingTime, rate, wallet, this.token.address, 0, { from: owner }
  37. ).should.be.rejectedWith(EVMRevert);
  38. });
  39. });
  40. it('should deny refunds before end', async function () {
  41. await this.crowdsale.claimRefund({ from: investor }).should.be.rejectedWith(EVMRevert);
  42. await increaseTimeTo(this.openingTime);
  43. await this.crowdsale.claimRefund({ from: investor }).should.be.rejectedWith(EVMRevert);
  44. });
  45. it('should deny refunds after end if goal was reached', async function () {
  46. await increaseTimeTo(this.openingTime);
  47. await this.crowdsale.sendTransaction({ value: goal, from: investor });
  48. await increaseTimeTo(this.afterClosingTime);
  49. await this.crowdsale.claimRefund({ from: investor }).should.be.rejectedWith(EVMRevert);
  50. });
  51. it('should allow refunds after end if goal was not reached', async function () {
  52. await increaseTimeTo(this.openingTime);
  53. await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor });
  54. await increaseTimeTo(this.afterClosingTime);
  55. await this.crowdsale.finalize({ from: owner });
  56. const pre = await ethGetBalance(investor);
  57. await this.crowdsale.claimRefund({ from: investor, gasPrice: 0 })
  58. .should.be.fulfilled;
  59. const post = await ethGetBalance(investor);
  60. post.minus(pre).should.be.bignumber.equal(lessThanGoal);
  61. });
  62. it('should forward funds to wallet after end if goal was reached', async function () {
  63. await increaseTimeTo(this.openingTime);
  64. await this.crowdsale.sendTransaction({ value: goal, from: investor });
  65. await increaseTimeTo(this.afterClosingTime);
  66. const pre = await ethGetBalance(wallet);
  67. await this.crowdsale.finalize({ from: owner });
  68. const post = await ethGetBalance(wallet);
  69. post.minus(pre).should.be.bignumber.equal(goal);
  70. });
  71. });