RefundableCrowdsale.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 EVMThrow from './helpers/EVMThrow'
  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('./helpers/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}).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 increaseTimeTo(this.startTime)
  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 increaseTimeTo(this.startTime)
  38. await this.crowdsale.sendTransaction({value: goal, from: investor})
  39. await increaseTimeTo(this.afterEndTime)
  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 increaseTimeTo(this.startTime)
  44. await this.crowdsale.sendTransaction({value: lessThanGoal, from: investor})
  45. await increaseTimeTo(this.afterEndTime)
  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 increaseTimeTo(this.startTime)
  55. await this.crowdsale.sendTransaction({value: goal, from: investor})
  56. await increaseTimeTo(this.afterEndTime)
  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. })