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

# Tron batch processor

> Configure TronBatchProcessor data sources, requests, field selection, context, API keys, and Prometheus metrics.

`TronBatchProcessor` from `@subsquid/tron-processor` retrieves Tron blocks from the SQD Network v2 gateway, the Tron HTTP API, or both.

## Data sources

Configure at least one data source. Use both for fast historical sync plus recent blocks.

```ts theme={"system"}
import {TronBatchProcessor} from '@subsquid/tron-processor'

const processor = new TronBatchProcessor()
  .setGateway({
    url: 'https://v2.archive.subsquid.io/network/tron-mainnet',
    apiKey: process.env.SQD_API_KEY,
  })
  .setHttpApi({
    url: 'https://api.trongrid.io',
    strideConcurrency: 2,
    strideSize: 10,
    headPollInterval: 5_000,
  })
```

`setGateway()` also accepts a URL string. In that form the API key defaults to `process.env.SQD_API_KEY`. `GatewaySettings` supports `url`, `apiKey`, and `requestTimeout`.

## Requests

Request objects use `where` for filters, `include` for relations, and an optional `range`.

| Method                                 | `where` filters                      | `include` relations            |
| -------------------------------------- | ------------------------------------ | ------------------------------ |
| `addLog()`                             | `address`, `topic0` through `topic3` | `transaction`                  |
| `addTransaction()`                     | `type`                               | `logs`, `internalTransactions` |
| `addTransferTransaction()`             | `owner`, `to`                        | `logs`, `internalTransactions` |
| `addTransferAssetTransaction()`        | `owner`, `to`, `asset`               | `logs`, `internalTransactions` |
| `addTriggerSmartContractTransaction()` | `owner`, `contract`, `sighash`       | `logs`, `internalTransactions` |
| `addInternalTransaction()`             | `caller`, `transferTo`               | `transaction`                  |

```ts theme={"system"}
processor.addTriggerSmartContractTransaction({
  where: {
    contract: ['41a614f803b6fd780986a42c78ec9c7f77e6ded13c'],
    sighash: ['a9059cbb'],
  },
  include: {logs: true, internalTransactions: true},
})
```

Tron hexadecimal addresses and hashes do not use a `0x` prefix.

`includeAllBlocks(range?)` includes blocks without matching items. `setBlockRange({from, to?})` applies a global bound.

## Field selection

The top-level selector keys are singular:

```ts theme={"system"}
processor.setFields({
  block: {parentHash: true, timestamp: true},
  transaction: {hash: true, fee: true},
  log: {address: true, data: true, topics: true},
  internalTransaction: {
    hash: true,
    callerAddress: true,
    transferToAddress: true,
  },
})
```

Use `log`, not `logs`. The plural key is ignored because it is not part of `FieldSelection`.

## Handler context and block data

`processor.run(database, handler)` calls the handler with:

```ts theme={"system"}
type DataHandlerContext<Store, Fields> = {
  log: Logger
  store: Store
  blocks: Block<Fields>[]
  isHead: boolean
}
```

Each block contains `header`, `transactions`, `logs`, and `internalTransactions`. Logs and internal transactions expose an optional `transaction` plus `getTransaction()`. Transactions expose related `logs` and `internalTransactions` collections when requested.

Use `TronBatchProcessorFields<typeof processor>` to derive the selected field type.

## Prometheus

Use `setPrometheusServer(server)` for a custom server. `setPrometheusPort()` is deprecated and throws if a server was already configured. Without an explicit port, `PROCESSOR_PROMETHEUS_PORT` takes precedence over `PROMETHEUS_PORT`; otherwise the processor selects an ephemeral port.
