Hosted Graph Nodes

How to create and use subgraphs on the Fantom Sonic builders testnet

What is this API for

Current solution uses Graph Node to allow developers to deploy and query their subgraphs so they can use this service for indexing data about deployed contracts, logs, and calls. Unfortunately, The Graph hosted service and Subgraph Studio don't contain the Sonic builders testnet network yet, so you have to use the CLI interface to interact with Graph Node.

Using already created subgraphs

If you already have your subgraphs created and have contracts deployed on the same addresses on the Sonic network, you can change only the network.

Creating subgraph

For creating and using subgraphs on the Sonic network, you can use this article as a guideline with more details. However, Sonic is not supported with the Graph CLI yet. There are a few things that are different.

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 is not possible to select the Sonic network, so use fantom as a network and we will replace it manually later. Fill in your GitHub user name (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 will look 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 change anything in your subgraph to obtain needed indexing. More details on how to do that can be found on The Graph documentation.

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

graph build

Authorization

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

Obtaining this token is similar to how it's done on The Graph 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. https://graph.public-sonic.fantom.network/service/login

Then you can store your token to the Graph CLI:

graph auth https://graph.public-sonic.fantom.network/service "<ACCESS_TOKEN>"

Register your subgraph

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

graph create --node https://graph.public-sonic.fantom.network/service "<GITHUB_USER_NAME/mySubgraph>"

Deploying

Finally, you can deploy your subgraph with this command:

graph deploy --node https://graph.public-sonic.fantom.network/service --ipfs https://graph.public-sonic.fantom.network/ "<GITHUB_USER_NAME/mySubgraph>"

There is information about endpoints to query your subgraph at the end of the log of the deploy proccess, but they're wrong, so please use the next section to find out how to query it.

Browsing

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

https://graph.public-sonic.fantom.network/gql/subgraphs/name/<GITHUB_USER_NAME/mySubgraph>/graphql

The endpoint for your application is:

https://graph.public-sonic.fantom.network/gql/subgraphs/name/<GITHUB_USER_NAME/mySubgraph>

Still a work in progress

We are preparing a website similar to Hosted Service, so you will be able to see synchronization status and subgraph errors, so please check this page regularly.

Last updated

© 2024 Fantom Foundation