RefundableCrowdsale.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. describe('creating a valid crowdsale', function () {
  15. it('should fail with zero goal', async function () {
  16. await RefundableCrowdsale.new(this.startBlock, this.endBlock, rate, wallet, 0, {from: owner}).should.be.rejectedWith(EVMThrow);
  17. })
  18. });
  19. beforeEach(async function () {
  20. this.startBlock = web3.eth.blockNumber + 10
  21. this.endBlock = web3.eth.blockNumber + 20
  22. this.crowdsale = await RefundableCrowdsale.new(this.startBlock, this.endBlock, rate, wallet, goal, {from: owner})
  23. })
  24. it('should deny refunds before end', async function () {
  25. await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow)
  26. await advanceToBlock(this.endBlock - 1)
  27. await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow)
  28. })
  29. it('should deny refunds after end if goal was reached', async function () {
  30. await advanceToBlock(this.startBlock - 1)
  31. await this.crowdsale.sendTransaction({value: goal, from: investor})
  32. await advanceToBlock(this.endBlock)
  33. await this.crowdsale.claimRefund({from: investor}).should.be.rejectedWith(EVMThrow)
  34. })
  35. it('should allow refunds after end if goal was not reached', async function () {
  36. await advanceToBlock(this.startBlock - 1)
  37. await this.crowdsale.sendTransaction({value: lessThanGoal, from: investor})
  38. await advanceToBlock(this.endBlock)
  39. await this.crowdsale.finalize({from: owner})
  40. const pre = web3.eth.getBalance(investor)
  41. await this.crowdsale.claimRefund({from: investor, gasPrice: 0})
  42. .should.be.fulfilled
  43. const post = web3.eth.getBalance(investor)
  44. post.minus(pre).should.be.bignumber.equal(lessThanGoal)
  45. })
  46. it('should forward funds to wallet after end if goal was reached', async function () {
  47. await advanceToBlock(this.startBlock - 1)
  48. await this.crowdsale.sendTransaction({value: goal, from: investor})
  49. await advanceToBlock(this.endBlock)
  50. const pre = web3.eth.getBalance(wallet)
  51. await this.crowdsale.finalize({from: owner})
  52. const post = web3.eth.getBalance(wallet)
  53. post.minus(pre).should.be.bignumber.equal(goal)
  54. })
  55. })