PaymentSplitter.sol 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. pragma solidity ^0.5.0;
  2. import "../GSN/Context.sol";
  3. import "../math/SafeMath.sol";
  4. /**
  5. * @title PaymentSplitter
  6. * @dev This contract allows to split Ether payments among a group of accounts. The sender does not need to be aware
  7. * that the Ether will be split in this way, since it is handled transparently by the contract.
  8. *
  9. * The split can be in equal parts or in any other arbitrary proportion. The way this is specified is by assigning each
  10. * account to a number of shares. Of all the Ether that this contract receives, each account will then be able to claim
  11. * an amount proportional to the percentage of total shares they were assigned.
  12. *
  13. * `PaymentSplitter` follows a _pull payment_ model. This means that payments are not automatically forwarded to the
  14. * accounts but kept in this contract, and the actual transfer is triggered as a separate step by calling the {release}
  15. * function.
  16. */
  17. contract PaymentSplitter is Context {
  18. using SafeMath for uint256;
  19. event PayeeAdded(address account, uint256 shares);
  20. event PaymentReleased(address to, uint256 amount);
  21. event PaymentReceived(address from, uint256 amount);
  22. uint256 private _totalShares;
  23. uint256 private _totalReleased;
  24. mapping(address => uint256) private _shares;
  25. mapping(address => uint256) private _released;
  26. address[] private _payees;
  27. /**
  28. * @dev Creates an instance of `PaymentSplitter` where each account in `payees` is assigned the number of shares at
  29. * the matching position in the `shares` array.
  30. *
  31. * All addresses in `payees` must be non-zero. Both arrays must have the same non-zero length, and there must be no
  32. * duplicates in `payees`.
  33. */
  34. constructor (address[] memory payees, uint256[] memory shares) public payable {
  35. // solhint-disable-next-line max-line-length
  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. function () external payable {
  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 {
  89. require(_shares[account] > 0, "PaymentSplitter: account has no shares");
  90. uint256 totalReceived = address(this).balance.add(_totalReleased);
  91. uint256 payment = totalReceived.mul(_shares[account]).div(_totalShares).sub(_released[account]);
  92. require(payment != 0, "PaymentSplitter: account is not due payment");
  93. _released[account] = _released[account].add(payment);
  94. _totalReleased = _totalReleased.add(payment);
  95. account.transfer(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.add(shares_);
  110. emit PayeeAdded(account, shares_);
  111. }
  112. }