Last updated
Was this helpful?
Last updated
Was this helpful?
The allows to you to programmatically stream a Substreams using the Go programming language. The library handles reconnections and provides best practices for error handling.
The contains an example that you can use as the starting point to build your custom sink logic. After cloning the repository, move to the go
directory.
This example is built in the form of a CLI by using the cobra
library. You can run the program by running the following command structure:
In the following command, go run .
is used to execute the main.go
file. The mainnet.eth.streamingfast.io:443 https://spkg.io/streamingfast/substreams-eth-block-meta-v0.4.3.spkg db_out
part of the command are useful parameters passed to the Go program (separated by a whitespace).
In the parameters, you pass the Substreams endpoint, the package, and the module to execute.
Inspect the Code
The example contains code comments, which are very useful to understand and adjust the code to your logic needs. Let's inspect the most important parts of the code:
Create a new sink object from the parameters passed to the program:
It is necessary to handle two kind of Substreams response messages:
blockScopedData
: sent by the server whenever a new block is discovered in the blockchain. Contains all the block information that you can decode.
blockUndoSignal
: sent every time there is a fork in the blockchain. Because you have probably read incorrect blocks in the blockScopedData
message, you must rewind back to the latest valid block.
When you run the sinker, you pass two different functions to handle these messages: