SplitPayment.test.js 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 EVMThrow = require('../helpers/EVMThrow.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. beforeEach(async function () {
  12. this.payees = [payee1, payee2, payee3];
  13. this.shares = [20, 10, 70];
  14. this.contract = await SplitPayment.new(this.payees, this.shares);
  15. });
  16. it('should accept payments', async function () {
  17. await ethSendTransaction({ from: owner, to: this.contract.address, value: amount });
  18. const balance = await ethGetBalance(this.contract.address);
  19. balance.should.be.bignumber.equal(amount);
  20. });
  21. it('should store shares if address is payee', async function () {
  22. const shares = await this.contract.shares.call(payee1);
  23. shares.should.be.bignumber.not.equal(0);
  24. });
  25. it('should not store shares if address is not payee', async function () {
  26. const shares = await this.contract.shares.call(nonpayee1);
  27. shares.should.be.bignumber.equal(0);
  28. });
  29. it('should throw if no funds to claim', async function () {
  30. await expectThrow(this.contract.claim({ from: payee1 }), EVMThrow);
  31. });
  32. it('should throw if non-payee want to claim', async function () {
  33. await ethSendTransaction({ from: payer1, to: this.contract.address, value: amount });
  34. await expectThrow(this.contract.claim({ from: nonpayee1 }), EVMThrow);
  35. });
  36. it('should distribute funds to payees', async function () {
  37. await ethSendTransaction({ from: payer1, to: this.contract.address, value: amount });
  38. // receive funds
  39. const initBalance = await ethGetBalance(this.contract.address);
  40. initBalance.should.be.bignumber.equal(amount);
  41. // distribute to payees
  42. const initAmount1 = await ethGetBalance(payee1);
  43. await this.contract.claim({ from: payee1 });
  44. const profit1 = await ethGetBalance(payee1) - initAmount1;
  45. assert(Math.abs(profit1 - web3.toWei(0.20, 'ether')) < 1e16);
  46. const initAmount2 = await ethGetBalance(payee2);
  47. await this.contract.claim({ from: payee2 });
  48. const profit2 = await ethGetBalance(payee2) - initAmount2;
  49. assert(Math.abs(profit2 - web3.toWei(0.10, 'ether')) < 1e16);
  50. const initAmount3 = await ethGetBalance(payee3);
  51. await this.contract.claim({ from: payee3 });
  52. const profit3 = await ethGetBalance(payee3) - initAmount3;
  53. assert(Math.abs(profit3 - web3.toWei(0.70, 'ether')) < 1e16);
  54. // end balance should be zero
  55. const endBalance = await ethGetBalance(this.contract.address);
  56. endBalance.should.be.bignumber.equal(0);
  57. // check correct funds released accounting
  58. const totalReleased = await this.contract.totalReleased.call();
  59. totalReleased.should.be.bignumber.equal(initBalance);
  60. });
  61. });