SplitPayment.sol 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. pragma solidity ^0.4.24;
  2. import "../math/SafeMath.sol";
  3. /**
  4. * @title SplitPayment
  5. * @dev This contract can be used when payments need to be received by a group
  6. * of people and split proportionately to some number of shares they own.
  7. */
  8. contract SplitPayment {
  9. using SafeMath for uint256;
  10. uint256 private totalShares_ = 0;
  11. uint256 private totalReleased_ = 0;
  12. mapping(address => uint256) private shares_;
  13. mapping(address => uint256) private released_;
  14. address[] private payees_;
  15. /**
  16. * @dev Constructor
  17. */
  18. constructor(address[] _payees, uint256[] _shares) public payable {
  19. require(_payees.length == _shares.length);
  20. require(_payees.length > 0);
  21. for (uint256 i = 0; i < _payees.length; i++) {
  22. _addPayee(_payees[i], _shares[i]);
  23. }
  24. }
  25. /**
  26. * @dev payable fallback
  27. */
  28. function () external payable {}
  29. /**
  30. * @return the total shares of the contract.
  31. */
  32. function totalShares() public view returns(uint256) {
  33. return totalShares_;
  34. }
  35. /**
  36. * @return the total amount already released.
  37. */
  38. function totalReleased() public view returns(uint256) {
  39. return totalReleased_;
  40. }
  41. /**
  42. * @return the shares of an account.
  43. */
  44. function shares(address _account) public view returns(uint256) {
  45. return shares_[_account];
  46. }
  47. /**
  48. * @return the amount already released to an account.
  49. */
  50. function released(address _account) public view returns(uint256) {
  51. return released_[_account];
  52. }
  53. /**
  54. * @return the address of a payee.
  55. */
  56. function payee(uint256 index) public view returns(address) {
  57. return payees_[index];
  58. }
  59. /**
  60. * @dev Release one of the payee's proportional payment.
  61. * @param _payee Whose payments will be released.
  62. */
  63. function release(address _payee) public {
  64. require(shares_[_payee] > 0);
  65. uint256 totalReceived = address(this).balance.add(totalReleased_);
  66. uint256 payment = totalReceived.mul(
  67. shares_[_payee]).div(
  68. totalShares_).sub(
  69. released_[_payee]
  70. );
  71. require(payment != 0);
  72. assert(address(this).balance >= payment);
  73. released_[_payee] = released_[_payee].add(payment);
  74. totalReleased_ = totalReleased_.add(payment);
  75. _payee.transfer(payment);
  76. }
  77. /**
  78. * @dev Add a new payee to the contract.
  79. * @param _payee The address of the payee to add.
  80. * @param _shares The number of shares owned by the payee.
  81. */
  82. function _addPayee(address _payee, uint256 _shares) internal {
  83. require(_payee != address(0));
  84. require(_shares > 0);
  85. require(shares_[_payee] == 0);
  86. payees_.push(_payee);
  87. shares_[_payee] = _shares;
  88. totalShares_ = totalShares_.add(_shares);
  89. }
  90. }