CappedCrowdsale.test.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 EVMRevert from '../helpers/EVMRevert';
  6. const BigNumber = web3.BigNumber;
  7. require('chai')
  8. .use(require('chai-as-promised'))
  9. .use(require('chai-bignumber')(BigNumber))
  10. .should();
  11. const CappedCrowdsale = artifacts.require('CappedCrowdsaleImpl');
  12. const MintableToken = artifacts.require('MintableToken');
  13. contract('CappedCrowdsale', function ([_, wallet]) {
  14. const rate = new BigNumber(1000);
  15. const cap = ether(300);
  16. const lessThanCap = ether(60);
  17. before(async function () {
  18. // Advance to the next block to correctly read time in the solidity "now" function interpreted by testrpc
  19. await advanceBlock();
  20. });
  21. beforeEach(async function () {
  22. this.startTime = latestTime() + duration.weeks(1);
  23. this.endTime = this.startTime + duration.weeks(1);
  24. this.token = await MintableToken.new();
  25. this.crowdsale = await CappedCrowdsale.new(this.startTime, this.endTime, rate, wallet, cap, this.token.address);
  26. await this.token.transferOwnership(this.crowdsale.address);
  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(EVMRevert);
  31. });
  32. });
  33. describe('accepting payments', function () {
  34. beforeEach(async function () {
  35. await increaseTimeTo(this.startTime);
  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(EVMRevert);
  44. });
  45. it('should reject payments that exceed cap', async function () {
  46. await this.crowdsale.send(cap.plus(1)).should.be.rejectedWith(EVMRevert);
  47. });
  48. });
  49. describe('ending', function () {
  50. beforeEach(async function () {
  51. await increaseTimeTo(this.startTime);
  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. });