bdk_chain/
balance.rs

1use bitcoin::Amount;
2
3/// Balance, differentiated into various categories.
4#[derive(Debug, PartialEq, Eq, Clone, Default)]
5#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
6pub struct Balance {
7    /// All coinbase outputs not yet matured
8    pub immature: Amount,
9    /// Unconfirmed UTXOs generated by a wallet tx
10    pub trusted_pending: Amount,
11    /// Unconfirmed UTXOs received from an external wallet
12    pub untrusted_pending: Amount,
13    /// Confirmed and immediately spendable balance
14    pub confirmed: Amount,
15}
16
17impl Balance {
18    /// Get sum of trusted_pending and confirmed coins.
19    ///
20    /// This is the balance you can spend right now that shouldn't get cancelled via another party
21    /// double spending it.
22    pub fn trusted_spendable(&self) -> Amount {
23        self.confirmed + self.trusted_pending
24    }
25
26    /// Get the whole balance visible to the wallet.
27    pub fn total(&self) -> Amount {
28        self.confirmed + self.trusted_pending + self.untrusted_pending + self.immature
29    }
30}
31
32impl core::fmt::Display for Balance {
33    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
34        write!(
35            f,
36            "{{ immature: {}, trusted_pending: {}, untrusted_pending: {}, confirmed: {} }}",
37            self.immature, self.trusted_pending, self.untrusted_pending, self.confirmed
38        )
39    }
40}
41
42impl core::ops::Add for Balance {
43    type Output = Self;
44
45    fn add(self, other: Self) -> Self {
46        Self {
47            immature: self.immature + other.immature,
48            trusted_pending: self.trusted_pending + other.trusted_pending,
49            untrusted_pending: self.untrusted_pending + other.untrusted_pending,
50            confirmed: self.confirmed + other.confirmed,
51        }
52    }
53}