SplitPayment.test.js 4.3 KB

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