WhitelistedCrowdsale.test.js 4.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. const { ether } = require('../helpers/ether');
  2. const BigNumber = web3.BigNumber;
  3. require('chai')
  4. .use(require('chai-as-promised'))
  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 }).should.be.fulfilled;
  22. await this.crowdsale.buyTokens(authorized, { value: value, from: authorized }).should.be.fulfilled;
  23. await this.crowdsale.buyTokens(authorized, { value: value, from: unauthorized }).should.be.fulfilled;
  24. });
  25. it('should reject payments to not whitelisted (from whichever buyers)', async function () {
  26. await this.crowdsale.sendTransaction({ value, from: unauthorized }).should.be.rejected;
  27. await this.crowdsale.buyTokens(unauthorized, { value: value, from: unauthorized }).should.be.rejected;
  28. await this.crowdsale.buyTokens(unauthorized, { value: value, from: authorized }).should.be.rejected;
  29. });
  30. it('should reject payments to addresses removed from whitelist', async function () {
  31. await this.crowdsale.removeAddressFromWhitelist(authorized);
  32. await this.crowdsale.buyTokens(authorized, { value: value, from: authorized }).should.be.rejected;
  33. });
  34. });
  35. describe('reporting whitelisted', function () {
  36. it('should correctly report whitelisted addresses', async function () {
  37. let isAuthorized = await this.crowdsale.whitelist(authorized);
  38. isAuthorized.should.equal(true);
  39. let 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 }).should.be.fulfilled;
  54. await this.crowdsale.buyTokens(authorized, { value: value, from: unauthorized }).should.be.fulfilled;
  55. await this.crowdsale.buyTokens(anotherAuthorized, { value: value, from: authorized }).should.be.fulfilled;
  56. await this.crowdsale.buyTokens(anotherAuthorized, { value: value, from: unauthorized }).should.be.fulfilled;
  57. });
  58. it('should reject payments to not whitelisted (with whichever buyers)', async function () {
  59. await this.crowdsale.send(value).should.be.rejected;
  60. await this.crowdsale.buyTokens(unauthorized, { value: value, from: unauthorized }).should.be.rejected;
  61. await this.crowdsale.buyTokens(unauthorized, { value: value, from: authorized }).should.be.rejected;
  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 }).should.be.fulfilled;
  66. await this.crowdsale.buyTokens(anotherAuthorized, { value: value, from: authorized }).should.be.rejected;
  67. });
  68. });
  69. describe('reporting whitelisted', function () {
  70. it('should correctly report whitelisted addresses', async function () {
  71. let isAuthorized = await this.crowdsale.whitelist(authorized);
  72. isAuthorized.should.equal(true);
  73. let isAnotherAuthorized = await this.crowdsale.whitelist(anotherAuthorized);
  74. isAnotherAuthorized.should.equal(true);
  75. let isntAuthorized = await this.crowdsale.whitelist(unauthorized);
  76. isntAuthorized.should.equal(false);
  77. });
  78. });
  79. });
  80. });