From 06ac1883b6f0039d7befb8d3e4f0bd9b711d63b2 Mon Sep 17 00:00:00 2001 From: YaroShkvorets Date: Tue, 6 Feb 2024 16:49:41 -0500 Subject: [PATCH] add bitcoin txo substream --- Cargo.toml | 1 + antelope.oracles/substreams.yaml | 2 +- bitcoin.txo/Cargo.toml | 20 +++++++++++++++++ bitcoin.txo/Makefile | 34 +++++++++++++++++++++++++++++ bitcoin.txo/README.md | 5 +++++ bitcoin.txo/proto/bitcoin.txo.proto | 5 +++++ bitcoin.txo/src/lib.rs | 25 +++++++++++++++++++++ bitcoin.txo/src/pb/bitcoin.txo.rs | 8 +++++++ bitcoin.txo/src/pb/mod.rs | 8 +++++++ bitcoin.txo/substreams.yaml | 33 ++++++++++++++++++++++++++++ 10 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 bitcoin.txo/Cargo.toml create mode 100644 bitcoin.txo/Makefile create mode 100644 bitcoin.txo/README.md create mode 100644 bitcoin.txo/proto/bitcoin.txo.proto create mode 100644 bitcoin.txo/src/lib.rs create mode 100644 bitcoin.txo/src/pb/bitcoin.txo.rs create mode 100644 bitcoin.txo/src/pb/mod.rs create mode 100644 bitcoin.txo/substreams.yaml diff --git a/Cargo.toml b/Cargo.toml index f88359f..f7577c3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ members = [ "bitcoin.ordinals", "sx.stats", "blocks", + "bitcoin.txo", ] [workspace.dependencies] diff --git a/antelope.oracles/substreams.yaml b/antelope.oracles/substreams.yaml index 29e9fa2..360ce60 100644 --- a/antelope.oracles/substreams.yaml +++ b/antelope.oracles/substreams.yaml @@ -65,4 +65,4 @@ modules: inputs: - map: map_quotes output: - type: proto:sf.substreams.sink.entity.v1.EntityChanges \ No newline at end of file + type: proto:sf.substreams.sink.entity.v1.EntityChanges diff --git a/bitcoin.txo/Cargo.toml b/bitcoin.txo/Cargo.toml new file mode 100644 index 0000000..8e5a0bc --- /dev/null +++ b/bitcoin.txo/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "bitcoin_txo" +version = "0.1.0" +authors = ["Yaro "] +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 } diff --git a/bitcoin.txo/Makefile b/bitcoin.txo/Makefile new file mode 100644 index 0000000..61d62ed --- /dev/null +++ b/bitcoin.txo/Makefile @@ -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 diff --git a/bitcoin.txo/README.md b/bitcoin.txo/README.md new file mode 100644 index 0000000..0115ea9 --- /dev/null +++ b/bitcoin.txo/README.md @@ -0,0 +1,5 @@ +# Bitcoin TXOs Substream + +> Bitcoin TXO counter + +## Usage diff --git a/bitcoin.txo/proto/bitcoin.txo.proto b/bitcoin.txo/proto/bitcoin.txo.proto new file mode 100644 index 0000000..3e25b39 --- /dev/null +++ b/bitcoin.txo/proto/bitcoin.txo.proto @@ -0,0 +1,5 @@ +syntax = "proto3"; + +package bitcoin.txo; + +message Txo { int64 txo_count = 1; } diff --git a/bitcoin.txo/src/lib.rs b/bitcoin.txo/src/lib.rs new file mode 100644 index 0000000..a3d2c06 --- /dev/null +++ b/bitcoin.txo/src/lib.rs @@ -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 { + let txo_count = txo_store.get_last("txo").unwrap(); + log::info!("txo_count: {}", txo_count); + Ok(Txo { txo_count }) +} diff --git a/bitcoin.txo/src/pb/bitcoin.txo.rs b/bitcoin.txo/src/pb/bitcoin.txo.rs new file mode 100644 index 0000000..e5dade9 --- /dev/null +++ b/bitcoin.txo/src/pb/bitcoin.txo.rs @@ -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) diff --git a/bitcoin.txo/src/pb/mod.rs b/bitcoin.txo/src/pb/mod.rs new file mode 100644 index 0000000..ec95cea --- /dev/null +++ b/bitcoin.txo/src/pb/mod.rs @@ -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) + } +} diff --git a/bitcoin.txo/substreams.yaml b/bitcoin.txo/substreams.yaml new file mode 100644 index 0000000..b60e856 --- /dev/null +++ b/bitcoin.txo/substreams.yaml @@ -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