Created
August 30, 2023 08:34
-
-
Save MiloTruck/d8f9b75dc4105823dde841f06af87fbf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: UNLICENSED | |
pragma solidity 0.8.19; | |
library Execution { | |
function functionCallWithValue( | |
address to, | |
uint256 value, | |
bytes memory data | |
) internal returns (bytes memory) { | |
assembly { | |
// Make a low-level call | |
let result := call(gas(), to, value, add(data, 0x20), mload(data), 0, 0) | |
// Copy the returned data. | |
returndatacopy(0, 0, returndatasize()) | |
// Revert on error, otherwise return data from the call | |
switch result | |
case 0 { | |
revert(0, returndatasize()) | |
} | |
default { | |
return(0, returndatasize()) | |
} | |
} | |
} | |
} | |
contract Example { | |
function execute( | |
address receiver, | |
uint256 value, | |
bytes memory data | |
) public payable returns (bytes memory) { | |
// If there's no data, send value to receiver address | |
if (data.length == 0) { | |
return Execution.functionCallWithValue(receiver, value, data); | |
} | |
// Some other functionality here... | |
} | |
function batchExecute( | |
address[] memory receiver, | |
uint256[] memory value, | |
bytes[] memory data | |
) external payable { | |
require( | |
receiver.length == value.length && receiver.length == data.length, | |
"Arrays lengths unequal" | |
); | |
for (uint256 i; i < receiver.length; i++) { | |
execute(receiver[i], value[i], data[i]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment