PaymentSplitter.sol 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../token/ERC20/utils/SafeERC20.sol";
  4. import "../utils/Address.sol";
  5. import "../utils/Context.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. * NOTE: This contract assumes that ERC20 tokens will behave similarly to native tokens (Ether). Rebasing tokens, and
  20. * tokens that apply fees during transfers, are likely to not be supported as expected. If in doubt, we encourage you
  21. * to run tests before sending real value to this contract.
  22. */
  23. contract PaymentSplitter is Context {
  24. event PayeeAdded(address account, uint256 shares);
  25. event PaymentReleased(address to, uint256 amount);
  26. event ERC20PaymentReleased(IERC20 indexed token, address to, uint256 amount);
  27. event PaymentReceived(address from, uint256 amount);
  28. uint256 private _totalShares;
  29. uint256 private _totalReleased;
  30. mapping(address => uint256) private _shares;
  31. mapping(address => uint256) private _released;
  32. address[] private _payees;
  33. mapping(IERC20 => uint256) private _erc20TotalReleased;
  34. mapping(IERC20 => mapping(address => uint256)) private _erc20Released;
  35. /**
  36. * @dev Creates an instance of `PaymentSplitter` where each account in `payees` is assigned the number of shares at
  37. * the matching position in the `shares` array.
  38. *
  39. * All addresses in `payees` must be non-zero. Both arrays must have the same non-zero length, and there must be no
  40. * duplicates in `payees`.
  41. */
  42. constructor(address[] memory payees, uint256[] memory shares_) payable {
  43. require(payees.length == shares_.length, "PaymentSplitter: payees and shares length mismatch");
  44. require(payees.length > 0, "PaymentSplitter: no payees");
  45. for (uint256 i = 0; i < payees.length; i++) {
  46. _addPayee(payees[i], shares_[i]);
  47. }
  48. }
  49. /**
  50. * @dev The Ether received will be logged with {PaymentReceived} events. Note that these events are not fully
  51. * reliable: it's possible for a contract to receive Ether without triggering this function. This only affects the
  52. * reliability of the events, and not the actual splitting of Ether.
  53. *
  54. * To learn more about this see the Solidity documentation for
  55. * https://solidity.readthedocs.io/en/latest/contracts.html#fallback-function[fallback
  56. * functions].
  57. */
  58. receive() external payable virtual {
  59. emit PaymentReceived(_msgSender(), msg.value);
  60. }
  61. /**
  62. * @dev Getter for the total shares held by payees.
  63. */
  64. function totalShares() public view returns (uint256) {
  65. return _totalShares;
  66. }
  67. /**
  68. * @dev Getter for the total amount of Ether already released.
  69. */
  70. function totalReleased() public view returns (uint256) {
  71. return _totalReleased;
  72. }
  73. /**
  74. * @dev Getter for the total amount of `token` already released. `token` should be the address of an IERC20
  75. * contract.
  76. */
  77. function totalReleased(IERC20 token) public view returns (uint256) {
  78. return _erc20TotalReleased[token];
  79. }
  80. /**
  81. * @dev Getter for the amount of shares held by an account.
  82. */
  83. function shares(address account) public view returns (uint256) {
  84. return _shares[account];
  85. }
  86. /**
  87. * @dev Getter for the amount of Ether already released to a payee.
  88. */
  89. function released(address account) public view returns (uint256) {
  90. return _released[account];
  91. }
  92. /**
  93. * @dev Getter for the amount of `token` tokens already released to a payee. `token` should be the address of an
  94. * IERC20 contract.
  95. */
  96. function released(IERC20 token, address account) public view returns (uint256) {
  97. return _erc20Released[token][account];
  98. }
  99. /**
  100. * @dev Getter for the address of the payee number `index`.
  101. */
  102. function payee(uint256 index) public view returns (address) {
  103. return _payees[index];
  104. }
  105. /**
  106. * @dev Triggers a transfer to `account` of the amount of Ether they are owed, according to their percentage of the
  107. * total shares and their previous withdrawals.
  108. */
  109. function release(address payable account) public virtual {
  110. require(_shares[account] > 0, "PaymentSplitter: account has no shares");
  111. uint256 totalReceived = address(this).balance + totalReleased();
  112. uint256 payment = _pendingPayment(account, totalReceived, released(account));
  113. require(payment != 0, "PaymentSplitter: account is not due payment");
  114. _released[account] += payment;
  115. _totalReleased += payment;
  116. Address.sendValue(account, payment);
  117. emit PaymentReleased(account, payment);
  118. }
  119. /**
  120. * @dev Triggers a transfer to `account` of the amount of `token` tokens they are owed, according to their
  121. * percentage of the total shares and their previous withdrawals. `token` must be the address of an IERC20
  122. * contract.
  123. */
  124. function release(IERC20 token, address account) public virtual {
  125. require(_shares[account] > 0, "PaymentSplitter: account has no shares");
  126. uint256 totalReceived = token.balanceOf(address(this)) + totalReleased(token);
  127. uint256 payment = _pendingPayment(account, totalReceived, released(token, account));
  128. require(payment != 0, "PaymentSplitter: account is not due payment");
  129. _erc20Released[token][account] += payment;
  130. _erc20TotalReleased[token] += payment;
  131. SafeERC20.safeTransfer(token, account, payment);
  132. emit ERC20PaymentReleased(token, account, payment);
  133. }
  134. /**
  135. * @dev internal logic for computing the pending payment of an `account` given the token historical balances and
  136. * already released amounts.
  137. */
  138. function _pendingPayment(
  139. address account,
  140. uint256 totalReceived,
  141. uint256 alreadyReleased
  142. ) private view returns (uint256) {
  143. return (totalReceived * _shares[account]) / _totalShares - alreadyReleased;
  144. }
  145. /**
  146. * @dev Add a new payee to the contract.
  147. * @param account The address of the payee to add.
  148. * @param shares_ The number of shares owned by the payee.
  149. */
  150. function _addPayee(address account, uint256 shares_) private {
  151. require(account != address(0), "PaymentSplitter: account is the zero address");
  152. require(shares_ > 0, "PaymentSplitter: shares are 0");
  153. require(_shares[account] == 0, "PaymentSplitter: account already has shares");
  154. _payees.push(account);
  155. _shares[account] = shares_;
  156. _totalShares = _totalShares + shares_;
  157. emit PayeeAdded(account, shares_);
  158. }
  159. }