forked from umbracle/ethgo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unpack for evm revert errors (umbracle#151)
* Add unpack for evm revert errors * Rebase
- Loading branch information
Showing
2 changed files
with
41 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package abi | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
) | ||
|
||
var revertId = []byte{0x8, 0xC3, 0x79, 0xA0} | ||
|
||
func UnpackRevertError(b []byte) (string, error) { | ||
if !bytes.HasPrefix(b, revertId) { | ||
return "", fmt.Errorf("revert error prefix not found") | ||
} | ||
|
||
b = b[4:] | ||
tt := MustNewType("tuple(string)") | ||
vals, err := tt.Decode(b) | ||
if err != nil { | ||
return "", err | ||
} | ||
revVal := vals.(map[string]interface{})["0"].(string) | ||
return revVal, nil | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package abi | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestUnpackRevertError(t *testing.T) { | ||
data := "08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000d72657665727420726561736f6e00000000000000000000000000000000000000" | ||
|
||
raw, err := decodeHex(data) | ||
assert.NoError(t, err) | ||
|
||
reason, err := UnpackRevertError(raw) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "revert reason", reason) | ||
} |