Skip to content

Instantly share code, notes, and snippets.

@drgorillamd
Created November 12, 2021 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drgorillamd/39f7e18438166e69499e02c4b6692285 to your computer and use it in GitHub Desktop.
Save drgorillamd/39f7e18438166e69499e02c4b6692285 to your computer and use it in GitHub Desktop.
delegatecall & custom error
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