Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Indexing Protocols & On-Chain Analytics

Hello! In our previous lesson, we explored how decentralized oracles like Chainlink solve the critical problem of bringing external, real-world data onto the blockchain. Today, we'll tackle the reverse challenge: how to efficiently get data off the blockchain to power analytics, dashboards, and complex user interfaces.

Given your background in business intelligence and data warehousing, you're deeply familiar with the challenges of querying raw, transactional data. You know that to build responsive reports and analytics in PowerBI, you don't query the operational database directly; you query a structured, indexed data warehouse built through ETL processes. Blockchains present a similar, yet more pronounced, problem.

This lesson will explain the role of indexing protocols, focusing on The Graph, the industry standard. You will learn how The Graph acts as a decentralized ETL and data warehousing layer for blockchains, enabling the kind of sophisticated data analytics and application front-ends that are essential for the tokenized money use cases you aim to build.

1. The Challenge: Querying a Write-Optimized System

Blockchains like Ethereum are designed primarily as secure, decentralized transaction ledgers. They are highly optimized for writing data (i.e., processing and confirming transactions) and ensuring every node in the network reaches the same state. They are not, however, designed for the complex read queries required by modern applications.

Trying to build a rich application, like a portfolio tracker or an analytics dashboard, by making direct calls to a blockchain node (via JSON-RPC) is analogous to running complex analytical queries against a live, non-indexed transactional database. It is possible, but incredibly inefficient, slow, and expensive. You would need to make numerous calls, process raw event logs, and stitch together historical states within your application's logic.

This excellent article from Startupik frames the problem perfectly.

The Graph Workflow: How to Query Blockchain Data ...

This article clearly articulates why querying raw blockchain data is impractical for building applications. It will resonate strongly with the data challenges you've encountered in the traditional BI world.

Please read the introduction and the section titled "Why Blockchain Data Feels Easy Until You Actually Need It". As you read, notice the parallels between the difficulties described and the reasons for building data warehouses in your field of expertise.

The image below provides a concise comparison between making direct calls and using an indexed data service, which is the approach we'll be exploring.

This table contrasts the characteristics of using pre-compiled, indexed data versus direct JSON RPC API calls. It highlights the significant advantages of indexing for response time and suitability for data-heavy applications.

As you can see, for any application with non-trivial data requirements—such as analytics for a security token—relying on direct RPC calls is not a scalable solution.

2. The Solution: The Graph Protocol and Subgraphs

The Graph is a decentralized protocol for indexing and querying data from blockchains. It allows developers to define how on-chain data should be collected, transformed, and stored in a way that can be easily queried using the standard GraphQL API language. This process effectively creates a custom, performant, and decentralized data backend for your smart contracts.

The core concept in The Graph ecosystem is the subgraph. A subgraph is a custom indexing blueprint for a specific set of smart contracts. You can think of it as defining a purpose-built data mart for your application. Instead of querying raw blockchain events, your application queries the subgraph's clean, structured entities, like User, Token, or Transfer.

This video from the OpenZeppelin team provides a great introduction to the problem The Graph solves and its core components.

Indexing Smart Contracts with OpenZeppelin Subgraphs & The Graph

This video explains why an indexing layer is necessary and introduces the three main parts of a subgraph. The speaker's analogy to web2 database patterns will be familiar to you.

Watch from the beginning to where the speaker finishes explaining relational data. Pay close attention to the distinction between building a centralized indexer yourself versus using The Graph's decentralized network.

3. The Anatomy of a Subgraph

As you just heard, creating a subgraph involves defining its structure and logic through three key files. This process is strikingly similar to defining an ETL job and its destination schema in a data warehouse project. Let's examine each component.

We'll use this comprehensive guide from SettleMint as our reference for the technical details.

Subgraphs - Blockchain data indexing with The Graph

This resource provides a clear, structured breakdown of a subgraph's components. It's the perfect reference for understanding how the pieces fit together.

First, read the brief introduction to get a formal definition. Then, navigate to the section "Enterprise Subgraph Architecture" and read the subsections that define the three core components: The Manifest (subgraph.yaml): This file is the main configuration. It specifies which contracts to index, which network they are on, and which events to listen for. This is analogous to the configuration file for an ETL pipeline, defining your data sources. Read the short description and then jump to the section "Writing the subgraph manifest" to see an example. The GraphQL Schema (schema.graphql): This file defines the data model for your API—the "tables" and "columns" of your data store. You define entities (like tables) and their fields. This directly corresponds to defining a database schema. Read the description here, and then the section "Defining the GraphQL schema" for details. The Mappings (AssemblyScript): This is the transformation logic—the "T" in ETL. You write functions in AssemblyScript (a subset of TypeScript) that are triggered by smart contract events. These functions process the raw event data and save it into the entities defined in your schema. Read the brief description, followed by the section "Creating the mapping logic" to see how it works.

To summarize the workflow:

  1. A transaction occurs on-chain and your smart contract emits an event (e.g., Transfer).
  2. A Graph Node, which is monitoring the blockchain, sees this event.
  3. The node checks your subgraph.yaml and sees that it is supposed to handle this event with a specific mapping function.
  4. It executes your mapping function in AssemblyScript, which takes the event data, transforms it, and creates or updates entities.
  5. These entities are stored in a database, ready to be queried via the GraphQL API defined by your schema.graphql.

4. A Decentralized Network of Data Providers

A crucial aspect of The Graph is that it's not just a tool for building an indexer—it's a protocol for a decentralized network of indexers. This avoids reintroducing a single point of failure, which you would have if you hosted your own indexing server.

The network consists of several key participants who are economically incentivized by The Graph's native token (GRT).

This diagram illustrates the economic loop of The Graph Network. Data Consumers pay query fees, which are distributed to the Network Participants (Indexers, Curators, Delegators) who work together to provide and secure the indexed data.
  • Indexers are node operators that stake GRT and run the hardware to index subgraphs and serve queries. They earn query fees and indexing rewards for their service.
  • Curators are data consumers, subgraph developers, or community members who signal which subgraphs are high-quality and should be indexed. They stake GRT on subgraphs and earn a portion of the query fees from those subgraphs.
  • Delegators are individuals who want to contribute to securing the network without running a Graph Node themselves. They delegate their GRT to existing Indexers and earn a portion of the fees and rewards the Indexer collects.
  • Data Consumers (e.g., your dApp) are the end-users who query the subgraphs and pay fees for the data they consume.

This decentralized model ensures that the data layer for your dApp is as resilient and censorship-resistant as the blockchain itself.

5. Application to Tokenized Asset Analytics

Let's bring this back to your goal of building solutions for tokenized money. The Graph is not just a technical curiosity; it is the enabling infrastructure for building the rich, data-driven experiences that users and institutions expect.

For the security token you designed, you could build a subgraph to track and expose data for:

  • A real-time cap table: Query for a list of all token holders and their balances.
  • Historical trading analytics: Aggregate transfer data to calculate daily volume, price history (if combined with DEX data), and other metrics.
  • Dividend distribution tracking: Query all DividendWithdrawn events to create an auditable history of payments for each investor.
  • Compliance monitoring: Create a dashboard that shows all transfer attempts, including reverted ones, to monitor compliance rule enforcement.

These are exactly the types of business intelligence use cases you are familiar with, but applied to on-chain assets. The Graph provides the performant data layer to make them possible.

The Graph Workflow: How to Query Blockchain Data ...

Let's revisit the Startupik article to see how this translates to business value and look at more use cases.

Please read the section "Where The Graph Creates Leverage for Startups". Then, find the SettleMint article again and read the list of examples in the section "Use cases enabled by subgraphs". This will give you a broad perspective on what's possible.

Conclusion

In this lesson, we've explored the critical role of indexing protocols in making blockchain data accessible and useful. You've learned that direct queries to blockchain nodes are inefficient for data-intensive applications, creating a problem analogous to querying transactional databases for BI purposes.

The Graph protocol provides a decentralized solution, allowing you to define, deploy, and query custom APIs called subgraphs. This transforms raw, unstructured on-chain data into a performant, structured data layer, enabling the creation of sophisticated analytics and user-facing applications for your tokenized assets.

Key Takeaways:

  • The Querying Problem: Blockchains are write-optimized, making complex read queries for applications slow, expensive, and impractical.
  • The Graph as a Solution: It's a decentralized indexing protocol that acts as an ETL and data warehousing layer for blockchains, serving data via GraphQL.
  • Anatomy of a Subgraph: A subgraph is defined by three core components: the manifest (subgraph.yaml) for configuration, the schema (schema.graphql) for the data model, and the mappings (AssemblyScript) for the transformation logic.
  • Decentralized and Robust: The Graph is a network of economically-incentivized participants (Indexers, Curators, Delegators) that ensures the data layer is as resilient as the blockchain itself.
  • Enabling Analytics: For tokenized money use cases, subgraphs are essential for building portfolio trackers, compliance dashboards, historical analytics, and other data-rich features.

We have now covered the final piece of major ecosystem tooling. In our last lesson of the course, we will circle back to security and conduct a final review of our token contracts, consolidating all the best practices you've learned to ensure they are as robust as possible before a hypothetical deployment.

Can't find a good explanation? Sign up and we'll make it for you

Sign up