PaymentSplitter.test.js 4.8 KB

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