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('mocks/CappedCrowdsaleImpl.sol');
  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.crowdsale = await CappedCrowdsale.new(this.startTime, this.endTime, rate, wallet, cap);
  25. this.token = MintableToken.at(await this.crowdsale.token());
  26. });
  27. describe('creating a valid crowdsale', function () {
  28. it('should fail with zero cap', async function () {
  29. await CappedCrowdsale.new(this.startTime, this.endTime, rate, wallet, 0).should.be.rejectedWith(EVMRevert);
  30. });
  31. });
  32. describe('accepting payments', function () {
  33. beforeEach(async function () {
  34. await increaseTimeTo(this.startTime);
  35. });
  36. it('should accept payments within cap', async function () {
  37. await this.crowdsale.send(cap.minus(lessThanCap)).should.be.fulfilled;
  38. await this.crowdsale.send(lessThanCap).should.be.fulfilled;
  39. });
  40. it('should reject payments outside cap', async function () {
  41. await this.crowdsale.send(cap);
  42. await this.crowdsale.send(1).should.be.rejectedWith(EVMRevert);
  43. });
  44. it('should reject payments that exceed cap', async function () {
  45. await this.crowdsale.send(cap.plus(1)).should.be.rejectedWith(EVMRevert);
  46. });
  47. });
  48. describe('ending', function () {
  49. beforeEach(async function () {
  50. await increaseTimeTo(this.startTime);
  51. });
  52. it('should not be ended if under cap', async function () {
  53. let hasEnded = await this.crowdsale.hasEnded();
  54. hasEnded.should.equal(false);
  55. await this.crowdsale.send(lessThanCap);
  56. hasEnded = await this.crowdsale.hasEnded();
  57. hasEnded.should.equal(false);
  58. });
  59. it('should not be ended if just under cap', async function () {
  60. await this.crowdsale.send(cap.minus(1));
  61. let hasEnded = await this.crowdsale.hasEnded();
  62. hasEnded.should.equal(false);
  63. });
  64. it('should be ended if cap reached', async function () {
  65. await this.crowdsale.send(cap);
  66. let hasEnded = await this.crowdsale.hasEnded();
  67. hasEnded.should.equal(true);
  68. });
  69. });
  70. });