-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
91 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use common::structs::BlockTimestamp; | ||
use substreams_bitcoin::pb::btc::v1::Block; | ||
|
||
use crate::pb::bitcoin::Block as OutputBlock; | ||
|
||
pub fn collect_block(block: &Block, timestamp: &BlockTimestamp) -> OutputBlock { | ||
OutputBlock { | ||
time: Some(timestamp.time), | ||
height: block.height, | ||
date: timestamp.date.clone(), | ||
hash: block.hash.clone(), | ||
bits: block.bits.clone(), | ||
chainwork: block.chainwork.clone(), | ||
difficulty: block.difficulty, | ||
total_fees: calculate_total_fees(block), | ||
total_reward: calculate_total_reward(block), | ||
mint_reward: calculate_mint_reward(block.height), | ||
merkle_root: block.merkle_root.clone(), | ||
transaction_count: block.tx.len() as u64, | ||
nonce: block.nonce as u32, | ||
// Get the coinbase from the first transaction | ||
coinbase: block.tx.first().unwrap().vin.first().unwrap().coinbase.clone(), | ||
previous_block_hash: block.previous_hash.clone(), | ||
size: block.size, | ||
stripped_size: block.stripped_size, | ||
version: block.version, | ||
weight: block.weight, | ||
} | ||
} | ||
|
||
fn calculate_total_fees(block: &Block) -> f64 { | ||
let total_reward = calculate_total_reward(block); | ||
let mint_reward = calculate_mint_reward(block.height); | ||
|
||
total_reward - mint_reward | ||
} | ||
|
||
fn calculate_total_reward(block: &Block) -> f64 { | ||
// Assume the first transaction is the coinbase transaction | ||
if let Some(coinbase_tx) = block.tx.first() { | ||
coinbase_tx.vout.iter().map(|vout| vout.value).sum() | ||
} else { | ||
0.0 // No transactions in the block (shouldn't happen in practice) | ||
} | ||
} | ||
|
||
fn calculate_mint_reward(height: i64) -> f64 { | ||
// Bitcoin's block reward halving schedule | ||
let halvings = height / 210_000; | ||
if halvings >= 64 { | ||
return 0.0; // All bitcoins mined | ||
} | ||
|
||
50.0 / 2_f64.powi(halvings as i32) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
use common::utils::build_timestamp; | ||
use substreams::{errors::Error, pb::substreams::Clock}; | ||
use substreams_bitcoin::pb::btc::v1::Block; | ||
|
||
use crate::pb::bitcoin::Events; | ||
use crate::{blocks::collect_block, pb::bitcoin::Events}; | ||
|
||
#[substreams::handlers::map] | ||
pub fn map_events(_block: Block, _clock: Clock) -> Result<Events, Error> { | ||
Ok(Events::default()) | ||
pub fn map_events(clock: Clock, block: Block) -> Result<Events, Error> { | ||
let timestamp = build_timestamp(&clock); | ||
|
||
let events = Events { | ||
blocks: vec![collect_block(&block, ×tamp)], | ||
transactions: Vec::new(), | ||
inputs: Vec::new(), | ||
outputs: Vec::new(), | ||
}; | ||
|
||
Ok(events) | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,6 @@ | ||
mod blocks; | ||
mod events; | ||
mod inputs; | ||
mod outputs; | ||
mod pb; | ||
mod transactions; |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters