> ## Documentation Index
> Fetch the complete documentation index at: https://docs.chainstack.com/llms.txt
> Use this file to discover all available pages before exploring further.

# trace_block | BNB Chain

> BNB API method that returns traces for all the transactions within a specific block. Available on BNB Chain via Chainstack JSON-RPC nodes.

BNB API method that returns traces for all the transactions within a specific block. Developers can use the `trace_block` method to gain insight into the behavior of smart contracts within a block, analyze gas usage, and optimize their contracts accordingly. This method is only available on an Erigon instance.

<Check>
  **Get your own node endpoint today**

  [Start for free](https://console.chainstack.com/) and get your app to production levels immediately. No credit card required.

  You can sign up with your GitHub, X, Google, or Microsoft account.
</Check>

## Parameters

* `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. A chain reorganization is to be expected.
  * `safe` — the block that received justification from the beacon chain. Although this block could be involved in a chain reorganization, it would necessitate either a coordinated attack by the majority of validators or an instance of severe propagation latency.
  * `finalized` — the block accepted as canonical by more than 2/3 of the validators. A chain reorganization is extremely unlikely, and it would require at least 1/3 of the staked ETH to be burned.
  * `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.

## Response

* `result` — an object containing the traces of all of the transactions in the block:
  * `action` — an object that describes the action taken by the transaction:
    * `from` — the address of the sender who initiated the transaction.
    * `callType` — the type of call, `call` or `delegatecall`, two ways to invoke a function in a smart contract. `call` creates a new environment for the function to work in, so changes made in that function won't affect the environment where the function was called. `delegatecall` doesn't create a new environment. Instead, it runs the function within the environment of the caller, so changes made in that function will affect the caller's environment.
    * `gas` — the units of gas included in the transaction by the sender.
    * `input` — the optional input data sent with the transaction, usually used to interact with smart contracts.
    * `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.
    * `value` — the value of the native token transferred along with the transaction, in Wei.
    * `blockHash` — the hash of the block in which the transaction was included.
    * `blockNumber` — the number of the block in which the transaction was included.
    * `error` — a string that indicates whether the transaction was successful or not. `null` if successful, `Reverted` if not.
    * `result` — an object that contains additional data about the execution of the transaction:
      * `gasUsed` — the total used gas by the call, encoded as hexadecimal.
      * `output` — the return value of the call, encoded as a hexadecimal string.
    * `subtraces` — the number of sub-traces created during execution. When a transaction is executed on the EVM, it may trigger additional sub-executions, such as when a smart contract calls another smart contract or when an external account is accessed.
    * `traceAddress` — an array that indicates the position of the transaction in the trace.
    * `transactionHash` — the hash of the transaction.
    * `transactionPosition` — the position of the transaction in the block.
    * `type` — the type of action taken by the transaction, `call` or `create`. `call`  is the most common type of trace and occurs when a smart contract invokes another contract's function. `create` represents the creation of a new smart contract. This type of trace occurs when a smart contract is deployed to the blockchain.

## `trace_block` code examples

<CodeGroup>
  ```javascript web3.js theme={"system"}
  const { Web3, Web3PluginBase } = require("web3");
  const NODE_URL = "CHAINSTACK_NODE_URL";
  const web3 = new Web3(NODE_URL);

  // Define the TraceBlockPlugin class
  class TraceBlockPlugin extends Web3PluginBase {
      pluginNamespace = 'erigon';

      async traceBlock(blockId) {
          return this.requestManager.send({
              method: 'trace_block',
              params: [blockId],
          });
      }
  }

  // Register the plugin
  web3.registerPlugin(new TraceBlockPlugin());

  async function traceBlock(block) {
      const result = await web3.erigon.traceBlock(block);
      console.log(result);
  }

  traceBlock("latest");
  ```

  ```javascript ethers.js theme={"system"}
  const ethers = require('ethers');
  const NODE_URL = "CHAINSTACK_NODE_URL";
  const provider = new ethers.JsonRpcProvider(NODE_URL);

  const traceBlock = async (block) => {
    const traces = await provider.send("trace_block", [block]);
    console.log(traces);
  };

  traceBlock("latest")
  ```

  ```python web3.py theme={"system"}
  from web3 import Web3  
  node_url = "CHAINSTACK_NODE_URL" 
  web3 = Web3.HTTPProvider(node_url)

  block = "latest"

  traces = web3.provider.make_request('trace_block', [block])
  print(traces)
  ```
</CodeGroup>

## Use case

A practical use case for the `trace_block` method on BNB Smart Chain can be to analyze the most recent block on the network. For example, developers can gain valuable insights into the block's behavior by examining the amount of gas supplied by all the transactions and the actual amount of gas utilized. This can be the first step to developing a chain explorer.


## OpenAPI

````yaml /openapi/bnb_node_api/trace_block.json POST /35848e183f3e3303c8cfeacbea831cab
openapi: 3.0.0
info:
  title: BNB Node API
  version: 1.0.0
  description: This is an API for interacting with an BNB node.
servers:
  - url: https://bsc-mainnet.core.chainstack.com
security: []
paths:
  /35848e183f3e3303c8cfeacbea831cab:
    post:
      tags:
        - upload
      summary: trace_block
      operationId: trace_block
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                id:
                  type: integer
                  default: 1
                jsonrpc:
                  type: string
                  default: '2.0'
                method:
                  type: string
                  default: trace_block
                params:
                  type: array
                  items:
                    anyOf:
                      - type: string
                        title: Block identifier
                        description: The block number or tag.
                  default:
                    - latest
      responses:
        '200':
          description: The block's trace.
          content:
            application/json:
              schema:
                type: object
                properties:
                  jsonrpc:
                    type: string
                  id:
                    type: integer
                  result:
                    type: object

````