Indexing Moonbeam with Subsquid¶
Introduction¶
Subsquid is a query node framework for Substrate-based blockchains. In very simple terms, Subsquid can be thought of as an ETL (extract, transform, and load) tool with a GraphQL server included. It enables comprehensive filtering, pagination, and even full-text search capabilities.
Subsquid has native and full support for both Ethereum Virtual Machine (EVM) and Substrate data. Since Moonbeam is a Substrate-based smart contact platform that is EVM-compatible, Subsquid can be used to index both EVM and Substrate-based data. Subsquid offers a Substrate Archive and Processor and an EVM Archive and Processor. The Substrate Archive and Processor can be used to index both Substrate and EVM data. This allows developers to extract on-chain data from any of the Moonbeam networks and process EVM logs as well as Substrate entities (events, extrinsics, and storage items) in one single project and serve the resulting data with one single GraphQL endpoint. If you exclusively want to index EVM data, it is recommended to use the EVM Archive and Processor.
This guide will show you how to create Substrate and EVM projects with Subsquid and configure it to index data on Moonbeam.
The information presented herein is for informational purposes only and has been provided by third parties. Moonbeam does not endorse any project listed and described on the Moonbeam docs website (https://docs.moonbeam.network/).
Checking Prerequisites¶
To get started with Subsquid, you'll need to have the following:
Note
The squid template is not compatible with yarn
, so you'll need to use npm
instead.
Index Substrate Data on Moonbeam¶
To get started indexing Substrate data on Moonbeam, you'll need to create a Subsquid project and configure it for Moonbeam by taking the following steps:
-
Create a Subsquid project based on the Substrate template by running:
sqd init <insert-squid-name> --template substrate
For more information on getting started with this template, please check out the Quickstart: Substrate chains guide on Subsquid's documentation site.
-
To configure your Subsquid project to run on Moonbeam, you'll need to update the
typegen.json
file. Thetypegen.json
file is responsible for generating TypeScript interface classes for your data. Depending on the network you're indexing data on, thespecVersions
value in thetypegen.json
file should be configured as follows:"specVersions": "https://moonbeam.archive.subsquid.io/graphql",
"specVersions": "https://moonriver.archive.subsquid.io/graphql",
"specVersions": "https://moonbase.archive.subsquid.io/graphql",
-
Modify the
src/processor.ts
file, which is where squids instantiate the processor, configure it, and attach handler functions. The processor fetches historical on-chain data from an Archive, which is a specialized data lake. You'll need to configure your processor to pull data from the Archive that corresponds to the network you are indexing data on:const processor = new SubstrateBatchProcessor(); processor.setDataSource({ chain: INSERT_RPC_API_ENDPOINT, // Resolves to "https://moonbeam.archive.subsquid.io" archive: lookupArchive("moonbeam", { type: "Substrate" }), });
const processor = new SubstrateBatchProcessor(); processor.setDataSource({ chain: INSERT_RPC_API_ENDPOINT, // Resolves to "https://moonriver.archive.subsquid.io" archive: lookupArchive("moonriver", { type: "Substrate" }), });
const processor = new SubstrateBatchProcessor(); processor.setDataSource({ chain: https://rpc.api.moonbase.moonbeam.network, // Resolves to "https://moonbase.archive.subsquid.io" archive: lookupArchive("moonbase", { type: "Substrate" }), });
And that's all you have to do to configure your Subsquid project to index Substrate data on Moonbeam! Now you can update the schema.graphql
, typgen.json
, and src/processor.ts
files to index the data you need for your project!
Index Ethereum Data on Moonbeam¶
To get started indexing EVM data on Moonbeam, you'll need to create a Subsquid project and configure it for Moonbeam by taking the following steps:
-
You can create a Subsquid project for EVM data by using the generic EVM template or you can use the ABI template for indexing data related to a specific contract:
sqd init <insert-squid-name> --template evm
sqd init <insert-squid-name> --template abi
For more information on getting started with both of these templates, please check out the following Subsquid docs:
-
To configure your Subsquid project to run on Moonbeam, you'll need to update the
typegen.json
file. Thetypegen.json
file is responsible for generating TypeScript interface classes for your data. Depending on the network you're indexing data on, thespecVersions
value in thetypegen.json
file should be configured as follows:"specVersions": "https://moonbeam.archive.subsquid.io/graphql",
"specVersions": "https://moonriver.archive.subsquid.io/graphql",
"specVersions": "https://moonbase.archive.subsquid.io/graphql",
-
Modify the
src/processor.ts
file, which is where squids instantiate the processor, configure it, and attach handler functions. The processor fetches historical on-chain data from an Archive, which is a specialized data lake. You'll need to configure your processor to pull data from the Archive that corresponds to the network you are indexing data on:const processor = new EvmBatchProcessor(); processor.setDataSource({ chain: INSERT_RPC_API_ENDPOINT, // Resolves to "https://moonbeam-evm.archive.subsquid.io" archive: lookupArchive("moonbeam", { type: "EVM" }) });
const processor = new EvmBatchProcessor(); processor.setDataSource({ chain: INSERT_RPC_API_ENDPOINT, // Resolves to "https://moonriver-evm.archive.subsquid.io" archive: lookupArchive("moonriver", { type: "EVM" }), });
const processor = new EvmBatchProcessor(); processor.setDataSource({ chain: https://rpc.api.moonbase.moonbeam.network, // Resolves to "https://moonbase-evm.archive.subsquid.io" archive: lookupArchive("moonbase", { type: "EVM" }), });
And that's all you have to do to configure your Subsquid project to index EVM data on Moonbeam! Now you can update the schema.graphql
, typgen.json
, and src/processor.ts
files to index the data you need for your project!
If you're interested in a step-by-step tutorial to get started indexing data on Moonbeam, you can check out the Index NFT Token Transfers on Moonbeam with Subsquid tutorial!
| Created: April 5, 2022