15

I want to use debug.traceTransaction to get internal transactions from NodeJS using web3 API. I'm using web3 0.17 beta and I don't see any debug_traceTransaction on the code. Am I missing something or I have to call directly to the RPC?

Paul Exchange
  • 2,723
  • 3
  • 21
  • 26

2 Answers2

12

As documented in debug.traceTransaction(...), you can only access this API call using the geth JavaScript console or the JSON-RPC API. There is no web3 API that I know of. There are some unofficial extension web3 APIs, but I don't know if debug.traceTransaction is supported by these. Looks like you have to use JSON-RPC to access this data.


geth JavaScript API

Start geth with the appropriate parameters to enable RPC and the debug module:

geth --cache=1024 --rpc --rpcapi "eth,net,web3,debug" console

We can trace the transaction 0x3684f071b34da1116282ee88a106a8f2a266d273ef7d8964957f65128fb58d77 in the geth console:

> debug.traceTransaction("0x3684f071b34da1116282ee88a106a8f2a266d273ef7d8964957f65128fb58d77")
{
  gas: 45480,
  returnValue: "0000000000000000000000000000000000000000000000000000000000000001",
  structLogs: [{
      depth: 1,
      error: "",
      gas: 76741,
      gasCost: 3,
      memory: null,
      op: "PUSH1",
      pc: 0,
      stack: [],
      storage: {}
    ...


JSON-RPC API

And here is an example trace using the JSON-RPC API:

curl localhost:8545 -X POST --header 'Content-type: application/json' --data '{"jsonrpc":"2.0", "method":"debug_traceTransaction", "params":["0x3684f071b34da1116282ee88a106a8f2a266d273ef7d8964957f65128fb58d77", {}], "id":1}'
{"jsonrpc":"2.0","id":1,"result":{"gas":45480,"returnValue":"0000000000000000000000000000000000000000000000000000000000000001","structLogs":[{"pc":0,"op":"PUSH1","gas":76741,"gasCost":3,"depth":1,"error":"","stack":[],"memory":null,"storage":{}} ...


See also:



Update Dec 19 2016 Responding To @Avatar's Comment Below

(Including the comment below @Pablo Yabo's answer)

If I start geth using the following command line:

Iota:~ user$ geth --rpc console

And I try to execute the following curl command:

Iota:~ user$ curl localhost:8545 -X POST --header 'Content-type: application/json' --data '{"jsonrpc":"2.0", "method":"debug_traceTransaction", "params":["0x3684f071b34da1116282ee88a106a8f2a266d273ef7d8964957f65128fb58d77", {}], "id":1}'

I get the following result:

{"jsonrpc":"2.0","id":1,"error":{"code":-32601,"message":"The method debug_traceTransaction does not exist/is not available"}}

If instead I start geth with the following command:

Iota:~ user$  geth --rpc --rpcapi "eth,net,web3,debug" console

And I run the same curl command, I get the transaction trace as shown below:

{"jsonrpc":"2.0","id":1,"result":{"gas":45480,"returnValue":"0000000000000000000000000000000000000000000000000000000000000001","structLogs":[{"pc":0,"op":"PUSH1","gas":76741,"gasCost":3,"depth":1,"error":null,"stack":[],"memory":null,"storage":{}},{"pc":2,"op":"PUSH1","gas":76738,"gasCost":3,"depth":1,"error":null,"stack":["0000000000000000000000000000000000000000000000000000000000000060"],"memory":null,"storage":{}},{ ...

Also note that you cannot get the debug_traceTransaction information from the section of the blockchain that you have --fast synced.

BokkyPooBah
  • 40,274
  • 14
  • 123
  • 193
  • I get err == null but the result contains an error "Missing trie node fc764c15d494d80384b70d1eccf399af403eb7eb91f17da32bd1729d722374... (length: 82)". Does it mean that my blockchain is incomplete? – Paul Exchange Oct 20 '16 at 15:39
  • 1
    I confirm that was the case. After restarting the blockchain without any --fast modificator it works perfect. – Paul Exchange Oct 20 '16 at 17:10
  • 1
    Could we call debug.traceTransaction(...) through nodejs? The answer below confused my mind, thanks. – alper Dec 19 '16 at 09:58
  • @Avatar, see the update to my answer above. – BokkyPooBah Dec 19 '16 at 12:24
8

Just to ease things. In NodeJS:

var web3 = require('../tools/web3');
web3.web3.currentProvider.sendAsync({
    method: "debug_traceTransaction",
    params: ['0x3fac854179691e377fc1aa180b71a4033b6bb3bde2a7ef00bc8e78f849ad356e', {}],
    jsonrpc: "2.0",
    id: "2"
}, function (err, result) {
    ...
});

Works perfect! If your blockchain was downloaded with --fast you will have to download it again.

Paul Exchange
  • 2,723
  • 3
  • 21
  • 26
  • It gives the error as: "{ jsonrpc: '2.0', id: '2', error: { code: -32601, message: 'The method debug_traceTransaction does not exist/is not available' } }" – alper Dec 19 '16 at 09:56
  • Your blockchain was downloaded with --fast so it doesn't contain debug information. You should download it again without --fast. – Paul Exchange Jan 09 '17 at 13:59
  • I did not use --fast to download the blockchain. @Pablo Yabo – alper Jan 09 '17 at 14:35
  • It looks the debug information isn't there. Maybe it was downloaded by an old version which didn't get debug information. I suggest downloading it again. You can do it in a VM and you can test if it works as soon as it downloads the first block, you don't need to wait until finishes it. – Paul Exchange Jan 10 '17 at 15:42
  • i'm using infura instance and getting the same issue "The method debug_traceTransaction does not exist/is not available" – atul Apr 17 '20 at 15:48