Created
May 18, 2020 07:36
-
-
Save gitpusha/ae58ed52dddc34bc447b7aafc9de1567 to your computer and use it in GitHub Desktop.
A solidity library to cast returndata into its string error message
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
// "SPDX-License-Identifier: UNLICENSED" | |
pragma solidity ^0.6.8; | |
library GelatoDebug { | |
function revertWithErrorString(bytes memory _bytes, string memory _tracingInfo) | |
internal | |
pure | |
{ | |
// 68: 32-location, 32-length, 4-ErrorSelector, UTF-8 err | |
if (_bytes.length % 32 == 4) { | |
bytes4 selector; | |
assembly { selector := mload(add(0x20, _bytes)) } | |
if (selector == 0x08c379a0) { // Function selector for Error(string) | |
assembly { _bytes := add(_bytes, 68) } | |
revert(string(abi.encodePacked(_tracingInfo, string(_bytes)))); | |
} else { | |
revert(string(abi.encodePacked(_tracingInfo, "NoErrorSelector"))); | |
} | |
} else { | |
revert(string(abi.encodePacked(_tracingInfo, "UnexpectedReturndata"))); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment