From 8d6318977c08bec8015be370463f11903d2e3689 Mon Sep 17 00:00:00 2001 From: zeroknots Date: Wed, 21 Feb 2024 11:09:49 +0700 Subject: [PATCH] chore: gas optimizations in ExecutionHelper --- .../safe7579/src/core/ExecutionHelper.sol | 43 ++++++++++++++++--- accounts/safe7579/src/core/HookManager.sol | 3 ++ 2 files changed, 40 insertions(+), 6 deletions(-) diff --git a/accounts/safe7579/src/core/ExecutionHelper.sol b/accounts/safe7579/src/core/ExecutionHelper.sol index 0a2a0c8b..0dc64d53 100644 --- a/accounts/safe7579/src/core/ExecutionHelper.sol +++ b/accounts/safe7579/src/core/ExecutionHelper.sol @@ -13,6 +13,14 @@ import "../interfaces/ISafe.sol"; abstract contract ExecutionHelper { error ExecutionFailed(); + /** + * Execute call on Safe + * @dev This function will revert if the call fails + * @param safe address of the safe + * @param target address of the contract to call + * @param value value of the transaction + * @param callData data of the transaction + */ function _execute( address safe, address target, @@ -25,6 +33,15 @@ abstract contract ExecutionHelper { if (!success) revert ExecutionFailed(); } + /** + * Execute call on Safe, get return value from call + * @dev This function will revert if the call fails + * @param safe address of the safe + * @param target address of the contract to call + * @param value value of the transaction + * @param callData data of the transaction + * @return returnData data returned from the call + */ function _executeReturnData( address safe, address target, @@ -40,26 +57,40 @@ abstract contract ExecutionHelper { if (!success) revert ExecutionFailed(); } + /** + * Execute call on Safe + * @dev This function will revert if the call fails + * @param safe address of the safe + * @param executions ERC-7579 struct for batched executions + */ function _execute(address safe, Execution[] calldata executions) internal { uint256 length = executions.length; for (uint256 i; i < length; i++) { - _execute(safe, executions[i].target, executions[i].value, executions[i].callData); + Execution calldata execution = executions[i]; + _execute(safe, execution.target, execution.value, execution.callData); } } + /** + * Execute call on Safe + * @dev This function will revert if the call fails + * @param safe address of the safe + * @param executions ERC-7579 struct for batched executions + * @return returnDatas array returned datas from the batched calls + */ function _executeReturnData( address safe, Execution[] calldata executions ) internal - returns (bytes[] memory retData) + returns (bytes[] memory returnDatas) { uint256 length = executions.length; - retData = new bytes[](length); + returnDatas = new bytes[](length); for (uint256 i; i < length; i++) { - retData[i] = _executeReturnData( - safe, executions[i].target, executions[i].value, executions[i].callData - ); + Execution calldata execution = executions[i]; + returnDatas[i] = + _executeReturnData(safe, execution.target, execution.value, execution.callData); } } } diff --git a/accounts/safe7579/src/core/HookManager.sol b/accounts/safe7579/src/core/HookManager.sol index 841b5e03..c36fa52a 100644 --- a/accounts/safe7579/src/core/HookManager.sol +++ b/accounts/safe7579/src/core/HookManager.sol @@ -20,10 +20,13 @@ abstract contract HookManager is ModuleManager { address hook = $hookManager[msg.sender]; bool isHookEnabled = hook != address(0); bytes memory hookPreContext; + + // pre hook if (isHookEnabled) hookPreContext = _doPreHook(hook); _; // <-- hooked Function Bytecode here + // post hook if (isHookEnabled) _doPostHook(hook, hookPreContext); }