Skip to content

Commit

Permalink
Add Balance struct and conversion from BdkBalance
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit authored and notmandatory committed Sep 8, 2022
1 parent 4e15bad commit 3c6075a
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Remove dictionary `ExtendedKeyInfo {mnenonic, xprv, fingerprint}` [#154]
- Remove interface `Transaction` [#190]
- Changed `Wallet` interface `list_transaction()` to return array of `TransactionDetails` [#190]
- Update `bdk` dependency version to 0.22 [#193]
- APIs Added [#154]
- `generate_mnemonic()`, returns string mnemonic
- `interface DescriptorSecretKey`
Expand All @@ -37,6 +38,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#154]: https://github.com/bitcoindevkit/bdk-ffi/pull/154
[#184]: https://github.com/bitcoindevkit/bdk-ffi/pull/184
[#185]: https://github.com/bitcoindevkit/bdk-ffi/pull/185
[#193]: https://github.com/bitcoindevkit/bdk-ffi/pull/193

## [v0.8.0]
- Update BDK to version 0.20.0 [#169]
Expand Down
12 changes: 11 additions & 1 deletion src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace bdk {
enum BdkError {
"InvalidU32Bytes",
"Generic",
"MissingCachedScripts",
"ScriptDoesntHaveAddressForm",
"NoRecipients",
"NoUtxosSelected",
Expand Down Expand Up @@ -73,6 +74,15 @@ dictionary SqliteDbConfiguration {
string path;
};

dictionary Balance {
u64 immature;
u64 trusted_pending;
u64 untrusted_pending;
u64 confirmed;
u64 spendable;
u64 total;
};

[Enum]
interface DatabaseConfig {
Memory();
Expand Down Expand Up @@ -176,7 +186,7 @@ interface Wallet {
AddressInfo get_address(AddressIndex address_index);

[Throws=BdkError]
u64 get_balance();
Balance get_balance();

[Throws=BdkError]
boolean sign([ByRef] PartiallySignedBitcoinTransaction psbt);
Expand Down
38 changes: 33 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use bdk::wallet::tx_builder::ChangeSpendPolicy;
use bdk::wallet::AddressIndex as BdkAddressIndex;
use bdk::wallet::AddressInfo as BdkAddressInfo;
use bdk::{
BlockTime, Error, FeeRate, KeychainKind, SignOptions, SyncOptions as BdkSyncOptions,
Wallet as BdkWallet,
Balance as BdkBalance, BlockTime, Error, FeeRate, KeychainKind, SignOptions,
SyncOptions as BdkSyncOptions, Wallet as BdkWallet,
};
use std::collections::HashSet;
use std::convert::{From, TryFrom};
Expand Down Expand Up @@ -150,7 +150,7 @@ pub struct TransactionDetails {
/// Sent value (sats)
/// Sum of owned inputs of this transaction.
pub sent: u64,
/// Fee value (sats) if available.
/// Fee value (sats) if confirmed.
/// The availability of the fee depends on the backend. It's never None with an Electrum
/// Server backend, but it could be None with a Bitcoin RPC node without txindex that receive
/// funds while offline.
Expand Down Expand Up @@ -246,6 +246,34 @@ impl From<&OutPoint> for BdkOutPoint {
}
}

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,
/// Get sum of trusted_pending and confirmed coins
pub spendable: u64,
/// Get the whole balance visible to the wallet
pub total: u64,
}

impl From<BdkBalance> for Balance {
fn from(bdk_balance: BdkBalance) -> Self {
Balance {
immature: bdk_balance.immature,
trusted_pending: bdk_balance.trusted_pending,
untrusted_pending: bdk_balance.untrusted_pending,
confirmed: bdk_balance.confirmed,
spendable: bdk_balance.get_spendable(),
total: bdk_balance.get_total(),
}
}
}

/// A transaction output, which defines new coins to be created from old ones.
pub struct TxOut {
/// The value of the output, in satoshis.
Expand Down Expand Up @@ -402,8 +430,8 @@ impl Wallet {

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

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

0 comments on commit 3c6075a

Please sign in to comment.