Skip to content

Commit

Permalink
[#4] Optimize gas usage by removing conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
akshay-ap committed Jul 3, 2023
1 parent e030e3c commit 926191d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
26 changes: 13 additions & 13 deletions contracts/SafeProtocolMediator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ contract SafeProtocolMediator is ISafeProtocolMediator {
error InvalidPrevModuleAddress(address module);
error ZeroPageSizeNotAllowed();

modifier onlyEnabledModule(ISafe safe) {
if (enabledModules[address(safe)][msg.sender].nextModulePointer == address(0)) {
modifier onlyEnabledModule(address safe) {
if (enabledModules[safe][msg.sender].nextModulePointer == address(0)) {
revert MoudleNotEnabled(msg.sender);
}
_;
Expand All @@ -58,20 +58,20 @@ contract SafeProtocolMediator is ISafeProtocolMediator {
/**
* @notice This function executes non-delegate call(s) on a safe if the module is enabled on the Safe.
* If any one of the actions fail, the transaction reverts.
* @param safe A Safe instance
* @param safe Address of the Safe instance
* @param transaction A struct of type SafeTransaction containing information of about the action(s) to be executed.
* Users can add logic to validate metahash through a transaction guard.
* @return data bytes types containing the result of the executed action.
*/
function executeTransaction(
ISafe safe,
address safe,
SafeTransaction calldata transaction
) external override onlyEnabledModule(safe) returns (bytes[] memory data) {
data = new bytes[](transaction.actions.length);
uint256 length = transaction.actions.length;
for (uint256 i = 0; i < length; ++i) {
SafeProtocolAction memory safeProtocolAction = transaction.actions[i];
(bool isActionSuccessful, bytes memory resultData) = safe.execTransactionFromModuleReturnData(
(bool isActionSuccessful, bytes memory resultData) = ISafe(safe).execTransactionFromModuleReturnData(
safeProtocolAction.to,
safeProtocolAction.value,
safeProtocolAction.data,
Expand All @@ -80,44 +80,44 @@ contract SafeProtocolMediator is ISafeProtocolMediator {

// Even if one action fails, revert the transaction.
if (!isActionSuccessful) {
revert ActionExecutionFailed(address(safe), transaction.metaHash, i);
revert ActionExecutionFailed(safe, transaction.metaHash, i);
} else {
data[i] = resultData;
}
}

emit ActionsExecuted(address(safe), transaction.metaHash, transaction.nonce);
emit ActionsExecuted(safe, transaction.metaHash, transaction.nonce);
}

/**
* @notice This function executes a delegate call on a safe if the module is enabled and
* root access it granted.
* @param safe A Safe instance
* @param safe Address of the Safe instance
* @param rootAccess A struct of type SafeRootAccess containing information of about the action to be executed.
* Users can add logic to validate metahash through a transaction guard.
* @return data bytes types containing the result of the executed action.
*/
function executeRootAccess(
ISafe safe,
address safe,
SafeRootAccess calldata rootAccess
) external override onlyEnabledModule(safe) returns (bytes memory data) {
SafeProtocolAction memory safeProtocolAction = rootAccess.action;

if (!ISafeProtocolModule(msg.sender).requiresRootAccess() || !enabledModules[address(safe)][msg.sender].rootAddressGranted) {
if (!ISafeProtocolModule(msg.sender).requiresRootAccess() || !enabledModules[safe][msg.sender].rootAddressGranted) {
revert ModuleRequiresRootAccess(msg.sender);
}

bool success;
(success, data) = safe.execTransactionFromModuleReturnData(
(success, data) = ISafe(safe).execTransactionFromModuleReturnData(
safeProtocolAction.to,
safeProtocolAction.value,
safeProtocolAction.data,
1
);
if (success) {
emit RootAccessActionExecuted(address(safe), rootAccess.metaHash);
emit RootAccessActionExecuted(safe, rootAccess.metaHash);
} else {
revert RootAccessActionExecutionFailed(address(safe), rootAccess.metaHash);
revert RootAccessActionExecutionFailed(safe, rootAccess.metaHash);
}
}

Expand Down
8 changes: 4 additions & 4 deletions contracts/interfaces/Mediator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@ interface ISafeProtocolMediator {
/**
* @notice This function allows enabled modules to execute non-delegate call transactions thorugh a Safe.
* It should validate the status of the module through the registry and allows only listed and non-flagged components to execute transactions.
* @param safe Instance of a Safe account
* @param safe Address of a Safe account
* @param transaction SafeTransaction instance containing payload information about the transaction
* @return data Array of bytes types returned upon the successful execution of all the actions. The size of the array will be the same as the size of the actions
* in case of succcessful execution. Empty if the call failed.
*/
function executeTransaction(ISafe safe, SafeTransaction calldata transaction) external returns (bytes[] memory data);
function executeTransaction(address safe, SafeTransaction calldata transaction) external returns (bytes[] memory data);

/**
* @notice This function allows enabled modules to execute delegate call transactions thorugh a Safe.
* It should validate the status of the module through the registry and allows only listed and non-flagged components to execute transactions.
* @param safe Instance of a Safe account
* @param safe Address of a Safe account
* @param rootAccess SafeTransaction instance containing payload information about the transaction
* @return data Arbitrary length bytes data returned upon the successful execution. The size of the array will be the same as the size of the actions
* in case of succcessful execution. Empty if the call failed.
*/
function executeRootAccess(ISafe safe, SafeRootAccess calldata rootAccess) external returns (bytes memory data);
function executeRootAccess(address safe, SafeRootAccess calldata rootAccess) external returns (bytes memory data);
}
4 changes: 2 additions & 2 deletions contracts/test/TestModule.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ contract TestModule is BaseTestModule {
ISafe safe,
SafeTransaction calldata safetx
) external returns (bytes[] memory data) {
(data) = mediator.executeTransaction(safe, safetx);
(data) = mediator.executeTransaction(address(safe), safetx);
}
}

Expand All @@ -38,6 +38,6 @@ contract TestModuleWithRootAccess is BaseTestModule {
ISafe safe,
SafeRootAccess calldata safeRootAccesstx
) external returns (bytes memory data) {
(data) = mediator.executeRootAccess(safe, safeRootAccesstx);
(data) = mediator.executeRootAccess(address(safe), safeRootAccesstx);
}
}

0 comments on commit 926191d

Please sign in to comment.