Skip to content

Instantly share code, notes, and snippets.

@cleanunicorn
Created February 19, 2023 10:35
Show Gist options
  • Save cleanunicorn/36bc355e08b3253ade6263913a51dbdd to your computer and use it in GitHub Desktop.
Save cleanunicorn/36bc355e08b3253ade6263913a51dbdd to your computer and use it in GitHub Desktop.
Decode output and make sure you don't fail
contract ToBeExecuted {
// Calling this method creates problems because it returns only 1 uint
// when 2 uint's are expected
function toCall() public returns (uint) {
return (1234);
}
}
contract Executor {
function call(address contract_) public returns (bool, bytes memory, bool, uint, uint) {
// Low level call
(bool ok, bytes memory response) = contract_.call(
abi.encodeWithSignature("toCall()")
);
// Try to decode response
try this.decodeResponse(response) returns (uint a_, uint b_) {
return (ok, response, true, a_, b_);
} catch {
return (ok, response, false, 0, 0);
}
}
function decodeResponse(bytes memory response) public view returns (uint, uint) {
(uint a, uint b) = abi.decode(response, (uint, uint));
return (a, b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment