PaymentSplitter.test.js 4.7 KB

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