IndividuallyCappedCrowdsale.test.js 3.7 KB

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