diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index 6b24fedb..fe41684d 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -140,6 +140,8 @@ interface Wallet { constructor(Descriptor descriptor, Descriptor? change_descriptor, Network network, WalletType wallet_type); AddressInfo get_address(AddressIndex address_index); + + Balance get_balance(); }; enum WalletType { @@ -165,4 +167,14 @@ interface AddressIndex { New(); LastUnused(); Peek(u32 index); +}; + + +interface Balance { + u64 immature(); + u64 trusted_pending(); + u64 untrusted_pending(); + u64 confirmed(); + u64 trusted_spendable(); + u64 total(); }; \ No newline at end of file diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index bc60850f..095c81be 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -5,6 +5,7 @@ pub fn hello_world() -> String { } use bdk::bitcoin::Network as BdkNetwork; +use bdk::wallet::Balance as BdkBalance; pub enum Network { /// Mainnet Bitcoin. @@ -209,7 +210,46 @@ use bdk::KeychainKind; // } // } // } -// + + + +pub struct Balance { + pub inner: BdkBalance, +} + +impl Balance { + /// All coinbase outputs not yet matured. + fn immature(&self) -> u64 { + self.inner.immature + } + + /// Unconfirmed UTXOs generated by a wallet tx. + fn trusted_pending(&self) -> u64 { + self.inner.trusted_pending + } + + /// Unconfirmed UTXOs received from an external wallet. + fn untrusted_pending(&self) -> u64 { + self.inner.untrusted_pending + } + + /// Confirmed and immediately spendable balance. + fn confirmed(&self) -> u64 { + self.inner.confirmed + } + + /// Get sum of trusted_pending and confirmed coins. + fn trusted_spendable(&self) -> u64 { + self.inner.trusted_spendable() + } + + /// Get the whole balance visible to the wallet. + fn total(&self) -> u64 { + self.inner.total() + } +} + + // pub struct Balance { // // All coinbase outputs not yet matured // pub immature: u64, diff --git a/bdk-ffi/src/wallet.rs b/bdk-ffi/src/wallet.rs index 8eba3e13..a706022e 100644 --- a/bdk-ffi/src/wallet.rs +++ b/bdk-ffi/src/wallet.rs @@ -858,6 +858,7 @@ // } // } use crate::descriptor::Descriptor; +use crate::Balance; use crate::{AddressIndex, AddressInfo, Network}; use bdk::Wallet as BdkWallet; use std::sync::{Arc, Mutex, MutexGuard}; @@ -903,8 +904,16 @@ impl Wallet { self.get_wallet().get_address(address_index.into()).into() } - // TODO 10: Do we need this mutex + // TODO: Do we need this mutex pub(crate) fn get_wallet(&self) -> MutexGuard { self.inner_mutex.lock().expect("wallet") } + + // TODO: Why is the Arc required here? + pub fn get_balance(&self) -> Arc { + // Arc::new(self.get_wallet().get_balance().into()) + let bdk_balance = self.get_wallet().get_balance(); + let balance = Balance { inner: bdk_balance }; + Arc::new(balance) + } }