IndividuallyCappedCrowdsale.test.js 4.0 KB

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