Created
November 12, 2021 20:40
-
-
Save drgorillamd/39f7e18438166e69499e02c4b6692285 to your computer and use it in GitHub Desktop.
delegatecall & custom error
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
pragma solidity "0.8.7"; | |
contract CallMeAndIRevert { | |
error ErrorA(); // 0x5283ed77 | |
error ErrorB(); // 0x8a25c831 | |
function triggerErrorAorB() external { | |
if(block.timestamp % 2 == 0) revert ErrorA(); | |
else revert ErrorB(); | |
} | |
} | |
contract Caller { | |
address target; | |
constructor() { | |
target = address(new CallMeAndIRevert()); | |
} | |
function callA() external returns(string memory) { | |
bytes memory data = abi.encodeWithSelector(bytes4(keccak256("triggerErrorAorB()"))); // 0x7da48f70 | |
bytes32 err; | |
assembly { | |
let result := delegatecall(sub(gas(), 5000), sload(target.slot), add(data, 0x20), mload(data), mload(0x40), 32) | |
err := mload(mload(0x40)) | |
} | |
if(err == bytes32(abi.encodeWithSelector(bytes4(keccak256("ErrorA()"))))) return "ErrorA"; | |
if(err == bytes32(abi.encodeWithSelector(bytes4(keccak256("ErrorB()"))))) return "ErrorB"; | |
else return "gReAt SuCcEsS"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment