Create a Subgraph on The Graph Protocol
Step 1: Define the data I want to query
I want to query the GRT token on Arbitrum. The entities that I would want to be able to query are:
- wallets and (somehow) determine which ones are indexers
- GRT rewards
- dapp contracts that use the graph protocol
- The amount of query fees spent
- The network where the dApp is run
What do I want to do with the query?
All of my queries are time-series queries, so are counts in the period:
- GRT spent on query fees
- By Top N (is this supported?) by dApp Contract addresses
- GRT rewards issued
- By Top N Indexer wallet addresses
- GRT bridged
- By Top N bridges
This should help me to know what I want to include in the graphQL schema, if it's available.
Step 2: Understand the associated contract
I looked up on Arbiscan the associated GRT contract: $0.08 | Graph Token (GRT) Token Tracker | Arbiscan
0x9623063377AD1B27544C965cCd7342f7EA7e88C7
So I am going to miniscan.xyz and entered the contract above for the Arbitrum network.
The UX is easier than Arbiscan (What if Etherscan became a data platform?) and gives me alot of relevant information:
Step 3: Initialize Subgraph
Install the CLI tool:
npm install -g @graphprotocol/graph-cli
Initialize the subgraph with graph init --from-contract <contract-address> --abi <abi-file> --network mainnet
So my <contract-address>
is 0x9623063377AD1B27544C965cCd7342f7EA7e88C7
<abi-file>
is https://miniscan.xyz/code?network=arbitrum&address=0x9623063377ad1b27544c965ccd7342f7ea7e88c7&codeType=ABI
So let's see what happens when I enter:
graph init --from-contract 0x9623063377AD1B27544C965cCd7342f7EA7e88C7
I'm asked which protocol, and select Ethereum
Then asked which tool, I select subgraph-studio
My slug tims-grt-subgraph
Directory: just hit return
I know i want to run on Arbitrum, so I will use the arbitrum-one
network.
It responds with Fetching ABI from Etherscan
Contract address
I hit return
because it populates with the contract above.
Start block
I hit tab return for the default
Contract name
- I didn't know what this is, so I followed the video tutorial and grabbed the name from Miniscan: L2GraphToken
At first things look good:
Index contract events as entities (Y/n) · true
Generate subgraph
Write subgraph to directory
✔ Create subgraph scaffold
✔ Initialize networks config
But then I got an error:
✖ Failed to initialize subgraph repository: stdout maxBuffer length exceeded
RangeError: stdout maxBuffer length exceeded
Code: ERR_CHILD_PROCESS_STDIO_MAXBUFFER
So I piped everything to a file to see if that helped:
graph init --from-contract 0x9623063377AD1B27544C965cCd7342f7EA7e88C7 2>&1 | tee graph_init_output.txt
And it worked:
Subgraph tims-grt created in tims-grt2
Next steps:
1. Run `graph auth` to authenticate with your deploy key.
2. Type `cd tims-grt2` to enter the subgraph.
3. Run `npm run deploy` to deploy the subgraph.
Step 4 Log into Studio to get keys to authenticate
I log in and Create Subgraph
Question that came up for me: does the slug generated need to match the slug I have given? Does the order matter, meaning, should I have created the Subgraph in studio first? The document [1] didn't reference that Studio set up before diving into the CLI. The video [2] did, and I'm not sure, so as a User I definitely feel some anxiety at this point.
Second issue: I don't have a deployment URL, but it's displayed in the tutorial:
Mine looks like this!
So questions now in my mind:
- Is there a hard sequence. Studio must be set up before going to CLI? If so, how would I have known?
- What does it mean to not have a development query URL?
Continuing:
graph auth --studio <hide this deploy key>
I get a Deploy key set for https://api.studio.thegraph.com/deploy/
Then cd
into the directory (this is nice it's a cute and paste from Studio).
Step 5: codegen
and build
graph codegen && graph build
graph codegen
checks for type safety, which I achieved with the response:
Types generated successfully
However, compiling failed!
I got an error:
Error: could not locate `node_modules` in parent directories of subgraph manifest
After jumping in The Graph public Discord, I found that I am not the only one!
This is what the Discord bot responded with:
Make sure you're in the correct directory where node_modules is located. If the issue persists, try running `npm install` in your terminal [[1]](https://discord.com/channels/438038660412342282/438070183794573313/1128803970681417819 "[1]
(https://discord.com/channels/438038660412342282/438070183794573313/1128803970681417819)").
![](https://images-ext-1.discordapp.net/external/0MCc5Ha5H5_PuWdAqmVyYjQG6c05XPzuZfn3yudF6Y8/%3Fsize%3D1024/https/cdn.discordapp.com/avatars/955744627481255976/3eb60a12a1ff46c6fdbe240d9e726400.png)Generated using AwesomeQA AI
- That's a weird response at this point since up to now, I have been using the npm packages
- I believe I have been in the right directory
But I tried again after confirming that node_modules
is in the same directory and it worked!
graph build
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 apiVersion from 0.0.5 to 0.0.6
Skip migration: Bump manifest specVersion from 0.0.1 to 0.0.2
Skip migration: Bump manifest specVersion from 0.0.2 to 0.0.4
✔ Apply migrations
✔ Load subgraph from subgraph.yaml
Compile data source: L2GraphToken => build/L2GraphToken/L2GraphToken.wasm
✔ Compile subgraph
Copy schema file build/schema.graphql
Write subgraph file build/L2GraphToken/abis/L2GraphToken.json
Write subgraph manifest build/subgraph.yaml
✔ Write compiled subgraph to build/
Build completed: build/subgraph.yaml
That was not a good experience and somewhere, whether in the error message or in the docs make it easier to confirm I'm in the right place.
Step 5: YAML
Based on the above, seems like I should look at the generated files:
- schema.graphql
- L2GraphToken.json
- subgraph.yaml
So the spacing isn't correct cutting and pasting from VSCode, but oh well.
specVersion: 0.0.5
schema:
file: ./schema.graphql
dataSources:
- kind: ethereum
name: L2GraphToken
network: arbitrum-one
source:
address: "0x9623063377AD1B27544C965cCd7342f7EA7e88C7"
abi: L2GraphToken
startBlock: 42449274
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- AdminUpdated
- ImplementationUpdated
- PendingImplementationUpdated
abis:
- name: L2GraphToken
file: ./abis/L2GraphToken.json
eventHandlers:
- event: AdminUpdated(indexed address,indexed address)
handler: handleAdminUpdated
- event: ImplementationUpdated(indexed address,indexed address)
handler: handleImplementationUpdated
- event: PendingImplementationUpdated(indexed address,indexed address)
handler: handlePendingImplementationUpdated
file: ./src/l-2-graph-token.ts
References
- Creating a Subgraph | Docs | The Graph
- The Graph 🛠️ Quickstart to Subgraph Development & Querying - Marcus Rein - YouTube