Add Balance struct and conversion from BdkBalance

This commit is contained in:
thunderbiscuit 2022-09-01 14:04:36 -04:00 committed by Steve Myers
parent 4e15badb14
commit 3c6075ad96
No known key found for this signature in database
GPG Key ID: 8105A46B22C2D051
3 changed files with 46 additions and 6 deletions

View File

@ -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`
@ -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]

View File

@ -7,6 +7,7 @@ namespace bdk {
enum BdkError {
"InvalidU32Bytes",
"Generic",
"MissingCachedScripts",
"ScriptDoesntHaveAddressForm",
"NoRecipients",
"NoUtxosSelected",
@ -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();
@ -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);

View File

@ -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};
@ -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.
@ -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.
@ -402,8 +430,8 @@ impl Wallet {
/// Return the balance, meaning the sum of this wallets 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 wallets signers.