The Graph
The Graph is an indexing protocol that provides an easy way to query blockchain data through decentralized APIs known as subgraphs.
With The Graph, developers can benefit from:
- Decentralized Indexing: Enables indexing blockchain data through multiple indexers, thus eliminating any single point of failure
- GraphQL Queries: Provides a powerful GraphQL interface for querying indexed data, making data retrieval super simple.
- Customization: Define your own logic for transforming & storing blockchain data. Reuse subgraphs published by other developers on The Graph Network.
Follow this quick-start guide to create, deploy, and query a subgraph within 5 minutes.
Benefits
Indexers like The Graph create a queryable data layer specifically tracking events and state changes from your smart contract. By monitoring and organizing this contract data, they enable complex queries that would be impossible through direct blockchain calls. This allows your dApp to efficiently access historical transactions, track relationships between entities, and analyze patterns over time - all without multiple RPC calls.
Prerequisites
- A crypto wallet
- A smart contract address on a supported network
- Node.js installed
- A package manager of your choice (npm, yarn or pnpm)
Quick Start guide
-
Proceed to the Subgraph Studio and login via one of the supported wallets.
-
Confirm the sign and sign a message to use Studio.
-
Click on Create a Subgraph on the main dashboard.
-
Specify the Subgraph Name.
-
Upon creating a Subgraph, you will see its dashboard, where you can a project description, source code URL, and a website URL.
-
On the right side, click on Select a network and pick Autonomys from the dropdown list.
-
Install Graph CLI using provided commands. You can use
yarn
,npm
,pnpm
, orbun
. -
Initialize the subgraph by running
graph init autonomys-subgraph
. This will create a boilerplate subgraph project. During the initialization, you will need to specify a few things:
-
Network (Autonomys)
-
Subgraph slug (feel free to use the default name)
-
Directory to create the subgraph in (feel free to use the default directory)
-
Smart contact address (the address of your smart contract deployed on Autonomys Taurus EVM domain)
-
ABI file (path)
-
Start block (feel free to use the default value)
-
Contract name
-
Index contract events as entities (true or false value)
Tip: you can find the contract ABI by looking at the
token_metadata
file. E.g. if you’re using Remix IDE, find your contract metadata file, collapse it on theabi
line 7, copy the entireabi
including the square brakets [] and save it into ajson
file on your PR.
-
Run
graph auth
to authenticate the project and provide your deploy key which you can find on the Subgraph Dashboard. -
Enter the directory with
cd project_name
and rungraph codegen && graph build
to build the project, you will see thebuild/subgraph.yaml
file being created. -
Deploy your subgraph by running
graph deploy subgraph-autonomys
. You will be asked to specify the version of the subgraph, e.g. v0.0.1. -
Proceed back to the Graph Studio, the status will change to Deployed.
-
The new tabs Playground, Endpoints, Logs will now be accessible to you! You can find usage examples of querying the data, accessing the data via API, and can test queries in the Playground without leaving the studio!
Congratulations, you’ve successfully setup and configured a subgraph for your application!
Querying data in your smart contract
Subgraphs are primarily designed to index and query events emitted by smart contracts.
- Let’s make some slight changes to the
Counter
contract we introduced in the Foundry guide and modify functions to emit events.
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
contract Counter {
uint256 public number;
event NumberSet(uint256 newNumber);
event NumberIncremented(uint256 newNumber);
event NumberDecremented(uint256 newNumber);
function setNumber(uint256 newNumber) public {
number = newNumber;
emit NumberSet(number);
}
function increment() public {
number++;
emit NumberIncremented(number);
}
function decrement() public {
number--;
emit NumberDecremented(number);
}
}
Note: This will affect the contract ABI, make sure to change it and upgrade your Subgraph application.
-
Then we’ll change the
Counter
contract state and set the number to5
by runningcast send YOUR_CONTRACT_ADDRESS "setNumber(uint256)" 5 --rpc-url https://auto-evm.taurus.autonomys.xyz/ws --private-key YOUR_KEY
-
Let’s trigger one more event in the
Counter
contract by calling theincrement()
function. We can do that by runningcast send YOUR_CONTRACT_ADDRESS "increment()" --rpc-url https://auto-evm.taurus.autonomys.xyz/ws --private-key YOUR_KEY
-
With two events emitted, let’s proceed to The Graph playground and query them!
-
Open the Graph Playground tab where you try running the following queries:
Query to Get All NumberSet Events
{ numberSets(first: 10, orderBy: blockTimestamp, orderDirection: desc) { id newNumber blockTimestamp blockNumber transactionHash } }
Query to Get All Increment Events
{ numberIncrementeds(first: 10, orderBy: blockTimestamp, orderDirection: desc) { id newNumber blockTimestamp blockNumber transactionHash } }
Get All Events (Set, Increment, Decrement) in Chronological Order
{ numberSets(orderBy: blockTimestamp) { id newNumber blockTimestamp blockNumber transactionHash __typename } numberIncrementeds(orderBy: blockTimestamp) { id newNumber blockTimestamp blockNumber transactionHash __typename } numberDecrementeds(orderBy: blockTimestamp) { id newNumber blockTimestamp blockNumber transactionHash __typename } }
Tip: you can also run queries directly from the terminal using
cURL
orgql
. You can find Example Usage under the Endpoints tab.