debug_traceCall | Avalanche

Avalanche API method that traces the execution of eth_call within the context of a specific block's execution. This method uses the final state of the parent block as its base and allows developers to trace the execution of a particular call.

πŸ“˜

Learn how to deploy a node with the debug and trace API methods enabled.

Parameters

  • object β€” the transaction call object:

    • from β€” (optional) the string of the address used to send the transaction.
    • to β€” the string of the address to which the transaction is directed, a wallet, or a smart contract.
    • gas β€” (optional) the maximum amount of gas that can be used by the transaction.
    • gasprice β€” (optional) the amount of gas price the sender is willing to pay for each gas unit in Wei.
    • value β€” (optional) the value sent with this transaction, encoded as hexadecimal.
    • data β€” (optional) additional data to be sent with the call, usually used to invoke functions from smart contracts as a string of the hash of the method signature and encoded parameters; see the Ethereum Contract ABI.
  • quantity or tag β€” the integer of a block encoded as hexadecimal or the string with:

    • latest β€” the most recent block in the blockchain and the current state of the blockchain at the most recent block

    • earliest β€” the earliest available or genesis block

    • pending β€” the pending state and transactions block. The current state of transactions that have been broadcast to the network but have not yet been included in a block.

      πŸ“˜

      See the default block parameter.

  • object β€” (optional) an object identifying the type of tracer and its configuration:

    • 4byteTracer β€” tracer that captures the function signatures and call data sizes for all functions executed during a transaction, creating a map that links each selector and size combination to the number of times it occurred. This provides valuable information about the frequency and usage of each function within the transaction.

    • callTracer β€” tracer that captures information on all call frames executed during a transaction. The resulting nested list of call frames is organized into a tree structure that reflects the way the Ethereum Virtual Machine works and can be used for debugging and analysis purposes.

    • prestateTracer β€” tracer with two modes: prestate and diff, where the former returns the accounts needed to execute a transaction, and the latter returns the differences between the pre and post-states of the transaction. The tracer operates by re-executing the transaction and tracking every state change made, resulting in an object with the account addresses as keys and the corresponding trie leaves as values.

      πŸ“˜

      Find a complete list of available built-in tracers in the debug and trace overview.

You can also use additional configuration parameters. The following settings are available:

  • disableStorage β€” when enabled, it prevents tracing of storage changes made by the transaction being analyzed, which can reduce the resource requirements of the analysis. By default, debug_traceTransaction traces both memory and storage changes, but storage tracing can be particularly resource-intensive, especially for large transactions.

  • disableStack β€” when enabled, it skips tracing of stack changes made by the transaction being analyzed. This can be helpful in optimizing performance and resource usage, especially when analyzing complex transactions, but may result in reduced analysis accuracy that should be considered.

  • enableMemory β€” when false, it prevents tracing of memory changes made by the transaction being analyzed, which can reduce the resource requirements of the analysis. This can be helpful in optimizing performance and resource usage, especially when analyzing complex transactions, but may result in reduced analysis accuracy that should be considered.

  • enableReturnData β€” when false, it prevents the method from tracing the return data produced by the transaction being analyzed. The return data tracing can be particularly resource-intensive, requiring a significant amount of time and processing power to complete.

  • timeout β€” setting that allows developers to customize the method's timeout period for JavaScript-based tracing calls. The default timeout is 5s, and you can find the values formats in the Go documentation.

πŸ“˜

It's worth noting that when using one of the three built-in tracers in Polygon clients, the enableMemory, disableStorage, disableStack, and enableReturnDatasettings will not have any effect.

When no built-in tracer is selected, the response defaults to the Struct/opcode logger.

Response types

4byteTracer response

  • object β€” the 4byteTracer traces object:
    • result β€” a map of the function signature, the call data size, and how many times the function was called.

callTracer response

  • object β€” the callTracer traces object:
    • from β€” the address of the sender who initiated the transaction.
    • gas β€” the units of gas included in the transaction by the sender.
    • gasused β€” the total used gas by the call, encoded as hexadecimal.
    • to β€” the address of the recipient of the transaction if it was a transaction to an address. For contract creation transactions, this field is null.
    • input β€” the optional input data sent with the transaction., usually used to interact with smart contracts.
    • output β€” the return value of the call, encoded as a hexadecimal string.
    • error β€” an error message in case the execution failed.
    • revertReason β€” the reason why the transaction was reverted, returned by the smart contract, if any.
    • calls β€” a list of sub-calls made by the contract during the call, each represented as a nested call frame object.

prestateTracer response

  • object β€” the prestateTracer traces object:
    • smart contract address β€” the address of the smart contract associated with the result.
      • balance β€” the balance of the contract, expressed in Wei and encoded as a hexadecimal string.
      • code β€” the bytecode of the contract, encoded as a hexadecimal string.
      • nonce β€” the nonce of the account associated with the contract, represented as an unsigned integer.
      • storage β€” a map of key-value pairs representing the storage slots of the contract. The keys and values are both encoded as hexadecimal strings.

Struct/opcode logger response

  • object β€” the Struct/opcode logger traces object:
    • pc β€” the current program counter of the transaction, which represents the location of the current opcode being executed. A register that keeps track of the address of the next instruction to be executed in a program.
    • op β€” the current opcode being executed, such as PUSH1, ADD, or SWAP1. A code that represents a specific operation or instruction that a computer or processor can understand and execute.
    • gas β€” the amount of gas remaining in the transaction at the current step of execution.
    • gasCost β€” the amount of gas consumed by the current opcode being executed.
    • depth β€” the current depth of the call stack for the transaction, reflecting the number of active frames representing nested function calls.
    • stack β€” the contents of the EVM stack at the current step of the transaction's execution. The stack is a data structure used by the EVM to manage and manipulate data during the execution of opcodes
    • memory β€” the contents of the memory at the current step of execution. A temporary data storage location that is available to smart contracts during execution.

debug_traceCall code examples

const Web3 = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

// Define the debug_traceCall custom method
web3.extend({
  property: 'debug',
  methods: [{
    name: 'traceCall',
    call: 'debug_traceCall',
    params: 3,
    inputFormatter: [web3.extend.formatters.inputCallFormatter, web3.extend.formatters.inputCallBlockFormatter, web3.extend.formatters.inputCallTraceTypeFormatter],
    outputFormatter: web3.extend.formatters.outputCallFormatter
  }]
});

async function traceCall() {

  const call = {
    from: '0xbdd87538521123603205c2d14cf566851f244983',
    to: '0xd2d4189265b7af891524a66f650e4cd592357484',
    gas: '0x6DDD00',
    gasPrice: '0x62F762A9A',
    data: '0x157a5ea900000000000000000000000000000000000000000000000000000000000000fb000000000000000000000000000000000000000000000000000000000000012c000000000000000000000000bdd87538521123603205c2d14cf566851f2449830000000000000000000000000000000000000000000000000000000000000001'
  }

  const block = '0x1A284B9'

  // Specify the type of tracer: 4byteTracer, callTracer, or prestateTracer. Leave empty {} for Struct/opcode logger.
  const tracer = {'tracer' : '4byteTracer'};
  const result = await web3.debug.traceCall(call, block, tracer);
  console.log(result);
}

traceCall();
const ethers = require('ethers');
const NODE_URL = "CHAINSTACK_NODE_URL";
const provider = new ethers.JsonRpcProvider(NODE_URL);

const traceCall = async () => {

  const call = {
    from: '0xbdd87538521123603205c2d14cf566851f244983',
    to: '0xd2d4189265b7af891524a66f650e4cd592357484',
    gas: '0x6DDD00',
    gasPrice: '0x62F762A9A',
    data: '0x157a5ea900000000000000000000000000000000000000000000000000000000000000fb000000000000000000000000000000000000000000000000000000000000012c000000000000000000000000bdd87538521123603205c2d14cf566851f2449830000000000000000000000000000000000000000000000000000000000000001'
  }

  const block = '0x1A284B9'

  // Specify the type of tracer: 4byteTracer, callTracer, or prestateTracer. Leave empty {} for Struct/opcode logger.
  const tracer = {};
  const traces = await provider.send("debug_traceCall", [call, block, tracer]);
  console.log(traces);
};

traceCall()
from web3 import Web3  
node_url = "CHAINSTACK_NODE_URL" 
web3 = Web3.HTTPProvider(node_url)

call = {
    'from': '0xbdd87538521123603205c2d14cf566851f244983',
    'to': '0xd2d4189265b7af891524a66f650e4cd592357484',
    'gas': '0x6DDD00',
    'gasPrice': '0x62F762A9A',
    'data': '0x157a5ea900000000000000000000000000000000000000000000000000000000000000fb000000000000000000000000000000000000000000000000000000000000012c000000000000000000000000bdd87538521123603205c2d14cf566851f2449830000000000000000000000000000000000000000000000000000000000000001'
  }

block = '0x1A284B9'

# Specify the type of tracer: 4byteTracer, callTracer, or prestateTracer. Leave empty {} for Struct/opcode logger.
tracer = { "tracer": 'prestateTracer' }
tx_traces = web3.provider.make_request('debug_traceCall', [call, block, tracer])
print(tx_traces)

Use case

A practical use case for the debug_traceCall method is to inspect the internal transactions that happened during a smart contract call. For example, a developer can examine a past transaction using the callTracer tracer where an account used a proxy smart contract to deploy a new instance using the CREATE2 method.

πŸ“˜

Read Deploying a deterministic smart contract on Ethereum to learn more about the CREATE2 method and how to deploy a smart contract to a predetermined address.

The following code is an implementation of the logic to inspect internal transactions using web3.js:

const Web3 = require("web3");
const NODE_URL = "CHAINSTACK_NODE_URL";
const web3 = new Web3(NODE_URL);

// Define the debug_traceCall custom method
web3.extend({
  property: 'debug',
  methods: [{
    name: 'traceCall',
    call: 'debug_traceCall',
    params: 3,
    inputFormatter: [web3.extend.formatters.inputCallFormatter, web3.extend.formatters.inputCallBlockFormatter, web3.extend.formatters.inputCallTraceTypeFormatter],
    outputFormatter: web3.extend.formatters.outputCallFormatter
  }]
});

async function traceCall(call, block) {
  try {
    // Specify the type of tracer: 4byteTracer, callTracer, or prestateTracer.
    // Leave empty {} for Struct/opcode logger.
    const tracer = { tracer: 'callTracer' };

    // Trace the call
    const result = await web3.debug.traceCall(call, block, tracer);
    const calls = result.calls;

    console.log(`===== Internal Calls =====`);

    // Extract and display the desired properties from each result object using a loop
    for (const callObj of calls) {
      const from = callObj.from;
      const to = callObj.to;
      const type = callObj.type;

      console.log(`From: ${from}`);
      console.log(`To: ${to}`);
      console.log(`Type: ${type}\n`);
    }
  } catch (error) {
    console.error(`Error while tracing call: ${error}`);
  }
}

async function main() {
  try {
    const call = {
      from: '0x215462dc79523ac795216e1baa27586840fa9382',
      to: '0x0c30d3d66bc7c73a83fda929888c34dcb24fd599',
      gas: '0x773788',
      gasPrice: '0x5D21DBA00',
      data: '0xd7665a48000000000000000000000000b7499a92fc36e9053a4324affae59d333635d9c300000000000000000000000060781c2586d68229fde47564546784ab3faca982000000000000000000000000215462dc79523ac795216e1baa27586840fa938200000000000000000000000077e5ba3bb034290d014063edff5eaac8cd6ba33500000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000060000000000000000000000000eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee000000000000000000000000000000000000000000000029d36b4b517f24c0000000000000000000000000008b206547cfe6f35a77ddab2d6d97260765a349ef'
    };

    // Define the block number or block tag to trace
    const block = '0x1A28663';

    await traceCall(call, block);
  } catch (error) {
    console.error(`Error while executing main: ${error}`);
  }
}

main();

The traceCall function is an asynchronous function that takes two parameters: call and block. The call parameter is an object that contains the details of the call that needs to be traced. In this case, those are the details of a transaction that deployed a smart contract instance using the CREATE2 method on the Avalanche blockchain. The block parameter is the block number or block tag to trace.

The function first creates a tracer object that specifies the type of tracer to be used. In this case, it's set to callTracer, which means that the function will trace all the internal calls made by the smart contract during the execution of the specified call.

The function then calls the web3.debug.traceCall function (which was previously created using web3.extend) with the call, block, and tracer parameters. This function returns an object that contains information about the internal calls made by the smart contract during the execution of the specified call.

The function then loops through the array of calls in the result object and extracts the from, to, and type properties of each call object. The from property is the address of the sender of the call, the to property is the address of the smart contract being called, and the type property represents the type of internal calls, such as a contract creation or an external function call.

Finally, the function logs the extracted information to the console.

The main function is also an asynchronous function that simply calls the traceCall function with the appropriate parameters. It also wraps the function call in a try-catch block to handle any errors that might occur during execution.

Try the debug_traceCall RPC method yourself

Language
Click Try It! to start a request and see the response here!