debug_traceBlockByNumber - Base

The debug_traceBlockByNumber method returns traces for all transactions in a block, identified by block number. This is useful for analyzing all activity within a specific block when you know the block number.

Parameters

ParameterTypeRequiredDescription
blockNumberstringYesBlock number in hex, or "latest", "earliest", "pending"
traceOptionsobjectNoTracing options object

Trace Options

FieldTypeDescription
tracerstringTracer type: "callTracer", "prestateTracer", or custom JS
tracerConfigobjectConfiguration for the tracer
timeoutstringTimeout for the trace operation

Request

cURL

bashcurl --location --request POST 'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/' \
--header 'Content-Type: application/json' \
--data-raw '{
    "jsonrpc": "2.0",
    "method": "debug_traceBlockByNumber",
    "params": ["0x12d687f", {"tracer": "callTracer"}],
    "id": "getblock.io"
}'

axios-example.js

javascriptconst axios = require('axios');

const blockNumber = '0x12d687f';

const response = await axios.post('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/', {
    jsonrpc: '2.0',
    method: 'debug_traceBlockByNumber',
    params: [blockNumber, { tracer: 'callTracer' }],
    id: 'getblock.io'
}, {
    headers: { 'Content-Type': 'application/json' }
});

const traces = response.data.result;
console.log('Total Transactions Traced:', traces.length);
traces.forEach((trace, index) => {
    console.log(`Tx ${index}: ${trace.result.type}`);
});

trace_block.py

pythonimport requests

block_number = '0x12d687f'

response = requests.post(
    'https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/',
    headers={'Content-Type': 'application/json'},
    json={
        'jsonrpc': '2.0',
        'method': 'debug_traceBlockByNumber',
        'params': [block_number, {'tracer': 'callTracer'}],
        'id': 'getblock.io'
    }
)

result = response.json()
traces = result['result']
print(f'Total Transactions Traced: {len(traces)}')

main.rs

rustuse reqwest::Client;
use serde_json::{json, Value};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new();
    
    let response = client
        .post("https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/")
        .header("Content-Type", "application/json")
        .json(&json!({
            "jsonrpc": "2.0",
            "method": "debug_traceBlockByNumber",
            "params": ["0x12d687f", {"tracer": "callTracer"}],
            "id": "getblock.io"
        }))
        .send()
        .await?
        .json::<Value>()
        .await?;
    
    let traces = response["result"].as_array().unwrap();
    println!("Total Traces: {}", traces.len());
    Ok(())
}

Response

response.json

json{
    "jsonrpc": "2.0",
    "id": "getblock.io",
    "result": [
        {
            "txHash": "0x...",
            "result": {
                "type": "CALL",
                "from": "0x...",
                "to": "0x...",
                "value": "0x0",
                "gas": "0x...",
                "gasUsed": "0x...",
                "input": "0x...",
                "output": "0x...",
                "calls": []
            }
        }
    ]
}

Response Parameters

ParameterTypeDescription
jsonrpcstringJSON-RPC protocol version ("2.0")
idstringRequest identifier matching the request
resultarrayArray of transaction traces

Trace Object

FieldTypeDescription
txHashstringTransaction hash
resultobjectTrace result object

Use Cases

  • Sequential Analysis: Analyze blocks in sequence by number
  • Historical Research: Trace historical block activity
  • Gas Analysis: Profile gas usage per block
  • Indexing: Build transaction traces for indexing
  • Monitoring: Track activity at specific block heights

Error Handling

Error CodeMessageDescription
-32602Invalid paramsInvalid block number or options
-32603Internal errorTrace execution failed
-32000Block not foundBlock does not exist

Web3 Integration

ethers-trace.js

javascriptconst { ethers } = require('ethers');

const provider = new ethers.JsonRpcProvider('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/');

async function traceBlockByNumber(blockNumber) {
    // Convert number to hex if needed
    const blockHex = typeof blockNumber === 'number' 
        ? '0x' + blockNumber.toString(16) 
        : blockNumber;
    
    const traces = await provider.send('debug_traceBlockByNumber', [
        blockHex,
        { tracer: 'callTracer' }
    ]);
    
    console.log('Block:', blockNumber);
    console.log('Total Transactions:', traces.length);
    
    let totalGas = 0n;
    traces.forEach((trace, index) => {
        const gasUsed = BigInt(trace.result.gasUsed);
        totalGas += gasUsed;
        
        console.log(`\nTransaction ${index}:`);
        console.log('  Type:', trace.result.type);
        console.log('  Gas Used:', gasUsed.toString());
    });
    
    console.log('\nTotal Gas Used:', totalGas.toString());
    
    return traces;
}

// Trace latest block
async function traceLatestBlock() {
    const blockNumber = await provider.getBlockNumber();
    return traceBlockByNumber(blockNumber);
}

traceLatestBlock();

viem-trace.js

javascriptimport { createPublicClient, http, numberToHex } from 'viem';
import { base } from 'viem/chains';

const client = createPublicClient({
    chain: base,
    transport: http('https://shared.eu-central-1.getblock.io/<ACCESS-TOKEN>/')
});

async function traceBlockByNumber(blockNumber) {
    const blockHex = typeof blockNumber === 'bigint' 
        ? numberToHex(blockNumber) 
        : blockNumber;
    
    const traces = await client.request({
        method: 'debug_traceBlockByNumber',
        params: [blockHex, { tracer: 'callTracer' }]
    });
    
    console.log('Total Transactions:', traces.length);
    
    traces.forEach((trace, index) => {
        console.log(`Tx ${index}:`, {
            hash: trace.txHash,
            type: trace.result.type,
            gasUsed: parseInt(trace.result.gasUsed, 16)
        });
    });
    
    return traces;
}

// Trace specific block
traceBlockByNumber('0x12d687f');

// Trace latest
async function traceLatest() {
    const blockNumber = await client.getBlockNumber();
    return traceBlockByNumber(blockNumber);
}