Skip to content

khoa-klaytn/example-subgraph

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

example-subgraph

Example Subgraph for Baobab ERC20 contract.

  • tracks n_transfers, the number of transfers made by each address

This example uses the following contract details:

Table of Contents

Prerequisites

Run this example

  1. Install dependencies
pnpm i
  1. Generate typings
pnpm run codegen
  1. Build the subgraph
pnpm run build

Rerun when src/* changes

  1. Create a subgraph on the graph node
export GRAPH_ADMIN_URL=<Graph Node Admin URL>
pnpm run create
  1. Deploy the subgraph
export GRAPH_ADMIN_URL=<Graph Node Admin URL>
export IPFS_ADMIN_URL=<IPFS Node Admin URL>
pnpm run deploy

Note: The Graph Node must be running on Baobab testnet.

  1. Query the subgraph

A GraphQL UI & API should be exposed at <Graph Node Query URL>/subgraphs/name/example-subgraph

⚠️ QUERY URL, not Admin URL!

Subgraph your own contract

  1. Deploy your own ERC20 contract

Paste the ABI JSON into abis/<Contract Name>.json

Edit subgraph.yaml:

  • dataSources > 0:
    • abis > 0:
      • name: <ContractName>
      • file: ./abis/<ContractName>.json`
    • source:
      • address: <Contract Address>
      • abi: <ContractName>
      • startBlock: <Start Block>
  1. Define Event Handlers under subgraph.yaml dataSources > 0 > eventHandlers

    • event should exactly match event signatures in the contract ABI.

E.g., to index the Transfer event, if the ABI is

      {
        "indexed": true,
        "internalType": "address",
        "name": "from",
        "type": "address"
      },
      {
        "indexed": true,
        "internalType": "address",
        "name": "to",
        "type": "address"
      },
      {
        "indexed": false,
        "internalType": "uint256",
        "name": "value",
        "type": "uint256"
      }
    ],
    "name": "Transfer"

... the event signature should be Transfer(indexed address,indexed address,uint256)

  1. Define the GraphQL schema in schema.graphql
    • Entities & fields you want to track

List queryable entities under subgraph.yaml dataSources > 0 > entities

  1. Generate typings
pnpm run codegen

Rerun when abis/<Contract Name>, schema.graphql or subgraph.yaml changes

  1. Write & export handler functions in src/mappings.ts

  2. Build the subgraph

pnpm run build

Rerun when src/* changes

  1. Create a subgraph on the graph node
export GRAPH_ADMIN_URL=<Graph Node Admin URL>
pnpm run create
  1. Deploy the subgraph
export GRAPH_ADMIN_URL=<Graph Node Admin URL>
export IPFS_ADMIN_URL=<IPFS Node Admin URL>
pnpm run deploy

Note: The Graph Node must be running on the same network as the deployed contract.

  1. Query the subgraph

A GraphQL UI & API should be exposed at <Graph Node Query URL>/subgraphs/name/<Subgraph Name>

⚠️ QUERY URL, not Admin URL!

Releases

No releases published

Packages

No packages published