IndividuallyCappedCrowdsale.test.js 4.1 KB

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