zebra_rpc/methods/types/
get_blockchain_info.rs1use 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#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, Getters, new)]
16#[serde(rename_all = "camelCase")]
17pub struct GetBlockchainInfoBalance {
18 #[serde(skip_serializing_if = "String::is_empty", default)]
20 id: String,
21 #[getter(copy)]
23 chain_value: Zec<NonNegative>,
24 #[getter(copy)]
26 chain_value_zat: Amount<NonNegative>,
27 monitored: bool,
29 #[serde(skip_serializing_if = "Option::is_none", default)]
31 #[getter(copy)]
32 value_delta: Option<Zec<NegativeAllowed>>,
33 #[serde(skip_serializing_if = "Option::is_none", default)]
35 #[getter(copy)]
36 value_delta_zat: Option<Amount>,
37}
38
39impl GetBlockchainInfoBalance {
40 pub fn zero_pools() -> [Self; 5] {
42 Self::value_pools(Default::default(), None)
43 }
44
45 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 pub fn transparent(
64 amount: Amount<NonNegative>,
65 delta: Option<Amount<NegativeAllowed>>,
66 ) -> Self {
67 Self::new_internal("transparent", amount, delta)
68 }
69
70 pub fn sprout(amount: Amount<NonNegative>, delta: Option<Amount<NegativeAllowed>>) -> Self {
72 Self::new_internal("sprout", amount, delta)
73 }
74
75 pub fn sapling(amount: Amount<NonNegative>, delta: Option<Amount<NegativeAllowed>>) -> Self {
77 Self::new_internal("sapling", amount, delta)
78 }
79
80 pub fn orchard(amount: Amount<NonNegative>, delta: Option<Amount<NegativeAllowed>>) -> Self {
82 Self::new_internal("orchard", amount, delta)
83 }
84
85 pub fn deferred(amount: Amount<NonNegative>, delta: Option<Amount<NegativeAllowed>>) -> Self {
87 Self::new_internal("lockbox", amount, delta)
88 }
89
90 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 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}