PaymentSplitter.sol 4.6 KB

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