RefundableCrowdsale.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import moment from 'moment'
  2. import ether from './helpers/ether'
  3. import advanceToBlock from './helpers/advanceToBlock'
  4. import increaseTime from './helpers/increaseTime'
  5. import latestTime from './helpers/latestTime'
  6. import EVMThrow from './helpers/EVMThrow'
  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('./helpers/RefundableCrowdsaleImpl.sol')
  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 advanceToBlock(web3.eth.getBlock('latest').number + 1)
  20. })
  21. beforeEach(async function () {
  22. this.startTime = latestTime().unix() + moment.duration(1, 'week').asSeconds();
  23. this.endTime = latestTime().unix() + moment.duration(2, 'week').asSeconds();
  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}).should.be.rejectedWith(EVMThrow);
  29. })
  30. });
  31. it('should deny refunds before end', async function () {
  32. await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow)
  33. await increaseTime(moment.duration(2, 'week'))
  34. await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow)
  35. })
  36. it('should deny refunds after end if goal was reached', async function () {
  37. await increaseTime(moment.duration(1, 'week'))
  38. await this.crowdsale.sendTransaction({value: goal, from: investor})
  39. await increaseTime(moment.duration(1.1, 'week'))
  40. await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow)
  41. })
  42. it('should allow refunds after end if goal was not reached', async function () {
  43. await increaseTime(moment.duration(1, 'week'))
  44. await this.crowdsale.sendTransaction({value: lessThanGoal, from: investor})
  45. await increaseTime(moment.duration(1.1, 'week'))
  46. await this.crowdsale.finalize({from: owner})
  47. const pre = web3.eth.getBalance(investor)
  48. await this.crowdsale.claimRefund({from: investor, gasPrice: 0})
  49. .should.be.fulfilled
  50. const post = web3.eth.getBalance(investor)
  51. post.minus(pre).should.be.bignumber.equal(lessThanGoal)
  52. })
  53. it('should forward funds to wallet after end if goal was reached', async function () {
  54. await increaseTime(moment.duration(1, 'week'))
  55. await this.crowdsale.sendTransaction({value: goal, from: investor})
  56. await increaseTime(moment.duration(1.1, 'week'))
  57. const pre = web3.eth.getBalance(wallet)
  58. await this.crowdsale.finalize({from: owner})
  59. const post = web3.eth.getBalance(wallet)
  60. post.minus(pre).should.be.bignumber.equal(goal)
  61. })
  62. })