PaymentSplitter.test.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. const ERC20 = artifacts.require('$ERC20');
  6. contract('PaymentSplitter', function (accounts) {
  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. this.token = await ERC20.new('MyToken', 'MT');
  43. await this.token.$_mint(owner, ether('1000'));
  44. });
  45. it('has total shares', async function () {
  46. expect(await this.contract.totalShares()).to.be.bignumber.equal('100');
  47. });
  48. it('has payees', async function () {
  49. await Promise.all(this.payees.map(async (payee, index) => {
  50. expect(await this.contract.payee(index)).to.equal(payee);
  51. expect(await this.contract.released(payee)).to.be.bignumber.equal('0');
  52. expect(await this.contract.releasable(payee)).to.be.bignumber.equal('0');
  53. }));
  54. });
  55. describe('accepts payments', function () {
  56. it('Ether', async function () {
  57. await send.ether(owner, this.contract.address, amount);
  58. expect(await balance.current(this.contract.address)).to.be.bignumber.equal(amount);
  59. });
  60. it('Token', async function () {
  61. await this.token.transfer(this.contract.address, amount, { from: owner });
  62. expect(await this.token.balanceOf(this.contract.address)).to.be.bignumber.equal(amount);
  63. });
  64. });
  65. describe('shares', function () {
  66. it('stores shares if address is payee', async function () {
  67. expect(await this.contract.shares(payee1)).to.be.bignumber.not.equal('0');
  68. });
  69. it('does not store shares if address is not payee', async function () {
  70. expect(await this.contract.shares(nonpayee1)).to.be.bignumber.equal('0');
  71. });
  72. });
  73. describe('release', function () {
  74. describe('Ether', function () {
  75. it('reverts if no funds to claim', async function () {
  76. await expectRevert(this.contract.release(payee1),
  77. 'PaymentSplitter: account is not due payment',
  78. );
  79. });
  80. it('reverts if non-payee want to claim', async function () {
  81. await send.ether(payer1, this.contract.address, amount);
  82. await expectRevert(this.contract.release(nonpayee1),
  83. 'PaymentSplitter: account has no shares',
  84. );
  85. });
  86. });
  87. describe('Token', function () {
  88. it('reverts if no funds to claim', async function () {
  89. await expectRevert(this.contract.release(this.token.address, payee1),
  90. 'PaymentSplitter: account is not due payment',
  91. );
  92. });
  93. it('reverts if non-payee want to claim', async function () {
  94. await this.token.transfer(this.contract.address, amount, { from: owner });
  95. await expectRevert(this.contract.release(this.token.address, nonpayee1),
  96. 'PaymentSplitter: account has no shares',
  97. );
  98. });
  99. });
  100. });
  101. describe('tracks releasable and released', function () {
  102. it('Ether', async function () {
  103. await send.ether(payer1, this.contract.address, amount);
  104. const payment = amount.divn(10);
  105. expect(await this.contract.releasable(payee2)).to.be.bignumber.equal(payment);
  106. await this.contract.release(payee2);
  107. expect(await this.contract.releasable(payee2)).to.be.bignumber.equal('0');
  108. expect(await this.contract.released(payee2)).to.be.bignumber.equal(payment);
  109. });
  110. it('Token', async function () {
  111. await this.token.transfer(this.contract.address, amount, { from: owner });
  112. const payment = amount.divn(10);
  113. expect(await this.contract.releasable(this.token.address, payee2, {})).to.be.bignumber.equal(payment);
  114. await this.contract.release(this.token.address, payee2);
  115. expect(await this.contract.releasable(this.token.address, payee2, {})).to.be.bignumber.equal('0');
  116. expect(await this.contract.released(this.token.address, payee2)).to.be.bignumber.equal(payment);
  117. });
  118. });
  119. describe('distributes funds to payees', function () {
  120. it('Ether', async function () {
  121. await send.ether(payer1, this.contract.address, amount);
  122. // receive funds
  123. const initBalance = await balance.current(this.contract.address);
  124. expect(initBalance).to.be.bignumber.equal(amount);
  125. // distribute to payees
  126. const tracker1 = await balance.tracker(payee1);
  127. const receipt1 = await this.contract.release(payee1);
  128. const profit1 = await tracker1.delta();
  129. expect(profit1).to.be.bignumber.equal(ether('0.20'));
  130. expectEvent(receipt1, 'PaymentReleased', { to: payee1, amount: profit1 });
  131. const tracker2 = await balance.tracker(payee2);
  132. const receipt2 = await this.contract.release(payee2);
  133. const profit2 = await tracker2.delta();
  134. expect(profit2).to.be.bignumber.equal(ether('0.10'));
  135. expectEvent(receipt2, 'PaymentReleased', { to: payee2, amount: profit2 });
  136. const tracker3 = await balance.tracker(payee3);
  137. const receipt3 = await this.contract.release(payee3);
  138. const profit3 = await tracker3.delta();
  139. expect(profit3).to.be.bignumber.equal(ether('0.70'));
  140. expectEvent(receipt3, 'PaymentReleased', { to: payee3, amount: profit3 });
  141. // end balance should be zero
  142. expect(await balance.current(this.contract.address)).to.be.bignumber.equal('0');
  143. // check correct funds released accounting
  144. expect(await this.contract.totalReleased()).to.be.bignumber.equal(initBalance);
  145. });
  146. it('Token', async function () {
  147. expect(await this.token.balanceOf(payee1)).to.be.bignumber.equal('0');
  148. expect(await this.token.balanceOf(payee2)).to.be.bignumber.equal('0');
  149. expect(await this.token.balanceOf(payee3)).to.be.bignumber.equal('0');
  150. await this.token.transfer(this.contract.address, amount, { from: owner });
  151. expectEvent(
  152. await this.contract.release(this.token.address, payee1),
  153. 'ERC20PaymentReleased',
  154. { token: this.token.address, to: payee1, amount: ether('0.20') },
  155. );
  156. await this.token.transfer(this.contract.address, amount, { from: owner });
  157. expectEvent(
  158. await this.contract.release(this.token.address, payee1),
  159. 'ERC20PaymentReleased',
  160. { token: this.token.address, to: payee1, amount: ether('0.20') },
  161. );
  162. expectEvent(
  163. await this.contract.release(this.token.address, payee2),
  164. 'ERC20PaymentReleased',
  165. { token: this.token.address, to: payee2, amount: ether('0.20') },
  166. );
  167. expectEvent(
  168. await this.contract.release(this.token.address, payee3),
  169. 'ERC20PaymentReleased',
  170. { token: this.token.address, to: payee3, amount: ether('1.40') },
  171. );
  172. expect(await this.token.balanceOf(payee1)).to.be.bignumber.equal(ether('0.40'));
  173. expect(await this.token.balanceOf(payee2)).to.be.bignumber.equal(ether('0.20'));
  174. expect(await this.token.balanceOf(payee3)).to.be.bignumber.equal(ether('1.40'));
  175. });
  176. });
  177. });
  178. });