internal/ethapi: expose revert reason

This commit is contained in:
Marius van der Wijden 2024-10-24 05:04:55 +02:00
parent 50a02c49d9
commit 410510ac0c

View file

@ -29,10 +29,16 @@ import (
// revertError is an API error that encompasses an EVM revert with JSON error // revertError is an API error that encompasses an EVM revert with JSON error
// code and a binary data blob. // code and a binary data blob.
type revertError struct { type revertError struct {
error
reason string // revert reason hex encoded reason string // revert reason hex encoded
} }
func (e *revertError) Error() string {
if e.reason != "" {
return fmt.Sprintf("%s: %v", vm.ErrExecutionReverted, e.reason)
}
return vm.ErrExecutionReverted.Error()
}
// ErrorCode returns the JSON error code for a revert. // ErrorCode returns the JSON error code for a revert.
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal // See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal
func (e *revertError) ErrorCode() int { func (e *revertError) ErrorCode() int {
@ -46,16 +52,12 @@ func (e *revertError) ErrorData() interface{} {
// newRevertError creates a revertError instance with the provided revert data. // newRevertError creates a revertError instance with the provided revert data.
func newRevertError(revert []byte) *revertError { func newRevertError(revert []byte) *revertError {
var err error reason := hexutil.Encode(revert)
reason, errUnpack := abi.UnpackRevert(revert) if r, errUnpack := abi.UnpackRevert(revert); errUnpack == nil {
if errUnpack == nil { reason = r
err = fmt.Errorf("%w: %v", vm.ErrExecutionReverted, reason)
} else {
err = fmt.Errorf("%w: %v", vm.ErrExecutionReverted, hexutil.Encode(revert))
} }
return &revertError{ return &revertError{
error: err, reason: reason,
reason: hexutil.Encode(revert),
} }
} }