PaymentSplitter.test.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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(PaymentSplitter.new([], []));
  8. });
  9. it('rejects more payees than shares', async function () {
  10. await shouldFail.reverting(PaymentSplitter.new([payee1, payee2, payee3], [20, 30]));
  11. });
  12. it('rejects more shares than payees', async function () {
  13. await shouldFail.reverting(PaymentSplitter.new([payee1, payee2], [20, 30, 40]));
  14. });
  15. it('rejects null payees', async function () {
  16. await shouldFail.reverting(PaymentSplitter.new([payee1, ZERO_ADDRESS], [20, 30]));
  17. });
  18. it('rejects zero-valued shares', async function () {
  19. await shouldFail.reverting(PaymentSplitter.new([payee1, payee2], [20, 0]));
  20. });
  21. it('rejects repeated payees', async function () {
  22. await shouldFail.reverting(PaymentSplitter.new([payee1, payee1], [20, 30]));
  23. });
  24. context('once deployed', function () {
  25. beforeEach(async function () {
  26. this.payees = [payee1, payee2, payee3];
  27. this.shares = [20, 10, 70];
  28. this.contract = await PaymentSplitter.new(this.payees, this.shares);
  29. });
  30. it('should have total shares', async function () {
  31. (await this.contract.totalShares()).should.be.bignumber.equal('100');
  32. });
  33. it('should have payees', async function () {
  34. await Promise.all(this.payees.map(async (payee, index) => {
  35. (await this.contract.payee(index)).should.be.equal(payee);
  36. (await this.contract.released(payee)).should.be.bignumber.equal('0');
  37. }));
  38. });
  39. it('should accept payments', async function () {
  40. await send.ether(owner, this.contract.address, amount);
  41. (await balance.current(this.contract.address)).should.be.bignumber.equal(amount);
  42. });
  43. it('should store shares if address is payee', async function () {
  44. (await this.contract.shares(payee1)).should.be.bignumber.not.equal('0');
  45. });
  46. it('should not store shares if address is not payee', async function () {
  47. (await this.contract.shares(nonpayee1)).should.be.bignumber.equal('0');
  48. });
  49. it('should throw if no funds to claim', async function () {
  50. await shouldFail.reverting(this.contract.release(payee1));
  51. });
  52. it('should throw if non-payee want to claim', async function () {
  53. await send.ether(payer1, this.contract.address, amount);
  54. await shouldFail.reverting(this.contract.release(nonpayee1));
  55. });
  56. it('should distribute funds to payees', async function () {
  57. await send.ether(payer1, this.contract.address, amount);
  58. // receive funds
  59. const initBalance = await balance.current(this.contract.address);
  60. initBalance.should.be.bignumber.equal(amount);
  61. // distribute to payees
  62. const initAmount1 = await balance.current(payee1);
  63. const { logs: logs1 } = await this.contract.release(payee1, { gasPrice: 0 });
  64. const profit1 = (await balance.current(payee1)).sub(initAmount1);
  65. profit1.should.be.bignumber.equal(ether('0.20', 'ether'));
  66. expectEvent.inLogs(logs1, 'PaymentReleased', { to: payee1, amount: profit1 });
  67. const initAmount2 = await balance.current(payee2);
  68. const { logs: logs2 } = await this.contract.release(payee2, { gasPrice: 0 });
  69. const profit2 = (await balance.current(payee2)).sub(initAmount2);
  70. profit2.should.be.bignumber.equal(ether('0.10', 'ether'));
  71. expectEvent.inLogs(logs2, 'PaymentReleased', { to: payee2, amount: profit2 });
  72. const initAmount3 = await balance.current(payee3);
  73. const { logs: logs3 } = await this.contract.release(payee3, { gasPrice: 0 });
  74. const profit3 = (await balance.current(payee3)).sub(initAmount3);
  75. profit3.should.be.bignumber.equal(ether('0.70', 'ether'));
  76. expectEvent.inLogs(logs3, 'PaymentReleased', { to: payee3, amount: profit3 });
  77. // end balance should be zero
  78. (await balance.current(this.contract.address)).should.be.bignumber.equal('0');
  79. // check correct funds released accounting
  80. (await this.contract.totalReleased()).should.be.bignumber.equal(initBalance);
  81. });
  82. });
  83. });