From 3962af30e14ba7f5057d27f9085c854da811c9db Mon Sep 17 00:00:00 2001 From: wanger Date: Thu, 13 Mar 2025 01:01:22 +0800 Subject: [PATCH] feat: add TraceCallWithCallTracer and TraceTransactionWithCallTracer --- eth/tracers/native/call.go | 4 +++ ethclient/gethclient/gethclient.go | 41 ++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/eth/tracers/native/call.go b/eth/tracers/native/call.go index c2247d1ce4..b85df1b98e 100644 --- a/eth/tracers/native/call.go +++ b/eth/tracers/native/call.go @@ -38,6 +38,10 @@ func init() { tracers.DefaultDirectory.Register("callTracer", newCallTracer, false) } +type CallFrame struct { + callFrame +} + type callLog struct { Address common.Address `json:"address"` Topics []common.Hash `json:"topics"` diff --git a/ethclient/gethclient/gethclient.go b/ethclient/gethclient/gethclient.go index 02b2598b37..6397099b52 100644 --- a/ethclient/gethclient/gethclient.go +++ b/ethclient/gethclient/gethclient.go @@ -29,6 +29,8 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/hexutil" "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/eth/tracers" + "github.com/ethereum/go-ethereum/eth/tracers/native" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/rpc" ) @@ -348,3 +350,42 @@ func (o BlockOverrides) MarshalJSON() ([]byte, error) { } return json.Marshal(output) } + +// geth debug module + +// TraceTransactionWithCallTracer trace a transaction with call tracer +func (c *Client) TraceTransactionWithCallTracer(ctx context.Context, txHash common.Hash, config *tracers.TraceCallConfig) (*native.CallFrame, error) { + // var result interface{} + var result native.CallFrame + tracer := "callTracer" + if config == nil { + config = &tracers.TraceCallConfig{ + TraceConfig: tracers.TraceConfig{ + Tracer: &tracer, + }, + } + } + err := c.c.CallContext(ctx, &result, "debug_traceTransaction", txHash, config) + if err != nil { + return nil, err + } + return &result, nil +} + +// TraceCallWithCallTracer trace a call with call tracer +func (c *Client) TraceCallWithCallTracer(ctx context.Context, args ethereum.CallMsg, config *tracers.TraceCallConfig, blockNumber *big.Int) (*native.CallFrame, error) { + var result native.CallFrame + if config == nil { + tracer := "callTracer" + config = &tracers.TraceCallConfig{ + TraceConfig: tracers.TraceConfig{ + Tracer: &tracer, + }, + } + } + err := c.c.CallContext(ctx, &result, "debug_traceCall", toCallArg(args), toBlockNumArg(blockNumber), config) + if err != nil { + return nil, err + } + return &result, nil +}