|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +/// @author thirdweb |
| 5 | + |
| 6 | +import "./interface/IPlatformFee_V1.sol"; |
| 7 | + |
| 8 | +/** |
| 9 | + * @title Platform Fee |
| 10 | + * @notice Thirdweb's `PlatformFee` is a contract extension to be used with any base contract. It exposes functions for setting and reading |
| 11 | + * the recipient of platform fee and the platform fee basis points, and lets the inheriting contract perform conditional logic |
| 12 | + * that uses information about platform fees, if desired. |
| 13 | + */ |
| 14 | + |
| 15 | +abstract contract PlatformFee is IPlatformFee { |
| 16 | + /// @dev The address that receives all platform fees from all sales. |
| 17 | + address private platformFeeRecipient; |
| 18 | + |
| 19 | + /// @dev The % of primary sales collected as platform fees. |
| 20 | + uint16 private platformFeeBps; |
| 21 | + |
| 22 | + /// @dev Fee type variants: percentage fee and flat fee |
| 23 | + PlatformFeeType private platformFeeType; |
| 24 | + |
| 25 | + /// @dev The flat amount collected by the contract as fees on primary sales. |
| 26 | + uint256 private flatPlatformFee; |
| 27 | + |
| 28 | + /// @dev Returns the platform fee recipient and bps. |
| 29 | + function getPlatformFeeInfo() public view override returns (address, uint16) { |
| 30 | + return (platformFeeRecipient, uint16(platformFeeBps)); |
| 31 | + } |
| 32 | + |
| 33 | + /// @dev Returns the platform fee bps and recipient. |
| 34 | + function getFlatPlatformFeeInfo() public view returns (address, uint256) { |
| 35 | + return (platformFeeRecipient, flatPlatformFee); |
| 36 | + } |
| 37 | + |
| 38 | + /// @dev Returns the platform fee bps and recipient. |
| 39 | + function getPlatformFeeType() public view returns (PlatformFeeType) { |
| 40 | + return platformFeeType; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @notice Updates the platform fee recipient and bps. |
| 45 | + * @dev Caller should be authorized to set platform fee info. |
| 46 | + * See {_canSetPlatformFeeInfo}. |
| 47 | + * Emits {PlatformFeeInfoUpdated Event}; See {_setupPlatformFeeInfo}. |
| 48 | + * |
| 49 | + * @param _platformFeeRecipient Address to be set as new platformFeeRecipient. |
| 50 | + * @param _platformFeeBps Updated platformFeeBps. |
| 51 | + */ |
| 52 | + function setPlatformFeeInfo(address _platformFeeRecipient, uint256 _platformFeeBps) external override { |
| 53 | + if (!_canSetPlatformFeeInfo()) { |
| 54 | + revert("Not authorized"); |
| 55 | + } |
| 56 | + _setupPlatformFeeInfo(_platformFeeRecipient, _platformFeeBps); |
| 57 | + } |
| 58 | + |
| 59 | + /// @dev Sets the platform fee recipient and bps |
| 60 | + function _setupPlatformFeeInfo(address _platformFeeRecipient, uint256 _platformFeeBps) internal { |
| 61 | + if (_platformFeeBps > 10_000) { |
| 62 | + revert("Exceeds max bps"); |
| 63 | + } |
| 64 | + |
| 65 | + platformFeeBps = uint16(_platformFeeBps); |
| 66 | + platformFeeRecipient = _platformFeeRecipient; |
| 67 | + |
| 68 | + emit PlatformFeeInfoUpdated(_platformFeeRecipient, _platformFeeBps); |
| 69 | + } |
| 70 | + |
| 71 | + /// @notice Lets a module admin set a flat fee on primary sales. |
| 72 | + function setFlatPlatformFeeInfo(address _platformFeeRecipient, uint256 _flatFee) external { |
| 73 | + if (!_canSetPlatformFeeInfo()) { |
| 74 | + revert("Not authorized"); |
| 75 | + } |
| 76 | + |
| 77 | + _setupFlatPlatformFeeInfo(_platformFeeRecipient, _flatFee); |
| 78 | + } |
| 79 | + |
| 80 | + /// @dev Sets a flat fee on primary sales. |
| 81 | + function _setupFlatPlatformFeeInfo(address _platformFeeRecipient, uint256 _flatFee) internal { |
| 82 | + flatPlatformFee = _flatFee; |
| 83 | + platformFeeRecipient = _platformFeeRecipient; |
| 84 | + |
| 85 | + emit FlatPlatformFeeUpdated(_platformFeeRecipient, _flatFee); |
| 86 | + } |
| 87 | + |
| 88 | + /// @notice Lets a module admin set platform fee type. |
| 89 | + function setPlatformFeeType(PlatformFeeType _feeType) external { |
| 90 | + if (!_canSetPlatformFeeInfo()) { |
| 91 | + revert("Not authorized"); |
| 92 | + } |
| 93 | + _setupPlatformFeeType(_feeType); |
| 94 | + } |
| 95 | + |
| 96 | + /// @dev Sets platform fee type. |
| 97 | + function _setupPlatformFeeType(PlatformFeeType _feeType) internal { |
| 98 | + platformFeeType = _feeType; |
| 99 | + |
| 100 | + emit PlatformFeeTypeUpdated(_feeType); |
| 101 | + } |
| 102 | + |
| 103 | + /// @dev Returns whether platform fee info can be set in the given execution context. |
| 104 | + function _canSetPlatformFeeInfo() internal view virtual returns (bool); |
| 105 | +} |
0 commit comments