zebra_rpc/methods/types/
get_blockchain_info.rs

1//! Types used in `getblockchaininfo` RPC method.
2
3use derive_getters::Getters;
4use derive_new::new;
5use zebra_chain::{
6    amount::{Amount, NegativeAllowed, NonNegative},
7    value_balance::ValueBalance,
8};
9
10use zec::Zec;
11
12use super::*;
13
14/// A value pool's balance in Zec and Zatoshis
15#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, Getters, new)]
16#[serde(rename_all = "camelCase")]
17pub struct GetBlockchainInfoBalance {
18    /// Name of the pool
19    #[serde(skip_serializing_if = "String::is_empty", default)]
20    id: String,
21    /// Total amount in the pool, in ZEC
22    #[getter(copy)]
23    chain_value: Zec<NonNegative>,
24    /// Total amount in the pool, in zatoshis
25    #[getter(copy)]
26    chain_value_zat: Amount<NonNegative>,
27    /// Whether the value pool balance is being monitored.
28    monitored: bool,
29    /// Change to the amount in the pool produced by this block, in ZEC
30    #[serde(skip_serializing_if = "Option::is_none", default)]
31    #[getter(copy)]
32    value_delta: Option<Zec<NegativeAllowed>>,
33    /// Change to the amount in the pool produced by this block, in zatoshis
34    #[serde(skip_serializing_if = "Option::is_none", default)]
35    #[getter(copy)]
36    value_delta_zat: Option<Amount>,
37}
38
39impl GetBlockchainInfoBalance {
40    /// Returns a list of [`GetBlockchainInfoBalance`]s converted from the default [`ValueBalance`].
41    pub fn zero_pools() -> [Self; 5] {
42        Self::value_pools(Default::default(), None)
43    }
44
45    /// Creates a new [`GetBlockchainInfoBalance`] from a pool name and its value balance
46    /// and optionally with a delta value.
47    pub(crate) fn new_internal(
48        id: impl ToString,
49        amount: Amount<NonNegative>,
50        delta_amount: Option<Amount<NegativeAllowed>>,
51    ) -> Self {
52        Self {
53            id: id.to_string(),
54            chain_value: Zec::from(amount),
55            chain_value_zat: amount,
56            monitored: amount.zatoshis() != 0,
57            value_delta: delta_amount.map(Zec::from),
58            value_delta_zat: delta_amount,
59        }
60    }
61
62    /// Creates a [`GetBlockchainInfoBalance`] for the transparent pool.
63    pub fn transparent(
64        amount: Amount<NonNegative>,
65        delta: Option<Amount<NegativeAllowed>>,
66    ) -> Self {
67        Self::new_internal("transparent", amount, delta)
68    }
69
70    /// Creates a [`GetBlockchainInfoBalance`] for the Sprout pool.
71    pub fn sprout(amount: Amount<NonNegative>, delta: Option<Amount<NegativeAllowed>>) -> Self {
72        Self::new_internal("sprout", amount, delta)
73    }
74
75    /// Creates a [`GetBlockchainInfoBalance`] for the Sapling pool.
76    pub fn sapling(amount: Amount<NonNegative>, delta: Option<Amount<NegativeAllowed>>) -> Self {
77        Self::new_internal("sapling", amount, delta)
78    }
79
80    /// Creates a [`GetBlockchainInfoBalance`] for the Orchard pool.
81    pub fn orchard(amount: Amount<NonNegative>, delta: Option<Amount<NegativeAllowed>>) -> Self {
82        Self::new_internal("orchard", amount, delta)
83    }
84
85    /// Creates a [`GetBlockchainInfoBalance`] for the Deferred pool.
86    pub fn deferred(amount: Amount<NonNegative>, delta: Option<Amount<NegativeAllowed>>) -> Self {
87        Self::new_internal("deferred", amount, delta)
88    }
89
90    /// Converts a [`ValueBalance`] to a list of [`GetBlockchainInfoBalance`]s.
91    pub fn value_pools(
92        value_balance: ValueBalance<NonNegative>,
93        delta_balance: Option<ValueBalance<NegativeAllowed>>,
94    ) -> [Self; 5] {
95        [
96            Self::transparent(
97                value_balance.transparent_amount(),
98                delta_balance.map(|b| b.transparent_amount()),
99            ),
100            Self::sprout(
101                value_balance.sprout_amount(),
102                delta_balance.map(|b| b.sprout_amount()),
103            ),
104            Self::sapling(
105                value_balance.sapling_amount(),
106                delta_balance.map(|b| b.sapling_amount()),
107            ),
108            Self::orchard(
109                value_balance.orchard_amount(),
110                delta_balance.map(|b| b.orchard_amount()),
111            ),
112            Self::deferred(
113                value_balance.deferred_amount(),
114                delta_balance.map(|b| b.deferred_amount()),
115            ),
116        ]
117    }
118
119    /// Converts a [`ValueBalance`] to a [`GetBlockchainInfoBalance`] representing the total chain supply.
120    pub fn chain_supply(value_balance: ValueBalance<NonNegative>) -> Self {
121        Self::value_pools(value_balance, None)
122            .into_iter()
123            .reduce(|a, b| {
124                GetBlockchainInfoBalance::new_internal(
125                    "",
126                    (a.chain_value_zat + b.chain_value_zat)
127                        .expect("sum of value balances should not overflow"),
128                    None,
129                )
130            })
131            .expect("at least one pool")
132    }
133}