Triggers
Substreams triggers allow you to embed Substreams data directly in your subgraph. Essentially, you import the Protobufs emitted by your Substreams module and you receive the data in the handler of your subgraph.
For example, consider that you want to consume the transactions emitted by the map_filter_transactions module of the Ethereum Explorer Substreams. With Substreams triggers, you can define a special subgraph handler that imports the Protobuf object and lets you manipulate the data just like a usual AssemblyScript object.
The Protobuf used in the map_filter_transactions
module is the following:
You can generate the previous Protobuf in AssemblyScript and import it as part of your subgraph in a special handler:
You decode the bytes (which contains the Substreams data) into the generated object,
Transactions
. Now you can use the object like any other AssemblyScript object.Loop over the transactions
Create a new subgraph entity for every transaction.
Tutorial: Import the Substreams Explorer package transactions into a subgraph
Following the previous example, the Substreams Sink Examples repository, contains a subgraph importing the transactions through Substreams triggers. Clone the repository and move to the subgraph-triggers-transactions
. To run this tutorial you will need:
The Graph CLI, to build and deploy the subgraph.
Node (>17) and NPM installed.
The
buf
command installed, to generate the Protobuf schemas.
Install the Dependencies
If you are familiar with subgraphs, then the structure of the project should be easy to understand:
// image
The
proto
folder contains the Protobuf definitions of your Substreams (i.e. the definitions you want to import into the subgraph).The
src
folder contains the source code. Mainly, themapping.ts
file with the handlers and thepb
folder with the generated TS code for the Protobuf.Currently, it is necessary to have the Substreams package (
spkg
) that you want to import in your filesystem, so that you subgraph can read it.
To get started, install the dependencies of the project:
Generate the Substreams Protobufs:
The previous command generates the Substreams Protobuf contained in the ethereum-explorer-v0.1.2.spkg
package. In this example, you only want to import the eth.transaction.v1.Transactions
object.
The Protobuf object is generated in AssemblyScript, so you can import it in the subgraph code.
Generate the subgraph schema:
Inspect the Code
The
subgraph.yaml
file defines the Substreams triggers as a data source. The subgraph will be deploy on Ethereum Mainnet (mainnet
).
The
src/mapping.ts
contains the handler of the trigger,handleTransactions
:
Import the generated Substreams Protobuf.
Import the generated GraphQL schema.
Decode the Substreams Protobuf.
Create and save the subgraph entity.
Build and Deploy the Subgraph
To test the application, you can deploy the subgraph to The Graph Studio. You must create an account and authentication your computer first. The official documentation covers the steps needed to deploy a subgraph to the Studio.
Build the subgraph:
Deploy the subgraph:
Now, you can access and query the subgraph in the Studio.
Last updated