Created
February 19, 2023 10:35
-
-
Save cleanunicorn/36bc355e08b3253ade6263913a51dbdd to your computer and use it in GitHub Desktop.
Decode output and make sure you don't fail
This file contains 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
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