RefundableCrowdsale.test.js 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 MintableToken = artifacts.require('MintableToken');
  13. contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) {
  14. const rate = new BigNumber(1000);
  15. const goal = ether(800);
  16. const lessThanGoal = ether(750);
  17. before(async function () {
  18. // Advance to the next block to correctly read time in the solidity "now" function interpreted by testrpc
  19. await advanceBlock();
  20. });
  21. beforeEach(async function () {
  22. this.startTime = latestTime() + duration.weeks(1);
  23. this.endTime = this.startTime + duration.weeks(1);
  24. this.afterEndTime = this.endTime + duration.seconds(1);
  25. this.token = await MintableToken.new();
  26. this.crowdsale = await RefundableCrowdsale.new(
  27. this.startTime, this.endTime, rate, wallet, goal, this.token.address, { from: owner }
  28. );
  29. await this.token.transferOwnership(this.crowdsale.address);
  30. });
  31. describe('creating a valid crowdsale', function () {
  32. it('should fail with zero goal', async function () {
  33. await RefundableCrowdsale.new(this.startTime, this.endTime, rate, wallet, 0, { from: owner })
  34. .should.be.rejectedWith(EVMRevert);
  35. });
  36. });
  37. it('should deny refunds before end', async function () {
  38. await this.crowdsale.claimRefund({ from: investor }).should.be.rejectedWith(EVMRevert);
  39. await increaseTimeTo(this.startTime);
  40. await this.crowdsale.claimRefund({ from: investor }).should.be.rejectedWith(EVMRevert);
  41. });
  42. it('should deny refunds after end if goal was reached', async function () {
  43. await increaseTimeTo(this.startTime);
  44. await this.crowdsale.sendTransaction({ value: goal, from: investor });
  45. await increaseTimeTo(this.afterEndTime);
  46. await this.crowdsale.claimRefund({ from: investor }).should.be.rejectedWith(EVMRevert);
  47. });
  48. it('should allow refunds after end if goal was not reached', async function () {
  49. await increaseTimeTo(this.startTime);
  50. await this.crowdsale.sendTransaction({ value: lessThanGoal, from: investor });
  51. await increaseTimeTo(this.afterEndTime);
  52. await this.crowdsale.finalize({ from: owner });
  53. const pre = web3.eth.getBalance(investor);
  54. await this.crowdsale.claimRefund({ from: investor, gasPrice: 0 })
  55. .should.be.fulfilled;
  56. const post = web3.eth.getBalance(investor);
  57. post.minus(pre).should.be.bignumber.equal(lessThanGoal);
  58. });
  59. it('should forward funds to wallet after end if goal was reached', async function () {
  60. await increaseTimeTo(this.startTime);
  61. await this.crowdsale.sendTransaction({ value: goal, from: investor });
  62. await increaseTimeTo(this.afterEndTime);
  63. const pre = web3.eth.getBalance(wallet);
  64. await this.crowdsale.finalize({ from: owner });
  65. const post = web3.eth.getBalance(wallet);
  66. post.minus(pre).should.be.bignumber.equal(goal);
  67. });
  68. });