|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.19; |
| 3 | + |
| 4 | +import {PaymentSplitter, IERC20Upgradeable} from "@0xsequence/contracts-library/payments/PaymentSplitter.sol"; |
| 5 | +import { |
| 6 | + IPaymentCombiner, IPaymentCombinerFunctions |
| 7 | +} from "@0xsequence/contracts-library/payments/IPaymentCombiner.sol"; |
| 8 | +import {IERC165} from "@0xsequence/erc-1155/contracts/interfaces/IERC165.sol"; |
| 9 | +import {Clones} from "@openzeppelin/contracts/proxy/Clones.sol"; |
| 10 | + |
| 11 | +/** |
| 12 | + * Deployer of Payment Splitter proxies. |
| 13 | + * @dev Unlike other factories in this library, payment splitters are unowned and not upgradeable. |
| 14 | + */ |
| 15 | +contract PaymentCombiner is IPaymentCombiner, IERC165 { |
| 16 | + using Clones for address; |
| 17 | + |
| 18 | + address private immutable _IMPLEMENTATION; |
| 19 | + |
| 20 | + mapping(address => address[]) private _payeeSplitters; |
| 21 | + |
| 22 | + /** |
| 23 | + * Creates a Payment Splitter Factory. |
| 24 | + */ |
| 25 | + constructor() { |
| 26 | + _IMPLEMENTATION = address(new PaymentSplitter()); |
| 27 | + } |
| 28 | + |
| 29 | + /// @inheritdoc IPaymentCombinerFunctions |
| 30 | + function implementationAddress() external view returns (address) { |
| 31 | + return _IMPLEMENTATION; |
| 32 | + } |
| 33 | + |
| 34 | + /// @inheritdoc IPaymentCombinerFunctions |
| 35 | + function deploy(address[] calldata payees, uint256[] calldata shares) external returns (address proxyAddr) { |
| 36 | + bytes32 salt = _determineSalt(payees, shares); |
| 37 | + proxyAddr = _IMPLEMENTATION.cloneDeterministic(salt); |
| 38 | + PaymentSplitter(payable(proxyAddr)).initialize(payees, shares); |
| 39 | + emit PaymentSplitterDeployed(proxyAddr); |
| 40 | + |
| 41 | + // Add the payees to the list of payee splitters |
| 42 | + for (uint256 i = 0; i < payees.length; i++) { |
| 43 | + _payeeSplitters[payees[i]].push(proxyAddr); |
| 44 | + } |
| 45 | + |
| 46 | + return proxyAddr; |
| 47 | + } |
| 48 | + |
| 49 | + /// @inheritdoc IPaymentCombinerFunctions |
| 50 | + function determineAddress(address[] calldata payees, uint256[] calldata shares) |
| 51 | + external |
| 52 | + view |
| 53 | + returns (address proxyAddr) |
| 54 | + { |
| 55 | + bytes32 salt = _determineSalt(payees, shares); |
| 56 | + return _IMPLEMENTATION.predictDeterministicAddress(salt); |
| 57 | + } |
| 58 | + |
| 59 | + /// @dev Computes the deployment salt for a Payment Splitter. |
| 60 | + function _determineSalt(address[] calldata payees, uint256[] calldata shares) internal pure returns (bytes32) { |
| 61 | + return keccak256(abi.encode(payees, shares)); |
| 62 | + } |
| 63 | + |
| 64 | + /// @inheritdoc IPaymentCombinerFunctions |
| 65 | + function listPayeeSplitters(address payee) external view returns (address[] memory splitterAddrs) { |
| 66 | + return _payeeSplitters[payee]; |
| 67 | + } |
| 68 | + |
| 69 | + /// @inheritdoc IPaymentCombinerFunctions |
| 70 | + function listReleasable(address payee, address tokenAddr) |
| 71 | + external |
| 72 | + view |
| 73 | + returns (address[] memory splitterAddrs, uint256[] memory pendingShares) |
| 74 | + { |
| 75 | + address[] memory payeeSplitters = _payeeSplitters[payee]; |
| 76 | + uint256 len = payeeSplitters.length; |
| 77 | + uint256[] memory payeePendingShares = new uint256[](len); |
| 78 | + |
| 79 | + if (tokenAddr == address(0)) { |
| 80 | + for (uint256 i = 0; i < len;) { |
| 81 | + payeePendingShares[i] = PaymentSplitter(payable(payeeSplitters[i])).releasable(payee); |
| 82 | + unchecked { |
| 83 | + i++; |
| 84 | + } |
| 85 | + } |
| 86 | + } else { |
| 87 | + for (uint256 i = 0; i < len;) { |
| 88 | + payeePendingShares[i] = |
| 89 | + PaymentSplitter(payable(payeeSplitters[i])).releasable(IERC20Upgradeable(tokenAddr), payee); |
| 90 | + unchecked { |
| 91 | + i++; |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + return (payeeSplitters, payeePendingShares); |
| 97 | + } |
| 98 | + |
| 99 | + /// @inheritdoc IPaymentCombinerFunctions |
| 100 | + function release(address payable payee, address tokenAddr, address[] calldata splitterAddrs) external { |
| 101 | + uint256 len = splitterAddrs.length; |
| 102 | + if (tokenAddr == address(0)) { |
| 103 | + for (uint256 i = 0; i < len;) { |
| 104 | + PaymentSplitter(payable(splitterAddrs[i])).release(payee); |
| 105 | + unchecked { |
| 106 | + i++; |
| 107 | + } |
| 108 | + } |
| 109 | + } else { |
| 110 | + for (uint256 i = 0; i < len;) { |
| 111 | + PaymentSplitter(payable(splitterAddrs[i])).release(IERC20Upgradeable(tokenAddr), payee); |
| 112 | + unchecked { |
| 113 | + i++; |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + /// @inheritdoc IERC165 |
| 120 | + function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) { |
| 121 | + return type(IPaymentCombiner).interfaceId == interfaceId |
| 122 | + || type(IPaymentCombinerFunctions).interfaceId == interfaceId || type(IERC165).interfaceId == interfaceId; |
| 123 | + } |
| 124 | +} |
0 commit comments