simplify ethclient

This commit is contained in:
aodhgan 2025-10-13 12:50:24 -07:00
parent 455f09d00a
commit 1d662b50a6

View file

@ -18,7 +18,6 @@
package ethclient package ethclient
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"errors" "errors"
@ -698,40 +697,36 @@ func (ec *Client) SendTransaction(ctx context.Context, tx *types.Transaction) er
return ec.c.CallContext(ctx, nil, "eth_sendRawTransaction", hexutil.Encode(data)) return ec.c.CallContext(ctx, nil, "eth_sendRawTransaction", hexutil.Encode(data))
} }
// SendRawTransactionSync submits a signed tx and waits for a receipt (or until // SendTransactionSync submits a signed tx and waits for a receipt (or until
// the optional timeout elapses on the server side). If timeout == 0, the server // the optional timeout elapses on the server side). If timeout == 0, the server
// uses its default. // uses its default.
func (ec *Client) SendRawTransactionSync( func (ec *Client) SendTransactionSync(
ctx context.Context, ctx context.Context,
tx *types.Transaction, tx *types.Transaction,
timeout time.Duration, timeout ...time.Duration,
) (*types.Receipt, error) { ) (*types.Receipt, error) {
var buf bytes.Buffer raw, err := tx.MarshalBinary()
if err := tx.EncodeRLP(&buf); err != nil { if err != nil {
return nil, err return nil, err
} }
return ec.SendRawTransactionSyncRaw(ctx, buf.Bytes(), timeout) return ec.SendRawTransactionSync(ctx, raw, timeout...)
} }
// SendRawTransactionSyncRaw is the low-level variant that takes the raw RLP. func (ec *Client) SendRawTransactionSync(
func (ec *Client) SendRawTransactionSyncRaw(
ctx context.Context, ctx context.Context,
rawTx []byte, rawTx []byte,
timeout time.Duration, timeout ...time.Duration,
) (*types.Receipt, error) { ) (*types.Receipt, error) {
var out *types.Receipt var ms *hexutil.Uint64
if len(timeout) > 0 && timeout[0] > 0 {
// Build params: raw bytes as hex, plus optional timeout as hexutil.Uint64 v := hexutil.Uint64(timeout[0] / time.Millisecond)
params := []any{hexutil.Bytes(rawTx)} ms = &v
if timeout > 0 {
t := hexutil.Uint64(timeout.Milliseconds())
params = append(params, t)
} }
var receipt types.Receipt
if err := ec.c.CallContext(ctx, &out, "eth_sendRawTransactionSync", params...); err != nil { if err := ec.c.CallContext(ctx, &receipt, "eth_sendRawTransactionSync", hexutil.Bytes(rawTx), ms); err != nil {
return nil, err return nil, err
} }
return out, nil return &receipt, nil
} }
// RevertErrorData returns the 'revert reason' data of a contract call. // RevertErrorData returns the 'revert reason' data of a contract call.