Skip to content

Commit

Permalink
ethclient: add RevertErrorData function and example (#30669)
Browse files Browse the repository at this point in the history
Here I'm adding a new helper function that extracts the revert reason of
a contract call. Unfortunately, this aspect of the API is underspecified.
See these spec issues for more detail:

- ethereum/execution-apis#232
- ethereum/execution-apis#463
- ethereum/execution-apis#523

The function added here only works with Geth-like servers that return
error code `3`. We will not be able to support all possible servers.
However, if there is a specific server implementation that makes it
possible to extract the same info, we could add it in the same function
as well.

---------

Co-authored-by: Marius van der Wijden <m.vanderwijden@live.de>
  • Loading branch information
fjl and MariusVanDerWijden authored Nov 7, 2024
1 parent 4bac6e6 commit e92e22a
Show file tree
Hide file tree
Showing 4 changed files with 294 additions and 164 deletions.
17 changes: 17 additions & 0 deletions ethclient/ethclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,23 @@ func (ec *Client) SendTransaction(ctx context.Context, tx *types.Transaction) er
return ec.c.CallContext(ctx, nil, "eth_sendRawTransaction", hexutil.Encode(data))
}

// RevertErrorData returns the 'revert reason' data of a contract call.
//
// This can be used with CallContract and EstimateGas, and only when the server is Geth.
func RevertErrorData(err error) ([]byte, bool) {
var ec rpc.Error
var ed rpc.DataError
if errors.As(err, &ec) && errors.As(err, &ed) && ec.ErrorCode() == 3 {
if eds, ok := ed.ErrorData().(string); ok {
revertData, err := hexutil.Decode(eds)
if err == nil {
return revertData, true
}
}
}
return nil, false
}

func toBlockNumArg(number *big.Int) string {
if number == nil {
return "latest"
Expand Down
Loading

0 comments on commit e92e22a

Please sign in to comment.