AllowanceCrowdsale.test.js 3.4 KB

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