Skip to content

Commit

Permalink
Add unpack for evm revert errors (umbracle#151)
Browse files Browse the repository at this point in the history
* Add unpack for evm revert errors

* Rebase
  • Loading branch information
ferranbt authored Feb 15, 2022
1 parent 1d971ac commit 0b2d679
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
23 changes: 23 additions & 0 deletions abi/revert.go
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
}
18 changes: 18 additions & 0 deletions abi/revert_test.go
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)
}

0 comments on commit 0b2d679

Please sign in to comment.