From c1383d7a90679be9ade0674f27fa113363f04a93 Mon Sep 17 00:00:00 2001 From: Matthew Date: Wed, 29 Nov 2023 16:52:49 -0600 Subject: [PATCH] feat: add sent_and_received method on wallet --- bdk-ffi/src/bdk.udl | 7 +++++++ bdk-ffi/src/lib.rs | 5 +++++ bdk-ffi/src/wallet.rs | 9 ++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/bdk-ffi/src/bdk.udl b/bdk-ffi/src/bdk.udl index 58acc0f5..bfffeceb 100644 --- a/bdk-ffi/src/bdk.udl +++ b/bdk-ffi/src/bdk.udl @@ -95,6 +95,8 @@ interface Wallet { [Throws=BdkError] boolean sign(PartiallySignedTransaction psbt); + + SentAndReceivedValues sent_and_received([ByRef] Transaction tx); }; interface Update {}; @@ -235,6 +237,11 @@ dictionary ScriptAmount { u64 amount; }; +dictionary SentAndReceivedValues { + u64 sent; + u64 received; +}; + // ------------------------------------------------------------------------ // bdk crate - bitcoin re-exports // ------------------------------------------------------------------------ diff --git a/bdk-ffi/src/lib.rs b/bdk-ffi/src/lib.rs index 185529b4..0ea6ad68 100644 --- a/bdk-ffi/src/lib.rs +++ b/bdk-ffi/src/lib.rs @@ -39,6 +39,11 @@ pub struct ScriptAmount { pub amount: u64, } +pub struct SentAndReceivedValues { + pub sent: u64, + pub received: u64, +} + /// A derived address and the index it was found at. pub struct AddressInfo { /// Child index of this address. diff --git a/bdk-ffi/src/wallet.rs b/bdk-ffi/src/wallet.rs index 5778e55f..0afbe794 100644 --- a/bdk-ffi/src/wallet.rs +++ b/bdk-ffi/src/wallet.rs @@ -1,4 +1,4 @@ -use crate::bitcoin::{OutPoint, PartiallySignedTransaction}; +use crate::bitcoin::{OutPoint, PartiallySignedTransaction, Transaction}; use crate::descriptor::Descriptor; use crate::{AddressIndex, AddressInfo, Network, ScriptAmount}; use crate::{Balance, Script}; @@ -13,6 +13,8 @@ use bdk::{SignOptions, Wallet as BdkWallet}; use bdk::wallet::tx_builder::ChangeSpendPolicy; use std::sync::{Arc, Mutex, MutexGuard}; +use crate::SentAndReceivedValues; + #[derive(Debug)] pub struct Wallet { // TODO 8: Do we really need the mutex on the wallet? Could this be an Arc? @@ -92,6 +94,11 @@ impl Wallet { .sign(&mut psbt, SignOptions::default()) .map_err(|e| BdkError::Generic(e.to_string())) } + + pub fn sent_and_received(&self, tx: &Transaction) -> SentAndReceivedValues { + let (sent, received): (u64, u64) = self.get_wallet().sent_and_received(&tx.clone().into()); + SentAndReceivedValues { sent, received } + } } pub struct Update(pub(crate) BdkUpdate);