SplitPayment.test.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. const { ethGetBalance } = require('../helpers/web3');
  2. const { sendEther } = require('./../helpers/sendTransaction');
  3. const { ether } = require('../helpers/ether');
  4. const { ZERO_ADDRESS } = require('./../helpers/constants');
  5. const BigNumber = web3.BigNumber;
  6. require('chai')
  7. .use(require('chai-bignumber')(BigNumber))
  8. .should();
  9. const shouldFail = require('../helpers/shouldFail');
  10. const SplitPayment = artifacts.require('SplitPayment');
  11. contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1, payer1]) {
  12. const amount = ether(1.0);
  13. it('rejects an empty set of payees', async function () {
  14. await shouldFail.reverting(SplitPayment.new([], []));
  15. });
  16. it('rejects more payees than shares', async function () {
  17. await shouldFail.reverting(SplitPayment.new([payee1, payee2, payee3], [20, 30]));
  18. });
  19. it('rejects more shares than payees', async function () {
  20. await shouldFail.reverting(SplitPayment.new([payee1, payee2], [20, 30, 40]));
  21. });
  22. it('rejects null payees', async function () {
  23. await shouldFail.reverting(SplitPayment.new([payee1, ZERO_ADDRESS], [20, 30]));
  24. });
  25. it('rejects zero-valued shares', async function () {
  26. await shouldFail.reverting(SplitPayment.new([payee1, payee2], [20, 0]));
  27. });
  28. it('rejects repeated payees', async function () {
  29. await shouldFail.reverting(SplitPayment.new([payee1, payee1], [20, 30]));
  30. });
  31. context('once deployed', function () {
  32. beforeEach(async function () {
  33. this.payees = [payee1, payee2, payee3];
  34. this.shares = [20, 10, 70];
  35. this.contract = await SplitPayment.new(this.payees, this.shares);
  36. });
  37. it('should have total shares', async function () {
  38. (await this.contract.totalShares()).should.be.bignumber.equal(20 + 10 + 70);
  39. });
  40. it('should have payees', async function () {
  41. await Promise.all(this.payees.map(async (payee, index) => {
  42. (await this.contract.payee(index)).should.be.equal(payee);
  43. (await this.contract.released(payee)).should.be.bignumber.equal(0);
  44. }));
  45. });
  46. it('should accept payments', async function () {
  47. await sendEther(owner, this.contract.address, amount);
  48. (await ethGetBalance(this.contract.address)).should.be.bignumber.equal(amount);
  49. });
  50. it('should store shares if address is payee', async function () {
  51. (await this.contract.shares(payee1)).should.be.bignumber.not.equal(0);
  52. });
  53. it('should not store shares if address is not payee', async function () {
  54. (await this.contract.shares(nonpayee1)).should.be.bignumber.equal(0);
  55. });
  56. it('should throw if no funds to claim', async function () {
  57. await shouldFail.reverting(this.contract.release(payee1));
  58. });
  59. it('should throw if non-payee want to claim', async function () {
  60. await sendEther(payer1, this.contract.address, amount);
  61. await shouldFail.reverting(this.contract.release(nonpayee1));
  62. });
  63. it('should distribute funds to payees', async function () {
  64. await sendEther(payer1, this.contract.address, amount);
  65. // receive funds
  66. const initBalance = await ethGetBalance(this.contract.address);
  67. initBalance.should.be.bignumber.equal(amount);
  68. // distribute to payees
  69. const initAmount1 = await ethGetBalance(payee1);
  70. await this.contract.release(payee1);
  71. const profit1 = (await ethGetBalance(payee1)).sub(initAmount1);
  72. profit1.sub(web3.toWei(0.20, 'ether')).abs().should.be.bignumber.lt(1e16);
  73. const initAmount2 = await ethGetBalance(payee2);
  74. await this.contract.release(payee2);
  75. const profit2 = (await ethGetBalance(payee2)).sub(initAmount2);
  76. profit2.sub(web3.toWei(0.10, 'ether')).abs().should.be.bignumber.lt(1e16);
  77. const initAmount3 = await ethGetBalance(payee3);
  78. await this.contract.release(payee3);
  79. const profit3 = (await ethGetBalance(payee3)).sub(initAmount3);
  80. profit3.sub(web3.toWei(0.70, 'ether')).abs().should.be.bignumber.lt(1e16);
  81. // end balance should be zero
  82. (await ethGetBalance(this.contract.address)).should.be.bignumber.equal(0);
  83. // check correct funds released accounting
  84. (await this.contract.totalReleased()).should.be.bignumber.equal(initBalance);
  85. });
  86. });
  87. });