This repository has been archived by the owner on May 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 333
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(bindings): parse solidity custom errors (#163)
Co-authored-by: David <david@taiko.xyz>
- Loading branch information
1 parent
4b21027
commit 9a79127
Showing
6 changed files
with
151 additions
and
88 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 |
---|---|---|
@@ -1 +1 @@ | ||
796a13fe713b82afa813d4c27eb5559b66bd89e9 | ||
16993cdb081b831420c7e86d981afd11726197d1 |
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,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 | ||
} |
Large diffs are not rendered by default.
Oops, something went wrong.
Oops, something went wrong.