PaymentSplitter.sol 5.1 KB

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