Your First Injective Subgraph

The substreams init command allows you to easily auto-generate several types of Substreams projects. In this specific tutorial, you will learn how to bootstrap an Injective Substreams-powered Subgraph to filter all the Injective events that you want with almost no-code needed!

Before You Begin

Create the Project

The substreams init command allows you choose among several code-generation tools, depending on your needs. In this example, you will create an Injective Substreams module that streams data to a subgraph.

  1. In a command-line terminal, run substreams init. You can choose among several project types.

  2. Choose injective-subgraph. Now, you will input the parameters needed to create your Injective Substreams-powered Subgraph.

  3. Project name: give a name to your project. This will be the name of the folder in your filesystem.

  4. Chain: you can choose to index data on Injective Mainnet or Injective Testnet.

  5. Start block: first block where your Subgraph starts indexing data.

  6. Filtering options: the Injective data that you will received will be filtered based on the events (e.g. using the event type of the event attributes). There are several options:

    • Specific events: you get a list of events filtered by the event type and/or the event attributes. Note that you will only get the events specified.

    • All events in transactions where at least one event matches your query: in this case, you get all the events of the transaction as long as one event matches your criteria within the transaction.

    • Full transactions where at least one event matches your query: you get a list transactions containing at least one of the events specified.

The Injective Explorer gives you information about the different event types and attributes contained in a transaction.

Then, you will be asked to input the type and/or attributes that you want to use for the filtering.

  1. Directory: lastly, choose the directory where you want to create the project.

Done! A new Substreams-powered Subgraph project will be created in the specified directory.

Inspect the Project

Now, let's inspect the project and understand all the files created. In an IDE of your choice (e.g. VSCode), open the project.

  1. The src/mappings.ts file contains the source of the subgraph. Here, you define the handleEvents function, which receives all the filtered events from the Substreams. You can manipulate and aggregate the events to create the subgraph entities that you need.

  2. The .spkg file is the binary file generated by the Substreams. Essentially, this file contains the all the source code to filter the events that you selected when running the substreams init command.

The Graph Node (i.e. the software that runs your subgraph) uses the spkg file to extract the Injective data and provide it as AssemblyScript code to the handleEvents function.

  1. The schema.graphql file contains the subgraph entities. You can use this file to define the output model of your subgraph. The entities defined in the GraphQL schema are generated into AssemblyScript code by running the graph codegen.

  2. The subgraph.yaml file is the configuration file of the subgraph. It defines the source code of the subgraph (in this case, Substreams) and the handler that will receive the data extracted from Injective (in this case, the handleEvents function).

Build the Project

Before deploying your subgraph, you must install and generate all the necessary code:

  1. Run npm install to install the dependencies of the project.

  2. Run npm run generate to generate the output of the Substreams (i.e. the Injective event list) in AssemblyScript. This allows you to access the output of the Substreams inside the AssemblyScript code. In this case, the EventList object, representing a list of Injective events, is generated.

  3. Run npm run codegen to generate the subgraph entities that you have defined in the schema.graphql file.

Test the Subgraph

After installing all the necessary depdendencies of the project, you are now able to deploy the subgraph a local Graph Node.

Before publishing to decentralized network, you can test your subgraph locally, thus not incurring in any costs associated with the deployment. You can run a Graph Node instance (the software that indexers use) in a Docker environment, and replicate the deployment locally.

In the project that you created previously, the dev-environment folders contains all the necessary files to spin up a Docker-based Graph Node environment (note that you will need Docker installed and available in your computer).

  1. Spin up the environment by running the dev-environment/start.sh script. This script will create a Graph Node instance, a Postgres node and an IPFS node (all these dependencies are necessary for the local Graph Node to succesfully work).

  2. Once the Graph Node has started, in another command-line session, create a new subgraph using the NPM scripts provided in the project:

Note: The NPM scripts, defined in the scripts section of the package.json file, are just a wrapped of the Graph CLI commands.

npm run create-local

The previous command connects to the local Graph Node and creates a subgraph. You can always remove the subgraph using the npm run remove-local command.

  1. Deploy the subgraph.

npm run deploy-local

The previous command deploys the subgraph to the local Graph Node. You can now go back to the logs of the local Graph Node and check out the subgraph indexing.

In a production environment, you will publish your subgraph to the The Graph decentralized network (note that The Graph Studio, the hosted testing environment, is not supported for Injective).

Last updated