Crowdsale.test.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. const expectEvent = require('../helpers/expectEvent');
  2. const shouldFail = require('../helpers/shouldFail');
  3. const { balanceDifference } = require('../helpers/balanceDifference');
  4. const { ether } = require('../helpers/ether');
  5. const { ZERO_ADDRESS } = require('../helpers/constants');
  6. const { BigNumber } = require('../helpers/setup');
  7. const Crowdsale = artifacts.require('CrowdsaleMock');
  8. const SimpleToken = artifacts.require('SimpleToken');
  9. contract('Crowdsale', function ([_, investor, wallet, purchaser]) {
  10. const rate = new BigNumber(1);
  11. const value = ether(42);
  12. const tokenSupply = new BigNumber('1e22');
  13. const expectedTokenAmount = rate.mul(value);
  14. it('requires a non-null token', async function () {
  15. await shouldFail.reverting(
  16. Crowdsale.new(rate, wallet, 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 shouldFail.reverting(
  25. Crowdsale.new(0, wallet, this.token.address)
  26. );
  27. });
  28. it('requires a non-null wallet', async function () {
  29. await shouldFail.reverting(
  30. Crowdsale.new(rate, ZERO_ADDRESS, this.token.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 shouldFail.reverting(
  45. this.crowdsale.send(0, { from: purchaser })
  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 shouldFail.reverting(
  55. this.crowdsale.buyTokens(investor, { value: 0, from: purchaser })
  56. );
  57. });
  58. it('requires a non-null beneficiary', async function () {
  59. await shouldFail.reverting(
  60. this.crowdsale.buyTokens(ZERO_ADDRESS, { value: value, from: purchaser })
  61. );
  62. });
  63. });
  64. });
  65. describe('high-level purchase', function () {
  66. it('should log purchase', async function () {
  67. const { logs } = await this.crowdsale.sendTransaction({ value: value, from: investor });
  68. expectEvent.inLogs(logs, 'TokensPurchased', {
  69. purchaser: investor,
  70. beneficiary: investor,
  71. value: value,
  72. amount: expectedTokenAmount,
  73. });
  74. });
  75. it('should assign tokens to sender', async function () {
  76. await this.crowdsale.sendTransaction({ value: value, from: investor });
  77. (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
  78. });
  79. it('should forward funds to wallet', async function () {
  80. (await balanceDifference(wallet, () =>
  81. this.crowdsale.sendTransaction({ value, from: investor }))
  82. ).should.be.bignumber.equal(value);
  83. });
  84. });
  85. describe('low-level purchase', function () {
  86. it('should log purchase', async function () {
  87. const { logs } = await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  88. expectEvent.inLogs(logs, 'TokensPurchased', {
  89. purchaser: purchaser,
  90. beneficiary: investor,
  91. value: value,
  92. amount: expectedTokenAmount,
  93. });
  94. });
  95. it('should assign tokens to beneficiary', async function () {
  96. await this.crowdsale.buyTokens(investor, { value, from: purchaser });
  97. (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
  98. });
  99. it('should forward funds to wallet', async function () {
  100. (await balanceDifference(wallet, () =>
  101. this.crowdsale.buyTokens(investor, { value, from: purchaser }))
  102. ).should.be.bignumber.equal(value);
  103. });
  104. });
  105. });
  106. });
  107. });