IndividuallyCappedCrowdsale.test.js 3.7 KB

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