WhitelistedCrowdsale.test.js 4.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const { ether } = require('../helpers/ether');
  2. const { expectThrow } = require('../helpers/expectThrow');
  3. const BigNumber = web3.BigNumber;
  4. require('chai')
  5. .should();
  6. const WhitelistedCrowdsale = artifacts.require('WhitelistedCrowdsaleImpl');
  7. const SimpleToken = artifacts.require('SimpleToken');
  8. contract('WhitelistedCrowdsale', function ([_, wallet, authorized, unauthorized, anotherAuthorized]) {
  9. const rate = 1;
  10. const value = ether(42);
  11. const tokenSupply = new BigNumber('1e22');
  12. describe('single user whitelisting', function () {
  13. beforeEach(async function () {
  14. this.token = await SimpleToken.new();
  15. this.crowdsale = await WhitelistedCrowdsale.new(rate, wallet, this.token.address);
  16. await this.token.transfer(this.crowdsale.address, tokenSupply);
  17. await this.crowdsale.addAddressToWhitelist(authorized);
  18. });
  19. describe('accepting payments', function () {
  20. it('should accept payments to whitelisted (from whichever buyers)', async function () {
  21. await this.crowdsale.sendTransaction({ value, from: authorized });
  22. await this.crowdsale.buyTokens(authorized, { value: value, from: authorized });
  23. await this.crowdsale.buyTokens(authorized, { value: value, from: unauthorized });
  24. });
  25. it('should reject payments to not whitelisted (from whichever buyers)', async function () {
  26. await expectThrow(this.crowdsale.sendTransaction({ value, from: unauthorized }));
  27. await expectThrow(this.crowdsale.buyTokens(unauthorized, { value: value, from: unauthorized }));
  28. await expectThrow(this.crowdsale.buyTokens(unauthorized, { value: value, from: authorized }));
  29. });
  30. it('should reject payments to addresses removed from whitelist', async function () {
  31. await this.crowdsale.removeAddressFromWhitelist(authorized);
  32. await expectThrow(this.crowdsale.buyTokens(authorized, { value: value, from: authorized }));
  33. });
  34. });
  35. describe('reporting whitelisted', function () {
  36. it('should correctly report whitelisted addresses', async function () {
  37. const isAuthorized = await this.crowdsale.whitelist(authorized);
  38. isAuthorized.should.equal(true);
  39. const isntAuthorized = await this.crowdsale.whitelist(unauthorized);
  40. isntAuthorized.should.equal(false);
  41. });
  42. });
  43. });
  44. describe('many user whitelisting', function () {
  45. beforeEach(async function () {
  46. this.token = await SimpleToken.new();
  47. this.crowdsale = await WhitelistedCrowdsale.new(rate, wallet, this.token.address);
  48. await this.token.transfer(this.crowdsale.address, tokenSupply);
  49. await this.crowdsale.addAddressesToWhitelist([authorized, anotherAuthorized]);
  50. });
  51. describe('accepting payments', function () {
  52. it('should accept payments to whitelisted (from whichever buyers)', async function () {
  53. await this.crowdsale.buyTokens(authorized, { value: value, from: authorized });
  54. await this.crowdsale.buyTokens(authorized, { value: value, from: unauthorized });
  55. await this.crowdsale.buyTokens(anotherAuthorized, { value: value, from: authorized });
  56. await this.crowdsale.buyTokens(anotherAuthorized, { value: value, from: unauthorized });
  57. });
  58. it('should reject payments to not whitelisted (with whichever buyers)', async function () {
  59. await expectThrow(this.crowdsale.send(value));
  60. await expectThrow(this.crowdsale.buyTokens(unauthorized, { value: value, from: unauthorized }));
  61. await expectThrow(this.crowdsale.buyTokens(unauthorized, { value: value, from: authorized }));
  62. });
  63. it('should reject payments to addresses removed from whitelist', async function () {
  64. await this.crowdsale.removeAddressFromWhitelist(anotherAuthorized);
  65. await this.crowdsale.buyTokens(authorized, { value: value, from: authorized });
  66. await expectThrow(this.crowdsale.buyTokens(anotherAuthorized, { value: value, from: authorized }));
  67. });
  68. });
  69. describe('reporting whitelisted', function () {
  70. it('should correctly report whitelisted addresses', async function () {
  71. const isAuthorized = await this.crowdsale.whitelist(authorized);
  72. isAuthorized.should.equal(true);
  73. const isAnotherAuthorized = await this.crowdsale.whitelist(anotherAuthorized);
  74. isAnotherAuthorized.should.equal(true);
  75. const isntAuthorized = await this.crowdsale.whitelist(unauthorized);
  76. isntAuthorized.should.equal(false);
  77. });
  78. });
  79. });
  80. });