May 29, 2022

SubQuery Guide - Timestamp of Blocks

This Subquery example indexes the timestamp of each finalized block and it is an example of a CallHandler. By processing the timestamp.set extrinsic, and extracting the first arguments of it, we can retrieve the timestamp.

https://github.com/subquery/tutorials-block-timestamp

Let's start

Install Docker

curl -SL https://github.com/docker/compose/releases/download/v2.5.0/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

Install nodejs and yarn

sudo apt-get install npm  -y
sudo npm install -g n yarn -y

Install @subql/cli

sudo npm install -g @subql/cli

Okey, we are ready for create first project!

Create Project

subql init tutorials-block-timestamp

Name: name project

Network: Select "Polkadot"

Template: Select "Starter project"

Git repository: Git URL to a repo - not necessarily

RPC endpoint: https://polkadot.api.onfinality.io

Authors: your name

Description: info about your project - not necessarily

Version: 1.0.0

License : Apache-2.0

Install the new project’s dependencies

cd block-timestamp
yarn

Updating GraphQL

You need update the schema.graphql

type BlockTs @entity {
  # BlockHash
  id: ID!

  blockHeight: BigInt!

  timestamp: Date!
}
yarn codegen

Build the Project

yarn build

Run Project with Docker

sudo docker-compose up

Query your Project

Open link http://localhost:3000/

Insert in the left window and press Play

{
  query {
    blockTs(
      first: 10,
      orderBy: BLOCK_HEIGHT_DESC
    ) {
      nodes {
        id
        blockHeight
        timestamp
      }
    }
  }
}

Done!

You can also pass the wonderful "Hero course" - https://university.subquery.network/academy/herocourse/welcome.html