SplitPayment.sol 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. pragma solidity ^0.4.18;
  2. import "../math/SafeMath.sol";
  3. /**
  4. * @title SplitPayment
  5. * @dev Base contract that supports multiple payees claiming funds sent to this contract
  6. * according to the proportion they own.
  7. */
  8. contract SplitPayment {
  9. using SafeMath for uint256;
  10. uint256 public totalShares = 0;
  11. uint256 public totalReleased = 0;
  12. mapping(address => uint256) public shares;
  13. mapping(address => uint256) public released;
  14. address[] public payees;
  15. /**
  16. * @dev Constructor
  17. */
  18. function SplitPayment(address[] _payees, uint256[] _shares) public payable {
  19. require(_payees.length == _shares.length);
  20. for (uint256 i = 0; i < _payees.length; i++) {
  21. addPayee(_payees[i], _shares[i]);
  22. }
  23. }
  24. /**
  25. * @dev payable fallback
  26. */
  27. function () public payable {}
  28. /**
  29. * @dev Claim your share of the balance.
  30. */
  31. function claim() public {
  32. address payee = msg.sender;
  33. require(shares[payee] > 0);
  34. uint256 totalReceived = this.balance.add(totalReleased);
  35. uint256 payment = totalReceived.mul(shares[payee]).div(totalShares).sub(released[payee]);
  36. require(payment != 0);
  37. require(this.balance >= payment);
  38. released[payee] = released[payee].add(payment);
  39. totalReleased = totalReleased.add(payment);
  40. payee.transfer(payment);
  41. }
  42. /**
  43. * @dev Add a new payee to the contract.
  44. * @param _payee The address of the payee to add.
  45. * @param _shares The number of shares owned by the payee.
  46. */
  47. function addPayee(address _payee, uint256 _shares) internal {
  48. require(_payee != address(0));
  49. require(_shares > 0);
  50. require(shares[_payee] == 0);
  51. payees.push(_payee);
  52. shares[_payee] = _shares;
  53. totalShares = totalShares.add(_shares);
  54. }
  55. }