PaymentSplitter.sol 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. require(payees.length == shares_.length, "PaymentSplitter: payees and shares length mismatch");
  37. require(payees.length > 0, "PaymentSplitter: no payees");
  38. for (uint256 i = 0; i < payees.length; i++) {
  39. _addPayee(payees[i], shares_[i]);
  40. }
  41. }
  42. /**
  43. * @dev The Ether received will be logged with {PaymentReceived} events. Note that these events are not fully
  44. * reliable: it's possible for a contract to receive Ether without triggering this function. This only affects the
  45. * reliability of the events, and not the actual splitting of Ether.
  46. *
  47. * To learn more about this see the Solidity documentation for
  48. * https://solidity.readthedocs.io/en/latest/contracts.html#fallback-function[fallback
  49. * functions].
  50. */
  51. receive() external payable virtual {
  52. emit PaymentReceived(_msgSender(), msg.value);
  53. }
  54. /**
  55. * @dev Getter for the total shares held by payees.
  56. */
  57. function totalShares() public view returns (uint256) {
  58. return _totalShares;
  59. }
  60. /**
  61. * @dev Getter for the total amount of Ether already released.
  62. */
  63. function totalReleased() public view returns (uint256) {
  64. return _totalReleased;
  65. }
  66. /**
  67. * @dev Getter for the amount of shares held by an account.
  68. */
  69. function shares(address account) public view returns (uint256) {
  70. return _shares[account];
  71. }
  72. /**
  73. * @dev Getter for the amount of Ether already released to a payee.
  74. */
  75. function released(address account) public view returns (uint256) {
  76. return _released[account];
  77. }
  78. /**
  79. * @dev Getter for the address of the payee number `index`.
  80. */
  81. function payee(uint256 index) public view returns (address) {
  82. return _payees[index];
  83. }
  84. /**
  85. * @dev Triggers a transfer to `account` of the amount of Ether they are owed, according to their percentage of the
  86. * total shares and their previous withdrawals.
  87. */
  88. function release(address payable account) public virtual {
  89. require(_shares[account] > 0, "PaymentSplitter: account has no shares");
  90. uint256 totalReceived = address(this).balance + _totalReleased;
  91. uint256 payment = (totalReceived * _shares[account]) / _totalShares - _released[account];
  92. require(payment != 0, "PaymentSplitter: account is not due payment");
  93. _released[account] = _released[account] + payment;
  94. _totalReleased = _totalReleased + payment;
  95. Address.sendValue(account, payment);
  96. emit PaymentReleased(account, payment);
  97. }
  98. /**
  99. * @dev Add a new payee to the contract.
  100. * @param account The address of the payee to add.
  101. * @param shares_ The number of shares owned by the payee.
  102. */
  103. function _addPayee(address account, uint256 shares_) private {
  104. require(account != address(0), "PaymentSplitter: account is the zero address");
  105. require(shares_ > 0, "PaymentSplitter: shares are 0");
  106. require(_shares[account] == 0, "PaymentSplitter: account already has shares");
  107. _payees.push(account);
  108. _shares[account] = shares_;
  109. _totalShares = _totalShares + shares_;
  110. emit PayeeAdded(account, shares_);
  111. }
  112. }