Skip to main content

Humanode Quick Start

SubQuery TeamAbout 1 min

Humanode Quick Start

This quick guide aims to adapt the standard starter project and index all transfers, bioauthenitcation events, and online validator nodes from Humanode chain. Humanode is a standalone Substrate chain, but the same process applies to it as a normal Polkadot parachain or relaychain.

In the earlier Quickstart section , you should have taken note of three crucial files. To initiate the setup of a project from scratch, you can proceed to follow the steps outlined in the initialisation description.

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 all transfers, bioauthentication events, and online validator nodes from Humanode:

type BioauthNewAuthentication @entity {
  id: ID!
  blockNumber: Int!
  validatorPublicKey: String!
  timestamp: Date!
}

type ImOnlineSomeOffline @entity {
  id: ID!
  blockNumber: Int!
  accountsIds: [String]!
  timestamp: Date!
}
yarn
yarn codegen

This action will generate a new directory (or update the existing one) named src/types. Inside this directory, you will find automatically generated entity classes corresponding to each type defined in your schema.graphql. These classes facilitate type-safe operations for loading, reading, and writing entity fields. You can learn more about this process in the GraphQL Schema section.

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 Polkadot, there are three types of mapping handlers (and you can have more than one in each project):

  • BlockHanders: On each and every block, run a mapping function
  • EventHandlers: On each and every Event that matches optional filter criteria, run a mapping function
  • CallHanders: On each and every extrinsic call that matches optional filter criteria, run a mapping function

Note that the manifest file has already been set up correctly and doesn’t require significant changes, but you need to change the datasource handlers. This section lists the triggers that look for on the blockchain to start indexing.

Since we are planning to index all transfers, bioauthentication events, and online nodes, we need to update the datasources section as follows:

{
  dataSources: [
    {
      kind: SubstrateDatasourceKind.Runtime,
      startBlock: 1,
      mapping: {
        file: "./dist/index.js",
        handlers: [
          {
            kind: SubstrateHandlerKind.Event,
            handler: "handleBioauthNewAuthenticationEvent",
            filter: {
              module: "bioauth",
              method: "NewAuthentication",
            },
          },
          {
            kind: SubstrateHandlerKind.Event,
            handler: "handleImonlineSomeOfflineEvent",
            filter: {
              module: "imOnline",
              method: "SomeOffline",
            },
          },
        ],
      },
    },
  ],
}

This indicates that you will be running a handleBioauthNewAuthenticationEvent and handleImonlineSomeOfflineEvent mapping functions whenever there are events emitted from the bioauth and imOnline modules with the NewAuthentication and SomeOffline methods, respectively.

Check out our Manifest File documentation to get more information about the Project Manifest (project.ts) file.

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.

Mapping functions define how chain data is transformed into the optimized GraphQL entities that we previously defined in the schema.graphql file. Navigate to the default mapping function in the src/mappings directory. You will see two exported functions: handleBioauthNewAuthenticationEvent and handleImonlineSomeOfflineEvent.

The handleBioauthNewAuthenticationEvent and handleImonlineSomeOfflineEvent functions receive event data whenever an event matches the filters that you specified previously in the project.ts.

Update the handleBioauthNewAuthenticationEvent and handleImonlineSomeOfflineEvent functions as follows (note the additional imports):

export async function handleBioauthNewAuthenticationEvent(
  substrateEvent: SubstrateEvent
): Promise<void> {
  const { event, block, idx } = substrateEvent;

  const {
    data: [validatorPublicKey],
  } = event;

  const record = new BioauthNewAuthentication(
    `${block.block.header.number}-${idx}`
  );
  record.blockNumber = block.block.header.number.toNumber();
  record.timestamp = block.timestamp;
  record.validatorPublicKey = validatorPublicKey.toString();
  await record.save();
}

export async function handleImonlineSomeOfflineEvent(
  substrateEvent: SubstrateEvent<[]>
): Promise<void> {
  const { event, block, idx } = substrateEvent;

  const {
    data: [offline],
  } = event;

  const record = new ImOnlineSomeOffline(`${block.block.header.number}-${idx}`);
  record.blockNumber = block.block.header.number.toNumber();
  record.timestamp = block.timestamp;
  record.accountIds = [];
  for (const identification of offline as Vec<Codec>) {
    const [accountId, _fullIdentification] = identification as any as [
      Codec,
      Codec
    ];
    record.accountIds.push(accountId.toString());
  }
  await record.save();
}

Note

For more information on mapping functions, please refer to our Mappings documentation.

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:

yarn
yarn build

Important

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.

However, visit the Running SubQuery Locally to get more information on the file and the settings.

Run the following command under the project directory:

yarn
yarn start:docker

Note

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:

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

  2. You will see a GraphQL playground in the browser and the schemas which are ready to query.

  3. 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.

Try the following queries to understand how it works for your new SubQuery starter project. Don’t forget to learn more about the GraphQL Query language.

query {
  bioauthNewAuthentications(first: 5) {
   nodes{
      id
    }
  }
  imOnlineSomeOfflineByNodeId (nodeId : 5) {
    id
  }
  _metadata {
    lastProcessedHeight
    lastProcessedTimestamp
    targetHeight
  }
}

You will see the results similar to below:

{
  "data": {
    "bioauthNewAuthentications": {
      "nodes": [
        {
          "id": "940-2"
        },
        {
          "id": "1785-7"
        },
        {
          "id": "456-2"
        },
        {
          "id": "1118-2"
        },
        {
          "id": "3851-2"
        }
      ]
    },
    "imOnlineSomeOfflineByNodeId": null,
    "_metadata": {
      "lastProcessedHeight": 4435,
      "lastProcessedTimestamp": "1670316277673",
      "targetHeight": 289567
    }
  }
}

What's next?

Congratulations! You have now a locally running SubQuery project that accepts GraphQL API requests for transferring data.

Tip

Find out how to build a performant SubQuery project and avoid common mistakes in Project Optimisation.

Click here to learn what should be your next step in your SubQuery journey.