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 dictionary `ExtendedKeyInfo {mnenonic, xprv, fingerprint}` [#154]
- Remove interface `Transaction` [#190] - Remove interface `Transaction` [#190]
- Changed `Wallet` interface `list_transaction()` to return array of `TransactionDetails` [#190] - Changed `Wallet` interface `list_transaction()` to return array of `TransactionDetails` [#190]
- Update `bdk` dependency version to 0.22 [#193]
- APIs Added [#154] - APIs Added [#154]
- `generate_mnemonic()`, returns string mnemonic - `generate_mnemonic()`, returns string mnemonic
- `interface DescriptorSecretKey` - `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 [#154]: https://github.com/bitcoindevkit/bdk-ffi/pull/154
[#184]: https://github.com/bitcoindevkit/bdk-ffi/pull/184 [#184]: https://github.com/bitcoindevkit/bdk-ffi/pull/184
[#185]: https://github.com/bitcoindevkit/bdk-ffi/pull/185 [#185]: https://github.com/bitcoindevkit/bdk-ffi/pull/185
[#193]: https://github.com/bitcoindevkit/bdk-ffi/pull/193
## [v0.8.0] ## [v0.8.0]
- Update BDK to version 0.20.0 [#169] - Update BDK to version 0.20.0 [#169]

View File

@ -7,6 +7,7 @@ namespace bdk {
enum BdkError { enum BdkError {
"InvalidU32Bytes", "InvalidU32Bytes",
"Generic", "Generic",
"MissingCachedScripts",
"ScriptDoesntHaveAddressForm", "ScriptDoesntHaveAddressForm",
"NoRecipients", "NoRecipients",
"NoUtxosSelected", "NoUtxosSelected",
@ -73,6 +74,15 @@ dictionary SqliteDbConfiguration {
string path; string path;
}; };
dictionary Balance {
u64 immature;
u64 trusted_pending;
u64 untrusted_pending;
u64 confirmed;
u64 spendable;
u64 total;
};
[Enum] [Enum]
interface DatabaseConfig { interface DatabaseConfig {
Memory(); Memory();
@ -176,7 +186,7 @@ interface Wallet {
AddressInfo get_address(AddressIndex address_index); AddressInfo get_address(AddressIndex address_index);
[Throws=BdkError] [Throws=BdkError]
u64 get_balance(); Balance get_balance();
[Throws=BdkError] [Throws=BdkError]
boolean sign([ByRef] PartiallySignedBitcoinTransaction psbt); 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::AddressIndex as BdkAddressIndex;
use bdk::wallet::AddressInfo as BdkAddressInfo; use bdk::wallet::AddressInfo as BdkAddressInfo;
use bdk::{ use bdk::{
BlockTime, Error, FeeRate, KeychainKind, SignOptions, SyncOptions as BdkSyncOptions, Balance as BdkBalance, BlockTime, Error, FeeRate, KeychainKind, SignOptions,
Wallet as BdkWallet, SyncOptions as BdkSyncOptions, Wallet as BdkWallet,
}; };
use std::collections::HashSet; use std::collections::HashSet;
use std::convert::{From, TryFrom}; use std::convert::{From, TryFrom};
@ -150,7 +150,7 @@ pub struct TransactionDetails {
/// Sent value (sats) /// Sent value (sats)
/// Sum of owned inputs of this transaction. /// Sum of owned inputs of this transaction.
pub sent: u64, 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 /// 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 /// Server backend, but it could be None with a Bitcoin RPC node without txindex that receive
/// funds while offline. /// 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. /// A transaction output, which defines new coins to be created from old ones.
pub struct TxOut { pub struct TxOut {
/// The value of the output, in satoshis. /// 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 /// 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. /// on the internal database, which first needs to be Wallet.sync manually.
fn get_balance(&self) -> Result<u64, Error> { fn get_balance(&self) -> Result<Balance, Error> {
self.get_wallet().get_balance() self.get_wallet().get_balance().map(|b| b.into())
} }
/// Sign a transaction with all the wallets signers. /// Sign a transaction with all the wallets signers.