1//! Types used in `getblockchaininfo` RPC method.
23use zebra_chain::{
4 amount::{Amount, NonNegative},
5 value_balance::ValueBalance,
6};
78use zec::Zec;
910use super::*;
1112/// 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)]
18id: String,
19/// Total amount in the pool, in ZEC
20chain_value: Zec<NonNegative>,
21/// Total amount in the pool, in zatoshis
22chain_value_zat: Amount<NonNegative>,
23/// Whether the value pool balance is being monitored.
24monitored: bool,
25}
2627impl Balance {
28/// Returns a list of [`Balance`]s converted from the default [`ValueBalance`].
29pub fn zero_pools() -> [Self; 5] {
30Self::value_pools(Default::default())
31 }
3233/// Creates a new [`Balance`] from a pool name and its value balance.
34pub fn new(id: impl ToString, amount: Amount<NonNegative>) -> Self {
35Self {
36 id: id.to_string(),
37 chain_value: Zec::from(amount),
38 chain_value_zat: amount,
39 monitored: amount.zatoshis() != 0,
40 }
41 }
4243/// Creates a [`Balance`] for the transparent pool.
44pub fn transparent(amount: Amount<NonNegative>) -> Self {
45Self::new("transparent", amount)
46 }
4748/// Creates a [`Balance`] for the Sprout pool.
49pub fn sprout(amount: Amount<NonNegative>) -> Self {
50Self::new("sprout", amount)
51 }
5253/// Creates a [`Balance`] for the Sapling pool.
54pub fn sapling(amount: Amount<NonNegative>) -> Self {
55Self::new("sapling", amount)
56 }
5758/// Creates a [`Balance`] for the Orchard pool.
59pub fn orchard(amount: Amount<NonNegative>) -> Self {
60Self::new("orchard", amount)
61 }
6263/// Creates a [`Balance`] for the Deferred pool.
64pub fn deferred(amount: Amount<NonNegative>) -> Self {
65Self::new("deferred", amount)
66 }
6768/// Converts a [`ValueBalance`] to a list of [`Balance`]s.
69pub fn value_pools(value_balance: ValueBalance<NonNegative>) -> [Self; 5] {
70 [
71Self::transparent(value_balance.transparent_amount()),
72Self::sprout(value_balance.sprout_amount()),
73Self::sapling(value_balance.sapling_amount()),
74Self::orchard(value_balance.orchard_amount()),
75Self::deferred(value_balance.deferred_amount()),
76 ]
77 }
7879/// Converts a [`ValueBalance`] to a [`Balance`] representing the total chain supply.
80pub fn chain_supply(value_balance: ValueBalance<NonNegative>) -> Self {
81Self::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}