IndividuallyCappedCrowdsale.test.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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(this.crowdsale.setCap(alice, capAlice, { from: other }));
  31. });
  32. context('with individual caps', function () {
  33. beforeEach(async function () {
  34. await this.crowdsale.setCap(alice, capAlice, { from: capper });
  35. await this.crowdsale.setCap(bob, capBob, { from: capper });
  36. await this.token.transfer(this.crowdsale.address, tokenSupply);
  37. });
  38. describe('accepting payments', function () {
  39. it('should accept payments within cap', async function () {
  40. await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice });
  41. await this.crowdsale.buyTokens(bob, { value: lessThanCapBoth });
  42. });
  43. it('should reject payments outside cap', async function () {
  44. await this.crowdsale.buyTokens(alice, { value: capAlice });
  45. await shouldFail.reverting(this.crowdsale.buyTokens(alice, { value: 1 }));
  46. });
  47. it('should reject payments that exceed cap', async function () {
  48. await shouldFail.reverting(this.crowdsale.buyTokens(alice, { value: capAlice.addn(1) }));
  49. await shouldFail.reverting(this.crowdsale.buyTokens(bob, { value: capBob.addn(1) }));
  50. });
  51. it('should manage independent caps', async function () {
  52. await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice });
  53. await shouldFail.reverting(this.crowdsale.buyTokens(bob, { value: lessThanCapAlice }));
  54. });
  55. it('should default to a cap of zero', async function () {
  56. await shouldFail.reverting(this.crowdsale.buyTokens(charlie, { value: lessThanCapBoth }));
  57. });
  58. });
  59. describe('reporting state', function () {
  60. it('should report correct cap', async function () {
  61. (await this.crowdsale.getCap(alice)).should.be.bignumber.equal(capAlice);
  62. });
  63. it('should report actual contribution', async function () {
  64. await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice });
  65. (await this.crowdsale.getContribution(alice)).should.be.bignumber.equal(lessThanCapAlice);
  66. });
  67. });
  68. });
  69. });
  70. });