RefundableCrowdsale.test.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import ether from '../helpers/ether';
  2. import { advanceBlock } from '../helpers/advanceToBlock';
  3. import { increaseTimeTo, duration } from '../helpers/increaseTime';
  4. import latestTime from '../helpers/latestTime';
  5. import EVMRevert from '../helpers/EVMRevert';
  6. const BigNumber = web3.BigNumber;
  7. require('chai')
  8. .use(require('chai-as-promised'))
  9. .use(require('chai-bignumber')(BigNumber))
  10. .should();
  11. const RefundableCrowdsale = artifacts.require('RefundableCrowdsaleImpl');
  12. const SimpleToken = artifacts.require('SimpleToken');
  13. contract('RefundableCrowdsale', function ([_, owner, wallet, investor, purchaser]) {
  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 = latestTime() + duration.weeks(1);
  24. this.closingTime = this.openingTime + duration.weeks(1);
  25. this.afterClosingTime = this.closingTime + duration.seconds(1);
  26. this.token = await SimpleToken.new();
  27. this.crowdsale = await RefundableCrowdsale.new(
  28. this.openingTime, this.closingTime, rate, wallet, this.token.address, goal, { from: owner }
  29. );
  30. await this.token.transfer(this.crowdsale.address, tokenSupply);
  31. });
  32. describe('creating a valid crowdsale', function () {
  33. it('should fail with zero goal', async function () {
  34. await RefundableCrowdsale.new(
  35. this.openingTime, this.closingTime, rate, wallet, this.token.address, 0, { from: owner }
  36. ).should.be.rejectedWith(EVMRevert);
  37. });
  38. });
  39. it('should deny refunds before end', async function () {
  40. await this.crowdsale.claimRefund({ from: investor }).should.be.rejectedWith(EVMRevert);
  41. await increaseTimeTo(this.openingTime);
  42. await this.crowdsale.claimRefund({ from: investor }).should.be.rejectedWith(EVMRevert);
  43. });
  44. it('should deny refunds after end if goal was reached', async function () {
  45. await increaseTimeTo(this.openingTime);
  46. await this.crowdsale.sendTransaction({ value: goal, from: investor });
  47. await increaseTimeTo(this.afterClosingTime);
  48. await this.crowdsale.claimRefund({ from: investor }).should.be.rejectedWith(EVMRevert);
  49. });
  50. it('should allow refunds after end if goal was not reached', async function () {
  51. await increaseTimeTo(this.openingTime);
  52. await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor });
  53. await increaseTimeTo(this.afterClosingTime);
  54. await this.crowdsale.finalize({ from: owner });
  55. const pre = web3.eth.getBalance(investor);
  56. await this.crowdsale.claimRefund({ from: investor, gasPrice: 0 })
  57. .should.be.fulfilled;
  58. const post = web3.eth.getBalance(investor);
  59. post.minus(pre).should.be.bignumber.equal(lessThanGoal);
  60. });
  61. it('should forward funds to wallet after end if goal was reached', async function () {
  62. await increaseTimeTo(this.openingTime);
  63. await this.crowdsale.sendTransaction({ value: goal, from: investor });
  64. await increaseTimeTo(this.afterClosingTime);
  65. const pre = web3.eth.getBalance(wallet);
  66. await this.crowdsale.finalize({ from: owner });
  67. const post = web3.eth.getBalance(wallet);
  68. post.minus(pre).should.be.bignumber.equal(goal);
  69. });
  70. });