From a671c4f86b464975ac5dd8c4c85215f4bb8ec35d Mon Sep 17 00:00:00 2001 From: thunderbiscuit Date: Fri, 16 Sep 2022 07:10:19 -0400 Subject: [PATCH] Move samples into tests --- .gitignore | 4 +- api-docs/build.gradle.kts | 2 +- .../main/kotlin/org/bitcoindevkit/Samples.kt | 13 ------ .../src/main/kotlin/org/bitcoindevkit/bdk.kt | 34 ++++++++++++-- .../test/kotlin/org/bitcoindevkit/Samples.kt | 44 +++++++++++++++++++ 5 files changed, 77 insertions(+), 20 deletions(-) delete mode 100644 api-docs/src/main/kotlin/org/bitcoindevkit/Samples.kt create mode 100644 api-docs/src/test/kotlin/org/bitcoindevkit/Samples.kt diff --git a/.gitignore b/.gitignore index 2e06997..7b20211 100644 --- a/.gitignore +++ b/.gitignore @@ -15,5 +15,5 @@ xcuserdata .lsp .clj-kondo .idea -android/src/main/kotlin/org/bitcoindevkit/bdk.kt -jvm/src/main/kotlin/org/bitcoindevkit/bdk.kt +bdk-android/lib/src/main/kotlin/org/bitcoindevkit/bdk.kt +bdk-jvm/lib/src/main/kotlin/org/bitcoindevkit/bdk.kt diff --git a/api-docs/build.gradle.kts b/api-docs/build.gradle.kts index 9394b59..73fb6cf 100644 --- a/api-docs/build.gradle.kts +++ b/api-docs/build.gradle.kts @@ -29,7 +29,7 @@ tasks.withType().configureEach { moduleName.set("bdk-android") moduleVersion.set("0.9.0") includes.from("Module.md") - samples.from("src/main/kotlin/org/bitcoindevkit/Samples.kt") + samples.from("src/test/kotlin/org/bitcoindevkit/Samples.kt") } } } diff --git a/api-docs/src/main/kotlin/org/bitcoindevkit/Samples.kt b/api-docs/src/main/kotlin/org/bitcoindevkit/Samples.kt deleted file mode 100644 index 012d5cd..0000000 --- a/api-docs/src/main/kotlin/org/bitcoindevkit/Samples.kt +++ /dev/null @@ -1,13 +0,0 @@ -package org.bitcoindevkit - -class Samples { - val blockchainConfig: BlockchainConfig = BlockchainConfig.Electrum( - ElectrumConfig( - url = "ssl://electrum.blockstream.info:60002", - socks5 = null, - retry = 5u, - timeout = null, - stopGap = 10u - ) - ) -} diff --git a/api-docs/src/main/kotlin/org/bitcoindevkit/bdk.kt b/api-docs/src/main/kotlin/org/bitcoindevkit/bdk.kt index 8cf2cb9..12ce386 100644 --- a/api-docs/src/main/kotlin/org/bitcoindevkit/bdk.kt +++ b/api-docs/src/main/kotlin/org/bitcoindevkit/bdk.kt @@ -37,14 +37,40 @@ enum class AddressIndex { /** Return a new address after incrementing the current descriptor index. */ NEW, - /** - * Return the address for the current descriptor index if it has not been used in a received transaction. Otherwise return a new address as with `AddressIndex.NEW`. Use with caution, if the wallet has not yet detected an address has been used it could return an already used address. This function is primarily meant for situations where the caller is untrusted; for example when deriving donation addresses on-demand for a public web page. - */ + /** Return the address for the current descriptor index if it has not been used in a received transaction. Otherwise return a new address as with `AddressIndex.NEW`. Use with caution, if the wallet has not yet detected an address has been used it could return an already used address. This function is primarily meant for situations where the caller is untrusted; for example when deriving donation addresses on-demand for a public web page. */ LAST_UNUSED, } +/** + * Balance differentiated in various categories + * + * @sample org.bitcoindevkit.balanceSample + */ +data class Balance ( + /** All coinbase outputs not yet matured. */ + var immature: ULong, + + /** Unconfirmed UTXOs generated by a wallet tx. */ + var trustedPending: ULong, + + /** Unconfirmed UTXOs received from an external wallet. */ + var untrustedPending: ULong, + + /** Confirmed and immediately spendable balance. */ + var confirmed: ULong, + + /** The sum of trustedPending and confirmed coins. */ + var spendable: ULong, + + /** The whole balance visible to the wallet. */ + var total: ULong +) + /** * Type that can contain any of the database configurations defined by the library. + * + * @sample org.bitcoindevkit.memoryDatabaseConfigExample + * @sample org.bitcoindevkit.sqliteDatabaseConfigExample */ sealed class DatabaseConfig { /** Configuration for an in-memory database */ @@ -79,7 +105,7 @@ data class SledDbConfiguration( /** * Configuration for an Electrum blockchain. * - * @sample org.bitcoindevkit.Samples.blockchainConfig + * @sample org.bitcoindevkit.electrumBlockchainConfigExample */ data class ElectrumConfig ( /** URL of the Electrum server (such as ElectrumX, Esplora, BWT) may start with `ssl://` or `tcp://` and include a port, e.g. `ssl://electrum.blockstream.info:60002`. */ diff --git a/api-docs/src/test/kotlin/org/bitcoindevkit/Samples.kt b/api-docs/src/test/kotlin/org/bitcoindevkit/Samples.kt new file mode 100644 index 0000000..e2f5fd8 --- /dev/null +++ b/api-docs/src/test/kotlin/org/bitcoindevkit/Samples.kt @@ -0,0 +1,44 @@ +package org.bitcoindevkit + +fun balanceSample() { + object LogProgress : Progress { + override fun update(progress: Float, message: String?) {} + } + + val memoryDatabaseConfig = DatabaseConfig.Memory + private val blockchainConfig = BlockchainConfig.Electrum( + ElectrumConfig( + "ssl://electrum.blockstream.info:60002", + null, + 5u, + null, + 200u + ) + ) + val wallet = Wallet(descriptor, null, Network.TESTNET, memoryDatabaseConfig) + val blockchain = Blockchain(blockchainConfig) + wallet.sync(blockchain, LogProgress) + + val balance: Balance = wallet.getBalance() + println("Total wallet balance is ${balance.total}") +} + +fun electrumBlockchainConfigExample() { + val blockchainConfig = BlockchainConfig.Electrum( + ElectrumConfig( + "ssl://electrum.blockstream.info:60002", + null, + 5u, + null, + 200u + ) + ) +} + +fun memoryDatabaseConfigExample() { + val memoryDatabaseConfig = DatabaseConfig.Memory +} + +fun sqliteDatabaseConfigExample() { + val databaseConfig = DatabaseConfig.Sqlite(SqliteDbConfiguration("bdk-sqlite")) +}