CappedCrowdsale.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 CappedCrowdsale = artifacts.require('./helpers/CappedCrowdsaleImpl.sol')
  13. const MintableToken = artifacts.require('MintableToken')
  14. contract('CappedCrowdsale', function ([_, wallet]) {
  15. const rate = new BigNumber(1000)
  16. const cap = ether(300)
  17. const lessThanCap = ether(60)
  18. before(async function() {
  19. //Advance to the next block to correctly read time in the solidity "now" function interpreted by testrpc
  20. await advanceToBlock(web3.eth.getBlock('latest').number + 1)
  21. })
  22. beforeEach(async function () {
  23. this.startTime = latestTime().unix() + moment.duration(1, 'week').asSeconds();
  24. this.endTime = latestTime().unix() + moment.duration(2, 'week').asSeconds();
  25. this.crowdsale = await CappedCrowdsale.new(this.startTime, this.endTime, rate, wallet, cap)
  26. this.token = MintableToken.at(await this.crowdsale.token())
  27. })
  28. describe('creating a valid crowdsale', function () {
  29. it('should fail with zero cap', async function () {
  30. await CappedCrowdsale.new(this.startTime, this.endTime, rate, wallet, 0).should.be.rejectedWith(EVMThrow);
  31. })
  32. });
  33. describe('accepting payments', function () {
  34. beforeEach(async function () {
  35. await increaseTime(moment.duration(1, 'week'))
  36. })
  37. it('should accept payments within cap', async function () {
  38. await this.crowdsale.send(cap.minus(lessThanCap)).should.be.fulfilled
  39. await this.crowdsale.send(lessThanCap).should.be.fulfilled
  40. })
  41. it('should reject payments outside cap', async function () {
  42. await this.crowdsale.send(cap)
  43. await this.crowdsale.send(1).should.be.rejectedWith(EVMThrow)
  44. })
  45. it('should reject payments that exceed cap', async function () {
  46. await this.crowdsale.send(cap.plus(1)).should.be.rejectedWith(EVMThrow)
  47. })
  48. })
  49. describe('ending', function () {
  50. beforeEach(async function () {
  51. await increaseTime(moment.duration(1, 'week'))
  52. })
  53. it('should not be ended if under cap', async function () {
  54. let hasEnded = await this.crowdsale.hasEnded()
  55. hasEnded.should.equal(false)
  56. await this.crowdsale.send(lessThanCap)
  57. hasEnded = await this.crowdsale.hasEnded()
  58. hasEnded.should.equal(false)
  59. })
  60. it('should not be ended if just under cap', async function () {
  61. await this.crowdsale.send(cap.minus(1))
  62. let hasEnded = await this.crowdsale.hasEnded()
  63. hasEnded.should.equal(false)
  64. })
  65. it('should be ended if cap reached', async function () {
  66. await this.crowdsale.send(cap)
  67. let hasEnded = await this.crowdsale.hasEnded()
  68. hasEnded.should.equal(true)
  69. })
  70. })
  71. })