Skip to content

Commit

Permalink
feat: add transactions method on wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
reez committed Dec 12, 2023
1 parent c9019e9 commit 27c10a7
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@ class LiveWalletTest {
println("Balance: $balance")

assert(wallet.getBalance().total > 0uL)

println("Transactions count: ${wallet.transactions().count()}")
val transactions = wallet.transactions().take(3)
for (tx in transactions) {
val sentAndReceived = wallet.sentAndReceived(tx)
println("Transaction: ${tx.txid()}")
println("Sent ${sentAndReceived.sent}")
println("Received ${sentAndReceived.received}")
}
}

@Test
Expand Down
2 changes: 2 additions & 0 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ interface Wallet {
boolean sign(PartiallySignedTransaction psbt);

SentAndReceivedValues sent_and_received([ByRef] Transaction tx);

sequence<Transaction> transactions();
};

interface Update {};
Expand Down
25 changes: 25 additions & 0 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,31 @@ impl Wallet {
let (sent, received): (u64, u64) = self.get_wallet().sent_and_received(&tx.clone().into());
SentAndReceivedValues { sent, received }
}

pub fn transactions(&self) -> Vec<Arc<Transaction>> {
self.get_wallet()
.transactions()
.filter_map(|tx| {
let transaction = tx.tx_node.tx.clone();

let is_mine_input = transaction.input.iter().any(|input| {
let script = Script::from(input.script_sig.clone());
self.is_mine(Arc::new(script))
});

let is_mine_output = transaction.output.iter().any(|output| {
let script = Script::from(output.script_pubkey.clone());
self.is_mine(Arc::new(script))
});

if is_mine_input || is_mine_output {
Some(Arc::new(transaction.into()))
} else {
None
}
})
.collect()
}
}

pub struct SentAndReceivedValues {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ class LiveWalletTest {
println("Balance: ${wallet.getBalance().total}")

assert(wallet.getBalance().total > 0uL)

println("Transactions count: ${wallet.transactions().count()}")
val transactions = wallet.transactions().take(3)
for (tx in transactions) {
val sentAndReceived = wallet.sentAndReceived(tx)
println("Transaction: ${tx.txid()}")
println("Sent ${sentAndReceived.sent}")
println("Received ${sentAndReceived.received}")
}
}

@Test
Expand Down
11 changes: 10 additions & 1 deletion bdk-python/tests/test_live_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ def test_synced_balance(self):

self.assertGreater(wallet.get_balance().total, 0)

def test_broadcast_transaction(self):
print(f"Transactions count: {len(wallet.transactions())}")
transactions = wallet.transactions()[:3]
for tx in transactions:
sent_and_received = wallet.sent_and_received(tx)
print(f"Transaction: {tx.txid()}")
print(f"Sent {sent_and_received.sent}")
print(f"Received {sent_and_received.received}")


def test_broadcast_transaction(self):
descriptor: bdk.Descriptor = bdk.Descriptor(
"wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)",
bdk.Network.TESTNET
Expand Down
9 changes: 9 additions & 0 deletions bdk-swift/Tests/BitcoinDevKitTests/LiveWalletTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ final class LiveWalletTests: XCTestCase {
try wallet.applyUpdate(update: update)

XCTAssertGreaterThan(wallet.getBalance().total, UInt64(0))

print("Transactions count: \(wallet.transactions().count)")
let transactions = wallet.transactions().prefix(3)
for tx in transactions {
let sentAndReceived = wallet.sentAndReceived(tx: tx)
print("Transaction: \(tx.txid())")
print("Sent \(sentAndReceived.sent)")
print("Received \(sentAndReceived.received)")
}
}

func testBroadcastTransaction() throws {
Expand Down

0 comments on commit 27c10a7

Please sign in to comment.