forked from Gauddel/gelato-instadapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GelatoBytes.sol
executable file
·67 lines (63 loc) · 2.19 KB
/
GelatoBytes.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// "SPDX-License-Identifier: UNLICENSED"
pragma solidity 0.6.12;
library GelatoBytes {
function calldataSliceSelector(bytes calldata _bytes)
internal
pure
returns (bytes4 selector)
{
selector =
_bytes[0] |
(bytes4(_bytes[1]) >> 8) |
(bytes4(_bytes[2]) >> 16) |
(bytes4(_bytes[3]) >> 24);
}
function memorySliceSelector(bytes memory _bytes)
internal
pure
returns (bytes4 selector)
{
selector =
_bytes[0] |
(bytes4(_bytes[1]) >> 8) |
(bytes4(_bytes[2]) >> 16) |
(bytes4(_bytes[3]) >> 24);
}
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")));
}
}
function generateErrorString(bytes memory _bytes, string memory _tracingInfo)
internal
pure
returns (string memory)
{
// 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) }
return string(abi.encodePacked(_tracingInfo, string(_bytes)));
} else {
return string(abi.encodePacked(_tracingInfo, "NoErrorSelector"));
}
} else {
return string(abi.encodePacked(_tracingInfo, "UnexpectedReturndata"));
}
}
}