Simple Substreams Example
Last updated
Last updated
The BlockStats Susbtreams is a very basic Substreams, extracting data from the Injective blockchain.
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.
Clone the BlockStats Substreams GitHub repository and open it in an IDE of your choice (for example, VSCode).
Every Substreams project contains three main pieces:
The Protobuf definitions: the outputs of your Substreams, which you define through Protobuf schemas.
The source code: the Rust functions that extract the actual data from the blockchain.
The Substreams manifest: the substreams.yaml
file contains the configuration of your Substreams.
The proto
folder contains the Protobuf definitions for the output of your Substreams. In this example, only a BlockStats
Protobuf is defined as the output of the Substreams.
The src
folder contains the source code of the Substreams transformations. Specifically, the lib.rs
contains the Rust functions.
The substreams.yml
is the Substreams manifest, which defines relevant information, such as the inputs/outputs of every module or the Protobuf files.
Take a look at the substreams.yaml
file:
The network
field specifies which network is the Substreams going to be executed on.
Import the Cosmos Block Protobuf, which gives you access to the blockchain data.
Import the user-defined Protobuf schemas (i.e. the outputs of your Substreams).
Define a module. block_to_stats
, which will be mapped to the block_to_stats
Rust function in the source code.
Define the inputs of the module. In this case, the Block
Cosmos Protobuf.
Define the outputs of the module. In this case, the BlockStats
Protobuf, which you imported in #3
.
Build the Rust code:
Run the Substreams using the substreams run
command of the CLI:
substreams.yaml
is the Substreams manifest with the configurations.
block_to_stats
is the name of the module that you want to run (in this Substreams, there only one module).
-e mainnet.injective.streamingfast.io:443
is the StreamingFast (Substreams provider) endpoint that will read execute the Substreams and stream back the data.
--start-block=64987400 --stop-block=+1000
defines the start and stop block (start at block 64987400
and finish at block 64987500
, 100 blocks later).
The substreams run
displays the data extract at every block linearly, so it might be difficult to properly read the data if your execution happens through thousands of blocks. The substreams gui
allows you to jump between blocks and search content.
Review the GUI Reference to get more information on how to use this utility.
The lib.rs
file contains the only module defined in this Substreams, block_to_stats
.
Import the Cosmos Block
Protobuf object, which is passed as a parameter.
Import the BlockStats
Protobuf object, which is the return type of the function. This Rust object is automatically generated from the Protobuf defined in the proto
folder.
Declaration of the Rust function. Input: Injective block. Output: BlockStats
object, which is defined by the user and is consumable from the outside world.
Creation of the BlockStats
object.
Add data from the Block
Injective object to user-defined BlockStats
object. In this case, the height
of the block.
Add more data. In this case, the number of transactions contained in the block.