SplitPayment.test.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const { ethGetBalance, ethSendTransaction } = require('../helpers/web3');
  2. const BigNumber = web3.BigNumber;
  3. require('chai')
  4. .use(require('chai-bignumber')(BigNumber))
  5. .should();
  6. const { expectThrow } = require('../helpers/expectThrow');
  7. const { EVMRevert } = require('../helpers/EVMRevert.js');
  8. const SplitPayment = artifacts.require('SplitPayment');
  9. contract('SplitPayment', function ([_, owner, payee1, payee2, payee3, nonpayee1, payer1]) {
  10. const amount = web3.toWei(1.0, 'ether');
  11. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  12. it('cannot be created with no payees', async function () {
  13. await expectThrow(SplitPayment.new([], []), EVMRevert);
  14. });
  15. it('requires shares for each payee', async function () {
  16. await expectThrow(SplitPayment.new([payee1, payee2, payee3], [20, 30]), EVMRevert);
  17. });
  18. it('requires a payee for each share', async function () {
  19. await expectThrow(SplitPayment.new([payee1, payee2], [20, 30, 40]), EVMRevert);
  20. });
  21. it('requires non-null payees', async function () {
  22. await expectThrow(SplitPayment.new([payee1, ZERO_ADDRESS], [20, 30]), EVMRevert);
  23. });
  24. it('requires non-zero shares', async function () {
  25. await expectThrow(SplitPayment.new([payee1, payee2], [20, 0]), EVMRevert);
  26. });
  27. it('rejects repeated payees', async function () {
  28. await expectThrow(SplitPayment.new([payee1, payee1], [20, 0]), EVMRevert);
  29. });
  30. context('once deployed', function () {
  31. beforeEach(async function () {
  32. this.payees = [payee1, payee2, payee3];
  33. this.shares = [20, 10, 70];
  34. this.contract = await SplitPayment.new(this.payees, this.shares);
  35. });
  36. it('should accept payments', async function () {
  37. await ethSendTransaction({ from: owner, to: this.contract.address, value: amount });
  38. const balance = await ethGetBalance(this.contract.address);
  39. balance.should.be.bignumber.equal(amount);
  40. });
  41. it('should store shares if address is payee', async function () {
  42. const shares = await this.contract.shares.call(payee1);
  43. shares.should.be.bignumber.not.eq(0);
  44. });
  45. it('should not store shares if address is not payee', async function () {
  46. const shares = await this.contract.shares.call(nonpayee1);
  47. shares.should.be.bignumber.equal(0);
  48. });
  49. it('should throw if no funds to claim', async function () {
  50. await expectThrow(this.contract.claim({ from: payee1 }), EVMRevert);
  51. });
  52. it('should throw if non-payee want to claim', async function () {
  53. await ethSendTransaction({ from: payer1, to: this.contract.address, value: amount });
  54. await expectThrow(this.contract.claim({ from: nonpayee1 }), EVMRevert);
  55. });
  56. it('should distribute funds to payees', async function () {
  57. await ethSendTransaction({ from: payer1, to: this.contract.address, value: amount });
  58. // receive funds
  59. const initBalance = await ethGetBalance(this.contract.address);
  60. initBalance.should.be.bignumber.equal(amount);
  61. // distribute to payees
  62. const initAmount1 = await ethGetBalance(payee1);
  63. await this.contract.claim({ from: payee1 });
  64. const profit1 = (await ethGetBalance(payee1)).sub(initAmount1);
  65. profit1.sub(web3.toWei(0.20, 'ether')).abs().should.be.bignumber.lt(1e16);
  66. const initAmount2 = await ethGetBalance(payee2);
  67. await this.contract.claim({ from: payee2 });
  68. const profit2 = (await ethGetBalance(payee2)).sub(initAmount2);
  69. profit2.sub(web3.toWei(0.10, 'ether')).abs().should.be.bignumber.lt(1e16);
  70. const initAmount3 = await ethGetBalance(payee3);
  71. await this.contract.claim({ from: payee3 });
  72. const profit3 = (await ethGetBalance(payee3)).sub(initAmount3);
  73. profit3.sub(web3.toWei(0.70, 'ether')).abs().should.be.bignumber.lt(1e16);
  74. // end balance should be zero
  75. const endBalance = await ethGetBalance(this.contract.address);
  76. endBalance.should.be.bignumber.equal(0);
  77. // check correct funds released accounting
  78. const totalReleased = await this.contract.totalReleased.call();
  79. totalReleased.should.be.bignumber.equal(initBalance);
  80. });
  81. });
  82. });