Dojo DEX USDT Volume Subgraph Example
The USDT Exchanges Volume Subgraph tracks the historical USDT volume for the INJ-USDT
pair in the Dojo DEX.
Tip: This tutorial teaches you how to build a Substreams from scratch.
Remember that you can auto-generate your Substreams module by usig the code-generation tools.
The subgraph uses the Substreams triggers to import data from the Injective foundational modules.
Before You Begin
Inspect the Project
The subgraph.yaml
file defines the configuration of the data sources (i.e. where the subgraph should get the data from).
Path to the GraphQL schema, which defines the entities of the subgraph.
Network where data should be indexed. In this case,
injective-mainnet
.Substreams module imported in the subgraph. This module extracts all the events with
type = wasm
.Substreams package (
.spkg
) that contains thewasm_events
module.Definition of mappings. The
handleEvents
function will receive the data from thewasm_events
Substreams module to be processed by the subgraph.
Inspect the Schema
The schema.graphql
schema defines only one entity, USDTExchangeVolume
, which holds the historical amount of the USDT exchanged in the Dojo DEX for the INJ-USDT
pair.
The amount
field is updated every time that a new exchange happens in the DEX.
Inspect the Code
The handleEvents
function in the mappings.ts
file receives the filtered events of the Substreams (those with type = wasm
). The logic of the code finds out the USDT amount exchanged in the swap and updates the USDTExchangeVolume
entity, adding up the amount.
Definition of the
handleEvents
function. As a parameter, it receives an array of bytes, representing the events consumed from the Substreams.Decode the byte array into the
EventList
Protobuf object, which is the output of the Substreams.Load the
USDTExchangeVolume
subgraph entity, which will store the historical volume. If it is the first trade, then the entity will not exist, and it must be created.Iterate over the events and verify that the event type is
wasm
(type == wasm
). This should be already filtered by the Substreams, but it is also nice to re-check it.Iterate over the attributes of every event, finding out the neccesary information (contract address, action, ask amount, offer amount...).
Verify that the contract where the event is executed corresponds to the
INJ-USDT
pair in the Dojo DEX.Update the entity.
Deploy to a Local Graph Node
You can test your Substreams-powered Subgraph by deploying to a local Graph Node set-up. Take a look at the the Graph Node Local Development tutorial, which provides information on how to spin up a local environment for Graph Node.
First, clone the Substreams Development Environment GitHub respository and move to the graph-node
folder. Execute the start.sh
command with the Injective information (make sure you have Docker running in your computer).
The previous command will spin up a local IPFS node, a local Postgres database and a local Graph Node instance. Now, you can create a new subgraph in the Graph Node:
Then, you can deploy:
The subgraph will start indexing in the Graph Node, and you check out the different logs emitted by the subgraph.
Last updated