Local Service
In you want to manage your own infrastructure, you can use still the deployable services, but locally. This essetially means using the substreams alpha service
command pointing to a local Docker installation. The following tutorial teaches you how to use the Substreams:SQL deployable service locally.
Tutorial
In this tutorial, you will:
Generate a simple Substreams that extract all events from the Cryptopunks smart contract on Ethereum.
Feed your data into a local PostgreSQL database in a Docker-based development environment.
Develop and apply SQL transformations with dbt models.
Go from a local Substreams:SQL environment to a shared remote development environment.
Create a production
.spkg
package and test it locallyDeploy your
.spkg
to a production environment that fills a PostgreSQL database from your Substreams and applies dbt transformations automatically.
Generate a Substreams project from the Cryptopunks ABI
The cryptopunks address on mainnet is
0xb47e3cd837ddf8e4c57f05d70ab865de6e193bbb
Use the
substreams init
command to fetch the ABI from etherscan and scaffold a Substreams project:
Fill the requested information (name: cryptopunks
, protocol: ethereum
, chain: mainnet
, contract: b47e3cd837ddf8e4c57f05d70ab865de6e193bbb
)
See the list of events that have been identified above. By default, each event type is mapped to its own table. This happens in the
map_events()
function insidesrc/lib.rs
and the SQL schema is available inschema.sql
.Build the rust code:
You should see:
Start feeding a local database using 'substreams alpha service' and Docker
From another window:
Back to your substreams project:
Deploy your substreams locally to start putting data in your database:
Tip: A new PostgreSQL container will be created at port 5432
. The deployment will fail if there is another Docker container using that port.
Tip: You can also use substreams service deploy substreams.clickhouse.yaml
to use the Clickhouse engine instead of PostgreSQL. There is no postgraphile or pgweb in that case, you will need a tool like DataGrip to see the data.
You should see:
Look at some SQL data via pgweb at http://localhost:8081
Tip: You can run substreams alpha service pause
if you want to pause the sink from consuming Substreams data while you continue your development. substreams alpha service resume
will continue the progress.
Create a dbt project for transformations
Initialize the dbt project:
Tip: Choose 'postgres' as the database.
Move that project under 'dbt' folder:
Edit the dev credentials under $HOME/.dbt/profiles.yml file:
Tip: You can see the database credentials again by running substreams alpha service info
Test the dbt connection
You should see:
Remove the example models:
Create a datasource and a basic materialized view:
Create your views:
Check that the
punks_bought_per_hour
has been created.
Deploy your Substreams to the "hosted dev" environment
Deployments of type "dev" gives you full read/write access to the database and are a good way of working together on a dataset while developing your dbt models.
Tip: Here again, you can use the substreams.clickhouse.yaml
manifest to use a Clickhouse engine.
You should see this output:
Tip: You are getting SQL credentials in this output. They can be fetched again by running substreams alpha service info 46f5e9f6 -e https://deploy.streamingfast.io
.
Tip: The SQL endpoint will be assigned a specific port mapped to your deployment, in this example, port 17622.
Create another target in your
$HOME/.dbt/profiles.yml
replacingPORT_NUMBER
andRW_PASSWORD
with those assigned to you on the previous step:
You can iterate using
dbt run --target=remote
Other devs can work on that data and create dbt models.
Pack your production package and test it locally
When your dbt models are ready, you can pack everything (Substreams, dbt project, etc.) inside an .spkg
file and deploy it as production:
Add a
dbt_config
section to thesubstreams.sql.yaml
(orsubstreams.clickhouse.yaml
) file:
Start the
substreams alpha service serve
command in another window (if it is not still running from before...)
Stop the previous deployment completely (only a single deployment can run at the same time on local Docker environment)
Test your deployment locally, in production mode:
Tip: If using Clickhouse, you will need to set sink.config.rest_frontend.enabled
to true
: it is currently the only way to consume data in a 'production' deployment.
See that the database starts correctly and that the tables defined in
dbt
are being created correctlyWhen you are happy with the results, verify or bump the
version
field insubstreams.sql.yaml
, you can generate thecryptopunks-v0.1.0.spkg
file.
Deploy your production package to the "hosted prod" environment
The production environment does not allow direct SQL access at the moment, so your apps will need to access the data to either the postgraphile
frontend (or the rest
frontend when using Clickhouse)
Last updated