Auto EVMQuery On-chain Data with The Graph

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

  1. Proceed to the Subgraph Studio and login via one of the supported wallets.

    the-graph-1

  2. Confirm the sign and sign a message to use Studio.

    the-graph-2

    the-graph-3

  3. Click on Create a Subgraph on the main dashboard.

    the-graph-4

  4. Specify the Subgraph Name.

    the-graph-5

  5. Upon creating a Subgraph, you will see its dashboard, where you can a project description, source code URL, and a website URL.

    the-graph-6

  6. On the right side, click on Select a network and pick Autonomys from the dropdown list.

    the-graph-7

  7. Install Graph CLI using provided commands. You can use yarn, npm, pnpm, or bun.

  8. 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)

    the-graph-8

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 the abi line 7, copy the entire abi including the square brakets [] and save it into a json file on your PR.

the-graph-9

the-graph-10

  1. Run graph auth to authenticate the project and provide your deploy key which you can find on the Subgraph Dashboard.

  2. Enter the directory with cd project_name and run graph codegen && graph build to build the project, you will see the build/subgraph.yaml file being created.

    the-graph-11

  3. 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.

    the-graph-12

  4. Proceed back to the Graph Studio, the status will change to Deployed.

    the-graph-13

  5. 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!

    the-graph-14

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.

  1. 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.

  1. Then we’ll change the Counter contract state and set the number to 5 by running cast send YOUR_CONTRACT_ADDRESS "setNumber(uint256)" 5 --rpc-url https://auto-evm.taurus.autonomys.xyz/ws --private-key YOUR_KEY

  2. Let’s trigger one more event in the Counter contract by calling the increment() function. We can do that by running cast send YOUR_CONTRACT_ADDRESS "increment()" --rpc-url https://auto-evm.taurus.autonomys.xyz/ws --private-key YOUR_KEY

  3. With two events emitted, let’s proceed to The Graph playground and query them!

  4. 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 or gql. You can find Example Usage under the Endpoints tab.