SplitPayment.sol 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 account Whose payments will be released.
  62. */
  63. function release(address account) public {
  64. require(_shares[account] > 0);
  65. uint256 totalReceived = address(this).balance.add(_totalReleased);
  66. uint256 payment = totalReceived.mul(
  67. _shares[account]).div(
  68. _totalShares).sub(
  69. _released[account]
  70. );
  71. require(payment != 0);
  72. _released[account] = _released[account].add(payment);
  73. _totalReleased = _totalReleased.add(payment);
  74. account.transfer(payment);
  75. }
  76. /**
  77. * @dev Add a new payee to the contract.
  78. * @param account The address of the payee to add.
  79. * @param shares_ The number of shares owned by the payee.
  80. */
  81. function _addPayee(address account, uint256 shares_) internal {
  82. require(account != address(0));
  83. require(shares_ > 0);
  84. require(_shares[account] == 0);
  85. _payees.push(account);
  86. _shares[account] = shares_;
  87. _totalShares = _totalShares.add(shares_);
  88. }
  89. }