Crowdsale.test.js 4.9 KB

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