Crowdsale.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. const { balance, BN, constants, ether, expectEvent, shouldFail } = require('openzeppelin-test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const Crowdsale = artifacts.require('CrowdsaleMock');
  4. const SimpleToken = artifacts.require('SimpleToken');
  5. contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
  6. const rate = new BN(1);
  7. const value = ether('42');
  8. const tokenSupply = new BN('10').pow(new BN('22'));
  9. const expectedTokenAmount = rate.mul(value);
  10. it('requires a non-null token', async function () {
  11. await shouldFail.reverting(
  12. Crowdsale.new(rate, wallet, ZERO_ADDRESS)
  13. );
  14. });
  15. context('with token', async function () {
  16. beforeEach(async function () {
  17. this.token = await SimpleToken.new();
  18. });
  19. it('requires a non-zero rate', async function () {
  20. await shouldFail.reverting(
  21. Crowdsale.new(0, wallet, this.token.address)
  22. );
  23. });
  24. it('requires a non-null wallet', async function () {
  25. await shouldFail.reverting(
  26. Crowdsale.new(rate, ZERO_ADDRESS, this.token.address)
  27. );
  28. });
  29. context('once deployed', async function () {
  30. beforeEach(async function () {
  31. this.crowdsale = await Crowdsale.new(rate, wallet, this.token.address);
  32. await this.token.transfer(this.crowdsale.address, tokenSupply);
  33. });
  34. describe('accepting payments', function () {
  35. describe('bare payments', function () {
  36. it('should accept payments', async function () {
  37. await this.crowdsale.send(value, { from: purchaser });
  38. });
  39. it('reverts on zero-valued payments', async function () {
  40. await shouldFail.reverting(
  41. this.crowdsale.send(0, { from: purchaser })
  42. );
  43. });
  44. });
  45. describe('buyTokens', function () {
  46. it('should accept payments', async function () {
  47. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  48. });
  49. it('reverts on zero-valued payments', async function () {
  50. await shouldFail.reverting(
  51. this.crowdsale.buyTokens(investor, { value: 0, from: purchaser })
  52. );
  53. });
  54. it('requires a non-null beneficiary', async function () {
  55. await shouldFail.reverting(
  56. this.crowdsale.buyTokens(ZERO_ADDRESS, { value: value, from: purchaser })
  57. );
  58. });
  59. });
  60. });
  61. describe('high-level purchase', function () {
  62. it('should log purchase', async function () {
  63. const { logs } = await this.crowdsale.sendTransaction({ value: value, from: investor });
  64. expectEvent.inLogs(logs, 'TokensPurchased', {
  65. purchaser: investor,
  66. beneficiary: investor,
  67. value: value,
  68. amount: expectedTokenAmount,
  69. });
  70. });
  71. it('should assign tokens to sender', async function () {
  72. await this.crowdsale.sendTransaction({ value: value, from: investor });
  73. (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
  74. });
  75. it('should forward funds to wallet', async function () {
  76. (await balance.difference(wallet, () =>
  77. this.crowdsale.sendTransaction({ value, from: investor }))
  78. ).should.be.bignumber.equal(value);
  79. });
  80. });
  81. describe('low-level purchase', function () {
  82. it('should log purchase', async function () {
  83. const { logs } = await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  84. expectEvent.inLogs(logs, 'TokensPurchased', {
  85. purchaser: purchaser,
  86. beneficiary: investor,
  87. value: value,
  88. amount: expectedTokenAmount,
  89. });
  90. });
  91. it('should assign tokens to beneficiary', async function () {
  92. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  93. (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
  94. });
  95. it('should forward funds to wallet', async function () {
  96. (await balance.difference(wallet, () =>
  97. this.crowdsale.buyTokens(investor, { value, from: purchaser }))
  98. ).should.be.bignumber.equal(value);
  99. });
  100. });
  101. });
  102. });
  103. });