IndividuallyCappedCrowdsale.test.js 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. const { ether } = require('../helpers/ether');
  2. const shouldFail = require('../helpers/shouldFail');
  3. const { BigNumber } = require('../helpers/setup');
  4. const IndividuallyCappedCrowdsaleImpl = artifacts.require('IndividuallyCappedCrowdsaleImpl');
  5. const SimpleToken = artifacts.require('SimpleToken');
  6. const { shouldBehaveLikePublicRole } = require('../access/roles/PublicRole.behavior');
  7. contract('IndividuallyCappedCrowdsale', function (
  8. [_, capper, otherCapper, wallet, alice, bob, charlie, anyone, ...otherAccounts]) {
  9. const rate = new BigNumber(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 BigNumber('1e22');
  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. (await this.crowdsale.getCap(alice)).should.be.bignumber.equal(capAlice);
  30. });
  31. it('reverts when a non-capper sets a cap', async function () {
  32. await shouldFail.reverting(this.crowdsale.setCap(alice, capAlice, { from: anyone }));
  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(this.crowdsale.buyTokens(alice, { value: 1 }));
  48. });
  49. it('should reject payments that exceed cap', async function () {
  50. await shouldFail.reverting(this.crowdsale.buyTokens(alice, { value: capAlice.plus(1) }));
  51. await shouldFail.reverting(this.crowdsale.buyTokens(bob, { value: capBob.plus(1) }));
  52. });
  53. it('should manage independent caps', async function () {
  54. await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice });
  55. await shouldFail.reverting(this.crowdsale.buyTokens(bob, { value: lessThanCapAlice }));
  56. });
  57. it('should default to a cap of zero', async function () {
  58. await shouldFail.reverting(this.crowdsale.buyTokens(charlie, { value: lessThanCapBoth }));
  59. });
  60. });
  61. describe('reporting state', function () {
  62. it('should report correct cap', async function () {
  63. (await this.crowdsale.getCap(alice)).should.be.bignumber.equal(capAlice);
  64. });
  65. it('should report actual contribution', async function () {
  66. await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice });
  67. (await this.crowdsale.getContribution(alice)).should.be.bignumber.equal(lessThanCapAlice);
  68. });
  69. });
  70. });
  71. });
  72. });