Crowdsale.test.js 4.7 KB

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