Skip to main content

Indexing ERC20 Token Transfers on Base

Introduction

In this tutorial, you'll learn how to index ERC20 token transfers on the Base network using Envio HyperIndex. By leveraging the no-code contract import feature, you'll be able to quickly analyze USDC transfer activity, including identifying the largest transfers.

We'll create an indexer that tracks all USDC token transfers on Base by extracting the Transfer events emitted by the USDC contract. The entire process takes less than 5 minutes to set up and start querying data.

Prerequisites

Before starting, ensure you have the following installed:

  • Node.js (v18 or newer)
  • pnpm (v8 or newer)
  • Docker Desktop

For detailed installation instructions, see the Prerequisites section.

Step 1: Initialize Your Indexer

  1. Open your terminal in an empty directory and run:
pnpx envio init
  1. Name your indexer (we'll use "usdc-base-transfer-indexer" in this example):
Naming your indexer
  1. Choose your preferred language (TypeScript, JavaScript, or ReScript):
Selecting TypeScript

Step 2: Import the USDC Token Contract

  1. Select Contract ImportBlock ExplorerBase

  2. Enter the USDC token contract address on Base:

    0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913

    View on BaseScan

  3. Select the Transfer event:

    • Navigate using arrow keys (↑↓)
    • Press spacebar to select the event
Selecting Transfer event

Tip: You can select multiple events to index simultaneously if needed.

  1. When finished adding contracts, select I'm finished
Completing contract import

Step 3: Start Your Indexer

  1. If you have any running indexers, stop them first:
pnpm envio stop

Note: You can skip this step if this is your first time running an indexer.

  1. Start your new indexer:
pnpm envio dev

This command:

  • Starts the required Docker containers
  • Sets up your database
  • Launches the indexing process
  • Opens the Hasura GraphQL interface

Step 4: Understanding the Generated Code

Let's examine the key files that Envio generated:

1. config.yaml

This configuration file defines:

  • Network to index (Base)
  • Starting block for indexing
  • Contract address and ABI details
  • Events to track (Transfer)
Config YAML file

2. schema.graphql

This schema defines the data structures for the Transfer event:

  • Entity types based on event data
  • Field types for sender, receiver, and amount
  • Any relationships between entities
GraphQL schema file

3. src/EventHandlers.*

This file contains the business logic for processing events:

  • Functions that execute when Transfer events are detected
  • Data transformation and storage logic
  • Entity creation and relationship management
Event handlers file

Step 5: Exploring Your Indexed Data

Now you can interact with your indexed USDC transfer data:

Accessing Hasura

  1. Open Hasura at http://localhost:8080
  2. When prompted, enter the admin password: testing
Hasura login

Monitoring Indexing Progress

  1. Click the Data tab in the top navigation
  2. Find the _events_sync_state table to check indexing progress
  3. Observe which blocks are currently being processed
Indexing progress

Note: Thanks to Envio's HyperSync, you can index millions of USDC transfers in just minutes rather than hours or days with traditional methods.

Querying Indexed Events

  1. Click the API tab
  2. Construct a GraphQL query to explore your data

Here's an example query to fetch the 10 largest USDC transfers:

query LargestTransfers {
FiatTokenV2_2_Transfer(limit: 10, order_by: { value: desc }) {
from
to
value
blockTimestamp
}
}
  1. Click the Play button to execute your query
Query results

Conclusion

Congratulations! You've successfully created an indexer for USDC token transfers on Base. In just a few minutes, you've indexed over 3.6 million transfer events and can now query this data in real-time.

What You've Learned

  • How to initialize an indexer using Envio's contract import feature
  • How to index ERC20 token transfers on the Base network
  • How to query and analyze token transfer data using GraphQL

Next Steps

  • Try customizing the event handlers to add additional logic
  • Create aggregated statistics about token transfers
  • Add more tokens or events to your indexer
  • Deploy your indexer to Envio's hosted service

For more tutorials and advanced features, check out our documentation or watch our video walkthrough on YouTube.