// SPDX-License-Identifier: MIT // Adapted from OpenZeppelin Contracts (last updated v4.7.0) (finance/PaymentSplitter.sol) pragma solidity ^0.8.16; import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; import "@openzeppelin/contracts/utils/Address.sol"; import "@openzeppelin/contracts/utils/Context.sol"; import "./interface/IRoyaltySplitter.sol"; import "./ContractMetadata.sol"; abstract contract RoyaltySplitter is Context, ContractMetadata, IRoyaltySplitter { event PayeeAdded(address account, uint256 shares); event PaymentReleased(address to, uint256 amount); // event ERC20PaymentReleased(IERC20 indexed token, address to, uint256 amount); event PaymentReceived(address from, uint256 amount); uint256 private _totalShares; uint256 private _totalReleased; mapping(address => uint256) private _shares; mapping(address => uint256) private _released; address[] private _payees; // mapping(IERC20 => uint256) private _erc20TotalReleased; // mapping(IERC20 => mapping(address => uint256)) private _erc20Released; /** * @dev Creates an instance of `PaymentSplitter` where each account in `payees` is assigned the number of shares at * the matching position in the `shares` array. * * All addresses in `payees` must be non-zero. Both arrays must have the same non-zero length, and there must be no * duplicates in `payees`. */ constructor(RoyaltyRecipient[] memory _recipients, Metadata memory _metadata, uint16 _royaltyBps) payable { if (_recipients.length == 0) revert NoRecipientsError(); royaltyBps = _royaltyBps; if (_recipients.length == 1) { _ContractMetadata_initialize(_recipients[0].recipient, _metadata, _royaltyBps); } else { _ContractMetadata_initialize(address(0), _metadata, _royaltyBps); } for (uint256 i = 0; i < _recipients.length; i++) { _addPayee(_recipients[i].recipient, _recipients[i].shares); } } /** * @dev The Ether received will be logged with {PaymentReceived} events. Note that these events are not fully * reliable: it's possible for a contract to receive Ether without triggering this function. This only affects the * reliability of the events, and not the actual splitting of Ether. * * To learn more about this see the Solidity documentation for * https://solidity.readthedocs.io/en/latest/contracts.html#fallback-function[fallback * functions]. */ receive() external payable virtual { emit PaymentReceived(_msgSender(), msg.value); } /** * @dev Getter for the total shares held by payees. */ function totalShares() public view returns (uint256) { return _totalShares; } /** * @dev Getter for the total amount of Ether already released. */ function totalReleased() public view returns (uint256) { return _totalReleased; } /** * @dev Getter for the total amount of `token` already released. `token` should be the address of an IERC20 * contract. */ // function totalReleased(IERC20 token) public view returns (uint256) { // return _erc20TotalReleased[token]; // } /** * @dev Getter for the amount of shares held by an account. */ function shares(address account) public view returns (uint256) { return _shares[account]; } /** * @dev Getter for the amount of Ether already released to a payee. */ function released(address account) public view returns (uint256) { return _released[account]; } /** * @dev Getter for the amount of `token` tokens already released to a payee. `token` should be the address of an * IERC20 contract. */ // function released(IERC20 token, address account) public view returns (uint256) { // return _erc20Released[token][account]; // } /** * @dev Getter for the address of the payee number `index`. */ function payee(uint256 index) public view returns (address) { return _payees[index]; } /** * @dev Getter for the amount of payee's releasable Ether. */ function releasable(address account) public view returns (uint256) { uint256 totalReceived = address(this).balance + totalReleased(); return _pendingPayment(account, totalReceived, released(account)); } /** * @dev Getter for the amount of payee's releasable `token` tokens. `token` should be the address of an * IERC20 contract. */ // function releasable(IERC20 token, address account) public view returns (uint256) { // uint256 totalReceived = token.balanceOf(address(this)) + totalReleased(token); // return _pendingPayment(account, totalReceived, released(token, account)); // } /** * @dev Triggers a transfer to `account` of the amount of Ether they are owed, according to their percentage of the * total shares and their previous withdrawals. */ function release(address payable account) public virtual { if (_shares[account] == 0) revert NoSharesError(account); uint256 payment = releasable(account); if (payment == 0) revert NoPaymentError(account); // _totalReleased is the sum of all values in _released. // If "_totalReleased += payment" does not overflow, then "_released[account] += payment" cannot overflow. _totalReleased += payment; unchecked { _released[account] += payment; } Address.sendValue(account, payment); emit PaymentReleased(account, payment); } ///@dev release All shares for each payees function releaseAll() external { for (uint256 index = 0; index < _payees.length; index++) { release(payable(_payees[index])); } } ///@dev release All shares for each payees // function releaseAll(IERC20 token) external { // for (uint256 index = 0; index < _payees.length; index++) { // release(token, _payees[index]); // } // } /** * @dev Triggers a transfer to `account` of the amount of `token` tokens they are owed, according to their * percentage of the total shares and their previous withdrawals. `token` must be the address of an IERC20 * contract. */ // function release(IERC20 token, address account) public virtual { // require(_shares[account] > 0, "PaymentSplitter: account has no shares"); // uint256 payment = releasable(token, account); // require(payment != 0, "PaymentSplitter: account is not due payment"); // // _erc20TotalReleased[token] is the sum of all values in _erc20Released[token]. // // If "_erc20TotalReleased[token] += payment" does not overflow, then "_erc20Released[token][account] += payment" // // cannot overflow. // _erc20TotalReleased[token] += payment; // unchecked { // _erc20Released[token][account] += payment; // } // SafeERC20.safeTransfer(token, account, payment); // emit ERC20PaymentReleased(token, account, payment); // } /** * @dev internal logic for computing the pending payment of an `account` given the token historical balances and * already released amounts. */ function _pendingPayment(address account, uint256 totalReceived, uint256 alreadyReleased) private view returns (uint256) { return (totalReceived * _shares[account]) / _totalShares - alreadyReleased; } /** * @dev Add a new payee to the contract. * @param account The address of the payee to add. * @param shares_ The number of shares owned by the payee. */ function _addPayee(address account, uint256 shares_) private { if (account == address(0)) revert AccountCantBeZeroAddressError(); if (shares_ == 0) revert SharesCantBeZeroError(); if (_shares[account] > 0) revert AccountAlreadyHasSharesError(account); _payees.push(account); _shares[account] = shares_; _totalShares = _totalShares + shares_; emit PayeeAdded(account, shares_); } /** * @notice View royalty info for a given token and sale price. * @dev Returns royalty amount and recipient for `tokenId` and `salePrice`. * @param salePrice Sale price of the token. * * @return receiver Address of royalty recipient account. * @return royaltyAmount Royalty amount calculated at current royaltyBps value. */ function royaltyInfo(uint256, uint256 salePrice) external view virtual override returns (address receiver, uint256 royaltyAmount) { receiver = royaltyReciever(); royaltyAmount = (salePrice * royaltyBps) / 10_000; } }