Last active
August 20, 2022 02:06
-
-
Save PedroCailleret/eb1116b85d5c7a4940a51c1d257edc7f 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: AGPL-3.0-only | |
| pragma solidity 0.8.4; | |
| /// @author 0xtr4aum4 | |
| /// @notice This is a quick demo on how to revert custom errors with inline assembly. | |
| /// @dev 0x3ececff8 | |
| error PushFailed(); | |
| uint256 constant errsigPushFailedV1 = ( | |
| 0x3ececff800000000000000000000000000000000000000000000000000000000 | |
| ); | |
| uint256 constant errsigPushFailedV2 = ( | |
| 0x3ececff8FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF | |
| ); | |
| contract AssemblyCustomError { | |
| address private og; | |
| constructor() { | |
| og = msg.sender; | |
| } | |
| /// @dev Cost: 23382 gas | |
| function yulPushV1() | |
| external | |
| view | |
| { | |
| assembly { | |
| if eq(caller(), sload(og.slot)) { | |
| mstore(0, errsigPushFailedV1) | |
| revert(0, 4) | |
| } | |
| } | |
| } | |
| /// @dev Cost: 23501 gas | |
| function sanePush() | |
| external | |
| view | |
| { | |
| if(msg.sender == og) revert PushFailed(); | |
| } | |
| ///////////////////////////////////////////// | |
| /// @dev Cost: 21236 gas | |
| function _yulPushV2() | |
| external | |
| pure | |
| { | |
| assembly { | |
| mstore(0, errsigPushFailedV2) | |
| revert(0, 4) | |
| } | |
| } | |
| /// @dev Cost: 21214 gas | |
| function _yulPushV3() | |
| external | |
| pure | |
| { | |
| assembly { | |
| mstore(0x00, 0x3ececff8) | |
| revert(0x1c, 0x04) | |
| } | |
| } | |
| /// @dev Cost: 21280 gas | |
| function _yulPushV4() | |
| external | |
| pure | |
| { | |
| assembly { | |
| mstore(0, "PushFailed") | |
| revert(0x00, 0x20) | |
| } | |
| } | |
| /// @dev Cost: 21222 gas | |
| function _sanePush() | |
| external | |
| pure | |
| { | |
| revert PushFailed(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment