Skip to content

Commit

Permalink
balance
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Oct 14, 2023
1 parent b77719a commit 16bb272
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
12 changes: 12 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
};
42 changes: 41 additions & 1 deletion bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 10 additions & 1 deletion bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<BdkWallet> {
self.inner_mutex.lock().expect("wallet")
}

// TODO: Why is the Arc required here?
pub fn get_balance(&self) -> Arc<Balance> {
// 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)
}
}

0 comments on commit 16bb272

Please sign in to comment.