This task will introduce you to subgraphs and the process to deploy them.
This directory contains source code for a subgraph which we have deployed here. We will guide you through deploying this subgraph in your own account. This is the actual subgraph we use to index data from the Compound protocol, which we then show in our Compound project page!
Compound is a non-custodial lending protocol built on Ethereum. When you supply cryptoassets (tokens) to Compound, you receive corresponding cTokens. You can think of these as crypto-native certificates of deposit for a particular market which entitle you to an increasing quantity of the underlying asset on a future date. For example, if you deposit DAI, you will receive a certain amount the corresponding cToken, cDAI.
You can track real-time data related to these markets in the Compound markets page. The figure below shows data on the two biggest Compound markets (Ether and Dai as of Dec 8th 2021).
One of the coolest things about blockchains is that all data is open-source and can be tracked in real-time, so anyone can use tools like The Graph to obtain this same data. The subgraph you'll deploy does just that.
For example, to obtain the highlighted data on total supply, you can navigate to the subgraph playground page and use the query on the left side of the following image (see here for docs on the GraphQL query API syntax and features). The result of the query is shown on the right.
Note that id
-> 0x5d3a536e4d6dbd6114cc1ead35777bab948e3643
is the ethereum address for the cDAI market.
Our subgraph returns a total supply of ~4,643,947,708 DAI. DAI is a stablecoin whose value is very close to $1. Therefore, we can see how this value is very close to the total DAI supply in the markets screenshot (they don't match 1:1 because 1) the data in both sources wasn't queried at the same time and 2) 1 DAI is not exactly equal to $1).
Follow these steps:
- Navigate to The Graph's hosted service.
- Sign in using your GitHub credentials.
- Navigate to your dashboard.
- Click the "Add Subgraph" button.
- Fill in the required fields.
- Subgraph Name: Task 1.
- Account: leave as default (should be your name on GitHub).
- Subtitle: Task 1.
- Enable the "hide" button.
- Click "Create subgraph".
After these steps, you should be taken to your new subgraphs's page (https://thegraph.com/hosted-service/subgraph/YOUR-GITHUB-NAME/task-1
) which will show it as undeployed:
Now, assuming you already installed the Graph CLI as detailed in the main README file, we're ready to deploy the subgraph whose source code is this directory. At this stage, we simply want to take you through the mechanics of deploying a subgraph, and we will go into the details in task-2.
Ensure your terminal's current directory is the task-1
root directory, then start by installing the necessary dependencies:
$ npm ci
Generate AssemblyScript types from the subgraph's GraphQL schema (schema.graphql
file) and the contract ABIs (included in the abis
directory):
$ npm run codegen
Compile the subgraph:
$ npm run build
Change YOUR-GITHUB-NAME
to your actual GitHub username in package.json
.
Use your access token, which you can find in your subgraph page (see image above), to authenticate:
$ graph auth --product hosted-service <ACCESS_TOKEN>
Deploy:
$ npm run deploy
Your subgraph should now be deployed here: https://thegraph.com/hosted-service/subgraph/<YOUR-GITHUB-NAME>/task-1
.
Let's try querying the total supply of the cDAI
market:
In addition to filtering the search results for a specific market (cDAI
), we also performed the search for a specific ethereum block (7824774). If you try the same query in the production subgraph (deployed here) you should get the same result.
This concludes task 1! 💪