PaymentSplitter.sol 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../utils/Address.sol";
  4. import "../utils/Context.sol";
  5. import "../utils/math/SafeMath.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. event PayeeAdded(address account, uint256 shares);
  21. event PaymentReleased(address to, uint256 amount);
  22. event PaymentReceived(address from, uint256 amount);
  23. uint256 private _totalShares;
  24. uint256 private _totalReleased;
  25. mapping(address => uint256) private _shares;
  26. mapping(address => uint256) private _released;
  27. address[] private _payees;
  28. /**
  29. * @dev Creates an instance of `PaymentSplitter` where each account in `payees` is assigned the number of shares at
  30. * the matching position in the `shares` array.
  31. *
  32. * All addresses in `payees` must be non-zero. Both arrays must have the same non-zero length, and there must be no
  33. * duplicates in `payees`.
  34. */
  35. constructor (address[] memory payees, uint256[] memory shares_) payable {
  36. // solhint-disable-next-line max-line-length
  37. require(payees.length == shares_.length, "PaymentSplitter: payees and shares length mismatch");
  38. require(payees.length > 0, "PaymentSplitter: no payees");
  39. for (uint256 i = 0; i < payees.length; i++) {
  40. _addPayee(payees[i], shares_[i]);
  41. }
  42. }
  43. /**
  44. * @dev The Ether received will be logged with {PaymentReceived} events. Note that these events are not fully
  45. * reliable: it's possible for a contract to receive Ether without triggering this function. This only affects the
  46. * reliability of the events, and not the actual splitting of Ether.
  47. *
  48. * To learn more about this see the Solidity documentation for
  49. * https://solidity.readthedocs.io/en/latest/contracts.html#fallback-function[fallback
  50. * functions].
  51. */
  52. receive () external payable virtual {
  53. emit PaymentReceived(_msgSender(), msg.value);
  54. }
  55. /**
  56. * @dev Getter for the total shares held by payees.
  57. */
  58. function totalShares() public view returns (uint256) {
  59. return _totalShares;
  60. }
  61. /**
  62. * @dev Getter for the total amount of Ether already released.
  63. */
  64. function totalReleased() public view returns (uint256) {
  65. return _totalReleased;
  66. }
  67. /**
  68. * @dev Getter for the amount of shares held by an account.
  69. */
  70. function shares(address account) public view returns (uint256) {
  71. return _shares[account];
  72. }
  73. /**
  74. * @dev Getter for the amount of Ether already released to a payee.
  75. */
  76. function released(address account) public view returns (uint256) {
  77. return _released[account];
  78. }
  79. /**
  80. * @dev Getter for the address of the payee number `index`.
  81. */
  82. function payee(uint256 index) public view returns (address) {
  83. return _payees[index];
  84. }
  85. /**
  86. * @dev Triggers a transfer to `account` of the amount of Ether they are owed, according to their percentage of the
  87. * total shares and their previous withdrawals.
  88. */
  89. function release(address payable account) public virtual {
  90. require(_shares[account] > 0, "PaymentSplitter: account has no shares");
  91. uint256 totalReceived = address(this).balance + _totalReleased;
  92. uint256 payment = totalReceived * _shares[account] / _totalShares - _released[account];
  93. require(payment != 0, "PaymentSplitter: account is not due payment");
  94. _released[account] = _released[account] + payment;
  95. _totalReleased = _totalReleased + payment;
  96. Address.sendValue(account, payment);
  97. emit PaymentReleased(account, payment);
  98. }
  99. /**
  100. * @dev Add a new payee to the contract.
  101. * @param account The address of the payee to add.
  102. * @param shares_ The number of shares owned by the payee.
  103. */
  104. function _addPayee(address account, uint256 shares_) private {
  105. require(account != address(0), "PaymentSplitter: account is the zero address");
  106. require(shares_ > 0, "PaymentSplitter: shares are 0");
  107. require(_shares[account] == 0, "PaymentSplitter: account already has shares");
  108. _payees.push(account);
  109. _shares[account] = shares_;
  110. _totalShares = _totalShares + shares_;
  111. emit PayeeAdded(account, shares_);
  112. }
  113. }