SubQuery
SubQuery is a fast, flexible, and reliable open-source data decentralised infrastructure network. The SubQuery Data indexer is a open-source data indexer that provides you with custom APIs for your web3 project across all of our supported chains.
Preparation
1、Nodejs
Download from Github
Select node version
2、Docker Compose
Download from Github
Execution permission
Check the version
About SubQuery
1. Install the SubQuery CLI
Install SubQuery CLI globally on your terminal by using NPM. We do not encourage the use of yarn global
for installing @subql/cli
due to its poor dependency management. This may lead to multiple errors.
2. Initialise a new SubQuery Project
Run the following command inside the directory that you want to create a SubQuery project in:
You'll be asked certain questions as you proceed ahead:
Project name: A project name for your SubQuery project.
Network family: The layer-1 blockchain network family that this SubQuery project will index. Use the arrow keys to select from the available options (scroll down as there are multiple pages).
Network: The specific network that this SubQuery project will index. Use the arrow keys to select from the available options (scroll down as there are multiple pages).
Template project: Select a SubQuery template project that will provide a starting point in the development. For some networks we provide multiple examples.
RPC endpoint: Provide an HTTP or websocket URL to a running RPC endpoint, which will be used by default for this project. You can use public endpoints for different networks, your own private dedicated node, or just use the default endpoint. This RPC node must have the entire state of the data that you wish to index, so we recommend an archive node.
Git repository: Provide a Git URL to a repo that this SubQuery project will be hosted in.
Authors: Enter the owner of this SubQuery project here (e.g. your name!) or accept the provided default.
Description: Provide a short paragraph about your project that describes what data it contains and what users can do with it, or accept the provided default.
Version: Enter a custom version number or use the default (
1.0.0
).License: Provide the software license for this project or accept the default (
MIT
).
Let’s look at an example:
Ethereum Project Scaffolding
Finally, run the following command to install the new project’s dependencies from within the new project's directory.
You have now initialised your first SubQuery project with just a few simple steps. Let’s now customise the standard template project for a specific blockchain of interest.
3. Run a template
We use Ethereum packages, runtimes, and handlers (e.g. @subql/node-ethereum
, ethereum/Runtime
, and ethereum/*Hander
) for BEVM Canary Network. Since BEVM is an EVM-compatible layer-2 scaling solution, we can use the core Ethereum framework to index it.
Your Project Manifest File
The Project Manifest file is an entry point to your project. It defines most of the details on how SubQuery will index and transform the chain data.
For EVM chains, there are three types of mapping handlers (and you can have more than one in each project):
Update the datasources
section as follows:
datasources
section as follows:Update Your GraphQL Schema File
The schema.graphql
file determines the shape of your data from SubQuery due to the mechanism of the GraphQL query language. Hence, updating the GraphQL Schema file is the perfect place to start. It allows you to define your end goal right at the start.
Remove all existing entities and update the schema.graphql
file as follows. Here you can see we are indexing block information such as the id, blockHeight, transfer receiver and transfer sender along with an approvals and all of the attributes related to them (such as owner and spender etc.).
SubQuery simplifies and ensures type-safety when working with GraphQL entities, smart contracts, events, transactions, and logs. The SubQuery CLI will generate types based on your project's GraphQL schema and any contract ABIs included in the data sources.
You can conveniently import all these types:
Now that you have made essential changes to the GraphQL Schema file, let’s proceed ahead with the Mapping Function’s configuration.
Add a Mapping Function
Mapping functions define how blockchain data is transformed into the optimised GraphQL entities that we previously defined in the schema.graphql
file.
Navigate to the default mapping function in the src/mappings
directory. You will be able to see two exported functions handleLog
and handleTransaction
:
The handleLog
function receives a log
parameter of type TransferLog
which includes log data in the payload. We extract this data and then save this to the store using the .save()
function (Note that SubQuery will automatically save this to the database).
The handleTransaction
function receives a tx
parameter of type ApproveTransaction
which includes transaction data in the payload. We extract this data and then save this to the store using the .save()
function (Note that SubQuery will automatically save this to the database).
Build Your Project
Next, build your work to run your new SubQuery project. Run the build command from the project's root directory as given here:
Whenever you make changes to your mapping functions, you must rebuild your project.
Now, you are ready to run your first SubQuery project. Let’s check out the process of running your project in detail.
Whenever you create a new SubQuery Project, first, you must run it locally on your computer and test it and using Docker is the easiest and quickiest way to do this.
Run Your Project Locally with Docker
The docker-compose.yml
file defines all the configurations that control how a SubQuery node runs. For a new project, which you have just initialised, you won't need to change anything.
Run the following command under the project directory:
It may take a few minutes to download the required images and start the various nodes and Postgres databases.
Query your Project
Next, let's query our project. Follow these three simple steps to query your SubQuery project:
Open your browser and head to
http://localhost:3000
.You will see a GraphQL playground in the browser and the schemas which are ready to query.
Find the Docs tab on the right side of the playground which should open a documentation drawer. This documentation is automatically generated and it helps you find what entities and methods you can query.
You will see the result similar to below:
5. Make Changes to Your Project
There are 3 important files that need to be modified. These are:
The GraphQL Schema in
schema.graphql
.The Project Manifest in
project.ts
.The Mapping functions in
src/mappings/
directory.
6. EVM Project Scaffolding
Scaffolding saves time during SubQuery project creation by automatically generating typescript facades for EVM transactions, logs, and types.
When you are initalising a new project using the subql init
command, SubQuery will give you the option to set up a scaffolded SubQuery project based on your JSON ABI. If you select a compatible network type (EVM), it will prompt:
Once completed, you will have a scaffold project structure from your chosen ABI functions
/events
.
Last updated