Crowdsale.test.js 4.6 KB

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