Skip to main content

Tron Quickstart

This 5-minute tutorial shows you how to build a Tron indexer using Squid SDK. You’ll create a complete blockchain indexer that fetches, decodes, and serves Tron transaction and event data.

What you’ll build

Your Tron indexer (squid) will:
  • Fetch historical Tron transactions and logs from the SQD Network
  • Decode TRC20 token transfers and smart contract events
  • Save the data to a local PostgreSQL database
  • Start a GraphQL server with a rich API to query the indexed data
This tutorial focuses on Tron. For other chains, see the EVM Quickstart or Solana Quickstart.

Prerequisites

Before you begin, ensure you have:
1

Install Squid CLI

Install the Squid CLI globally:
npm i -g @subsquid/cli
Verify installation by running sqd --version
Squid CLI is a multi-purpose utility tool for scaffolding and managing indexers, both locally and in SQD Cloud.
2

Scaffold the indexer project

Create a new Tron squid project:
sqd init hello-tron-squid -t tron
cd hello-tron-squid
The template provides a pre-configured project structure for indexing Tron transactions and smart contract events.
3

Inspect the project structure

Explore the ./src folder:
src/
├── abi
   └── trc20.ts
├── main.ts
└── model
    ├── generated
   ├── index.ts
   └── transfer.model.ts
    └── index.ts
Key files explained: - src/abi/ - ABI utilities for decoding Tron smart contract data - src/model/ - TypeORM model classes auto-generated from schema.graphql for database operations - main.ts - Main executable containing data retrieval configuration and processing logic
The processor configuration for Tron:
main.ts
const processor = new TronBatchProcessor()
  // SQD Network gateway for Tron
  .setGateway("https://v2.archive.subsquid.io/network/tron-mainnet")
  // Optional RPC endpoint for real-time data
  .setRpcEndpoint("<tron-rpc-endpoint>")
  // Configure block range
  .setBlockRange({ from: 10000000 })
  // Add transaction logs to index
  .addLog({
    address: ["<contract-address>"],
    topic0: ["<event-topic>"],
  })
  // Select fields to retrieve
  .setFields({
    log: {
      transactionHash: true,
      data: true,
    },
    transaction: {
      hash: true,
    },
  });
The data processing and storage logic:
main.ts
processor.run(new TypeormDatabase({ supportHotBlocks: true }), async (ctx) => {
  const transfers = [];

  for (let block of ctx.blocks) {
    for (let log of block.logs) {
      // Decode TRC20 transfer event
      const { from, to, value } = decodeTRC20Transfer(log);

      transfers.push(
        new Transfer({
          id: log.id,
          blockNumber: block.header.height,
          timestamp: new Date(block.header.timestamp),
          transactionHash: log.transactionHash,
          from,
          to,
          value,
        })
      );
    }
  }

  // Batch insert all transfers
  await ctx.store.save(transfers);
});
4

Install dependencies and build

Install dependencies and build the project:
npm i
npm run build
Verify the build completed successfully by checking for the lib/ directory.
5

Start the database and processor

The processor continuously fetches data, decodes it, and stores it in PostgreSQL. All logic is defined in main.ts and is fully customizable.First, start a local PostgreSQL database (the template includes a Docker Compose file):
docker compose up -d
The processor connects to PostgreSQL using connection parameters from .env. Ensure the database is running before proceeding.
Apply database migrations:
npx squid-typeorm-migration apply
Then start the processor:
node -r dotenv/config lib/main.js
The indexer is now running and will begin processing Tron blocks.
6

Start the GraphQL API

Start the GraphQL API to serve the indexed data:
npx squid-graphql-server
The GraphQL playground is available at localhost:4350/graphql
7

Query the data

You can now query your indexed Tron data! Try this example query in the GraphQL playground:
{
  transfers(limit: 10) {
    id
    blockNumber
    timestamp
    from
    to
    value
  }
}
Congratulations! You’ve successfully built and deployed your first Tron blockchain indexer.