|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.29; |
| 3 | + |
| 4 | +// interfaces |
| 5 | +import {IFeeHook, FeeHookResult} from "./IFeeHook.sol"; |
| 6 | +import {IFeeManagerBase} from "./IFeeManager.sol"; |
| 7 | + |
| 8 | +// libraries |
| 9 | +import {BasisPoints} from "src/utils/libraries/BasisPoints.sol"; |
| 10 | +import {CurrencyTransfer} from "src/utils/libraries/CurrencyTransfer.sol"; |
| 11 | +import {CustomRevert} from "src/utils/libraries/CustomRevert.sol"; |
| 12 | +import {FeeCalculationMethod, FeeConfig, FeeManagerStorage} from "./FeeManagerStorage.sol"; |
| 13 | +import {FixedPointMathLib} from "solady/utils/FixedPointMathLib.sol"; |
| 14 | + |
| 15 | +/// @title FeeManagerBase |
| 16 | +/// @notice Base contract with internal fee management logic |
| 17 | +abstract contract FeeManagerBase is IFeeManagerBase { |
| 18 | + using CustomRevert for bytes4; |
| 19 | + using FeeManagerStorage for FeeManagerStorage.Layout; |
| 20 | + |
| 21 | + /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ |
| 22 | + /* INTERNAL CALCULATIONS */ |
| 23 | + /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ |
| 24 | + |
| 25 | + /// @notice Calculates base fee before hook processing |
| 26 | + /// @param config Fee configuration |
| 27 | + /// @param amount Base amount for percentage calculations |
| 28 | + /// @return baseFee The calculated base fee |
| 29 | + function _calculateBaseFee( |
| 30 | + FeeConfig storage config, |
| 31 | + uint256 amount |
| 32 | + ) internal view returns (uint256 baseFee) { |
| 33 | + FeeCalculationMethod method = config.method; |
| 34 | + if (method == FeeCalculationMethod.FIXED) { |
| 35 | + return config.fixedFee; |
| 36 | + } else if (method == FeeCalculationMethod.PERCENT) { |
| 37 | + return BasisPoints.calculate(amount, config.bps); |
| 38 | + } else if (method == FeeCalculationMethod.HYBRID) { |
| 39 | + uint256 percentFee = BasisPoints.calculate(amount, config.bps); |
| 40 | + return FixedPointMathLib.max(percentFee, config.fixedFee); |
| 41 | + } |
| 42 | + return 0; |
| 43 | + } |
| 44 | + |
| 45 | + /// @notice Calculates fee for estimation (view function) |
| 46 | + /// @dev Does not modify state, calls hook's calculateFee if configured |
| 47 | + /// @param feeType The type of fee to calculate |
| 48 | + /// @param user The address that would be charged |
| 49 | + /// @param amount The base amount for percentage calculations |
| 50 | + /// @param extraData Additional data passed to hooks |
| 51 | + /// @return finalFee The calculated fee amount |
| 52 | + function _calculateFee( |
| 53 | + bytes32 feeType, |
| 54 | + address user, |
| 55 | + uint256 amount, |
| 56 | + bytes calldata extraData |
| 57 | + ) internal view returns (uint256 finalFee) { |
| 58 | + FeeManagerStorage.Layout storage $ = FeeManagerStorage.getLayout(); |
| 59 | + FeeConfig storage config = $.feeConfigs[feeType]; |
| 60 | + |
| 61 | + // Check if fee is configured and enabled |
| 62 | + if (!config.enabled) return 0; |
| 63 | + |
| 64 | + // Calculate base fee |
| 65 | + uint256 baseFee = _calculateBaseFee(config, amount); |
| 66 | + |
| 67 | + // Apply hook if configured |
| 68 | + address hook = config.hook; |
| 69 | + if (hook != address(0)) { |
| 70 | + try IFeeHook(hook).calculateFee(feeType, user, baseFee, extraData) returns ( |
| 71 | + FeeHookResult memory result |
| 72 | + ) { |
| 73 | + return result.finalFee; |
| 74 | + } catch { |
| 75 | + // If hook fails, fall back to base fee |
| 76 | + return baseFee; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return baseFee; |
| 81 | + } |
| 82 | + |
| 83 | + /// @notice Charges fee and transfers it (state-changing) |
| 84 | + /// @dev Calls hook's onChargeFee if configured, then transfers currency |
| 85 | + /// @dev Note: `user` is metadata for hooks/events. Actual payment comes from msg.sender. |
| 86 | + /// @param feeType The type of fee to charge |
| 87 | + /// @param user The address for whom the fee is being charged (for hooks/events) |
| 88 | + /// @param amount The base amount for percentage calculations |
| 89 | + /// @param currency The currency contract (address(0) for native token) |
| 90 | + /// @param context Additional context passed to hooks |
| 91 | + /// @return finalFee The actual fee charged |
| 92 | + function _chargeFee( |
| 93 | + bytes32 feeType, |
| 94 | + address user, |
| 95 | + uint256 amount, |
| 96 | + address currency, |
| 97 | + uint256 maxFee, |
| 98 | + bytes calldata context |
| 99 | + ) internal virtual returns (uint256 finalFee) { |
| 100 | + FeeManagerStorage.Layout storage $ = FeeManagerStorage.getLayout(); |
| 101 | + FeeConfig storage config = _getFeeConfig(feeType); |
| 102 | + |
| 103 | + // Check if fee is configured and enabled |
| 104 | + if (!config.enabled) return 0; |
| 105 | + |
| 106 | + // Calculate base fee |
| 107 | + uint256 baseFee = _calculateBaseFee(config, amount); |
| 108 | + |
| 109 | + // Apply hook if configured |
| 110 | + address hook = config.hook; |
| 111 | + if (hook != address(0)) { |
| 112 | + FeeHookResult memory result = IFeeHook(hook).onChargeFee( |
| 113 | + feeType, |
| 114 | + user, |
| 115 | + baseFee, |
| 116 | + context |
| 117 | + ); |
| 118 | + |
| 119 | + finalFee = result.finalFee; |
| 120 | + } else { |
| 121 | + finalFee = baseFee; |
| 122 | + } |
| 123 | + |
| 124 | + // Enforce slippage protection |
| 125 | + if (finalFee > maxFee) FeeManager__ExceedsMaxFee.selector.revertWith(); |
| 126 | + |
| 127 | + // Convert address(0) to NATIVE_TOKEN for CurrencyTransfer library |
| 128 | + address feeCurrency = currency == address(0) ? CurrencyTransfer.NATIVE_TOKEN : currency; |
| 129 | + |
| 130 | + // For native token, validate msg.value matches maxFee |
| 131 | + if (feeCurrency == CurrencyTransfer.NATIVE_TOKEN && msg.value != maxFee) { |
| 132 | + CurrencyTransfer.MsgValueMismatch.selector.revertWith(); |
| 133 | + } |
| 134 | + |
| 135 | + // Transfer fee and/or refund excess if maxFee > 0 |
| 136 | + if (maxFee > 0) { |
| 137 | + address recipient = config.recipient; |
| 138 | + if (recipient == address(0)) recipient = $.protocolFeeRecipient; |
| 139 | + |
| 140 | + CurrencyTransfer.transferFeeWithRefund( |
| 141 | + feeCurrency, |
| 142 | + msg.sender, |
| 143 | + recipient, |
| 144 | + finalFee, |
| 145 | + maxFee |
| 146 | + ); |
| 147 | + |
| 148 | + if (finalFee > 0) { |
| 149 | + emit FeeCharged(feeType, user, currency, finalFee, recipient); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ |
| 155 | + /* INTERNAL CONFIGURATION */ |
| 156 | + /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ |
| 157 | + |
| 158 | + /// @notice Sets fee configuration |
| 159 | + /// @param feeType The fee type identifier |
| 160 | + /// @param recipient Fee recipient (uses global if zero address) |
| 161 | + /// @param method Calculation method |
| 162 | + /// @param bps Basis points (1-10000) |
| 163 | + /// @param fixedFee Fixed fee amount |
| 164 | + /// @param enabled Whether the fee is active |
| 165 | + function _setFeeConfig( |
| 166 | + bytes32 feeType, |
| 167 | + address recipient, |
| 168 | + FeeCalculationMethod method, |
| 169 | + uint16 bps, |
| 170 | + uint128 fixedFee, |
| 171 | + bool enabled |
| 172 | + ) internal { |
| 173 | + if (recipient == address(0)) FeeManager__InvalidRecipient.selector.revertWith(); |
| 174 | + if (bps > BasisPoints.MAX_BPS) FeeManager__InvalidBps.selector.revertWith(); |
| 175 | + |
| 176 | + FeeConfig storage config = _getFeeConfig(feeType); |
| 177 | + config.recipient = recipient; |
| 178 | + config.lastUpdated = uint48(block.timestamp); |
| 179 | + config.bps = bps; |
| 180 | + config.method = method; |
| 181 | + config.enabled = enabled; |
| 182 | + config.fixedFee = fixedFee; |
| 183 | + |
| 184 | + emit FeeConfigured(feeType, recipient, method, bps, fixedFee, enabled); |
| 185 | + } |
| 186 | + |
| 187 | + /// @notice Sets fee hook |
| 188 | + /// @param feeType The fee type identifier |
| 189 | + /// @param hook Address of the hook contract (zero to remove) |
| 190 | + function _setFeeHook(bytes32 feeType, address hook) internal { |
| 191 | + FeeConfig storage config = _getFeeConfig(feeType); |
| 192 | + config.hook = hook; |
| 193 | + emit FeeHookSet(feeType, hook); |
| 194 | + } |
| 195 | + |
| 196 | + /// @notice Sets protocol fee recipient |
| 197 | + /// @param recipient New protocol fee recipient |
| 198 | + function _setProtocolFeeRecipient(address recipient) internal { |
| 199 | + if (recipient == address(0)) FeeManager__InvalidRecipient.selector.revertWith(); |
| 200 | + FeeManagerStorage.Layout storage $ = FeeManagerStorage.getLayout(); |
| 201 | + $.protocolFeeRecipient = recipient; |
| 202 | + emit ProtocolFeeRecipientSet(recipient); |
| 203 | + } |
| 204 | + |
| 205 | + /*´:°•.°+.*•´.*:˚.°*.˚•´.°:°•.°•.*•´.*:˚.°*.˚•´.°:°•.°+.*•´.*:*/ |
| 206 | + /* INTERNAL GETTERS */ |
| 207 | + /*.•°:°.´+˚.*°.˚:*.´•*.+°.•°:´*.´•*.•°.•°:°.´:•˚°.*°.˚:*.´+°.•*/ |
| 208 | + |
| 209 | + /// @notice Returns fee configuration |
| 210 | + /// @param feeType The fee type identifier |
| 211 | + /// @return config The fee configuration |
| 212 | + function _getFeeConfig(bytes32 feeType) internal view returns (FeeConfig storage config) { |
| 213 | + return FeeManagerStorage.getLayout().feeConfigs[feeType]; |
| 214 | + } |
| 215 | + |
| 216 | + /// @notice Returns fee hook address |
| 217 | + /// @param feeType The fee type identifier |
| 218 | + /// @return hook The hook contract address |
| 219 | + function _getFeeHook(bytes32 feeType) internal view returns (address hook) { |
| 220 | + FeeConfig storage config = _getFeeConfig(feeType); |
| 221 | + return config.hook; |
| 222 | + } |
| 223 | + |
| 224 | + /// @notice Returns protocol fee recipient |
| 225 | + /// @return recipient The protocol fee recipient address |
| 226 | + function _getProtocolFeeRecipient() internal view returns (address recipient) { |
| 227 | + return FeeManagerStorage.getLayout().protocolFeeRecipient; |
| 228 | + } |
| 229 | +} |
0 commit comments