Creating Subgraphs on Linea: A Comprehensive Guide

Joerg Hiller  May 31, 2024 12:31  UTC 04:31

0 Min Read

As the internet continues to evolve, the decentralized web, commonly referred to as web3, is gaining traction. However, accessing and reading blockchain data remains a challenge. According to Linea, The Graph addresses this issue by providing a decentralized protocol for indexing and querying blockchain data.

Understanding The Graph

The Graph is an indexing protocol designed to make blockchain data easily accessible through open APIs known as subgraphs. This protocol leverages the participation of various network contributors who earn GRT, The Graph's native utility token, for their efforts in organizing data efficiently.

Key participants in The Graph network include:

  • Subgraph Developers: Create and publish subgraphs that are accessible to everyone.
  • Indexers: Operate nodes to index subgraphs and serve data to consumers.
  • Curators: Identify and curate valuable subgraphs for Indexers.
  • Delegators: Secure the network by delegating GRT to Indexers.

The decentralized nature of The Graph ensures open access to data, fostering innovation and collaboration within the web3 ecosystem.

Building a Subgraph on Linea

Linea’s ecosystem uses The Graph to access blockchain data, enhancing transparency and reducing censorship risks. Here is a step-by-step guide to building a subgraph on Linea:

Step 0: Create a New Subgraph

Begin by visiting Subgraph Studio and connecting your wallet. Once connected, select “create a subgraph,” choose Linea as the network, and click continue.

Step 1: Install the Graph CLI

Install the Graph CLI on your local machine using either npm or yarn:


$ npm install -g @graphprotocol/graph-cli
$ yarn global add @graphprotocol/graph-cli

Step 2: Initialize Your Subgraph

Initialize your subgraph from an existing contract:

graph init --studio <SUBGRAPH_SLUG>

Provide the necessary information such as protocol (select Ethereum), subgraph slug, local directory, network (Linea or Linea Sepolia), contract address, ABI, start block, contract name, and whether to index contract events as entities.

Step 3: Write Your Subgraph

Modify the scaffold subgraph by working with three main files:

  1. Manifest (subgraph.yaml): Defines the data sources your subgraph will index.
  2. Schema (schema.graphql): Defines the data to retrieve from the subgraph.
  3. AssemblyScript Mappings (mapping.ts): Translates data from your data sources to the entities defined in the schema.

For detailed instructions, refer to The Graph's documentation.

Step 4: Deploy to Subgraph Studio

Generate AssemblyScript types and compile your subgraph:


$ graph codegen
$ graph build

Authenticate and deploy your subgraph to a Graph Node:


$ graph auth --studio <DEPLOY_KEY>
$ graph deploy --studio <SUBGRAPH_SLUG>

Provide a version label, preferably using semver.

Step 5: Test Your Subgraph

Test your subgraph by making sample queries in the playground section and checking the logs for any errors. Use the GraphiQL Playground to query your subgraph's health if needed.

Step 6: Publish to The Graph’s Decentralized Network

After deploying and testing your subgraph, publish it to the decentralized network via Subgraph Studio. It is recommended to publish subgraphs to Arbitrum One for lower gas costs. Curate your subgraph using GRT to ensure it is indexed and available for querying promptly.

Step 7: Query Your Subgraph

Query your subgraph by sending GraphQL queries to your subgraph’s Query URL. For more information on querying data, refer to The Graph's querying documentation.



Read More