Skip to content

Commit a6af35c

Browse files
thunderbiscuitnotmandatory
authored andcommitted
Add Balance struct and conversion from BdkBalance
1 parent f35ff2b commit a6af35c

File tree

2 files changed

+47
-9
lines changed

2 files changed

+47
-9
lines changed

src/bdk.udl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace bdk {
77
enum BdkError {
88
"InvalidU32Bytes",
99
"Generic",
10+
"MissingCachedScripts",
1011
"ScriptDoesntHaveAddressForm",
1112
"NoRecipients",
1213
"NoUtxosSelected",
@@ -73,6 +74,15 @@ dictionary SqliteDbConfiguration {
7374
string path;
7475
};
7576

77+
dictionary Balance {
78+
u64 immature;
79+
u64 trusted_pending;
80+
u64 untrusted_pending;
81+
u64 confirmed;
82+
u64 spendable;
83+
u64 total;
84+
};
85+
7686
[Enum]
7787
interface DatabaseConfig {
7888
Memory();
@@ -181,7 +191,7 @@ interface Wallet {
181191
AddressInfo get_address(AddressIndex address_index);
182192

183193
[Throws=BdkError]
184-
u64 get_balance();
194+
Balance get_balance();
185195

186196
[Throws=BdkError]
187197
boolean sign([ByRef] PartiallySignedBitcoinTransaction psbt);
@@ -239,9 +249,9 @@ interface TxBuilder {
239249
TxBuilder enable_rbf_with_sequence(u32 nsequence);
240250

241251
TxBuilder add_data(sequence<u8> data);
242-
252+
243253
TxBuilder set_recipients(sequence<AddressAmount> recipients);
244-
254+
245255
[Throws=BdkError]
246256
PartiallySignedBitcoinTransaction finish([ByRef] Wallet wallet);
247257
};
@@ -285,4 +295,4 @@ interface DescriptorPublicKey {
285295
DescriptorPublicKey extend(DerivationPath path);
286296

287297
string as_string();
288-
};
298+
};

src/lib.rs

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use bdk::wallet::tx_builder::ChangeSpendPolicy;
2323
use bdk::wallet::AddressIndex as BdkAddressIndex;
2424
use bdk::wallet::AddressInfo as BdkAddressInfo;
2525
use bdk::{
26-
BlockTime, Error, FeeRate, KeychainKind, SignOptions, SyncOptions as BdkSyncOptions,
27-
Wallet as BdkWallet,
26+
Balance as BdkBalance, BlockTime, Error, FeeRate, KeychainKind, SignOptions,
27+
SyncOptions as BdkSyncOptions, Wallet as BdkWallet,
2828
};
2929
use std::collections::HashSet;
3030
use std::convert::{From, TryFrom};
@@ -150,7 +150,7 @@ pub struct TransactionDetails {
150150
/// Sent value (sats)
151151
/// Sum of owned inputs of this transaction.
152152
pub sent: u64,
153-
/// Fee value (sats) if available.
153+
/// Fee value (sats) if confirmed.
154154
/// The availability of the fee depends on the backend. It's never None with an Electrum
155155
/// Server backend, but it could be None with a Bitcoin RPC node without txindex that receive
156156
/// funds while offline.
@@ -273,6 +273,34 @@ impl From<&OutPoint> for BdkOutPoint {
273273
}
274274
}
275275

276+
pub struct Balance {
277+
// All coinbase outputs not yet matured
278+
pub immature: u64,
279+
/// Unconfirmed UTXOs generated by a wallet tx
280+
pub trusted_pending: u64,
281+
/// Unconfirmed UTXOs received from an external wallet
282+
pub untrusted_pending: u64,
283+
/// Confirmed and immediately spendable balance
284+
pub confirmed: u64,
285+
/// Get sum of trusted_pending and confirmed coins
286+
pub spendable: u64,
287+
/// Get the whole balance visible to the wallet
288+
pub total: u64,
289+
}
290+
291+
impl From<BdkBalance> for Balance {
292+
fn from(bdk_balance: BdkBalance) -> Self {
293+
Balance {
294+
immature: bdk_balance.immature,
295+
trusted_pending: bdk_balance.trusted_pending,
296+
untrusted_pending: bdk_balance.untrusted_pending,
297+
confirmed: bdk_balance.confirmed,
298+
spendable: bdk_balance.get_spendable(),
299+
total: bdk_balance.get_total(),
300+
}
301+
}
302+
}
303+
276304
/// A transaction output, which defines new coins to be created from old ones.
277305
pub struct TxOut {
278306
/// The value of the output, in satoshis.
@@ -429,8 +457,8 @@ impl Wallet {
429457

430458
/// Return the balance, meaning the sum of this wallet’s unspent outputs’ values. Note that this method only operates
431459
/// on the internal database, which first needs to be Wallet.sync manually.
432-
fn get_balance(&self) -> Result<u64, Error> {
433-
self.get_wallet().get_balance()
460+
fn get_balance(&self) -> Result<Balance, Error> {
461+
self.get_wallet().get_balance().map(|b| b.into())
434462
}
435463

436464
/// Sign a transaction with all the wallet’s signers.

0 commit comments

Comments
 (0)