Skip to main content
V2 gateway requests require an API key and the latest @subsquid/tron-processor. The linked example’s 0.0.x dependency cannot forward SQD_API_KEY to the v2 gateway.
In this tutorial we will look into a squid that indexes USDT transfers on the Tron Network. Pre-requisites: Node.js v20 or newer, Git, Docker.

Download the project

1

Clone the example and install the latest processor

2

Configure the v2 API key

Add the key to the example’s .env file. dotenv/config loads it when the processor starts.

TronBatchProcessor configuration

Squid processor is a term with two meanings:
  1. The process responsible for retrieving and transforming the data.
  2. The main object implementing that process.
On Tron, TronBatchProcessor is the class that should be used for processor objects. Our first step is to create the processor and configure it to fetch USDT transfers data:
src/main.ts
See also the TronBatchProcessor reference. The next step is to configure data decoding and transformation.

Processing the data

The other part of the squid processor configuration is the callback function used to process batches of filtered data, the batch handler. It is typically defined within a processor.run() call, like this:
Here,
  • processor is the object described in the previous section
  • database is a Database implementation specific to the target data sink. We want to store the data in a PostgreSQL database and present with a GraphQL API, so we provide a TypeormDatabase object here.
  • ctx is the batch context object that exposes a batch of data (at ctx.blocks) and any data persistence facilities derived from db (at ctx.store). See Handler context and block data for details on how the data batches are presented.
Batch handler is where the raw on-chain data is decoded, transformed and persisted. Here is our definition:
src/main.ts
This goes through all the logs in the block, verifies that they originate from USDT_ADDRESS and have a TRANSFER_TOPIC as the first topic, decodes the log and saves a Transfer record with the obtained data to the database. See Prepare the store for more details on how the database interface works.
Notice that we used the SQD EVM codec and squid-evm-typegen-generated module to decode events. Since Tron events are fully EVM-compatible, this is the recommended way.This tooling requires that the hex values are prefixed with 0x, as shown above.
At this point the squid is ready for its first test run. Add a temporary block range after new TronBatchProcessor() so that the test is reproducible and exits:
src/main.ts
Then build the project, start PostgreSQL, apply the migration, and run the processor:
The processor should exit after block 70,000,010. Verify that decoded transfers were stored:
Remove setBlockRange() when you are ready to continue indexing toward the chain head. Full code can be found here.