RefundableCrowdsale.test.js 3.0 KB

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