AllowanceCrowdsale.test.js 3.1 KB

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