Module 4 (sub 1, 2, 3, 4) SubQuery

๐Ÿ‡จ๐Ÿ‡ณ ๐Ÿ‡ฆ๐Ÿ‡ช ๐Ÿ‡ช๐Ÿ‡ธ ๐Ÿ‡ฐ๐Ÿ‡ท ๐Ÿ‡ฉ๐Ÿ‡ช ๐Ÿ‡ฏ๐Ÿ‡ต ๐Ÿ‡ฎ๐Ÿ‡ฉ ๐Ÿ‡ฎ๐Ÿ‡ณ ๐Ÿ‡น๐Ÿ‡ท ๐Ÿ‡น๐Ÿ‡ญ ๐Ÿ‡ป๐Ÿ‡ณ ๐Ÿ‡ท๐Ÿ‡บ ๐Ÿ‡บ๐Ÿ‡ฆ ๐Ÿ‡จ๐Ÿ‡ฟ

ยท

5 min read

Module 4 (sub 1, 2, 3, 4) SubQuery

Module 4 sub 1

Compile

  • NodeJS
  • NPM
  • Yarn
  • Docker
  • Docker Compose

thanks nodes guru..

wget -q -O subquery.sh https://api.nodes.guru/subquery.sh && chmod +x subquery.sh && sudo /bin/bash subquery.sh
git clone https://github.com/subquery/tutorials-simple-aggregation_v2 && cd tutorials-simple-aggregation_v2 && yarn install && yarn codegen && yarn build && docker-compose pull && docker-compose up

schema.graphql

type StakingReward @entity{
id: ID! #blockHeight-eventIdx
account: String!
balance: BigInt!
date: Date!
blockHeight: Int!
}

project.yaml

specVersion: 0.2.0
name: staking-rewards
version: 1.0.0
description: ''
repository: ''
schema:
  file: ./schema.graphql
network:
  genesisHash: '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3'
  endpoint: wss://polkadot.api.onfinality.io/public-ws
dataSources:
  - kind: substrate/Runtime
    startBlock: 7000000
    mapping:
      file: ./dist/index.js
      handlers:
      - handler: handleStakingRewarded
        kind: substrate/EventHandler
        filter:
          module: staking 
          method: Rewarded

mappingHandlers.ts

import {SubstrateEvent} from "@subql/types";
import {StakingReward} from "../types";
import {Balance} from "@polkadot/types/interfaces";

export async function handleStakingRewarded(event: SubstrateEvent):
Promise<void> {
    const {event: {data: [account, newReward]}} = event;
    const entity = new
StakingReward(`${event.block.block.header.number}-${event.idx.toString()
}`);
    entity.account = account.toString();
    entity.balance = (newReward as Balance).toBigInt();
    entity.date = event.block.timestamp;
    entity.blockHeight = event.block.block.header.number.toNumber();
    await entity.save();
}

docker-compose.yml

version: '2.1'

services:
  postgres:
    image: postgres:12-alpine
    ports:
      - 5432:5432
    volumes:
      - .data/postgres:/var/lib/postgresql/data
    environment:
      POSTGRES_PASSWORD: postgres

  subquery-node:
    image: onfinality/subql-node:v0.25.3
    depends_on:
      - "postgres"
    restart: always
    environment:
      DB_USER: postgres
      DB_PASS: postgres
      DB_DATABASE: postgres
      DB_HOST: postgres
      DB_PORT: 5432
    volumes:
      - ./:/app
    command:
      - -f=/app
      - --local

  graphql-engine:
    image: onfinality/subql-query:v0.8.0
    ports:
      - 3000:3000
    depends_on:
      - "postgres"
      - "subquery-node"
    restart: always
    environment:
      DB_USER: postgres
      DB_PASS: postgres
      DB_DATABASE: postgres
      DB_HOST: postgres
      DB_PORT: 5432
    command:
      - --name=app
      - --playground
      - --indexer=http://subquery-node:3000

server_ip:3000

query{
stakingRewards(first: 3 orderBy:BLOCK_HEIGHT_ASC){
nodes{
blockHeight
account
date
balance
}
}
}

repo for module 4-1

github.com/MikkiKill/tutorials-simple-aggre..

Module 4 sub 2

cd $HOME
git clone https://github.com/MikkiKill/tutorials-simple-aggregation_v2_m4-2 && cd tutorials-simple-aggregation_v2_m4-2 && yarn install && yarn codegen && yarn build && docker-compose pull && docker-compose up

schema.graphql

type StakingReward @entity{
id: ID! #blockHeight-eventIdx
account: String!
balance: BigInt!
date: Date!
blockHeight: Int!
}

type SumReward @entity{
id: ID! # AccountId
totalReward: BigInt!
blockheight: Int!
}

project.yaml

specVersion: 0.2.0
name: tonystake
version: 1.0.0
description: ''
repository: ''
schema:
  file: ./schema.graphql
network:
  genesisHash: '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3'
  endpoint: wss://polkadot.api.onfinality.io/public-ws
dataSources:
  - kind: substrate/Runtime
    startBlock: 7000000
    mapping:
      file: ./dist/index.js
      handlers:
      - handler: handleSumRewarded
        kind: substrate/EventHandler
        filter:
          module: staking
          method: Rewarded
      - handler: handleStakingRewarded
        kind: substrate/EventHandler
        filter:
          module: staking
          method: Rewarded

mappingHandlers.ts

import {SubstrateEvent} from "@subql/types";
import {StakingReward, SumReward} from "../types";
import {Balance} from "@polkadot/types/interfaces";

export async function handleStakingRewarded(event: SubstrateEvent):
Promise<void> {
    const {event: {data: [account, newReward]}} = event;
    const entity = new
StakingReward(`${event.block.block.header.number}-${event.idx.toString()
}`);
    entity.account = account.toString();
    entity.balance = (newReward as Balance).toBigInt();
    entity.date = event.block.timestamp;
    entity.blockHeight = event.block.block.header.number.toNumber();
    await entity.save();
}

function createSumReward(accountId: string): SumReward {
    const entity = new SumReward(accountId);
    entity.totalReward = BigInt(0);
    return entity;
}
export async function handleSumRewarded(event: SubstrateEvent):
Promise<void> {
    const {event: {data: [account, newReward]}} = event;
    let entity = await SumReward.get(account.toString());
    if (entity === undefined){
    entity = createSumReward(account.toString());
}
    entity.totalReward = entity.totalReward + (newReward as Balance).toBigInt();
    entity.blockheight = event.block.block.header.number.toNumber();
    await entity.save();
}

server_ip:3000

query{
sumRewards(first:3 orderBy:BLOCKHEIGHT_ASC){
nodes{
blockheight
id
totalReward
}
}
}

repo for module 4-2

github.com/MikkiKill/tutorials-simple-aggre..

Module 4 sub 3

cd $HOME
git clone https://github.com/MikkiKill/tutorials-simple-aggregation_v2_m4-3 && cd tutorials-simple-aggregation_v2_m4-3 && yarn install && yarn codegen && yarn build && docker-compose pull && docker-compose up

schema.graphql

type StakingReward @entity{
id: ID! #blockHeight-eventIdx
account: SumReward!
balance: BigInt!
date: Date!
blockheight: Int
}

type SumReward @entity{
id: ID! # AccountId
totalReward: BigInt!
blockheight: Int!
}

project.yaml

specVersion: 0.2.0
name: tonystake
version: 1.0.0
description: ''
repository: ''
schema:
  file: ./schema.graphql
network:
  genesisHash: '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3'
  endpoint: wss://polkadot.api.onfinality.io/public-ws
dataSources:
  - kind: substrate/Runtime
    startBlock: 7000000
    mapping:
      file: ./dist/index.js
      handlers:
      - handler: handleSumRewarded
        kind: substrate/EventHandler
        filter:
          module: staking
          method: Rewarded
      - handler: handleStakingRewarded
        kind: substrate/EventHandler
        filter:
          module: staking
          method: Rewarded

mappingHandlers.ts

import {SubstrateEvent} from "@subql/types";
import {StakingReward, SumReward} from "../types";
import {Balance} from "@polkadot/types/interfaces";

export async function handleStakingRewarded(event: SubstrateEvent):
Promise<void> {
    const {event: {data: [account, newReward]}} = event;
    const entity = new
StakingReward(`${event.block.block.header.number}-${event.idx.toString()
}`);
    entity.accountId = account.toString();
    entity.balance = (newReward as Balance).toBigInt();
    entity.date = event.block.timestamp;
    await entity.save();
}

function createSumReward(accountId: string): SumReward {
    const entity = new SumReward(accountId);
    entity.totalReward = BigInt(0);
    return entity;
}
export async function handleSumRewarded(event: SubstrateEvent):
Promise<void> {
    const {event: {data: [account, newReward]}} = event;
    let entity = await SumReward.get(account.toString());
    if (entity === undefined){
    entity = createSumReward(account.toString());
}
    entity.totalReward = entity.totalReward + (newReward as Balance).toBigInt();
    entity.blockheight = event.block.block.header.number.toNumber();
    await entity.save();
}

server_ip:3000

query{
sumRewards(filter:
{id:{equalTo:"16jWQMBXZNxfgXJmVL61gMX4uqtc9WTXV3c8DGx6DUKejm7"}}){
nodes{
blockheight
id
totalReward
stakingRewardsByAccountId{
nodes{
balance
}
}
}
}
}

repo for module 4-3

github.com/MikkiKill/tutorials-simple-aggre..

Module 4 sub 4

cd $HOME
git clone https://github.com/MikkiKill/tutorials-simple-aggregation_v2_m4-4 && cd tutorials-simple-aggregation_v2_m4-4 && yarn install && yarn codegen && yarn build && docker-compose pull && docker-compose up

schema.graphql

type StakingReward @entity{
id: ID! #blockHeight-eventIdx
account: SumReward!
balance: BigInt!
date: Date!
blockheight: Int
}

type SumReward @entity{
id: ID! # AccountId
totalReward: BigInt!
blockheight: Int
}

project.yaml

specVersion: 0.2.0
name: tonystake
version: 1.0.0
description: ''
repository: ''
schema:
  file: ./schema.graphql
network:
  genesisHash: '0x91b171bb158e2d3848fa23a9f1c25182fb8e20313b2c1eb49219da7a70ce90c3'
  endpoint: wss://polkadot.api.onfinality.io/public-ws
dataSources:
  - kind: substrate/Runtime
    startBlock: 6000000
    mapping:
      file: ./dist/index.js
      handlers:
      - handler: handleSumReward
        kind: substrate/EventHandler
        filter:
          module: staking
          method: Reward
      - handler: handleStakingReward
        kind: substrate/EventHandler
        filter:
          module: staking
          method: Reward

mappingHandlers.ts

import {SubstrateEvent} from "@subql/types";
import {StakingReward, SumReward} from "../types";
import {Balance} from "@polkadot/types/interfaces";

export async function handleStakingReward(event: SubstrateEvent):
Promise<void> {
    const {event: {data: [account, newReward]}} = event;
    const entity = new
StakingReward(`${event.block.block.header.number}-${event.idx.toString()
}`);
    entity.accountId = account.toString();
    entity.balance = (newReward as Balance).toBigInt();
    entity.date = event.block.timestamp;
    await entity.save();
}

function createSumReward(accountId: string): SumReward {
    const entity = new SumReward(accountId);
    entity.totalReward = BigInt(0);
    return entity;
}
export async function handleSumReward(event: SubstrateEvent):
Promise<void> {
    const {event: {data: [account, newReward]}} = event;
    let entity = await SumReward.get(account.toString());
    if (entity === undefined){
    entity = createSumReward(account.toString());
}
    entity.totalReward = entity.totalReward + (newReward as Balance).toBigInt();
    entity.blockheight = event.block.block.header.number.toNumber();
    await entity.save();
}

server_ip:3000

query{
sumRewards(first:3 orderBy:BLOCKHEIGHT_ASC){
nodes{
blockheight
id
totalReward
}
}
}

repo for module 4-4

github.com/MikkiKill/tutorials-simple-aggre..

official guide

drive.google.com/viewerng/viewer?url=https:..

Did you find this article valuable?

Support tonym00n by becoming a sponsor. Any amount is appreciated!

ย