AllowanceCrowdsale.test.js 3.4 KB

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