Crowdsale.test.js 4.6 KB

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