Skip to main content

brk_query/impl/mining/
block_rewards.rs

1use brk_error::Result;
2use brk_types::{BlockRewardsEntry, Cents, Dollars, Sats, TimePeriod};
3
4use super::block_window::BlockWindow;
5use crate::Query;
6
7impl Query {
8    /// Time-bucketed average block rewards (subsidy + fees) over
9    /// `time_period`. One entry per bucket, ordered chronologically. Each
10    /// entry carries the bucket's average height/timestamp, the round-half-up
11    /// mean of coinbase rewards in sats, and the bucket-mean USD spot price
12    /// (the spot price, not rewards-in-USD: clients multiply).
13    pub fn block_rewards(&self, time_period: TimePeriod) -> Result<Vec<BlockRewardsEntry>> {
14        let bw = BlockWindow::new(self, time_period)?;
15        let rewards: Vec<Sats> = bw.read(&self.computer().mining.rewards.coinbase.block.sats)?;
16        let prices: Vec<Cents> = bw.read(&self.computer().price.spot.cents.height)?;
17
18        Ok(bw
19            .buckets
20            .iter()
21            .map(|b| BlockRewardsEntry {
22                avg_height: b.avg_height,
23                timestamp: b.avg_timestamp,
24                avg_rewards: b.mean_rounded(&rewards),
25                usd: Dollars::from(b.mean_rounded(&prices)),
26            })
27            .collect())
28    }
29}