PaymentSplitter.test.js 4.9 KB

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