ethclient: add RevertErrorData function #30669 (#1393)

This commit is contained in:
Daniel Liu 2025-08-28 18:58:46 +08:00 committed by GitHub
parent 0662224c31
commit 47520a1316
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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"