Example Subgraph for Baobab ERC20 contract.
- tracks
n_transfers
, the number of transfers made by each address
This example uses the following contract details:
- Address: 0x569A8e0e23e8f338752B568b721075574426f693
- Start Block: 150554742
- pnpm
- Running Graph Node
- Running IPFS Node
- Install dependencies
pnpm i
- Generate typings
pnpm run codegen
- Build the subgraph
pnpm run build
Rerun when src/*
changes
- Create a subgraph on the graph node
export GRAPH_ADMIN_URL=<Graph Node Admin URL>
pnpm run create
- 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.
- Query the subgraph
A GraphQL UI & API should be exposed at <Graph Node Query URL>/subgraphs/name/example-subgraph
⚠️ QUERY URL, not Admin URL!
- 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>
-
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)
- Define the GraphQL schema in schema.graphql
- Entities & fields you want to track
List queryable entities under subgraph.yaml dataSources > 0 > entities
- Generate typings
- src/mappings.ts is not necessary for this step & previous steps
pnpm run codegen
Rerun when abis/<Contract Name>, schema.graphql or subgraph.yaml changes
-
Write & export handler functions in src/mappings.ts
-
Build the subgraph
pnpm run build
Rerun when src/* changes
- Create a subgraph on the graph node
export GRAPH_ADMIN_URL=<Graph Node Admin URL>
pnpm run create
- 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.
- Query the subgraph
A GraphQL UI & API should be exposed at <Graph Node Query URL>/subgraphs/name/<Subgraph Name>
⚠️ QUERY URL, not Admin URL!