Crowdsale.test.js 4.6 KB

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