IndividuallyCappedCrowdsale.test.js 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. const { BN, ether, shouldFail } = require('openzeppelin-test-helpers');
  2. const IndividuallyCappedCrowdsaleImpl = artifacts.require('IndividuallyCappedCrowdsaleImpl');
  3. const SimpleToken = artifacts.require('SimpleToken');
  4. const { shouldBehaveLikePublicRole } = require('../behaviors/access/roles/PublicRole.behavior');
  5. contract('IndividuallyCappedCrowdsale', function (
  6. [_, capper, otherCapper, wallet, alice, bob, charlie, other, ...otherAccounts]) {
  7. const rate = new BN(1);
  8. const capAlice = ether('10');
  9. const capBob = ether('2');
  10. const lessThanCapAlice = ether('6');
  11. const lessThanCapBoth = ether('1');
  12. const tokenSupply = new BN('10').pow(new BN('22'));
  13. beforeEach(async function () {
  14. this.token = await SimpleToken.new();
  15. this.crowdsale = await IndividuallyCappedCrowdsaleImpl.new(rate, wallet, this.token.address, { from: capper });
  16. });
  17. describe('capper role', function () {
  18. beforeEach(async function () {
  19. this.contract = this.crowdsale;
  20. await this.contract.addCapper(otherCapper, { from: capper });
  21. });
  22. shouldBehaveLikePublicRole(capper, otherCapper, otherAccounts, 'capper');
  23. });
  24. describe('individual caps', function () {
  25. it('sets a cap when the sender is a capper', async function () {
  26. await this.crowdsale.setCap(alice, capAlice, { from: capper });
  27. (await this.crowdsale.getCap(alice)).should.be.bignumber.equal(capAlice);
  28. });
  29. it('reverts when a non-capper sets a cap', async function () {
  30. await shouldFail.reverting.withMessage(this.crowdsale.setCap(alice, capAlice, { from: other }),
  31. 'CapperRole: caller does not have the Capper role'
  32. );
  33. });
  34. context('with individual caps', function () {
  35. beforeEach(async function () {
  36. await this.crowdsale.setCap(alice, capAlice, { from: capper });
  37. await this.crowdsale.setCap(bob, capBob, { from: capper });
  38. await this.token.transfer(this.crowdsale.address, tokenSupply);
  39. });
  40. describe('accepting payments', function () {
  41. it('should accept payments within cap', async function () {
  42. await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice });
  43. await this.crowdsale.buyTokens(bob, { value: lessThanCapBoth });
  44. });
  45. it('should reject payments outside cap', async function () {
  46. await this.crowdsale.buyTokens(alice, { value: capAlice });
  47. await shouldFail.reverting.withMessage(this.crowdsale.buyTokens(alice, { value: 1 }),
  48. 'IndividuallyCappedCrowdsale: beneficiary\'s cap exceeded'
  49. );
  50. });
  51. it('should reject payments that exceed cap', async function () {
  52. await shouldFail.reverting.withMessage(this.crowdsale.buyTokens(alice, { value: capAlice.addn(1) }),
  53. 'IndividuallyCappedCrowdsale: beneficiary\'s cap exceeded'
  54. );
  55. await shouldFail.reverting.withMessage(this.crowdsale.buyTokens(bob, { value: capBob.addn(1) }),
  56. 'IndividuallyCappedCrowdsale: beneficiary\'s cap exceeded'
  57. );
  58. });
  59. it('should manage independent caps', async function () {
  60. await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice });
  61. await shouldFail.reverting.withMessage(this.crowdsale.buyTokens(bob, { value: lessThanCapAlice }),
  62. 'IndividuallyCappedCrowdsale: beneficiary\'s cap exceeded'
  63. );
  64. });
  65. it('should default to a cap of zero', async function () {
  66. await shouldFail.reverting.withMessage(this.crowdsale.buyTokens(charlie, { value: lessThanCapBoth }),
  67. 'IndividuallyCappedCrowdsale: beneficiary\'s cap exceeded'
  68. );
  69. });
  70. });
  71. describe('reporting state', function () {
  72. it('should report correct cap', async function () {
  73. (await this.crowdsale.getCap(alice)).should.be.bignumber.equal(capAlice);
  74. });
  75. it('should report actual contribution', async function () {
  76. await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice });
  77. (await this.crowdsale.getContribution(alice)).should.be.bignumber.equal(lessThanCapAlice);
  78. });
  79. });
  80. });
  81. });
  82. });