From 47520a1316b0635288d89b9fa8bad7dec092c8ef Mon Sep 17 00:00:00 2001 From: Daniel Liu <139250065@qq.com> Date: Thu, 28 Aug 2025 18:58:46 +0800 Subject: [PATCH] ethclient: add RevertErrorData function #30669 (#1393) --- ethclient/ethclient.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index b3ef1322e4..7b9c66ccd9 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -638,6 +638,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"