Skip to content

Commit

Permalink
[#47] Create FunctionHandlerManager.sol and inherit in SafeProtocolMa…
Browse files Browse the repository at this point in the history
…nager
  • Loading branch information
akshay-ap committed Jul 27, 2023
1 parent f7aadcf commit 175d0b9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion contracts/SafeProtocolManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import {SafeProtocolAction, SafeTransaction, SafeRootAccess} from "./DataTypes.s
import {ISafeProtocolRegistry} from "./interfaces/Registry.sol";
import {RegistryManager} from "./base/RegistryManager.sol";
import {HooksManager} from "./base/HooksManager.sol";
import {FunctionHandlerManager} from "./base/FunctionHandlerManager.sol";
import {BaseGuard} from "@safe-global/safe-contracts/contracts/base/GuardManager.sol";
import {Enum} from "@safe-global/safe-contracts/contracts/common/Enum.sol";

/**
* @title SafeProtocolManager contract allows Safe users to set plugin through a Manager rather than directly enabling a plugin on Safe.
* Users have to first enable SafeProtocolManager as a plugin on a Safe and then enable other plugins through the mediator.
*/
contract SafeProtocolManager is ISafeProtocolManager, RegistryManager, HooksManager, BaseGuard {
contract SafeProtocolManager is ISafeProtocolManager, RegistryManager, HooksManager, FunctionHandlerManager, BaseGuard {
address internal constant SENTINEL_MODULES = address(0x1);

/**
Expand Down
44 changes: 44 additions & 0 deletions contracts/base/FunctionHandlerManager.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: LGPL-3.0-only
pragma solidity ^0.8.18;
import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";

/**
* @title This contract manages the function handlers for the Safe Account. The contract stores the
* information about Safe account, Function selectr and the function handler contract address.
*/
contract FunctionHandlerManager {
// Storage
/** @dev Mapping that stores information about Safe account, function selector, and address of the account.
*/
mapping(address => mapping(bytes4 => address)) public functionHandlers;

// Events
event FunctionHandlerChanged(address indexed safe, bytes4 indexed selector, address indexed functionHandler);

// Errors
error AddressDoesNotImplementFunctionHandlerInterface(address functionHandler);

/**
* @notice Returns the function handler for a Safe account and function selector.
* @param safe Address of the Safe account
* @param selector bytes4 function selector
* @return functionHandler Address of the contract to be set as a function handler
*/
function getFunctionHandler(address safe, bytes4 selector) external view returns (address functionHandler) {
functionHandler = functionHandlers[safe][selector];
}

/**
* @notice Sets the function handler for a Safe account and function selector. The msg.sender must be the account.
* @param selector bytes4 function selector
* @param functionHandler Address of the contract to be set as a function handler
*/
function setFunctionHandler(bytes4 selector, address functionHandler) external {
if (functionHandler != address(0) && !IERC165(functionHandler).supportsInterface(0x00000000)) {
revert AddressDoesNotImplementFunctionHandlerInterface(functionHandler);
}

functionHandlers[msg.sender][selector] = functionHandler;
emit FunctionHandlerChanged(msg.sender, selector, functionHandler);
}
}

0 comments on commit 175d0b9

Please sign in to comment.