zebra_rpc/methods/types/
get_blockchain_info.rs

1//! Types used in `getblockchaininfo` RPC method.
2
3use zebra_chain::{
4    amount::{Amount, NonNegative},
5    value_balance::ValueBalance,
6};
7
8use zec::Zec;
9
10use super::*;
11
12/// A value pool's balance in Zec and Zatoshis
13#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize)]
14#[serde(rename_all = "camelCase")]
15pub struct Balance {
16    /// Name of the pool
17    #[serde(skip_serializing_if = "String::is_empty", default)]
18    id: String,
19    /// Total amount in the pool, in ZEC
20    chain_value: Zec<NonNegative>,
21    /// Total amount in the pool, in zatoshis
22    chain_value_zat: Amount<NonNegative>,
23    /// Whether the value pool balance is being monitored.
24    monitored: bool,
25}
26
27impl Balance {
28    /// Returns a list of [`Balance`]s converted from the default [`ValueBalance`].
29    pub fn zero_pools() -> [Self; 5] {
30        Self::value_pools(Default::default())
31    }
32
33    /// Creates a new [`Balance`] from a pool name and its value balance.
34    pub fn new(id: impl ToString, amount: Amount<NonNegative>) -> Self {
35        Self {
36            id: id.to_string(),
37            chain_value: Zec::from(amount),
38            chain_value_zat: amount,
39            monitored: amount.zatoshis() != 0,
40        }
41    }
42
43    /// Creates a [`Balance`] for the transparent pool.
44    pub fn transparent(amount: Amount<NonNegative>) -> Self {
45        Self::new("transparent", amount)
46    }
47
48    /// Creates a [`Balance`] for the Sprout pool.
49    pub fn sprout(amount: Amount<NonNegative>) -> Self {
50        Self::new("sprout", amount)
51    }
52
53    /// Creates a [`Balance`] for the Sapling pool.
54    pub fn sapling(amount: Amount<NonNegative>) -> Self {
55        Self::new("sapling", amount)
56    }
57
58    /// Creates a [`Balance`] for the Orchard pool.
59    pub fn orchard(amount: Amount<NonNegative>) -> Self {
60        Self::new("orchard", amount)
61    }
62
63    /// Creates a [`Balance`] for the Deferred pool.
64    pub fn deferred(amount: Amount<NonNegative>) -> Self {
65        Self::new("deferred", amount)
66    }
67
68    /// Converts a [`ValueBalance`] to a list of [`Balance`]s.
69    pub fn value_pools(value_balance: ValueBalance<NonNegative>) -> [Self; 5] {
70        [
71            Self::transparent(value_balance.transparent_amount()),
72            Self::sprout(value_balance.sprout_amount()),
73            Self::sapling(value_balance.sapling_amount()),
74            Self::orchard(value_balance.orchard_amount()),
75            Self::deferred(value_balance.deferred_amount()),
76        ]
77    }
78
79    /// Converts a [`ValueBalance`] to a [`Balance`] representing the total chain supply.
80    pub fn chain_supply(value_balance: ValueBalance<NonNegative>) -> Self {
81        Self::value_pools(value_balance)
82            .into_iter()
83            .reduce(|a, b| {
84                Balance::new(
85                    "",
86                    (a.chain_value_zat + b.chain_value_zat)
87                        .expect("sum of value balances should not overflow"),
88                )
89            })
90            .expect("at least one pool")
91    }
92}