|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity >=0.8.18; |
| 3 | + |
| 4 | +import { IPRBProxy } from "./interfaces/IPRBProxy.sol"; |
| 5 | +import { IPRBProxyPlugin } from "./interfaces/IPRBProxyPlugin.sol"; |
| 6 | +import { IPRBProxyRegistry } from "./interfaces/IPRBProxyRegistry.sol"; |
| 7 | + |
| 8 | +/* |
| 9 | +
|
| 10 | +██████╗ ██████╗ ██████╗ ██████╗ ██████╗ ██████╗ ██╗ ██╗██╗ ██╗ |
| 11 | +██╔══██╗██╔══██╗██╔══██╗██╔══██╗██╔══██╗██╔═══██╗╚██╗██╔╝╚██╗ ██╔╝ |
| 12 | +██████╔╝██████╔╝██████╔╝██████╔╝██████╔╝██║ ██║ ╚███╔╝ ╚████╔╝ |
| 13 | +██╔═══╝ ██╔══██╗██╔══██╗██╔═══╝ ██╔══██╗██║ ██║ ██╔██╗ ╚██╔╝ |
| 14 | +██║ ██║ ██║██████╔╝██║ ██║ ██║╚██████╔╝██╔╝ ██╗ ██║ |
| 15 | +╚═╝ ╚═╝ ╚═╝╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ |
| 16 | +
|
| 17 | +*/ |
| 18 | + |
| 19 | +/// @title PRBProxy |
| 20 | +/// @dev See the documentation in {IPRBProxy}. |
| 21 | +contract PRBProxy is IPRBProxy { |
| 22 | + /*////////////////////////////////////////////////////////////////////////// |
| 23 | + CONSTANTS |
| 24 | + //////////////////////////////////////////////////////////////////////////*/ |
| 25 | + |
| 26 | + /// @inheritdoc IPRBProxy |
| 27 | + address public immutable override owner; |
| 28 | + |
| 29 | + /// @inheritdoc IPRBProxy |
| 30 | + IPRBProxyRegistry public immutable override registry; |
| 31 | + |
| 32 | + /*////////////////////////////////////////////////////////////////////////// |
| 33 | + CONSTRUCTOR |
| 34 | + //////////////////////////////////////////////////////////////////////////*/ |
| 35 | + |
| 36 | + /// @notice Creates the proxy by fetching the constructor params from the registry, optionally delegate calling |
| 37 | + /// to a target contract if one is provided. |
| 38 | + /// @dev The rationale of this approach is to have the proxy's CREATE2 address not depend on any constructor params. |
| 39 | + constructor() { |
| 40 | + registry = IPRBProxyRegistry(msg.sender); |
| 41 | + (address owner_, address target, bytes memory data) = registry.constructorParams(); |
| 42 | + owner = owner_; |
| 43 | + if (target != address(0)) { |
| 44 | + _execute(target, data); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /*////////////////////////////////////////////////////////////////////////// |
| 49 | + FALLBACK FUNCTIONS |
| 50 | + //////////////////////////////////////////////////////////////////////////*/ |
| 51 | + |
| 52 | + /// @notice Fallback function used to run plugins. |
| 53 | + /// @dev WARNING: anyone can call this function and thus run any installed plugin. |
| 54 | + fallback(bytes calldata data) external payable returns (bytes memory response) { |
| 55 | + // Check if the function selector points to a known installed plugin. |
| 56 | + IPRBProxyPlugin plugin = registry.getPluginByOwner({ owner: owner, method: msg.sig }); |
| 57 | + if (address(plugin) == address(0)) { |
| 58 | + revert PRBProxy_PluginNotInstalledForMethod({ caller: msg.sender, owner: owner, method: msg.sig }); |
| 59 | + } |
| 60 | + |
| 61 | + // Delegate call to the plugin. |
| 62 | + bool success; |
| 63 | + (success, response) = address(plugin).delegatecall(data); |
| 64 | + |
| 65 | + // Log the plugin run. |
| 66 | + emit RunPlugin(plugin, data, response); |
| 67 | + |
| 68 | + // Check if the call was successful or not. |
| 69 | + if (!success) { |
| 70 | + // If there is return data, the delegate call reverted with a reason or a custom error, which we bubble up. |
| 71 | + if (response.length > 0) { |
| 72 | + assembly { |
| 73 | + let returndata_size := mload(response) |
| 74 | + revert(add(32, response), returndata_size) |
| 75 | + } |
| 76 | + } else { |
| 77 | + revert PRBProxy_PluginReverted(plugin); |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /// @dev Called when `msg.value` is not zero and the call data is empty. |
| 83 | + receive() external payable { } |
| 84 | + |
| 85 | + /*////////////////////////////////////////////////////////////////////////// |
| 86 | + USER-FACING NON-CONSTANT FUNCTIONS |
| 87 | + //////////////////////////////////////////////////////////////////////////*/ |
| 88 | + |
| 89 | + /// @inheritdoc IPRBProxy |
| 90 | + function execute(address target, bytes calldata data) external payable override returns (bytes memory response) { |
| 91 | + // Check that the caller is either the owner or an envoy with permission. |
| 92 | + if (owner != msg.sender) { |
| 93 | + bool permission = registry.getPermissionByOwner({ owner: owner, envoy: msg.sender, target: target }); |
| 94 | + if (!permission) { |
| 95 | + revert PRBProxy_ExecutionUnauthorized({ owner: owner, caller: msg.sender, target: target }); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Delegate call to the target contract, and handle the response. |
| 100 | + response = _execute(target, data); |
| 101 | + } |
| 102 | + |
| 103 | + /*////////////////////////////////////////////////////////////////////////// |
| 104 | + INTERNAL NON-CONSTANT FUNCTIONS |
| 105 | + //////////////////////////////////////////////////////////////////////////*/ |
| 106 | + |
| 107 | + /// @notice Executes a DELEGATECALL to the provided target with the provided data. |
| 108 | + /// @dev Shared logic between the constructor and the `execute` function. |
| 109 | + function _execute(address target, bytes memory data) internal returns (bytes memory response) { |
| 110 | + // Check that the target is a contract. |
| 111 | + if (target.code.length == 0) { |
| 112 | + revert PRBProxy_TargetNotContract(target); |
| 113 | + } |
| 114 | + |
| 115 | + // Delegate call to the target contract. |
| 116 | + bool success; |
| 117 | + (success, response) = target.delegatecall(data); |
| 118 | + |
| 119 | + // Log the execution. |
| 120 | + emit Execute(target, data, response); |
| 121 | + |
| 122 | + // Check if the call was successful or not. |
| 123 | + if (!success) { |
| 124 | + // If there is return data, the delegate call reverted with a reason or a custom error, which we bubble up. |
| 125 | + if (response.length > 0) { |
| 126 | + assembly { |
| 127 | + // The length of the data is at `response`, while the actual data is at `response + 32`. |
| 128 | + let returndata_size := mload(response) |
| 129 | + revert(add(response, 32), returndata_size) |
| 130 | + } |
| 131 | + } else { |
| 132 | + revert PRBProxy_ExecutionReverted(); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments