Efficiently Querying Blockchain Data

KChain Solutions
4 min readFeb 1, 2023

--

1. Introduction

One of the key challenges in building decentralized applications is reading on-chain data in an efficient manner. One option is to set up your own node and synchronize it with the blockchain, or to use an infrastructure provider like Infura, Alchemy, or Chainstack. However, both of these approaches can have drawbacks in terms of costs, performance, and service resilience.

The Graph offers an efficient solution for indexing content on-chain and accessing data through its GraphQL API. With a relatively low learning curve and tools developed by the community, as well as a large number of pre-built APIs (known as Subgraphs), developers can quickly and easily build and deploy applications that use Ethereum data optimized for their specific needs.

2. The Subgraph

A Subgraph in TheGraph is a smaller, focused portion of Ethereum data that has been indexed and made searchable through the protocol. Subgraphs are created and defined by developers using GraphQL and can be used to index smart contract events, state, and other Ethereum data.

To create a Subgraph, you need to configure three files: a subgraph.yaml file with the subgraph manifest, a GraphQL schema that defines the data stored and how to query it, and AssemblyScript mappings to translate event data to the entities defined in the schema.

You can distribute a Subgraph in a decentralized or self-hosted way. The decentralized system through Subgraph Studio enables quick, efficient, and cost-effective deployment of APIs through an incentive mechanism where different actors contribute to maintaining the quality of service. However, this solution is limited to Ethereum/Goerli and does not support direct IPFS queries. If you want to publish on other networks such as Polygon, you can set up your own node where you can publish your Subgraphs independently, with costs shifting from pay-per-use to infrastructural expenses.

3. The Graph simple blog example

This repository serves as an Subgraph example of how to interact with the Goerli testnet and access to the events generated by the my-simple-blog repository.

To better understand this article please watch:

The events that the subgraph will index are

  • event NewBlog(address owner, address blogAddr);
  • event NewPost(address owner, address post, string URI);

3.1 How to run the code

3.1.1 Install graph-cli

npm install -g @graphprotocol/graph-cli

3.1.2 Login into your graph-cli account

Access with your metamask account to The Graph Studio and login with your api key into the graph-cli, as you can see in the image below.

3.1.3 Deploy code

Clone the thegraph-simple-blog repository. Enter the project directory with the terminal and run

  • npm run codegen
  • npm run deploy

At the end of the deployment, you will be redirected to the page where to perform GraphQL queries.

3.2 Understand the code

subgraph.yaml

a YAML file containing the subgraph manifest. This file holds all necessary configurations for compiling the code. It includes information about the smart contract to monitor, the reference network, the starting block for recording events, and the relationships between the ABI file, events, and event handlers.

dataSources:
- kind: ethereum
name: BlogFactory
network: goerli
source:
address: "0x2259Ff8FfEF4e92454a4ef1ED516291c5A2CC3fC"
abi: BlogFactory
startBlock: 8409000
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- NewBlog
abis:
- name: BlogFactory
file: ./abis/BlogFactory.json
eventHandlers:
- event: NewBlog(address,address)
handler: handleNewBlog
file: ./src/blog-factory.ts
templates:
- kind: ethereum
name: Blog
network: goerli
source:
abi: Blog
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- NewPost
abis:
- name: Blog
file: ./abis/Blog.json
eventHandlers:
- event: NewPost(address,address,string)
handler: handleNewPost
file: ./src/blog-factory.ts

It is important to note the distinction between a dataSource and a template. The template does not include any references to a smart contract because, in this particular scenario, the Blog addresses are dynamically generated through the BlogFactory.

The Blog address, to be listened to, will be created at runtime and can be seen from the line of code:

Blog.create(event.params.blogAddr);

In the file blog-factory.ts

./abis

The abis folder contains all the abi of the contract to index. This is required to map event into the manifest file.

schema.graphql

The GraphQL schema defines what data is stored for your subgraph, and how to query it via GraphQL. Required fields are represented by an exclamation point

./src/blog-factory.ts

The file contains all the application logic and translates an event into the schema that can be queried via GraphQL.

network.json

Contains the list of networks and the addresses of the associated contracts.

For more information, please don’t hesitate to reach out to us.

--

--

KChain Solutions
KChain Solutions

Written by KChain Solutions

With expertise and dedication, we drive meaningful industry change, embracing the future of technology

No responses yet