AllowanceCrowdsale.test.js 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. const expectEvent = require('../helpers/expectEvent');
  2. const { ether } = require('../helpers/ether');
  3. const shouldFail = require('../helpers/shouldFail');
  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 AllowanceCrowdsaleImpl = artifacts.require('AllowanceCrowdsaleImpl');
  11. const SimpleToken = artifacts.require('SimpleToken');
  12. contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenWallet]) {
  13. const rate = new BigNumber(1);
  14. const value = ether(0.42);
  15. const expectedTokenAmount = rate.mul(value);
  16. const tokenAllowance = new BigNumber('1e22');
  17. beforeEach(async function () {
  18. this.token = await SimpleToken.new({ from: tokenWallet });
  19. this.crowdsale = await AllowanceCrowdsaleImpl.new(rate, wallet, this.token.address, tokenWallet);
  20. await this.token.approve(this.crowdsale.address, tokenAllowance, { from: tokenWallet });
  21. });
  22. describe('accepting payments', function () {
  23. it('should have token wallet', async function () {
  24. (await this.crowdsale.tokenWallet()).should.be.equal(tokenWallet);
  25. });
  26. it('should accept sends', async function () {
  27. await this.crowdsale.send(value);
  28. });
  29. it('should accept payments', async function () {
  30. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  31. });
  32. });
  33. describe('high-level purchase', function () {
  34. it('should log purchase', async function () {
  35. const { logs } = await this.crowdsale.sendTransaction({ value: value, from: investor });
  36. expectEvent.inLogs(logs, 'TokensPurchased', {
  37. purchaser: investor,
  38. beneficiary: investor,
  39. value: value,
  40. amount: expectedTokenAmount,
  41. });
  42. });
  43. it('should assign tokens to sender', async function () {
  44. await this.crowdsale.sendTransaction({ value: value, from: investor });
  45. (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
  46. });
  47. it('should forward funds to wallet', async function () {
  48. const pre = await ethGetBalance(wallet);
  49. await this.crowdsale.sendTransaction({ value, from: investor });
  50. const post = await ethGetBalance(wallet);
  51. post.minus(pre).should.be.bignumber.equal(value);
  52. });
  53. });
  54. describe('check remaining allowance', function () {
  55. it('should report correct allowace left', async function () {
  56. const remainingAllowance = tokenAllowance - expectedTokenAmount;
  57. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  58. (await this.crowdsale.remainingTokens()).should.be.bignumber.equal(remainingAllowance);
  59. });
  60. context('when the allowance is larger than the token amount', function () {
  61. beforeEach(async function () {
  62. const amount = await this.token.balanceOf(tokenWallet);
  63. await this.token.approve(this.crowdsale.address, amount.plus(1), { from: tokenWallet });
  64. });
  65. it('should report the amount instead of the allowance', async function () {
  66. (await this.crowdsale.remainingTokens()).should.be.bignumber.equal(await this.token.balanceOf(tokenWallet));
  67. });
  68. });
  69. });
  70. describe('when token wallet is different from token address', function () {
  71. it('creation reverts', async function () {
  72. this.token = await SimpleToken.new({ from: tokenWallet });
  73. await shouldFail.reverting(AllowanceCrowdsaleImpl.new(rate, wallet, this.token.address, ZERO_ADDRESS));
  74. });
  75. });
  76. });