The Graph
The Graph is an indexing service which collects Ethereum events and exports them through GraphQL endpoints. It is widely used in the Ethereum ecosystem which supports fast and cheap queries for DApps.
This tutorial covers the following topics:
- Running a Graph node on Aurora.
- Creating and deploying a subgraph.
- Querying events from the subgraph.
Prerequisites
Before delving into the tutorial, you need to make sure that you have setup the following tools on you machine:
Running Graph Node
Clone
Clone the graph node source code
git clone https://github.com/graphprotocol/graph-node.git
cd graph-node
Configure
In order wire your local graph node with Aurora Testnet RPC, you should change the value of ethereum
section in docker/docker-compose.yaml
file from mainnet:http://host.docker.internal:8545
to 'aurora:https://testnet.aurora.dev'
.
....
environment:
postgres_host: postgres
postgres_user: graph-node
postgres_pass: let-me-in
postgres_db: graph-node
ipfs: 'ipfs:5001'
ethereum: 'aurora:https://testnet.aurora.dev'
GRAPH_LOG: info
...
Start
The following commands will setup the environment and start the graph indexer. The indexing process might take long time to have 100% sync with the chain. This has nothing to do with our tutorial but keep this process running in a separate terminal.
cd graph-node/docker
./setup.sh
docker-compose up
Create a subgraph
Now we are done with starting our graph node, the next step is to create and deploy a subgraph. The subgraph defines how the data on Ethereum will be indexed and stored on the graph node.
In this tutorial, we are going to use the subgraph example called GravatarRegistry (a simple on-chain Gravatar). The GravatarRegistry contract has two events:
event NewGravatar(uint id, address owner, string displayName, string imageUrl);
event UpdatedGravatar(uint id, address owner, string displayName, string imageUrl);
The contract was already deployed on Aurora Testnet. The deployed GravatarRegistry
contract address is 0x8773e6832f44b2C17AC78592ffCe407C62d8c36E
and the start block number is 74885768
.
Clone subgraph
Clone subgraph example repo.
git clone https://github.com/aurora-is-near/example-subgraph.git
cd example-subgraph
Install
yarn install
Configure the Subgraph
Update the address
and (the startBlock
optional) in subgraph.yaml
as follows:
...
network: aurora
source:
address: '0x8773e6832f44b2C17AC78592ffCe407C62d8c36E'
abi: Gravity
startBlock: 74885768
...
Also make sure you are pointing into aurora
as a network.
Generating types
yarn codegen
Skip migration: Bump mapping apiVersion from 0.0.1 to 0.0.2
Skip migration: Bump mapping apiVersion from 0.0.2 to 0.0.3
Skip migration: Bump mapping apiVersion from 0.0.3 to 0.0.4
Skip migration: Bump mapping apiVersion from 0.0.4 to 0.0.5
Skip migration: Bump mapping specVersion from 0.0.1 to 0.0.2
✔ Apply migrations
⚠ Warnings while loading subgraph from subgraph.yaml: Warnings in subgraph.yaml:
Path: repository
The repository is still set to https://github.com/graphprotocol/example-subgraph.
Please replace it with a link to your subgraph source code.
Path: description
The description is still the one from the example subgraph.
Please update it to tell users more about your subgraph.
✔ Load subgraph from subgraph.yaml
Load contract ABI from abis/Gravity.json
✔ Load contract ABIs
Generate types for contract ABI: Gravity (abis/Gravity.json)
Write types to generated/Gravity/Gravity.ts
✔ Generate types for contract ABIs