Mapping Blocks
This module takes a raw Ethereum block and returns a reduced version of a block, with just three pieces of information: hash, parent hash, and block number.
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.
Let's run the Substreams first, and then go through the code.
Running the Substreams
Running a Substreams usually requires three steps: generating the Rust Protobufs, building the WASM container, and using the Substream CLI to start the streaming. Make sure to run the following commands in the substreams-explorer/ethereum-explorer
folder:
Generate the Protobuf objects: The
.proto
files define a data model regardless of any programming language. However, in order to use this model in your Rust application, you must generate the corresponding Rust data structures. Note that runningmake protogen
is only necessary when making updates to any file in the proto folder.
Build the WASM module: The following command generates a WASM container from the Rust application, which you can find at
/target/wasm32-unknown-unknown/release/substreams.wasm
. Note that this is the same path provided in the Substreams manifest (substreams.yml
).
Streaming data through the CLI: The following command streams the Ethereum blockchain data, and applies the transformations contained in the
map_block_meta
module to every block.
Let's break down the command into pieces:
mainnet.eth.streamingfast.io:443
: is the StreamingFast Ethereum Mainnet endpoint where you are sending your Substreams for execution.substreams.yaml
: specifies the Substreams manifest.map_block_meta
: specifies the module to execute. Since the Ethereum Explorer application contains several modules, it is necessary to specify which one you want to execute.--start-block 17712040
: specifies the starting block (i.e. the block where Substreams will start streaming).--stop-block +1
: specifies how many blocks after the starting block should be considered. In this example,+1
means that the streaming will start at17712040
and finish at17712041
(just one block).
The output of the command should be similar to:
As you can see, the output is formatted as JSON, and the @data
field contains the actual output Protobuf of the module (BlockMeta
).
The BlockMeta
definition:
The JSON output:
Inspecting the Code
Although the code (which is in the map_block_meta.rs
file) for this module is pretty straightforward to understand, let's discuss its main parts.
Declaration of the module in the manifest (substreams.yml
):
Code of the module:
The #[substreams::handlers::map]
attribute annotates the map_block_meta
function as a Substreams mapper. The name of the function must match the name of the module in the Substreams manifest. The input of the function is a raw Ethereum block (pb::eth::v2::Block
).
In order to get the block metadata, you use the header
property.
Then, you simply create a BlockMeta
struct and return it.
Last updated