Add Wallet.balance()
This commit is contained in:
@@ -232,10 +232,35 @@ interface LibJna : Library {
|
||||
override fun getFieldOrder() = listOf("ok", "err")
|
||||
}
|
||||
|
||||
// void free_unspent_result (
|
||||
// void free_veclocalutxo_result (
|
||||
// FfiResult_Vec_LocalUtxo_t unspent_result);
|
||||
fun free_unspent_result(unspent_result: FfiResultVec_LocalUtxo_t.ByValue)
|
||||
fun free_veclocalutxo_result(unspent_result: FfiResultVec_LocalUtxo_t.ByValue)
|
||||
|
||||
// typedef struct {
|
||||
//
|
||||
// uint64_t ok;
|
||||
//
|
||||
// FfiError_t err;
|
||||
//
|
||||
// } FfiResult_uint64_t;
|
||||
open class FfiResult_uint64_t : Structure() {
|
||||
|
||||
class ByValue : FfiResult_uint64_t(), Structure.ByValue
|
||||
class ByReference : FfiResult_uint64_t(), Structure.ByReference
|
||||
|
||||
@JvmField
|
||||
var ok: Long = Long.MIN_VALUE
|
||||
|
||||
@JvmField
|
||||
var err: Short = 0
|
||||
|
||||
override fun getFieldOrder() = listOf("ok", "err")
|
||||
}
|
||||
|
||||
// void free_uint64_result (
|
||||
// FfiResult_uint64_t void_result);
|
||||
fun free_uint64_result(unspent_result: FfiResult_uint64_t.ByValue)
|
||||
|
||||
// FfiResultVoid_t sync_wallet (
|
||||
// OpaqueWallet_t const * opaque_wallet);
|
||||
fun sync_wallet(opaque_wallet: OpaqueWallet_t): FfiResultVoid_t.ByValue
|
||||
@@ -248,6 +273,10 @@ interface LibJna : Library {
|
||||
// OpaqueWallet_t const * opaque_wallet);
|
||||
fun list_unspent(opaque_wallet: OpaqueWallet_t): FfiResultVec_LocalUtxo_t.ByValue
|
||||
|
||||
// FfiResult_uint64_t balance (
|
||||
// OpaqueWallet_t const * opaque_wallet);
|
||||
fun balance(opaque_wallet: OpaqueWallet_t): FfiResult_uint64_t.ByValue
|
||||
|
||||
// DatabaseConfig_t * new_memory_config (void);
|
||||
fun new_memory_config(): DatabaseConfig_t
|
||||
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.bitcoindevkit.bdk.types
|
||||
|
||||
import org.bitcoindevkit.bdk.FfiException
|
||||
import org.bitcoindevkit.bdk.LibBase
|
||||
import org.bitcoindevkit.bdk.LibJna
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
class UInt64Result constructor(private val ffiResultUint64T: LibJna.FfiResult_uint64_t.ByValue) :
|
||||
LibBase() {
|
||||
|
||||
private val log: Logger = LoggerFactory.getLogger(UInt64Result::class.java)
|
||||
|
||||
fun value(): Long {
|
||||
val err = ffiResultUint64T.err
|
||||
val ok = ffiResultUint64T.ok
|
||||
when {
|
||||
err > 0 -> {
|
||||
throw FfiException(err)
|
||||
}
|
||||
else -> {
|
||||
return ok
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected fun finalize() {
|
||||
libJna.free_uint64_result(ffiResultUint64T)
|
||||
log.debug("$ffiResultUint64T freed")
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ class VecLocalUtxoResult(private val ffiResultVecLocalUtxoT: LibJna.FfiResultVec
|
||||
}
|
||||
|
||||
protected fun finalize() {
|
||||
libJna.free_unspent_result(ffiResultVecLocalUtxoT)
|
||||
libJna.free_veclocalutxo_result(ffiResultVecLocalUtxoT)
|
||||
log.debug("$ffiResultVecLocalUtxoT freed")
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import org.bitcoindevkit.bdk.DatabaseConfig
|
||||
import org.bitcoindevkit.bdk.LibBase
|
||||
import org.bitcoindevkit.bdk.LibJna
|
||||
import org.bitcoindevkit.bdk.types.StringResult
|
||||
import org.bitcoindevkit.bdk.types.UInt64Result
|
||||
import org.bitcoindevkit.bdk.types.VoidResult
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
@@ -42,4 +43,9 @@ class Wallet constructor(
|
||||
val vecLocalUtxoResult = VecLocalUtxoResult(libJna.list_unspent(wallet))
|
||||
return vecLocalUtxoResult.value()
|
||||
}
|
||||
|
||||
fun balance(): Long {
|
||||
val longResult = UInt64Result(libJna.balance(wallet))
|
||||
return longResult.value()
|
||||
}
|
||||
}
|
||||
@@ -96,4 +96,13 @@ abstract class LibTest : LibBase() {
|
||||
assertTrue(it.keychain!! >= 0)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun walletBalance() {
|
||||
val wallet = Wallet(desc, change, blockchainConfig, databaseConfig)
|
||||
wallet.sync()
|
||||
val balance = wallet.balance()
|
||||
//log.debug("balance from kotlin: $balance")
|
||||
assertTrue(balance > 0)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user