Skip to content

Commit

Permalink
add bitcoin txo substream
Browse files Browse the repository at this point in the history
  • Loading branch information
YaroShkvorets committed Feb 6, 2024
1 parent f4efbe7 commit 06ac188
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
"bitcoin.ordinals",
"sx.stats",
"blocks",
"bitcoin.txo",
]

[workspace.dependencies]
Expand Down
2 changes: 1 addition & 1 deletion antelope.oracles/substreams.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ modules:
inputs:
- map: map_quotes
output:
type: proto:sf.substreams.sink.entity.v1.EntityChanges
type: proto:sf.substreams.sink.entity.v1.EntityChanges
20 changes: 20 additions & 0 deletions bitcoin.txo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
name = "bitcoin_txo"
version = "0.1.0"
authors = ["Yaro <[email protected]>"]
description = "Bitcoin TXOs"
license = "MIT OR Apache-2.0"
edition = "2021"
publish = false

[badges]
maintenance = { status = "actively-developed" }

[lib]
crate-type = ["cdylib"]

[dependencies]
substreams-bitcoin = { workspace = true }
substreams = { workspace = true }
prost = { workspace = true }
prost-types = { workspace = true }
34 changes: 34 additions & 0 deletions bitcoin.txo/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
.PHONY: all
all:
make build
make pack
make graph
make info

.PHONY: build
build:
AR=/usr/local/opt/llvm/bin/llvm-ar CC=/usr/local/opt/llvm/bin/clang cargo build --target wasm32-unknown-unknown --release

.PHONY: protogen
protogen:
substreams protogen --exclude-paths sf/substreams,google

.PHONY: pack
pack:
substreams pack

.PHONY: graph
graph:
substreams graph

.PHONY: info
info:
substreams info

.PHONY: run
run:
substreams run -e bitcoin.substreams.pinax.network:443 map_txo -s 1 -t +100

.PHONY: gui
gui:
substreams gui -e bitcoin.substreams.pinax.network:443 map_txo -s 1 -t +100
5 changes: 5 additions & 0 deletions bitcoin.txo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Bitcoin TXOs Substream

> Bitcoin TXO counter
## Usage
5 changes: 5 additions & 0 deletions bitcoin.txo/proto/bitcoin.txo.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
syntax = "proto3";

package bitcoin.txo;

message Txo { int64 txo_count = 1; }
25 changes: 25 additions & 0 deletions bitcoin.txo/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use pb::bitcoin::txo::Txo;
use substreams::{
errors::Error,
store::{StoreAddInt64, StoreGetInt64},
};
use substreams::{log, prelude::*};
use substreams_bitcoin::pb::btc::v1::Block;

mod pb;

#[substreams::handlers::store]
pub fn store_txo(block: Block, s: StoreAddInt64) {
log::info!("txs: {}", block.transactions().count());
for (i, tx) in block.transactions().enumerate() {
log::info!("txos: {}", tx.vout.len());
s.add(i as u64, "txo", tx.vout.len() as i64);
}
}

#[substreams::handlers::map]
pub fn map_txo(txo_store: StoreGetInt64) -> Result<Txo, Error> {
let txo_count = txo_store.get_last("txo").unwrap();
log::info!("txo_count: {}", txo_count);
Ok(Txo { txo_count })
}
8 changes: 8 additions & 0 deletions bitcoin.txo/src/pb/bitcoin.txo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @generated
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Txo {
#[prost(int64, tag="1")]
pub txo_count: i64,
}
// @@protoc_insertion_point(module)
8 changes: 8 additions & 0 deletions bitcoin.txo/src/pb/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @generated
pub mod bitcoin {
// @@protoc_insertion_point(attribute:bitcoin.txo)
pub mod txo {
include!("bitcoin.txo.rs");
// @@protoc_insertion_point(bitcoin.txo)
}
}
33 changes: 33 additions & 0 deletions bitcoin.txo/substreams.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
specVersion: v0.1.0
package:
name: bitcoin_txo
version: v0.1.0
url: https://github.com/pinax-network/substreams
doc: Bitcoin TXOs

binaries:
default:
type: wasm/rust-v1
file: ../target/wasm32-unknown-unknown/release/bitcoin_txo.wasm

protobuf:
files:
- bitcoin.txo.proto
importPaths:
- ./proto

modules:
- name: store_txo
kind: store
inputs:
- source: sf.bitcoin.type.v1.Block
valueType: int64
updatePolicy: add

- name: map_txo
kind: map
inputs:
- store: store_txo
mode: get
output:
type: proto:bitcoin.txo

0 comments on commit 06ac188

Please sign in to comment.