Skip to content

Commit 13413be

Browse files
fjlMariusVanDerWijden
authored andcommittedNov 19, 2024
ethclient: add RevertErrorData function and example (#30669)
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>
1 parent 31bc9e6 commit 13413be

File tree

4 files changed

+294
-164
lines changed

4 files changed

+294
-164
lines changed
 

‎ethclient/ethclient.go

+17
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,23 @@ func (ec *Client) SendTransaction(ctx context.Context, tx *types.Transaction) er
630630
return ec.c.CallContext(ctx, nil, "eth_sendRawTransaction", hexutil.Encode(data))
631631
}
632632

633+
// RevertErrorData returns the 'revert reason' data of a contract call.
634+
//
635+
// This can be used with CallContract and EstimateGas, and only when the server is Geth.
636+
func RevertErrorData(err error) ([]byte, bool) {
637+
var ec rpc.Error
638+
var ed rpc.DataError
639+
if errors.As(err, &ec) && errors.As(err, &ed) && ec.ErrorCode() == 3 {
640+
if eds, ok := ed.ErrorData().(string); ok {
641+
revertData, err := hexutil.Decode(eds)
642+
if err == nil {
643+
return revertData, true
644+
}
645+
}
646+
}
647+
return nil, false
648+
}
649+
633650
func toBlockNumArg(number *big.Int) string {
634651
if number == nil {
635652
return "latest"

0 commit comments

Comments
 (0)