0

I am trying to call debug_traceTransaction in go by doing:

cl, err := ethclient.Dial("http://localhost:8545")
    
if err != nil {
    panic("Failed starting")
}

txnHash := common.HexToHash("0x60eb378a5ceeb61a72e06363e6f69f03a04b5dd0a76be51872f3f2563d5124e2")

encodedTxHash := hexutil.Encode(txnHash.Bytes())

var result interface{}

err = cl.CallContext(context.Background(), &result, "debug_traceTransaction", encodedTxHash, nil)

if err != nil {
    panic(err)
}

I am getting this error:

panic: invalid argument 0: json: cannot unmarshal non-string into Go value of type common.Hash
    
goroutine 1 [running]:
main.main()
        /root/atlas-erigon/atlas/main.go:432 +0x26c
exit status 2

How should I encode the transaction hash?

1 Answer 1

0

You can refer to this answer, that says:

Transaction tracing is not exposed via ethclient. You must connect over the regular rpc and invoke it as a regular call.

The reasoning for that is given here.

So, the revised code using the rpc would be:

package main

import (
    "context"
    "log"

    "github.com/ethereum/go-ethereum/common"
    "github.com/ethereum/go-ethereum/rpc"
)

func main() {
    rpcURL := "http://localhost:8545"

    txnHashHex := "0x60eb378a5ceeb61a72e06363e6f69f03a04b5dd0a76be51872f3f2563d5124e2"

    // Dial the Ethereum RPC endpoint
    cl, err := rpc.Dial(rpcURL)
    if err != nil {
        log.Fatalf("Failed to dial: %v", err)
    }

    // Convert the transaction hash from hex to bytes
    txnHash := common.HexToHash(txnHashHex)

    // Prepare the trace configuration
    traceConfig := map[string]interface{}{
        "tracer": "callTracer",
        "tracerConfig": map[string]interface{}{
            "withLog": true,
        },
    }

    // Call the debug_traceTransaction method
    result := new(interface{})
    err = cl.CallContext(context.Background(), result, "debug_traceTransaction", txnHash.Hex(), traceConfig)
    if err != nil {
        log.Fatalf("Failed to trace transaction: %v", err)
    }

    // Process the result as needed
    log.Printf("Trace result: %v", *result)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.