1//! Types used in `getblockchaininfo` RPC method.
23use derive_getters::Getters;
4use derive_new::new;
5use zebra_chain::{
6 amount::{Amount, NegativeAllowed, NonNegative},
7 value_balance::ValueBalance,
8};
910use zec::Zec;
1112use super::*;
1314/// 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)]
20id: String,
21/// Total amount in the pool, in ZEC
22#[getter(copy)]
23chain_value: Zec<NonNegative>,
24/// Total amount in the pool, in zatoshis
25#[getter(copy)]
26chain_value_zat: Amount<NonNegative>,
27/// Whether the value pool balance is being monitored.
28monitored: 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)]
32value_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)]
36value_delta_zat: Option<Amount>,
37}
3839impl GetBlockchainInfoBalance {
40/// Returns a list of [`GetBlockchainInfoBalance`]s converted from the default [`ValueBalance`].
41pub fn zero_pools() -> [Self; 5] {
42Self::value_pools(Default::default(), None)
43 }
4445/// Creates a new [`GetBlockchainInfoBalance`] from a pool name and its value balance
46 /// and optionally with a delta value.
47pub(crate) fn new_internal(
48 id: impl ToString,
49 amount: Amount<NonNegative>,
50 delta_amount: Option<Amount<NegativeAllowed>>,
51 ) -> Self {
52Self {
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 }
6162/// Creates a [`GetBlockchainInfoBalance`] for the transparent pool.
63pub fn transparent(
64 amount: Amount<NonNegative>,
65 delta: Option<Amount<NegativeAllowed>>,
66 ) -> Self {
67Self::new_internal("transparent", amount, delta)
68 }
6970/// Creates a [`GetBlockchainInfoBalance`] for the Sprout pool.
71pub fn sprout(amount: Amount<NonNegative>, delta: Option<Amount<NegativeAllowed>>) -> Self {
72Self::new_internal("sprout", amount, delta)
73 }
7475/// Creates a [`GetBlockchainInfoBalance`] for the Sapling pool.
76pub fn sapling(amount: Amount<NonNegative>, delta: Option<Amount<NegativeAllowed>>) -> Self {
77Self::new_internal("sapling", amount, delta)
78 }
7980/// Creates a [`GetBlockchainInfoBalance`] for the Orchard pool.
81pub fn orchard(amount: Amount<NonNegative>, delta: Option<Amount<NegativeAllowed>>) -> Self {
82Self::new_internal("orchard", amount, delta)
83 }
8485/// Creates a [`GetBlockchainInfoBalance`] for the Deferred pool.
86pub fn deferred(amount: Amount<NonNegative>, delta: Option<Amount<NegativeAllowed>>) -> Self {
87Self::new_internal("deferred", amount, delta)
88 }
8990/// Converts a [`ValueBalance`] to a list of [`GetBlockchainInfoBalance`]s.
91pub fn value_pools(
92 value_balance: ValueBalance<NonNegative>,
93 delta_balance: Option<ValueBalance<NegativeAllowed>>,
94 ) -> [Self; 5] {
95 [
96Self::transparent(
97 value_balance.transparent_amount(),
98 delta_balance.map(|b| b.transparent_amount()),
99 ),
100Self::sprout(
101 value_balance.sprout_amount(),
102 delta_balance.map(|b| b.sprout_amount()),
103 ),
104Self::sapling(
105 value_balance.sapling_amount(),
106 delta_balance.map(|b| b.sapling_amount()),
107 ),
108Self::orchard(
109 value_balance.orchard_amount(),
110 delta_balance.map(|b| b.orchard_amount()),
111 ),
112Self::deferred(
113 value_balance.deferred_amount(),
114 delta_balance.map(|b| b.deferred_amount()),
115 ),
116 ]
117 }
118119/// Converts a [`ValueBalance`] to a [`GetBlockchainInfoBalance`] representing the total chain supply.
120pub fn chain_supply(value_balance: ValueBalance<NonNegative>) -> Self {
121Self::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"),
128None,
129 )
130 })
131 .expect("at least one pool")
132 }
133}