AllowanceCrowdsale.test.js 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const expectEvent = require('../helpers/expectEvent');
  2. const { ether } = require('../helpers/ether');
  3. const { assertRevert } = require('../helpers/assertRevert');
  4. const { ethGetBalance } = require('../helpers/web3');
  5. const BigNumber = web3.BigNumber;
  6. require('chai')
  7. .use(require('chai-bignumber')(BigNumber))
  8. .should();
  9. const AllowanceCrowdsaleImpl = artifacts.require('AllowanceCrowdsaleImpl');
  10. const SimpleToken = artifacts.require('SimpleToken');
  11. contract('AllowanceCrowdsale', function ([_, investor, wallet, purchaser, tokenWallet]) {
  12. const rate = new BigNumber(1);
  13. const value = ether(0.42);
  14. const expectedTokenAmount = rate.mul(value);
  15. const tokenAllowance = new BigNumber('1e22');
  16. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  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(
  37. logs,
  38. 'TokensPurchased',
  39. {
  40. purchaser: investor,
  41. beneficiary: investor,
  42. value: value,
  43. amount: expectedTokenAmount });
  44. });
  45. it('should assign tokens to sender', async function () {
  46. await this.crowdsale.sendTransaction({ value: value, from: investor });
  47. (await this.token.balanceOf(investor)).should.be.bignumber.equal(expectedTokenAmount);
  48. });
  49. it('should forward funds to wallet', async function () {
  50. const pre = await ethGetBalance(wallet);
  51. await this.crowdsale.sendTransaction({ value, from: investor });
  52. const post = await ethGetBalance(wallet);
  53. post.minus(pre).should.be.bignumber.equal(value);
  54. });
  55. });
  56. describe('check remaining allowance', function () {
  57. it('should report correct allowace left', async function () {
  58. const remainingAllowance = tokenAllowance - expectedTokenAmount;
  59. await this.crowdsale.buyTokens(investor, { value: value, from: purchaser });
  60. (await this.crowdsale.remainingTokens()).should.be.bignumber.equal(remainingAllowance);
  61. });
  62. });
  63. describe('when token wallet is different from token address', function () {
  64. it('creation reverts', async function () {
  65. this.token = await SimpleToken.new({ from: tokenWallet });
  66. await assertRevert(AllowanceCrowdsaleImpl.new(rate, wallet, this.token.address, ZERO_ADDRESS));
  67. });
  68. });
  69. });