github.com/lmittmann/w3@v0.20.0/module/debug/call_trace.go (about)

     1  package debug
     2  
     3  import (
     4  	"encoding/json"
     5  	"math/big"
     6  
     7  	"github.com/ethereum/go-ethereum/common"
     8  	"github.com/ethereum/go-ethereum/common/hexutil"
     9  	"github.com/lmittmann/w3/internal/module"
    10  	"github.com/lmittmann/w3/w3types"
    11  )
    12  
    13  // CallTraceCall requests the call trace of the given message.
    14  func CallTraceCall(msg *w3types.Message, blockNumber *big.Int, overrides w3types.State) w3types.RPCCallerFactory[*CallTrace] {
    15  	return module.NewFactory(
    16  		"debug_traceCall",
    17  		[]any{msg, module.BlockNumberArg(blockNumber), &traceConfig{Tracer: "callTracer", Overrides: overrides}},
    18  		module.WithArgsWrapper[*CallTrace](msgArgsWrapper),
    19  	)
    20  }
    21  
    22  // CallTraceTx requests the call trace of the transaction with the given hash.
    23  func CallTraceTx(txHash common.Hash, overrides w3types.State) w3types.RPCCallerFactory[*CallTrace] {
    24  	return module.NewFactory[*CallTrace](
    25  		"debug_traceTransaction",
    26  		[]any{txHash, &traceConfig{Tracer: "callTracer", Overrides: overrides}},
    27  	)
    28  }
    29  
    30  type CallTrace struct {
    31  	From         common.Address
    32  	To           common.Address
    33  	Type         string
    34  	Gas          uint64
    35  	GasUsed      uint64
    36  	Value        *big.Int
    37  	Input        []byte
    38  	Output       []byte
    39  	Error        string
    40  	RevertReason string
    41  	Calls        []*CallTrace
    42  }
    43  
    44  // UnmarshalJSON implements the [json.Unmarshaler].
    45  func (c *CallTrace) UnmarshalJSON(data []byte) error {
    46  	type call struct {
    47  		From         common.Address `json:"from"`
    48  		To           common.Address `json:"to"`
    49  		Type         string         `json:"type"`
    50  		Gas          hexutil.Uint64 `json:"gas"`
    51  		GasUsed      hexutil.Uint64 `json:"gasUsed"`
    52  		Value        *hexutil.Big   `json:"value"`
    53  		Input        hexutil.Bytes  `json:"input"`
    54  		Output       hexutil.Bytes  `json:"output"`
    55  		Error        string         `json:"error"`
    56  		RevertReason string         `json:"revertReason"`
    57  		Calls        []*CallTrace   `json:"calls"`
    58  	}
    59  
    60  	var dec call
    61  	if err := json.Unmarshal(data, &dec); err != nil {
    62  		return err
    63  	}
    64  
    65  	c.From = dec.From
    66  	c.To = dec.To
    67  	c.Type = dec.Type
    68  	c.Gas = uint64(dec.Gas)
    69  	c.GasUsed = uint64(dec.GasUsed)
    70  	if dec.Value != nil {
    71  		c.Value = (*big.Int)(dec.Value)
    72  	}
    73  	c.Input = dec.Input
    74  	c.Output = dec.Output
    75  	c.Error = dec.Error
    76  	c.RevertReason = dec.RevertReason
    77  	c.Calls = dec.Calls
    78  	return nil
    79  }
    80  
    81  type traceConfig struct {
    82  	Tracer    string        `json:"tracer"`
    83  	Overrides w3types.State `json:"stateOverrides,omitempty"`
    84  }