IndividuallyCappedCrowdsale.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. (await this.crowdsale.getUserCap(alice)).should.be.bignumber.equal(capAlice);
  49. });
  50. it('should report actual contribution', async function () {
  51. await this.crowdsale.buyTokens(alice, { value: lessThanCapAlice });
  52. (await this.crowdsale.getUserContribution(alice)).should.be.bignumber.equal(lessThanCapAlice);
  53. });
  54. });
  55. });
  56. describe('group capping', function () {
  57. beforeEach(async function () {
  58. this.token = await SimpleToken.new();
  59. this.crowdsale = await CappedCrowdsale.new(rate, wallet, this.token.address);
  60. await this.crowdsale.setGroupCap([bob, charlie], capBob);
  61. await this.token.transfer(this.crowdsale.address, tokenSupply);
  62. });
  63. describe('accepting payments', function () {
  64. it('should accept payments within cap', async function () {
  65. await this.crowdsale.buyTokens(bob, { value: lessThanCapBoth });
  66. await this.crowdsale.buyTokens(charlie, { value: lessThanCapBoth });
  67. });
  68. it('should reject payments outside cap', async function () {
  69. await this.crowdsale.buyTokens(bob, { value: capBob });
  70. await expectThrow(this.crowdsale.buyTokens(bob, { value: 1 }), EVMRevert);
  71. await this.crowdsale.buyTokens(charlie, { value: capBob });
  72. await expectThrow(this.crowdsale.buyTokens(charlie, { value: 1 }), EVMRevert);
  73. });
  74. it('should reject payments that exceed cap', async function () {
  75. await expectThrow(this.crowdsale.buyTokens(bob, { value: capBob.plus(1) }), EVMRevert);
  76. await expectThrow(this.crowdsale.buyTokens(charlie, { value: capBob.plus(1) }), EVMRevert);
  77. });
  78. });
  79. describe('reporting state', function () {
  80. it('should report correct cap', async function () {
  81. (await this.crowdsale.getUserCap(bob)).should.be.bignumber.equal(capBob);
  82. (await this.crowdsale.getUserCap(charlie)).should.be.bignumber.equal(capBob);
  83. });
  84. });
  85. });
  86. });