A simple framework for extracting Bitcoin blockchain data using Substreams technology, with support for UTXO tracking and token balances in ClickHouse.
This project provides a comprehensive solution for Bitcoin blockchain analytics, focusing on:
bitcoin-substreams-analytics/
├── proto/ # Protocol Buffer definitions
│ ├── analytics.proto # Data models for block analytics
│ └── utxo.proto # Data models for UTXO tracking
├── src/ # Rust source code
│ ├── lib.rs # Main library implementation
│ ├── pb/ # Generated Protocol Buffer code
│ ├── utils/ # Utility functions
│ │ ├── mod.rs # Module definitions
│ │ └── bitcoin_utils.rs # Bitcoin-specific utilities
│ └── mappers/ # Data mapping modules
│ ├── mod.rs # Module definitions
│ ├── block.rs # Block data extraction
│ └── utxo.rs # UTXO tracking implementation
├── clickhouse-bitcoin/ # Modular ClickHouse integration
│ ├── src/ # ClickHouse-specific Rust code
│ ├── schema.sql # ClickHouse schema definition
│ ├── sink_config.yaml # ClickHouse sink configuration
│ ├── substreams.yaml # ClickHouse-specific Substreams manifest
│ ├── Cargo.toml # ClickHouse-specific dependencies
│ ├── Makefile # ClickHouse-specific build commands
│ └── README.md # ClickHouse integration documentation
├── substreams.yaml # Substreams manifest
├── simple-substreams.yaml # Simplified Substreams manifest
├── build.rs # Build script for protobuf generation
├── Cargo.toml # Rust package definition
├── Makefile # Build and run commands
├── clickhouse_schema.sql # ClickHouse schema for token balances
├── sink_config.yaml # Substreams sink configuration
├── CLICKHOUSE_SETUP.md # ClickHouse setup documentation
└── README.md # This file
Clone this repository:
git clone https://github.com/PaulieB14/bitcoin-substreams-analytics.git
cd bitcoin-substreams-analytics
Build the project (this will automatically generate Protocol Buffer code):
make build
Package the Substreams module:
make pack
Set up your Pinax API key in the .env.local
file:
SUBSTREAMS_API_TOKEN=your_pinax_api_key
The Makefile will automatically load this environment variable.
Run the Substreams with GUI:
make gui
Get information about the Substreams package:
make info
There are two ways to use ClickHouse with this project:
Install ClickHouse:
# For Docker
docker run -d --name clickhouse-server -p 8123:8123 -p 9000:9000 clickhouse/clickhouse-server
Create the ClickHouse schema:
# Using the ClickHouse client
cat clickhouse_schema.sql | clickhouse-client -h localhost
# Or using the HTTP interface
curl -X POST http://localhost:8123/ --data-binary @clickhouse_schema.sql
Configure the Substreams sink:
# Edit the sink_config.yaml file to match your ClickHouse connection details
# Run the sink (requires substreams-sink-sql)
substreams-sink-sql run \
pinax.firehose.xyz:443 \
sink_config.yaml \
substreams.spkg \
map_utxos
The clickhouse-bitcoin
directory contains a modular implementation of the ClickHouse integration that can be used independently:
Navigate to the clickhouse-bitcoin directory:
cd clickhouse-bitcoin
Set up the ClickHouse schema:
make setup
Run the integration:
make run
For more detailed instructions on either approach, see the CLICKHOUSE_SETUP.md
file or the clickhouse-bitcoin/README.md
file.
-- Example: Get top 10 addresses by balance
SELECT address, balance, utxo_count
FROM bitcoin_token_balances_latest
ORDER BY balance DESC
LIMIT 10;
The system extracts the following data from Bitcoin blocks:
The UTXO tracking module processes Bitcoin transactions to:
The project includes a comprehensive ClickHouse schema for:
There are two implementations of the ClickHouse integration:
clickhouse_schema.sql
, sink_config.yaml
, etc.)clickhouse-bitcoin
directory, this is a more modular implementation that can be used independentlySee the CLICKHOUSE_SETUP.md
file for detailed information on the main integration, or the clickhouse-bitcoin/README.md
file for information on the modular integration.
This project uses prost-build
to automatically generate Rust code from Protocol Buffer definitions. The generation happens during the build process via the build.rs
script.
To manually regenerate the Protocol Buffer code:
make protogen
To create your own custom Bitcoin Substreams modules:
proto/analytics.proto
src/lib.rs
and the appropriate mapper filessubstreams.yaml
Run tests with:
make test
To clean the build artifacts:
make clean
This project is licensed under the MIT License - see the LICENSE file for details.
substreams gui bitcoin-analytics@v0.1.1 map_block_metrics
substreams gui bitcoin-analytics@v0.1.1 map_utxos