From 6cbb36a68141aa7b9269892fbf80337bb228dd5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dillan=20Choini=C3=A8re?= Date: Fri, 22 Nov 2024 12:11:22 -0500 Subject: [PATCH] Add partial implementation of Inputs --- blocks/bitcoin/src/events.rs | 15 +++--------- blocks/bitcoin/src/inputs.rs | 38 ++++++++++++++++++++++++++++++ blocks/bitcoin/src/transactions.rs | 18 -------------- proto/bitcoin.proto | 2 +- 4 files changed, 42 insertions(+), 31 deletions(-) diff --git a/blocks/bitcoin/src/events.rs b/blocks/bitcoin/src/events.rs index 7607272..0ab5c02 100644 --- a/blocks/bitcoin/src/events.rs +++ b/blocks/bitcoin/src/events.rs @@ -1,10 +1,8 @@ -use std::collections::HashMap; - use common::utils::build_timestamp; use substreams::{errors::Error, pb::substreams::Clock}; -use substreams_bitcoin::pb::btc::v1::{Block, Vout}; +use substreams_bitcoin::pb::btc::v1::Block; -use crate::{blocks::collect_block, pb::bitcoin::Events, transactions::collect_transaction}; +use crate::{blocks::collect_block, inputs::collect_transaction_inputs, pb::bitcoin::Events, transactions::collect_transaction}; #[substreams::handlers::map] pub fn map_events(clock: Clock, block: Block) -> Result { @@ -19,15 +17,8 @@ pub fn map_events(clock: Clock, block: Block) -> Result { for (index, transaction) in block.tx.iter().enumerate() { events.transactions.push(collect_transaction(transaction, ×tamp, index as u32)); + events.inputs.extend(collect_transaction_inputs(transaction, ×tamp)); } Ok(events) } - -fn build_utxo_map(block: &Block) -> HashMap> { - let mut utxo_map = HashMap::new(); - for transaction in &block.tx { - utxo_map.insert(transaction.txid.clone(), transaction.vout.clone()); - } - utxo_map -} diff --git a/blocks/bitcoin/src/inputs.rs b/blocks/bitcoin/src/inputs.rs index e69de29..fc4d0c6 100644 --- a/blocks/bitcoin/src/inputs.rs +++ b/blocks/bitcoin/src/inputs.rs @@ -0,0 +1,38 @@ +use common::structs::BlockTimestamp; +use substreams_bitcoin::pb::btc::v1::Transaction; + +use crate::pb::bitcoin::Input; + +pub fn collect_transaction_inputs(transaction: &Transaction, timestamp: &BlockTimestamp) -> Vec { + let mut inputs = Vec::new(); + + for (index, input) in transaction.vin.iter().enumerate() { + let script_sig = input.script_sig.as_ref(); + + inputs.push(Input { + block_time: Some(timestamp.time), + block_date: timestamp.date.clone(), + block_height: timestamp.number as u32, + block_hash: timestamp.hash.clone(), + tx_id: transaction.txid.clone(), + index: index as u32, + spent_block_height: 0, // TODO: Need to look up from previous tx + spent_tx_id: input.txid.clone(), + spent_output_number: input.vout as u64, + value: 0.0, // TODO: Need to look up from previous tx + address: String::new(), // TODO: Need to look up from previous tx + r#type: String::new(), // TODO: Need to look up from previous tx + coinbase: input.coinbase.clone(), + is_coinbase: !input.coinbase.is_empty(), + script_asm: script_sig.map(|s| s.asm.clone()).unwrap_or_default(), + script_hex: script_sig.map(|s| s.hex.clone()).unwrap_or_default(), + script_desc: String::new(), // TODO: Need to investigate how Dune generates this + script_signature_asm: script_sig.map(|s| s.asm.clone()).unwrap_or_default(), + script_signature_hex: script_sig.map(|s| s.hex.clone()).unwrap_or_default(), + sequence: input.sequence as i64, + witness_data: input.txinwitness.clone(), + }); + } + + inputs +} diff --git a/blocks/bitcoin/src/transactions.rs b/blocks/bitcoin/src/transactions.rs index 5104ced..57dd2ba 100644 --- a/blocks/bitcoin/src/transactions.rs +++ b/blocks/bitcoin/src/transactions.rs @@ -27,21 +27,3 @@ pub fn collect_transaction(transaction: &Transaction, timestamp: &BlockTimestamp hex: transaction.hex.clone(), } } - -// fn calculate_transaction_values(tx: &Transaction, utxo_map: &HashMap>) -> (f64, f64) { -// // Calculate input value -// let mut input_value = 0.0; -// for vin in &tx.vin { -// if let Some(previous_outputs) = utxo_map.get(&vin.txid) { -// if let Some(output) = previous_outputs.get(vin.vout as usize) { -// input_value += output.value; -// } -// } -// } - -// let input_value = tx.vin.iter().map(|vin| vin.) -// // Calculate output value -// let output_value: f64 = tx.vout.iter().map(|vout| vout.value).sum(); - -// (input_value, output_value) -// } diff --git a/proto/bitcoin.proto b/proto/bitcoin.proto index dde4eb2..624be0f 100644 --- a/proto/bitcoin.proto +++ b/proto/bitcoin.proto @@ -1,6 +1,6 @@ syntax = "proto3"; -package bitcoin; +package pinax.bitcoin; import "google/protobuf/timestamp.proto";