PaymentSplitter.test.js 4.5 KB

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