Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
feat(bindings): parse solidity custom errors (#163)
Browse files Browse the repository at this point in the history
Co-authored-by: David <david@taiko.xyz>
  • Loading branch information
alexshliu and davidtaikocha authored Feb 22, 2023
1 parent 4b21027 commit 9a79127
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 88 deletions.
2 changes: 1 addition & 1 deletion bindings/.githead
Original file line number Diff line number Diff line change
@@ -1 +1 @@
796a13fe713b82afa813d4c27eb5559b66bd89e9
16993cdb081b831420c7e86d981afd11726197d1
50 changes: 50 additions & 0 deletions bindings/error/error.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package error

import (
"encoding/json"
"errors"
"fmt"

"github.com/ethereum/go-ethereum/crypto"
)

// Taken from: https://github.com/ethereum/go-ethereum/blob/master/rpc/json.go
type JsonRPCError struct {
Code int `json:"code"`
Message string `json:"message"`
Data interface{} `json:"data,omitempty"`
}

// Error implements the Go error interface.
func (err *JsonRPCError) Error() string {
if err.Message == "" {
return fmt.Sprintf("json-rpc error %d", err.Code)
}
return err.Message
}

// GetRevertReasonHash returns a solidity contract call revert reason hash.
func GetRevertReasonHash(err error) (string, error) {
bytes, err := json.Marshal(errors.Unwrap(err))
if err != nil {
return "", err
}
rpcError := new(JsonRPCError)
if err = json.Unmarshal(bytes, rpcError); err != nil {
return "", err
}
reasonHash, ok := rpcError.Data.(string)
if !ok {
return "", fmt.Errorf("invalid revert reason, %T", rpcError.Data)
}
return reasonHash, nil
}

// CheckExpectRevertReason checks if the revert reason in solidity contracts matches the expectation.
func CheckExpectRevertReason(expect string, revertErr error) (bool, error) {
reason, err := GetRevertReasonHash(revertErr)
if err != nil {
return false, err
}
return fmt.Sprintf("%#x", crypto.Keccak256([]byte(expect)))[:10] == reason, nil
}
146 changes: 84 additions & 62 deletions bindings/gen_taiko_l1.go

Large diffs are not rendered by default.

Loading

0 comments on commit 9a79127

Please sign in to comment.