Introduction to Blockchain Data Indexing with Subsquid

Web3 development has experienced rapid growth in 2023 and has gained significant developer adoption. Despite this adoption, web3 still faces a number of issues, with the biggest being scalability. How can dApps handle large user adoption in a scalable manner? It's clear that web3 needs to be faster than traditional web2 applications for mainstream users to prefer it over it’s traditional counter-part.

To address this issue, Subsquid offers a blockchain data indexer that uses "Archives" and "squids" to store and index data from over 70 networks, including EVM and Substrate chains. By leveraging the indexed data, developers can build better APIs that enable their web3 apps to become faster. The recent release of ArrowSquid allows developers to build squids that index blockchain data in real-time, further enhancing the speed and efficiency of web3 applications.

Overall, Subsquid offers a solution that can help address part of the scalability issues facing web3 development, allowing for faster and more efficient dApps that can handle large user adoption.

Steps to create your first squid

This guide assumes you have node installed and are using a unix based system. I haven’t tested it on window. You should also have some knowledge on GraphQL and TypeScript for a better experience with this guide.

If you don’t have node and TypeScript installed follow your operating system guide for installing node.

Operating System Pre-requisite

  1. Install node

  2. Install a Squid Cli

    npm install -g @subsquid/cli@latest

Create a Squid for Your Project

Run the following commands on your terminal. Also this command assumes your are building a Squid for an evm compatible chain.

  1. sqd init project-name --template evm

  2. cd project-name

  3. npm install

Customize Your Project to Accommodate Contract

  1. Edit your schema.graphql file to account for the contract entities you would like to index, the file can be found in your project root.

  2. Run a sqd codegen command to generate TypeORM classes for your squid.

  3. Copy your abi file(s) from your network explore and place in the abi folder located in same path as your project root. Then run a sqd typegen command.

  4. Create database migrations using sqd down to drop old schema, sqd migration:generate and sqd up for a new schema.

  5. Configure and setup your squid processor file and event handlers. File is usually located at the src/processor.ts from your project root.

  6. Run your squid processor using sqd process and serve to create a local GraphQL server sqd serve.

schema.graphql

Below is an example GraphQL schema file and you can learn more building entities for your squid from the Subsquid entities docs.

type Member @entity {
  id: ID!
  address: String!
  balance: BigInt!
  votingWeight: BigInt!
  delegate: Member
  lastUpdate: BigInt!
}

src/processor.ts

Below is an example snippet for configuring the squid processor file, you can learn more on how to configure Squid EVMBatchProcessor from their doc.

const processor = new EvmBatchProcessor()
  .setDataSource({
    chain: process.env.RPC_ENDPOINT,
    archive: "https://eth.archive.subsquid.io",
  })
  .setBlockRange({ from: 16586489 })
  .addLog(agreementFrameworkContractAddress, {
    filter: [
      [
        events.AgreementCreated.topic,
        events.AgreementJoined.topic,
        events.AgreementPositionUpdated.topic,
        events.AgreementFinalized.topic,
        events.AgreementDisputed.topic,
      ],
    ],
    data: {
      evmLog: {
        topics: true,
        data: true,
      },
      transaction: {
        hash: true,
      }
    }
  });

Deploy your Squid to Subsquid’s Aquarium

When blockchain data using Subsquid, you have the option to either self-host your squid or deploy using Subsquid hosted service (Aquarium ). This tutorial will be using Subsquid hosted service which is free up to 3 squids and your can apply for an upgrade if your project requires more capacity.

  1. Visit the Subsquid dashboard, and sign in with Github. Then, follow the instructions on your landing page to create your squid.

    Below is a code block of all the commands you need to run to get your squid deployed. I am also assuming your have your squid built out already and are on your project’s root directory.

    npm install -g @subsquid/cli
    sqd auth -k `deployment-key`
    sqd deploy .
    

If your squid was successfully deployed, you would have a link similar to the one I have below to query your indexed blockchain data.

Subscribe to Okhai a.ka. Tony Stark
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from Okhai a.ka. Tony Stark

Skeleton

Skeleton

Skeleton