Skip to content

Commit

Permalink
feat: add sent_and_received method on wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Dec 1, 2023
1 parent e5ded1a commit 55c36b5
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
7 changes: 7 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ interface Wallet {

[Throws=BdkError]
boolean sign(PartiallySignedTransaction psbt);

SentAndReceivedResult sent_and_received([ByRef] Transaction tx);
};

interface Update {};
Expand Down Expand Up @@ -235,6 +237,11 @@ dictionary ScriptAmount {
u64 amount;
};

dictionary SentAndReceivedResult {
u64 sent;
u64 received;
};

// ------------------------------------------------------------------------
// bdk crate - bitcoin re-exports
// ------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ pub struct ScriptAmount {
pub amount: u64,
}

pub struct SentAndReceivedResult {
pub sent: u64,
pub received: u64,
}

/// A derived address and the index it was found at.
pub struct AddressInfo {
/// Child index of this address.
Expand Down
9 changes: 8 additions & 1 deletion bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -13,6 +13,8 @@ use bdk::{SignOptions, Wallet as BdkWallet};
use bdk::wallet::tx_builder::ChangeSpendPolicy;
use std::sync::{Arc, Mutex, MutexGuard};

use crate::SentAndReceivedResult;

#[derive(Debug)]
pub struct Wallet {
// TODO 8: Do we really need the mutex on the wallet? Could this be an Arc?
Expand Down Expand Up @@ -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) -> SentAndReceivedResult {
let (sent, received): (u64, u64) = self.get_wallet().sent_and_received(&tx.clone().into());
SentAndReceivedResult { sent, received }
}
}

pub struct Update(pub(crate) BdkUpdate);
Expand Down

0 comments on commit 55c36b5

Please sign in to comment.