From 658969519231bc2b27063ffb3319cb04e49f5ed8 Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Wed, 11 Oct 2023 12:30:28 -0400 Subject: [PATCH] Temp 19: Add Balance type --- bdk-ffi/src/bdk.udl | 22 +++++ bdk-ffi/src/lib.rs | 80 ++++++++++++------- bdk-ffi/src/wallet.rs | 18 +++-- .../kotlin/org/bitcoindevkit/WalletTest.kt | 19 +++-- 4 files changed, 96 insertions(+), 43 deletions(-) diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index f189076b..8de8e732 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -137,6 +137,10 @@ interface Wallet { constructor(Descriptor descriptor, Descriptor? change_descriptor, Network network, WalletType wallet_type); AddressInfo get_address(AddressIndex address_index); + + Network network(); + + Balance get_balance(); }; enum WalletType { @@ -148,6 +152,10 @@ interface Address { [Throws=BdkError] constructor(string address, Network network); + Network network(); + + string to_qr_uri(); + string as_string(); }; @@ -163,3 +171,17 @@ interface AddressIndex { LastUnused(); Peek(u32 index); }; + +interface Balance { + u64 immature(); + + u64 trusted_pending(); + + u64 untrusted_pending(); + + u64 confirmed(); + + u64 trusted_spendable(); + + u64 total(); +}; diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index 1e804abb..5071d072 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -10,6 +10,7 @@ use bdk::bitcoin::Address as BdkAddress; use bdk::bitcoin::Network as BdkNetwork; use bdk::wallet::AddressIndex as BdkAddressIndex; use bdk::wallet::AddressInfo as BdkAddressInfo; +use bdk::wallet::Balance as BdkBalance; use bdk::Error as BdkError; use bdk::KeychainKind; use std::sync::Arc; @@ -146,7 +147,6 @@ impl From<&BdkAddressIndex> for AddressIndex { } } -// // /// A wallet transaction // #[derive(Debug, Clone, PartialEq, Eq, Default)] // pub struct TransactionDetails { @@ -168,6 +168,7 @@ impl From<&BdkAddressIndex> for AddressIndex { // /// transaction, unconfirmed transaction contains `None`. // pub confirmation_time: Option, // } + // // impl From for TransactionDetails { // fn from(tx_details: BdkTransactionDetails) -> Self { @@ -202,30 +203,51 @@ impl From<&BdkAddressIndex> for AddressIndex { // } // } // } -// -// // MIGRATION 1.0: Not sure why total and spendable are not in Balance anymore. -// 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, -// } -// + +pub struct Balance { + pub inner: BdkBalance, +} + +impl Balance { + /// All coinbase outputs not yet matured. + fn immature(&self) -> u64 { + self.inner.immature + } + + /// Unconfirmed UTXOs generated by a wallet tx. + fn trusted_pending(&self) -> u64 { + self.inner.trusted_pending + } + + /// Unconfirmed UTXOs received from an external wallet. + fn untrusted_pending(&self) -> u64 { + self.inner.untrusted_pending + } + + /// Confirmed and immediately spendable balance. + fn confirmed(&self) -> u64 { + self.inner.confirmed + } + + // TODO: Is this how I should name this method? The Rust side calls it get_spendable but it's + // weird to have just the last two being named like getters when they all sort of are. + /// Get sum of trusted_pending and confirmed coins. + fn trusted_spendable(&self) -> u64 { + self.inner.trusted_spendable() + } + + /// Get the whole balance visible to the wallet. + fn total(&self) -> u64 { + self.inner.total() + } +} + // impl From 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, -// } +// Balance { inner: bdk_balance } // } // } -// + // /// A transaction output, which defines new coins to be created from old ones. // #[derive(Debug, Clone)] // pub struct TxOut { @@ -381,13 +403,13 @@ impl From<&BdkAddressIndex> for AddressIndex { // self.inner.output.iter().map(|x| x.into()).collect() // } // } -// + // impl From for Transaction { // fn from(tx: BdkTransaction) -> Self { // Transaction { inner: tx } // } // } -// + /// A Bitcoin address. #[derive(Debug, PartialEq, Eq)] pub struct Address { @@ -427,9 +449,9 @@ impl Address { // } // } - // fn network(&self) -> Network { - // self.inner.network - // } + fn network(&self) -> Network { + self.inner.network.into() + } // fn script_pubkey(&self) -> Arc