SplitPayment.test.js 4.2 KB

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