From c88b33473b9cb3120008e6e22b35d50e0ef0a479 Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Thu, 16 May 2024 09:30:12 -0400 Subject: [PATCH] test: add memory wallet test --- .../org/bitcoindevkit/LiveMemoryWalletTest.kt | 36 +++++++++++++++ .../LiveMemoryWalletTests.swift | 44 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/LiveMemoryWalletTest.kt create mode 100644 bdk-swift/Tests/BitcoinDevKitTests/LiveMemoryWalletTests.swift diff --git a/bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/LiveMemoryWalletTest.kt b/bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/LiveMemoryWalletTest.kt new file mode 100644 index 0000000..14ad5b2 --- /dev/null +++ b/bdk-jvm/lib/src/test/kotlin/org/bitcoindevkit/LiveMemoryWalletTest.kt @@ -0,0 +1,36 @@ +package org.bitcoindevkit + +import kotlin.test.Test + +private const val SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net" +private const val TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud" + +class LiveMemoryWalletTest { + @Test + fun testSyncedBalance() { + val descriptor: Descriptor = Descriptor( + "wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", + Network.SIGNET + ) + val wallet: Wallet = Wallet.newNoPersist(descriptor, null, Network.SIGNET) + val esploraClient: EsploraClient = EsploraClient(SIGNET_ESPLORA_URL) + val fullScanRequest: FullScanRequest = wallet.startFullScan() + val update = esploraClient.fullScan(fullScanRequest, 10uL, 1uL) + wallet.applyUpdate(update) + wallet.commit() + println("Balance: ${wallet.getBalance().total.toSat()}") + + assert(wallet.getBalance().total.toSat() > 0uL) { + "Wallet balance must be greater than 0! Please send funds to ${wallet.revealNextAddress(KeychainKind.EXTERNAL).address.asString()} and try again." + } + + println("Transactions count: ${wallet.transactions().count()}") + val transactions = wallet.transactions().take(3) + for (tx in transactions) { + val sentAndReceived = wallet.sentAndReceived(tx.transaction) + println("Transaction: ${tx.transaction.txid()}") + println("Sent ${sentAndReceived.sent.toSat()}") + println("Received ${sentAndReceived.received.toSat()}") + } + } +} \ No newline at end of file diff --git a/bdk-swift/Tests/BitcoinDevKitTests/LiveMemoryWalletTests.swift b/bdk-swift/Tests/BitcoinDevKitTests/LiveMemoryWalletTests.swift new file mode 100644 index 0000000..50524f4 --- /dev/null +++ b/bdk-swift/Tests/BitcoinDevKitTests/LiveMemoryWalletTests.swift @@ -0,0 +1,44 @@ +import XCTest +@testable import BitcoinDevKit + +private let SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net" +private let TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud" + +final class LiveMemoryWalletTests: XCTestCase { + func testSyncedBalance() throws { + let descriptor = try Descriptor( + descriptor: "wpkh(tprv8ZgxMBicQKsPf2qfrEygW6fdYseJDDrVnDv26PH5BHdvSuG6ecCbHqLVof9yZcMoM31z9ur3tTYbSnr1WBqbGX97CbXcmp5H6qeMpyvx35B/84h/1h/0h/0/*)", + network: Network.signet + ) + let wallet = try Wallet.newNoPersist( + descriptor: descriptor, + changeDescriptor: nil, + network: .signet + ) + let esploraClient = EsploraClient(url: SIGNET_ESPLORA_URL) + let fullScanRequest: FullScanRequest = wallet.startFullScan() + let update = try esploraClient.fullScan( + fullScanRequest: fullScanRequest, + stopGap: 10, + parallelRequests: 1 + ) + try wallet.applyUpdate(update: update) + let _ = try wallet.commit() + let address = try wallet.revealNextAddress(keychain: KeychainKind.external).address.asString() + + XCTAssertGreaterThan( + wallet.getBalance().total.toSat(), + UInt64(0), + "Wallet must have positive balance, please send funds to \(address)" + ) + + print("Transactions count: \(wallet.transactions().count)") + let transactions = wallet.transactions().prefix(3) + for tx in transactions { + let sentAndReceived = wallet.sentAndReceived(tx: tx.transaction) + print("Transaction: \(tx.transaction.txid())") + print("Sent \(sentAndReceived.sent)") + print("Received \(sentAndReceived.received)") + } + } +}