PaymentSplitter.sol 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. pragma solidity ^0.5.0;
  2. import "../math/SafeMath.sol";
  3. /**
  4. * @title PaymentSplitter
  5. * @dev This contract allows to split Ether payments among a group of accounts. The sender does not need to be aware
  6. * that the Ether will be split in this way, since it is handled transparently by the contract.
  7. *
  8. * The split can be in equal parts or in any other arbitrary proportion. The way this is specified is by assigning each
  9. * account to a number of shares. Of all the Ether that this contract receives, each account will then be able to claim
  10. * an amount proportional to the percentage of total shares they were assigned.
  11. *
  12. * `PaymentSplitter` follows a _pull payment_ model. This means that payments are not automatically forwarded to the
  13. * accounts but kept in this contract, and the actual transfer is triggered as a separate step by calling the `release`
  14. * function.
  15. */
  16. contract PaymentSplitter {
  17. using SafeMath for uint256;
  18. event PayeeAdded(address account, uint256 shares);
  19. event PaymentReleased(address to, uint256 amount);
  20. event PaymentReceived(address from, uint256 amount);
  21. uint256 private _totalShares;
  22. uint256 private _totalReleased;
  23. mapping(address => uint256) private _shares;
  24. mapping(address => uint256) private _released;
  25. address[] private _payees;
  26. /**
  27. * @dev Creates an instance of `PaymentSplitter` where each account in `payees` is assigned the number of shares at
  28. * the matching position in the `shares` array.
  29. *
  30. * All addresses in `payees` must be non-zero. Both arrays must have the same non-zero length, and there must be no
  31. * duplicates in `payees`.
  32. */
  33. constructor (address[] memory payees, uint256[] memory shares) public payable {
  34. // solhint-disable-next-line max-line-length
  35. require(payees.length == shares.length, "PaymentSplitter: payees and shares length mismatch");
  36. require(payees.length > 0, "PaymentSplitter: no payees");
  37. for (uint256 i = 0; i < payees.length; i++) {
  38. _addPayee(payees[i], shares[i]);
  39. }
  40. }
  41. /**
  42. * @dev The Ether received will be logged with `PaymentReceived` events. Note that these events are not fully
  43. * reliable: it's possible for a contract to receive Ether without triggering this function. This only affects the
  44. * reliability of the events, and not the actual splitting of Ether.
  45. *
  46. * To learn more about this see the Solidity documentation for [fallback functions].
  47. *
  48. * [fallback functions]: https://solidity.readthedocs.io/en/latest/contracts.html#fallback-function
  49. */
  50. function () external payable {
  51. emit PaymentReceived(msg.sender, msg.value);
  52. }
  53. /**
  54. * @dev Getter for the total shares held by payees.
  55. */
  56. function totalShares() public view returns (uint256) {
  57. return _totalShares;
  58. }
  59. /**
  60. * @dev Getter for the total amount of Ether already released.
  61. */
  62. function totalReleased() public view returns (uint256) {
  63. return _totalReleased;
  64. }
  65. /**
  66. * @dev Getter for the amount of shares held by an account.
  67. */
  68. function shares(address account) public view returns (uint256) {
  69. return _shares[account];
  70. }
  71. /**
  72. * @dev Getter for the amount of Ether already released to a payee.
  73. */
  74. function released(address account) public view returns (uint256) {
  75. return _released[account];
  76. }
  77. /**
  78. * @dev Getter for the address of the payee number `index`.
  79. */
  80. function payee(uint256 index) public view returns (address) {
  81. return _payees[index];
  82. }
  83. /**
  84. * @dev Triggers a transfer to `account` of the amount of Ether they are owed, according to their percentage of the
  85. * total shares and their previous withdrawals.
  86. */
  87. function release(address payable account) public {
  88. require(_shares[account] > 0, "PaymentSplitter: account has no shares");
  89. uint256 totalReceived = address(this).balance.add(_totalReleased);
  90. uint256 payment = totalReceived.mul(_shares[account]).div(_totalShares).sub(_released[account]);
  91. require(payment != 0, "PaymentSplitter: account is not due payment");
  92. _released[account] = _released[account].add(payment);
  93. _totalReleased = _totalReleased.add(payment);
  94. account.transfer(payment);
  95. emit PaymentReleased(account, payment);
  96. }
  97. /**
  98. * @dev Add a new payee to the contract.
  99. * @param account The address of the payee to add.
  100. * @param shares_ The number of shares owned by the payee.
  101. */
  102. function _addPayee(address account, uint256 shares_) private {
  103. require(account != address(0), "PaymentSplitter: account is the zero address");
  104. require(shares_ > 0, "PaymentSplitter: shares are 0");
  105. require(_shares[account] == 0, "PaymentSplitter: account already has shares");
  106. _payees.push(account);
  107. _shares[account] = shares_;
  108. _totalShares = _totalShares.add(shares_);
  109. emit PayeeAdded(account, shares_);
  110. }
  111. }