SplitPayment.test.js 2.9 KB

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