Core Subgraph using GraphQL
Developers can use a self-hosted subgraph to extract data from the Core blockchain, processing and storing it for consumption via GraphQL. Let's take a look at how we can create and deploy this powerful tool.
Important URLs
We'll be using some standard URLs throughout this guide, which it’ll help to review before getting started.
MainNet
URL | Usage |
---|---|
Management URL: https://thegraph.coredao.org/deploy/ | Used to create/deploy subgraphs (--node or -g). |
IPFS URL: https://thegraph.coredao.org/ipfs/ | Used to deploy subgraphs. |
HTTP query URL: https://thegraph.coredao.org | Used to query data from subgraphs. |
Health check URL https://thegraph-is.coredao.org | Used to check subgraph health. |
TestNet
URL | Usage |
---|---|
Management URL: https://thegraph-node.test.btcs.network | Used to create/deploy subgraphs (--node or -g). |
IPFS URL: https://thegraph-ipfs.test.btcs.network | Used to deploy subgraphs. |
HTTP query URL: https://thegraph.test.btcs.network | Used to query data from subgraphs. |
Health check URL https://thegraph-sub.test.btcs.network | Used to check subgraph health. |
General Subgraph Setup Steps
Create Repository
To create your subgraph repository, follow the official guide from The Graph. In order to deploy onto the Core graph network, make sure to set your network name in subgraph.yaml
to core. Depending on your subgraph, there may be a few more changes to make. You’ll find additional details in the full example section below.
Create and Deploy
To create and deploy your subgraph, execute the following commands in your subgraph project repository.
# Create subgraph
graph create your-subgraph-name --node https://thegraph.coredao.org/deploy/
# Deploy subgraph
graph deploy your-subgraph-name --node https://thegraph.coredao.org/deploy/ --ipfs https://thegraph.coredao.org/ipfs/
The graph deploy
command should return the HTTP query URL for your subgraph, which you can integrate into your application as appropriate.
Example: Deploying a Uniswap V2 subgraph
Now that we understand the process, let's walk through a full example by setting up a Uniswap V2 subgraph on Core.
First, clone the Uniswap V2 subgraph repository, navigate to the project folder, then execute the following commands from that directory:
# Install dependencies with npm (or yarn)
npm install
# Generate subgraph code
npm run codegen
Now we'll make a few required changes to the subgraph.yaml and helpers.ts files:
- subgraph.yaml:
- Change network name to
core
on lines 9 and 37. - Update the factory address on line 11.
- Update the
startBlock
on line 13 to a reasonable block height (current block height available here).
- Change network name to
- src/mappings/helpers.ts:
- Update the factory address on line 11.
Finally, we'll run the create and deploy commands. In order to avoid compatibility issues, let's use the graph-cli installed in the project (instead of the global version) by prefixing the graph
command with ./node_modules/.bin/
.
# Create a new subgraph called uniswap-4-test
./node_modules/.bin/graph create uniswap-4-test --node https://thegraph.coredao.org/deploy/
# Deploy the uniswap-4-test subgraph
./node_modules/.bin/graph deploy uniswap-4-test --node https://thegraph.coredao.org/deploy/ --ipfs https://thegraph.coredao.org/ipfs/
# The deploy command should print an HTTP query URL similar to:
# https://thegraph.coredao.org/subgraphs/name/uniswap-4-test
Well done, your subgraph is deployed! Now you can integrate the HTTP query URL printed by the deploy command into your website and use it to query data.
Authorization
To prevent your subgraph from being overwritten by others, please contact us on discord for an authorization key.
If you are testing against the TestNet Graph, here is an access token for general testing purpose: a9a79c2aea604bfaa861ff93d46d0d11.