Created
May 18, 2020 07:38
-
-
Save gitpusha/648475e914001baa78ee3b9bd43838ef to your computer and use it in GitHub Desktop.
Solidity library to detect and convert returndata revert msg to UTF-8
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.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