Skip to content

In this quick tutorial we will learn how to index Substrate events alongside Moonbeam data and how to build dApps with the help of SubQuery

Notifications You must be signed in to change notification settings

deverka/moonbeam_subquery_tutorial

Repository files navigation

Moonbeam <> SubQuery Integration Tutorial

In this quick tutorial we will learn how to index Substrate events alongside Moonbeam data and how to build dApps with the help of SubQuery.

This project is also hosted by SubQuery's Managed Service and you can view it in SubQuery Explorer — a marketplace of all pulished projects. You can test queries directly in your browser using our GraphQl playground: https://explorer.subquery.network/subquery/subquery/Moonbeam-Subquery-Integration-Tutorial

Preparation

Environment and dependencies

  • Typescript is required to compile project and define types.

  • Both SubQuery CLI and generated Project have dependencies and require Node.

  • You will also need Yarn or NPM and Docker.

Install the SubQuery CLI

Install SubQuery CLI globally on your terminal with NPM. Note that we don't encourage the use of yarn global for installing @subql/cli due to its poor dependency management. This may lead to multiple errors.

npm install -g @subql/cli

Run help to see available commands provided by SubQuery CLI:

subql help

In case of Initializing the Starter Package (optional)

Inside the directory in which you want to create the SubQuery project run the following command and follow all the steps chosing project name, GitHub repo addres, network familay, rpc endpoint and more. Everything can by changed afterwords as well.

subql init project-name

Then you should see a folder with your project name has been created inside the directory, you can use this as the start point of your project. And the files should be identical as in the Directory Structure.

Configure the Project Further

If you want to change your project you will need to work on the following files:

  • The Manifest in project.yaml to configure your project
  • The GraphQL Schema in schema.graphql to define shape of the data
  • The Mapping functions in src/mappings/ directory to transform data coming from blockchain

Learn more

Build the Project

1. Install dependencies

Under the project directory, install the node dependencies by running the following command:

yarn install OR npm install

Learn more

2. Generate Associated Typescript

Next, we will generate the associated typescript with the following command:

yarn codegen OR npm run-script codegen

3. Build the project

This bundles the app into static files for production.

yarn build OR npm run-script codegen

Indexing and Query

1. Run Docker

Under the project directory run following command:

docker-compose pull && docker-compose up

2. Query this Project

Open your browser and head to http://localhost:3000.

Finally, you should see a GraphQL playground is showing in the explorer and the schemas that ready to query.

With this project can try to query with the following code to get a taste of how it works.

query {
    approvals (first: 5) {
        nodes {
            id
            value
            owner
            spender
        }
    }
    transactions (first: 5) {
        nodes {
            id
            value
            to: id
            from: id
        }
    }
    collators (last: 5) {
        nodes {
            id
            joinedDate
            leaveDate
        }
    }
    accounts(first: 5) {
        nodes {
            id
            sentTransactions {
                nodes {
                    id
                    value
                    to: id
                    from: id
                    contractAddress       
                }
            }
        }
    }
}

Useful Resources

About SubQuery

SubQuery is a blockchain developer toolkit enabling others to build Web3 applications of the future. A SubQuery project is a complete API to organise and query data from Layer-1 chains. Currently servicing Polkadot, Substrate, Avalanche, Terra and Cosmos projects, this data-as-a-service allows developers to focus on their core use case and front-end, without needing to waste time on building a custom backend for data processing. The SubQuery Network proposes to enable this same scalable and reliable solution, but in a completely decentralised way.

Linktree | Website | Discord | Telegram | Twitter | Matrix | LinkedIn | YouTube

About

In this quick tutorial we will learn how to index Substrate events alongside Moonbeam data and how to build dApps with the help of SubQuery

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published