AllowanceCrowdsale.test.js 3.4 KB

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