From substreams
Expert knowledge for developing Bitcoin Substreams. Use when working with Bitcoin blockchain data, UTXO model, transaction parsing, address extraction, or Esplora API compatibility.
How this skill is triggered — by the user, by Claude, or both
Slash command
/substreams:substreams-bitcoinThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
Expert assistant for building Bitcoin Substreams - processing Bitcoin blockchain data with UTXO model support.
Expert assistant for building Bitcoin Substreams - processing Bitcoin blockchain data with UTXO model support.
Understanding the fundamental differences between Bitcoin and EVM chains is crucial:
| Aspect | Bitcoin | Ethereum/EVM |
|---|---|---|
| Model | UTXO (Unspent Transaction Outputs) | Account-based |
| Balances | Sum of unspent outputs | Stored in account state |
| Transactions | Consume UTXOs, create new ones | Modify account states |
| Smart Contracts | Script (limited) | Full Turing-complete |
| Addresses | Derived from scripts | 20-byte identifiers |
| Witness Data | SegWit (separate) | Part of transaction |
The sf.bitcoin.v1.Block type provides:
use substreams_bitcoin::pb::btc::v1::{Block, Transaction, Vin, Vout};
// Block fields
block.hash // Block hash (hex string)
block.height // Block height
block.version // Block version
block.time // Timestamp (Unix)
block.mediantime // Median time
block.nonce // Mining nonce
block.bits // Difficulty bits
block.difficulty // Calculated difficulty
block.size // Block size (bytes)
block.weight // Block weight (SegWit)
block.stripped_size // Size without witness
block.merkle_root // Merkle root
block.previous_hash // Previous block hash
block.tx // Vec<Transaction>
// Transaction fields
tx.txid // Transaction ID
tx.hash // Transaction hash (differs from txid for SegWit)
tx.version // Version number
tx.size // Total size
tx.weight // Weight (SegWit)
tx.locktime // Lock time
tx.vin // Vec<Vin> - inputs
tx.vout // Vec<Vout> - outputs
// Input fields
vin.txid // Previous transaction ID
vin.vout // Previous output index
vin.coinbase // Coinbase data (empty for non-coinbase)
vin.txinwitness // Vec<String> - witness data
vin.sequence // Sequence number
vin.script_sig // Script signature
// Output fields
vout.value // Value in BTC (f64)
vout.n // Output index
vout.script_pub_key // Script public key with:
.hex // Hex-encoded script
.asm // Assembly representation
.r#type // Script type (p2pkh, p2sh, p2wpkh, etc.)
.address // Derived address
.req_sigs // Required signatures
use substreams::errors::Error;
use substreams::pb::substreams::Clock;
use substreams_bitcoin::pb::btc::v1::Block;
#[substreams::handlers::map]
fn map_block_data(_clock: Clock, block: Block) -> Result<MyBlockData, Error> {
Ok(MyBlockData {
hash: block.hash.clone(),
height: block.height as u64,
tx_count: block.tx.len() as u32,
timestamp: block.time as u64,
})
}
#[substreams::handlers::map]
fn map_transactions(_clock: Clock, block: Block) -> Result<Transactions, Error> {
let txs: Vec<TransactionData> = block.tx.iter().map(|tx| {
TransactionData {
txid: tx.txid.clone(),
is_coinbase: tx.vin.iter().any(|v| !v.coinbase.is_empty()),
input_count: tx.vin.len() as u32,
output_count: tx.vout.len() as u32,
}
}).collect();
Ok(Transactions { transactions: txs })
}
Extract addresses from output scripts:
fn extract_address(vout: &Vout) -> Option<String> {
vout.script_pub_key
.as_ref()
.map(|s| s.address.clone())
.filter(|a| !a.is_empty())
}
fn extract_address_type(vout: &Vout) -> String {
vout.script_pub_key
.as_ref()
.map(|s| s.r#type.clone())
.unwrap_or_default()
}
Track UTXOs per block (within single block scope):
#[substreams::handlers::map]
fn map_utxos(_clock: Clock, block: Block) -> Result<Utxos, Error> {
let mut utxos = Vec::new();
for tx in &block.tx {
for (idx, output) in tx.vout.iter().enumerate() {
if let Some(address) = extract_address(output) {
utxos.push(Utxo {
txid: tx.txid.clone(),
vout: idx as u32,
value: btc_to_sats(output.value),
address,
block_height: block.height as u32,
});
}
}
}
Ok(Utxos { utxos })
}
/// Convert BTC to satoshis
fn btc_to_sats(btc: f64) -> u64 {
(btc * 100_000_000.0) as u64
}
Handle SegWit witness data:
fn process_input(input: &Vin) -> InputData {
InputData {
txid: input.txid.clone(),
vout: input.vout,
is_coinbase: !input.coinbase.is_empty(),
witness: input.txinwitness.clone(), // Already Vec<String>
sequence: input.sequence,
}
}
// Aggregate all witness data for a transaction
fn get_tx_witness(tx: &Transaction) -> Vec<String> {
tx.vin
.iter()
.flat_map(|input| input.txinwitness.clone())
.collect()
}
Estimate transaction fees (accurate calculation requires UTXO lookup):
fn estimate_fee(tx: &Transaction) -> u64 {
// Coinbase transactions have no fee
if tx.vin.iter().any(|v| !v.coinbase.is_empty()) {
return 0;
}
let output_total: u64 = tx.vout
.iter()
.map(|o| btc_to_sats(o.value))
.sum();
// Estimate: assume ~1.5% fee rate for typical transactions
let estimated_input = (output_total as f64 * 1.015) as u64;
estimated_input.saturating_sub(output_total)
}
| Type | Prefix | Script Type | Description |
|---|---|---|---|
| P2PKH | 1 | pubkeyhash | Legacy pay-to-public-key-hash |
| P2SH | 3 | scripthash | Pay-to-script-hash (multisig, etc.) |
| P2WPKH | bc1q | witness_v0_keyhash | Native SegWit v0 |
| P2WSH | bc1q | witness_v0_scripthash | SegWit script hash |
| P2TR | bc1p | witness_v1_taproot | Taproot (SegWit v1) |
specVersion: v0.1.0
package:
name: my-bitcoin-substreams
version: v1.0.0
url: https://github.com/user/repo
description: Bitcoin data processor
network: bitcoin
protobuf:
files:
- my_types.proto
importPaths:
- ./proto
binaries:
default:
type: wasm/rust-v1
file: target/wasm32-unknown-unknown/release/my_bitcoin_substreams.wasm
modules:
- name: map_blocks
kind: map
initialBlock: 0
inputs:
- source: sf.substreams.v1.Clock
- source: sf.bitcoin.v1.Block
output:
type: proto:my.types.BlockData
[package]
name = "my_bitcoin_substreams"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib"]
[dependencies]
substreams = "0.0.21"
substreams-bitcoin = "1.0.0"
prost = "0.11.9"
prost-types = "0.11.9"
[build-dependencies]
prost-build = "0.11.9"
When building Esplora-compatible output, map to these structures:
{
"id": "block_hash",
"height": 800000,
"version": 536870912,
"timestamp": 1690168629,
"tx_count": 3721,
"size": 1621583,
"weight": 3993561,
"merkle_root": "...",
"previousblockhash": "...",
"difficulty": 53911173001054.59
}
{
"txid": "...",
"version": 2,
"locktime": 0,
"size": 225,
"weight": 573,
"fee": 4500,
"vin": [...],
"vout": [...],
"status": {
"confirmed": true,
"block_height": 800000,
"block_hash": "...",
"block_time": 1690168629
}
}
txid != hash for SegWit transactionsvin[0].coinbase contains coinbase dataDifferent output script types require different parsing:
OP_DUP OP_HASH160 <pubkeyhash> OP_EQUALVERIFY OP_CHECKSIGOP_HASH160 <scripthash> OP_EQUALOP_0 <keyhash>OP_1 <pubkey># Authenticate
substreams auth
# Run against Bitcoin endpoint
substreams run -e bitcoin \
my-substream.spkg \
map_blocks -s 800000 -t +100
# GUI mode
substreams gui -e bitcoin \
my-substream.spkg \
map_blocks -s 800000
npx claudepluginhub streamingfast/substreams-skills --plugin substreams-devProvides behavioral guidelines to reduce common LLM coding mistakes, focusing on simplicity, surgical changes, assumption surfacing, and verifiable success criteria.
Searches, retrieves, and installs Agent Skills from prompts.chat registry using MCP tools like search_skills and get_skill. Activates for finding skills, browsing catalogs, or extending Claude.