Skip to content

Commit

Permalink
Add partial implementation of Inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
zolting committed Nov 22, 2024
1 parent 48c9a56 commit 6cbb36a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 31 deletions.
15 changes: 3 additions & 12 deletions blocks/bitcoin/src/events.rs
Original file line number Diff line number Diff line change
@@ -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<Events, Error> {
Expand All @@ -19,15 +17,8 @@ pub fn map_events(clock: Clock, block: Block) -> Result<Events, Error> {

for (index, transaction) in block.tx.iter().enumerate() {
events.transactions.push(collect_transaction(transaction, &timestamp, index as u32));
events.inputs.extend(collect_transaction_inputs(transaction, &timestamp));
}

Ok(events)
}

fn build_utxo_map(block: &Block) -> HashMap<String, Vec<Vout>> {
let mut utxo_map = HashMap::new();
for transaction in &block.tx {
utxo_map.insert(transaction.txid.clone(), transaction.vout.clone());
}
utxo_map
}
38 changes: 38 additions & 0 deletions blocks/bitcoin/src/inputs.rs
Original file line number Diff line number Diff line change
@@ -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<Input> {
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
}
18 changes: 0 additions & 18 deletions blocks/bitcoin/src/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Vec<Vout>>) -> (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)
// }
2 changes: 1 addition & 1 deletion proto/bitcoin.proto
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
syntax = "proto3";

package bitcoin;
package pinax.bitcoin;

import "google/protobuf/timestamp.proto";

Expand Down

0 comments on commit 6cbb36a

Please sign in to comment.