WhitelistedCrowdsale.test.js 4.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. (await this.crowdsale.whitelist(authorized)).should.equal(true);
  38. (await this.crowdsale.whitelist(unauthorized)).should.equal(false);
  39. });
  40. });
  41. });
  42. describe('many user whitelisting', function () {
  43. beforeEach(async function () {
  44. this.token = await SimpleToken.new();
  45. this.crowdsale = await WhitelistedCrowdsale.new(rate, wallet, this.token.address);
  46. await this.token.transfer(this.crowdsale.address, tokenSupply);
  47. await this.crowdsale.addAddressesToWhitelist([authorized, anotherAuthorized]);
  48. });
  49. describe('accepting payments', function () {
  50. it('should accept payments to whitelisted (from whichever buyers)', async function () {
  51. await this.crowdsale.buyTokens(authorized, { value: value, from: authorized });
  52. await this.crowdsale.buyTokens(authorized, { value: value, from: unauthorized });
  53. await this.crowdsale.buyTokens(anotherAuthorized, { value: value, from: authorized });
  54. await this.crowdsale.buyTokens(anotherAuthorized, { value: value, from: unauthorized });
  55. });
  56. it('should reject payments to not whitelisted (with whichever buyers)', async function () {
  57. await expectThrow(this.crowdsale.send(value));
  58. await expectThrow(this.crowdsale.buyTokens(unauthorized, { value: value, from: unauthorized }));
  59. await expectThrow(this.crowdsale.buyTokens(unauthorized, { value: value, from: authorized }));
  60. });
  61. it('should reject payments to addresses removed from whitelist', async function () {
  62. await this.crowdsale.removeAddressFromWhitelist(anotherAuthorized);
  63. await this.crowdsale.buyTokens(authorized, { value: value, from: authorized });
  64. await expectThrow(this.crowdsale.buyTokens(anotherAuthorized, { value: value, from: authorized }));
  65. });
  66. });
  67. describe('reporting whitelisted', function () {
  68. it('should correctly report whitelisted addresses', async function () {
  69. (await this.crowdsale.whitelist(authorized)).should.equal(true);
  70. (await this.crowdsale.whitelist(anotherAuthorized)).should.equal(true);
  71. (await this.crowdsale.whitelist(unauthorized)).should.equal(false);
  72. });
  73. });
  74. });
  75. });