PaymentSplitter.test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. const { ethGetBalance } = require('../helpers/web3');
  2. const expectEvent = require('../helpers/expectEvent');
  3. const send = require('./../helpers/send');
  4. const { ether } = require('../helpers/ether');
  5. const { ZERO_ADDRESS } = require('./../helpers/constants');
  6. const BigNumber = web3.BigNumber;
  7. require('chai')
  8. .use(require('chai-bignumber')(BigNumber))
  9. .should();
  10. const shouldFail = require('../helpers/shouldFail');
  11. const PaymentSplitter = artifacts.require('PaymentSplitter');
  12. contract('PaymentSplitter', function ([_, owner, payee1, payee2, payee3, nonpayee1, payer1]) {
  13. const amount = ether(1.0);
  14. it('rejects an empty set of payees', async function () {
  15. await shouldFail.reverting(PaymentSplitter.new([], []));
  16. });
  17. it('rejects more payees than shares', async function () {
  18. await shouldFail.reverting(PaymentSplitter.new([payee1, payee2, payee3], [20, 30]));
  19. });
  20. it('rejects more shares than payees', async function () {
  21. await shouldFail.reverting(PaymentSplitter.new([payee1, payee2], [20, 30, 40]));
  22. });
  23. it('rejects null payees', async function () {
  24. await shouldFail.reverting(PaymentSplitter.new([payee1, ZERO_ADDRESS], [20, 30]));
  25. });
  26. it('rejects zero-valued shares', async function () {
  27. await shouldFail.reverting(PaymentSplitter.new([payee1, payee2], [20, 0]));
  28. });
  29. it('rejects repeated payees', async function () {
  30. await shouldFail.reverting(PaymentSplitter.new([payee1, payee1], [20, 30]));
  31. });
  32. context('once deployed', function () {
  33. beforeEach(async function () {
  34. this.payees = [payee1, payee2, payee3];
  35. this.shares = [20, 10, 70];
  36. this.contract = await PaymentSplitter.new(this.payees, this.shares);
  37. });
  38. it('should have total shares', async function () {
  39. (await this.contract.totalShares()).should.be.bignumber.equal(20 + 10 + 70);
  40. });
  41. it('should have payees', async function () {
  42. await Promise.all(this.payees.map(async (payee, index) => {
  43. (await this.contract.payee(index)).should.be.equal(payee);
  44. (await this.contract.released(payee)).should.be.bignumber.equal(0);
  45. }));
  46. });
  47. it('should accept payments', async function () {
  48. await send.ether(owner, this.contract.address, amount);
  49. (await ethGetBalance(this.contract.address)).should.be.bignumber.equal(amount);
  50. });
  51. it('should store shares if address is payee', async function () {
  52. (await this.contract.shares(payee1)).should.be.bignumber.not.equal(0);
  53. });
  54. it('should not store shares if address is not payee', async function () {
  55. (await this.contract.shares(nonpayee1)).should.be.bignumber.equal(0);
  56. });
  57. it('should throw if no funds to claim', async function () {
  58. await shouldFail.reverting(this.contract.release(payee1));
  59. });
  60. it('should throw if non-payee want to claim', async function () {
  61. await send.ether(payer1, this.contract.address, amount);
  62. await shouldFail.reverting(this.contract.release(nonpayee1));
  63. });
  64. it('should distribute funds to payees', async function () {
  65. await send.ether(payer1, this.contract.address, amount);
  66. // receive funds
  67. const initBalance = await ethGetBalance(this.contract.address);
  68. initBalance.should.be.bignumber.equal(amount);
  69. // distribute to payees
  70. const initAmount1 = await ethGetBalance(payee1);
  71. const { logs: logs1 } = await this.contract.release(payee1);
  72. const profit1 = (await ethGetBalance(payee1)).sub(initAmount1);
  73. profit1.sub(web3.toWei(0.20, 'ether')).abs().should.be.bignumber.lt(1e16);
  74. expectEvent.inLogs(logs1, 'PaymentReleased', { to: payee1, amount: profit1 });
  75. const initAmount2 = await ethGetBalance(payee2);
  76. const { logs: logs2 } = await this.contract.release(payee2);
  77. const profit2 = (await ethGetBalance(payee2)).sub(initAmount2);
  78. profit2.sub(web3.toWei(0.10, 'ether')).abs().should.be.bignumber.lt(1e16);
  79. expectEvent.inLogs(logs2, 'PaymentReleased', { to: payee2, amount: profit2 });
  80. const initAmount3 = await ethGetBalance(payee3);
  81. const { logs: logs3 } = await this.contract.release(payee3);
  82. const profit3 = (await ethGetBalance(payee3)).sub(initAmount3);
  83. profit3.sub(web3.toWei(0.70, 'ether')).abs().should.be.bignumber.lt(1e16);
  84. expectEvent.inLogs(logs3, 'PaymentReleased', { to: payee3, amount: profit3 });
  85. // end balance should be zero
  86. (await ethGetBalance(this.contract.address)).should.be.bignumber.equal(0);
  87. // check correct funds released accounting
  88. (await this.contract.totalReleased()).should.be.bignumber.equal(initBalance);
  89. });
  90. });
  91. });