PaymentSplitter.test.js 8.5 KB

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