AllowanceCrowdsale.test.js 3.5 KB

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