Hosted Graph Nodes

Introduction

Developers can use Graph Node for subgraph deployment and querying. With this service, they can index data about deployed contracts, logs, and calls. Unfortunately, The Graph's Hosted Service and Subgraph Studio don't contain the Sonic testnet yet, so you have to use the CLI interface to interact with Graph Node.

Creating Subgraph

This article provides more details on creating and using subgraphs on the Sonic testnet. However, please keep in mind that the Sonic testnet still needs to be supported by the Graph CLI, and there are some differences to be aware of.

Install the Graph CLI

The Graph CLI is written in JavaScript, and you will need to install either yarn or npm to use it; it is assumed that you have yarn in what follows.

Install with yarn:

yarn global add @graphprotocol/graph-cli

Install with npm:

npm install -g @graphprotocol/graph-cli

Init Subgraph

At the moment, it's not possible to select the Sonic testnet, so use fantom as a network. We'll replace it manually later. Fill in your GitHub username (case sensitive) as it will be used later for authentication.

$ graph init

✔ Protocol · ethereum
✔ Product for which to initialize · hosted-service
✔ Subgraph name · <GITHUB_USER_NAME>/mySubgraph
✔ Directory to create the subgraph in · mySubgraph
✔ Ethereum network · fantom
✔ Contract address · <CONTRACT ADDRESS>
✖ Failed to fetch ABI from Etherscan: ABI not found, try loading it from a local file
✔ Do you want to retry? (Y/n) · false
✖ Failed to fetch Start Block: Failed to fetch contract creation transaction hash
✔ Do you want to retry? (Y/n) · false
✔ ABI file (path) · ./myContract.json
✔ Start Block · <CONTRACT_DEPLOY_BLOCK_NR>
✔ Contract Name · MyContract
✔ Index contract events as entities (Y/n) · true/false
  Generate subgraph
  Write subgraph to directory
✔ Create subgraph scaffold
✔ Initialize networks config
✔ Initialize subgraph repository
✔ Install dependencies with yarn
✔ Generate ABI and schema types with yarn codegen
Add another contract? (y/n): 
Subgraph GITHUB_USER_NAME/mySubgraph created in mySubgraph

Go into the directory where your subgraph was created, open file networks.json, and rename fantom to sonic so it looks like this:

{
  "sonic": {
    "MyContract": {
      "address": "<CONTRACT ADDRESS>",
      "startBlock": 10000
    }
  }
}

Then open file subgraph.yaml and also replace fantom with sonic:

specVersion: 0.0.5
schema:
  file: ./schema.graphql
dataSources:
  - kind: ethereum
    name: MyContract
    network: sonic
    source:
    .
    .
    .

Build Your Subgraph

You can modify any aspect of your subgraph to achieve the desired indexing. More details on how to do that can be found in The Graph documentation.

After you've made all the changes, you can build your subgraph:

graph build

Authorization

For deploying your subgraph to the Sonic testnet, you have to obtain a security token, which you'll use for creating, deploying, and removing.

Obtaining this token is similar to how it's done on The Graph's Hosted Service. Go to this login page and log in with your GitHub account. After you log in, you will obtain an access token, which will be used for registering, deploying, and removing actions.

Then you can store your token in the Graph CLI:

graph auth https://graph.testnet.soniclabs.com/service "<ACCESS_TOKEN>"

Register Your Subgraph

Before you can deploy your subgraph, you have to register its name:

graph create --node https://graph.testnet.soniclabs.com/service "<GITHUB_USER_NAME/mySubgraph>"

Deploying

Finally, you can deploy your subgraph with this command:

graph deploy --node https://graph.testnet.soniclabs.com/service --ipfs https://graph.testnet.soniclabs.com/ "<GITHUB_USER_NAME/mySubgraph>"

The endpoints to query your subgraph are provided at the end of the deployment log. However, they need to be corrected. Please refer to the next section to learn the correct way to query your subgraph.

Browsing

If everything goes well, your subgraph will be ready, and you can view it in the GraphQL playground at this address:

https://graph.testnet.soniclabs.com/gql/subgraphs/name/<GITHUB_USER_NAME/mySubgraph>/graphql

The endpoint for your application is:

https://graph.testnet.soniclabs.com/gql/subgraphs/name/<GITHUB_USER_NAME/mySubgraph>

Status of your subgraph is:

https://graph.testnet.soniclabs.com/service/status/?subgraph=<GITHUB_USER_NAME/mySubgraph>

Last updated

© 2024 Sonic Labs