Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions crates/bdk/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,6 @@ pub struct LocalUtxo {
pub outpoint: OutPoint,
/// Transaction output
pub txout: TxOut,
/// Type of keychain
pub keychain: KeychainKind,
/// Whether this UTXO is spent or not
pub is_spent: bool,
/// The derivation index for the script pubkey in the wallet
Expand Down Expand Up @@ -268,6 +266,56 @@ impl Ord for TransactionDetails {
}
}

#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct Balance {
/// All coinbase outputs not yet matured
pub immature: u64,
/// Unconfirmed UTXOs generated by a wallet tx
pub trusted_pending: u64,
/// Unconfirmed UTXOs received from an external wallet
pub untrusted_pending: u64,
/// Confirmed and immediately spendable balance
pub confirmed: u64,
}

impl Balance {
/// Get sum of trusted_pending and confirmed coins.
///
/// This is the balance you can spend right now that shouldn't get cancelled via another party
/// double spending it.
pub fn trusted_spendable(&self) -> u64 {
self.confirmed + self.trusted_pending
}

/// Get the whole balance visible to the wallet.
pub fn total(&self) -> u64 {
self.confirmed + self.trusted_pending + self.untrusted_pending + self.immature
}
}

impl core::fmt::Display for Balance {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"{{ immature: {}, trusted_pending: {}, untrusted_pending: {}, confirmed: {} }}",
self.immature, self.trusted_pending, self.untrusted_pending, self.confirmed
)
}
}

impl core::ops::Add for Balance {
type Output = Self;

fn add(self, other: Self) -> Self {
Self {
immature: self.immature + other.immature,
trusted_pending: self.trusted_pending + other.trusted_pending,
untrusted_pending: self.untrusted_pending + other.untrusted_pending,
confirmed: self.confirmed + other.confirmed,
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
3 changes: 0 additions & 3 deletions crates/bdk/src/wallet/coin_selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,6 @@ mod test {
value,
script_pubkey: Script::new(),
},
keychain: KeychainKind::External,
is_spent: false,
derivation_index: 42,
confirmation_time,
Expand Down Expand Up @@ -771,7 +770,6 @@ mod test {
value: rng.gen_range(0..200000000),
script_pubkey: Script::new(),
},
keychain: KeychainKind::External,
is_spent: false,
derivation_index: rng.next_u32(),
confirmation_time: if rng.gen_bool(0.5) {
Expand Down Expand Up @@ -800,7 +798,6 @@ mod test {
value: utxos_value,
script_pubkey: Script::new(),
},
keychain: KeychainKind::External,
is_spent: false,
derivation_index: 42,
confirmation_time: ConfirmationTime::Unconfirmed,
Expand Down
2 changes: 2 additions & 0 deletions crates/bdk/src/wallet/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ impl FullyNodedExport {
) -> Result<Self, &'static str> {
let descriptor = wallet
.get_descriptor_for_keychain(KeychainKind::External)
.expect("external descriptor is always expected for default wallet")
.to_string_with_secret(
&wallet
.get_signers(KeychainKind::External)
Expand Down Expand Up @@ -147,6 +148,7 @@ impl FullyNodedExport {
true => {
let descriptor = wallet
.get_descriptor_for_keychain(KeychainKind::Internal)
.expect("we ensured that public descriptor exists at this point")
.to_string_with_secret(
&wallet
.get_signers(KeychainKind::Internal)
Expand Down
Loading