Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add transactions method on wallet #432

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
7 changes: 7 additions & 0 deletions bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ 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()
.map(|tx| Arc::new(tx.tx_node.tx.clone().into()))
.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 @@ -5,7 +5,7 @@ class TestLiveWallet(unittest.TestCase):

def test_synced_balance(self):
descriptor: bdk.Descriptor = bdk.Descriptor(
"wpkh([c258d2e4/84h/1h/0h]tpubDDYkZojQFQjht8Tm4jsS3iuEmKjTiEGjG6KnuFNKKJb5A6ZUCUZKdvLdSDWofKi4ToRCwb9poe1XdqfUnP4jaJjCB2Zwv11ZLgSbnZSNecE/0/*)",
"wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)",
bdk.Network.TESTNET
)
wallet: bdk.Wallet = bdk.Wallet.new_no_persist(
Expand All @@ -23,6 +23,15 @@ def test_synced_balance(self):

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

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/*)",
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
Loading