RefundableCrowdsale.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import ether from './helpers/ether'
  2. import advanceToBlock from './helpers/advanceToBlock'
  3. import EVMThrow from './helpers/EVMThrow'
  4. const BigNumber = web3.BigNumber
  5. require('chai')
  6. .use(require('chai-as-promised'))
  7. .use(require('chai-bignumber')(BigNumber))
  8. .should()
  9. const RefundableCrowdsale = artifacts.require('./helpers/RefundableCrowdsaleImpl.sol')
  10. contract('RefundableCrowdsale', function ([_, owner, wallet, investor]) {
  11. const rate = new BigNumber(1000)
  12. const goal = ether(800)
  13. const lessThanGoal = ether(750)
  14. beforeEach(async function () {
  15. this.startBlock = web3.eth.blockNumber + 10
  16. this.endBlock = web3.eth.blockNumber + 20
  17. this.crowdsale = await RefundableCrowdsale.new(this.startBlock, this.endBlock, rate, wallet, goal, {from: owner})
  18. })
  19. it('should deny refunds before end', async function () {
  20. await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow)
  21. await advanceToBlock(this.endBlock - 1)
  22. await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow)
  23. })
  24. it('should deny refunds after end if goal was reached', async function () {
  25. await advanceToBlock(this.startBlock - 1)
  26. await this.crowdsale.sendTransaction({value: goal, from: investor})
  27. await advanceToBlock(this.endBlock)
  28. await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow)
  29. })
  30. it('should allow refunds after end if goal was not reached', async function () {
  31. await advanceToBlock(this.startBlock - 1)
  32. await this.crowdsale.sendTransaction({value: lessThanGoal, from: investor})
  33. await advanceToBlock(this.endBlock)
  34. await this.crowdsale.finalize({from: owner})
  35. const pre = web3.eth.getBalance(investor)
  36. await this.crowdsale.claimRefund({from: investor, gasPrice: 0})
  37. .should.be.fulfilled
  38. const post = web3.eth.getBalance(investor)
  39. post.minus(pre).should.be.bignumber.equal(lessThanGoal)
  40. })
  41. it('should forward funds to wallet after end if goal was reached', async function () {
  42. await advanceToBlock(this.startBlock - 1)
  43. await this.crowdsale.sendTransaction({value: goal, from: investor})
  44. await advanceToBlock(this.endBlock)
  45. const pre = web3.eth.getBalance(wallet)
  46. await this.crowdsale.finalize({from: owner})
  47. const post = web3.eth.getBalance(wallet)
  48. post.minus(pre).should.be.bignumber.equal(goal)
  49. })
  50. })