Skip to content

Commit 3c6075a

Browse files
thunderbiscuitnotmandatory
authored andcommitted
Add Balance struct and conversion from BdkBalance
1 parent 4e15bad commit 3c6075a

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- Remove dictionary `ExtendedKeyInfo {mnenonic, xprv, fingerprint}` [#154]
1515
- Remove interface `Transaction` [#190]
1616
- Changed `Wallet` interface `list_transaction()` to return array of `TransactionDetails` [#190]
17+
- Update `bdk` dependency version to 0.22 [#193]
1718
- APIs Added [#154]
1819
- `generate_mnemonic()`, returns string mnemonic
1920
- `interface DescriptorSecretKey`
@@ -37,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3738
[#154]: https://github.com/bitcoindevkit/bdk-ffi/pull/154
3839
[#184]: https://github.com/bitcoindevkit/bdk-ffi/pull/184
3940
[#185]: https://github.com/bitcoindevkit/bdk-ffi/pull/185
41+
[#193]: https://github.com/bitcoindevkit/bdk-ffi/pull/193
4042

4143
## [v0.8.0]
4244
- Update BDK to version 0.20.0 [#169]

src/bdk.udl

Lines changed: 11 additions & 1 deletion
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();
@@ -176,7 +186,7 @@ interface Wallet {
176186
AddressInfo get_address(AddressIndex address_index);
177187

178188
[Throws=BdkError]
179-
u64 get_balance();
189+
Balance get_balance();
180190

181191
[Throws=BdkError]
182192
boolean sign([ByRef] PartiallySignedBitcoinTransaction psbt);

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.
@@ -246,6 +246,34 @@ impl From<&OutPoint> for BdkOutPoint {
246246
}
247247
}
248248

249+
pub struct Balance {
250+
// All coinbase outputs not yet matured
251+
pub immature: u64,
252+
/// Unconfirmed UTXOs generated by a wallet tx
253+
pub trusted_pending: u64,
254+
/// Unconfirmed UTXOs received from an external wallet
255+
pub untrusted_pending: u64,
256+
/// Confirmed and immediately spendable balance
257+
pub confirmed: u64,
258+
/// Get sum of trusted_pending and confirmed coins
259+
pub spendable: u64,
260+
/// Get the whole balance visible to the wallet
261+
pub total: u64,
262+
}
263+
264+
impl From<BdkBalance> for Balance {
265+
fn from(bdk_balance: BdkBalance) -> Self {
266+
Balance {
267+
immature: bdk_balance.immature,
268+
trusted_pending: bdk_balance.trusted_pending,
269+
untrusted_pending: bdk_balance.untrusted_pending,
270+
confirmed: bdk_balance.confirmed,
271+
spendable: bdk_balance.get_spendable(),
272+
total: bdk_balance.get_total(),
273+
}
274+
}
275+
}
276+
249277
/// A transaction output, which defines new coins to be created from old ones.
250278
pub struct TxOut {
251279
/// The value of the output, in satoshis.
@@ -402,8 +430,8 @@ impl Wallet {
402430

403431
/// Return the balance, meaning the sum of this wallet’s unspent outputs’ values. Note that this method only operates
404432
/// on the internal database, which first needs to be Wallet.sync manually.
405-
fn get_balance(&self) -> Result<u64, Error> {
406-
self.get_wallet().get_balance()
433+
fn get_balance(&self) -> Result<Balance, Error> {
434+
self.get_wallet().get_balance().map(|b| b.into())
407435
}
408436

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

0 commit comments

Comments
 (0)