IndividuallyCappedCrowdsale.test.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 CappedCrowdsale = artifacts.require('IndividuallyCappedCrowdsaleImpl');
  9. const SimpleToken = artifacts.require('SimpleToken');
  10. contract('IndividuallyCappedCrowdsale', function ([_, wallet, alice, bob, charlie]) {
  11. const rate = new BigNumber(1);
  12. const capAlice = ether(10);
  13. const capBob = ether(2);
  14. const lessThanCapAlice = ether(6);
  15. const lessThanCapBoth = ether(1);
  16. const tokenSupply = new BigNumber('1e22');
  17. describe('individual capping', function () {
  18. beforeEach(async function () {
  19. this.token = await SimpleToken.new();
  20. this.crowdsale = await CappedCrowdsale.new(rate, wallet, this.token.address);
  21. await this.crowdsale.setUserCap(alice, capAlice);
  22. await this.crowdsale.setUserCap(bob, capBob);
  23. await this.token.transfer(this.crowdsale.address, tokenSupply);
  24. });
  25. describe('accepting payments', function () {
  26. it('should accept payments within cap', async function () {
  27. await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice });
  28. await this.crowdsale.buyTokens(bob, { value: lessThanCapBoth });
  29. });
  30. it('should reject payments outside cap', async function () {
  31. await this.crowdsale.buyTokens(alice, { value: capAlice });
  32. await expectThrow(this.crowdsale.buyTokens(alice, { value: 1 }), EVMRevert);
  33. });
  34. it('should reject payments that exceed cap', async function () {
  35. await expectThrow(this.crowdsale.buyTokens(alice, { value: capAlice.plus(1) }), EVMRevert);
  36. await expectThrow(this.crowdsale.buyTokens(bob, { value: capBob.plus(1) }), EVMRevert);
  37. });
  38. it('should manage independent caps', async function () {
  39. await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice });
  40. await expectThrow(this.crowdsale.buyTokens(bob, { value: lessThanCapAlice }), EVMRevert);
  41. });
  42. it('should default to a cap of zero', async function () {
  43. await expectThrow(this.crowdsale.buyTokens(charlie, { value: lessThanCapBoth }), EVMRevert);
  44. });
  45. });
  46. describe('reporting state', function () {
  47. it('should report correct cap', async function () {
  48. const retrievedCap = await this.crowdsale.getUserCap(alice);
  49. retrievedCap.should.be.bignumber.equal(capAlice);
  50. });
  51. it('should report actual contribution', async function () {
  52. await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice });
  53. const retrievedContribution = await this.crowdsale.getUserContribution(alice);
  54. retrievedContribution.should.be.bignumber.equal(lessThanCapAlice);
  55. });
  56. });
  57. });
  58. describe('group capping', function () {
  59. beforeEach(async function () {
  60. this.token = await SimpleToken.new();
  61. this.crowdsale = await CappedCrowdsale.new(rate, wallet, this.token.address);
  62. await this.crowdsale.setGroupCap([bob, charlie], capBob);
  63. await this.token.transfer(this.crowdsale.address, tokenSupply);
  64. });
  65. describe('accepting payments', function () {
  66. it('should accept payments within cap', async function () {
  67. await this.crowdsale.buyTokens(bob, { value: lessThanCapBoth });
  68. await this.crowdsale.buyTokens(charlie, { value: lessThanCapBoth });
  69. });
  70. it('should reject payments outside cap', async function () {
  71. await this.crowdsale.buyTokens(bob, { value: capBob });
  72. await expectThrow(this.crowdsale.buyTokens(bob, { value: 1 }), EVMRevert);
  73. await this.crowdsale.buyTokens(charlie, { value: capBob });
  74. await expectThrow(this.crowdsale.buyTokens(charlie, { value: 1 }), EVMRevert);
  75. });
  76. it('should reject payments that exceed cap', async function () {
  77. await expectThrow(this.crowdsale.buyTokens(bob, { value: capBob.plus(1) }), EVMRevert);
  78. await expectThrow(this.crowdsale.buyTokens(charlie, { value: capBob.plus(1) }), EVMRevert);
  79. });
  80. });
  81. describe('reporting state', function () {
  82. it('should report correct cap', async function () {
  83. const retrievedCapBob = await this.crowdsale.getUserCap(bob);
  84. retrievedCapBob.should.be.bignumber.equal(capBob);
  85. const retrievedCapCharlie = await this.crowdsale.getUserCap(charlie);
  86. retrievedCapCharlie.should.be.bignumber.equal(capBob);
  87. });
  88. });
  89. });
  90. });