Add Wallet.listTransactions()

This commit is contained in:
Steve Myers
2021-07-04 22:10:16 -07:00
parent cd813a14b1
commit b437b78668
13 changed files with 384 additions and 74 deletions

View File

@@ -16,10 +16,10 @@ interface LibJna : Library {
class ByReference : FfiResult_char_ptr_t(), Structure.ByReference
@JvmField
var ok: String = ""
var ok: String? = null
@JvmField
var err: Short = 0
var err: Short? = null
override fun getFieldOrder() = listOf("ok", "err")
}
@@ -38,7 +38,7 @@ interface LibJna : Library {
class ByReference : FfiResultVoid_t(), Structure.ByReference
@JvmField
var err: Short = 0
var err: Short? = null
override fun getFieldOrder() = listOf("err")
}
@@ -97,10 +97,10 @@ interface LibJna : Library {
class ByReference : FfiResult_OpaqueWallet_ptr_t(), Structure.ByReference
@JvmField
var ok: OpaqueWallet_t = OpaqueWallet_t()
var ok: OpaqueWallet_t? = null
@JvmField
var err: Short = 0
var err: Short? = null
override fun getFieldOrder() = listOf("ok", "err")
}
@@ -224,10 +224,10 @@ interface LibJna : Library {
class ByReference : FfiResultVec_LocalUtxo_t(), Structure.ByReference
@JvmField
var ok: Vec_LocalUtxo_t = Vec_LocalUtxo_t()
var ok: Vec_LocalUtxo_t? = null
@JvmField
var err: Short = 0
var err: Short? = null
override fun getFieldOrder() = listOf("ok", "err")
}
@@ -249,10 +249,10 @@ interface LibJna : Library {
class ByReference : FfiResult_uint64_t(), Structure.ByReference
@JvmField
var ok: Long = Long.MIN_VALUE
var ok: Long? = null
@JvmField
var err: Short = 0
var err: Short? = null
override fun getFieldOrder() = listOf("ok", "err")
}
@@ -288,4 +288,101 @@ interface LibJna : Library {
// void free_database_config (
// DatabaseConfig_t * database_config);
fun free_database_config(database_config: DatabaseConfig_t)
// typedef struct {
//
// char * txid;
//
// uint64_t timestamp;
//
// uint64_t received;
//
// uint64_t sent;
//
// uint64_t fees;
//
// int32_t height;
//
// } TransactionDetails_t;
open class TransactionDetails_t : Structure() {
class ByValue : TransactionDetails_t(), Structure.ByValue
class ByReference : TransactionDetails_t(), Structure.ByReference
@JvmField
var txid: String? = null
@JvmField
var timestamp: Long? = null
@JvmField
var received: Long? = null
@JvmField
var sent: Long? = null
@JvmField
var fees: Long? = null
@JvmField
var height: Int? = null
override fun getFieldOrder() = listOf("txid", "timestamp", "received", "sent", "fees", "height")
}
// typedef struct {
//
// TransactionDetails_t * ptr;
//
// size_t len;
//
// size_t cap;
//
// } Vec_TransactionDetails_t;
open class Vec_TransactionDetails_t : Structure() {
class ByReference : Vec_TransactionDetails_t(), Structure.ByReference
class ByValue : Vec_TransactionDetails_t(), Structure.ByValue
@JvmField
var ptr: TransactionDetails_t.ByReference? = null
@JvmField
var len: NativeLong? = null
@JvmField
var cap: NativeLong? = null
override fun getFieldOrder() = listOf("ptr", "len", "cap")
}
// typedef struct {
//
// Vec_TransactionDetails_t ok;
//
// FfiError_t err;
//
// } FfiResult_Vec_TransactionDetails_t;
open class FfiResult_Vec_TransactionDetails_t : Structure() {
class ByValue : FfiResult_Vec_TransactionDetails_t(), Structure.ByValue
class ByReference : FfiResult_Vec_TransactionDetails_t(), Structure.ByReference
@JvmField
var ok: Vec_TransactionDetails_t? = null
@JvmField
var err: Short? = null
override fun getFieldOrder() = listOf("ok", "err")
}
// FfiResult_Vec_TransactionDetails_t list_transactions (
// OpaqueWallet_t const * opaque_wallet);
fun list_transactions(opaque_wallet: OpaqueWallet_t): FfiResult_Vec_TransactionDetails_t.ByValue
// void free_vectxdetails_result (
// FfiResult_Vec_TransactionDetails_t txdetails_result);
fun free_vectxdetails_result(txdetails_result: FfiResult_Vec_TransactionDetails_t.ByValue)
}

View File

@@ -12,8 +12,8 @@ class StringResult constructor(private val ffiResultCharPtrT: LibJna.FfiResult_c
private val log: Logger = LoggerFactory.getLogger(StringResult::class.java)
fun value(): String {
val err = ffiResultCharPtrT.err
val ok = ffiResultCharPtrT.ok
val err = ffiResultCharPtrT.err!!
val ok = ffiResultCharPtrT.ok!!
when {
err > 0 -> {
throw FfiException(err)

View File

@@ -12,8 +12,8 @@ class UInt64Result constructor(private val ffiResultUint64T: LibJna.FfiResult_ui
private val log: Logger = LoggerFactory.getLogger(UInt64Result::class.java)
fun value(): Long {
val err = ffiResultUint64T.err
val ok = ffiResultUint64T.ok
val err = ffiResultUint64T.err!!
val ok = ffiResultUint64T.ok!!
when {
err > 0 -> {
throw FfiException(err)

View File

@@ -12,7 +12,7 @@ class VoidResult constructor(private val ffiResultVoidT: LibJna.FfiResultVoid_t.
private val log: Logger = LoggerFactory.getLogger(VoidResult::class.java)
fun value(): Unit {
val err = ffiResultVoidT.err
val err = ffiResultVoidT.err!!
when {
err > 0 -> {

View File

@@ -12,8 +12,8 @@ class VecLocalUtxoResult(private val ffiResultVecLocalUtxoT: LibJna.FfiResultVec
private val log: Logger = LoggerFactory.getLogger(VecLocalUtxoResult::class.java)
fun value(): Array<LibJna.LocalUtxo_t.ByReference> {
val err = ffiResultVecLocalUtxoT.err
val ok = ffiResultVecLocalUtxoT.ok
val err = ffiResultVecLocalUtxoT.err!!
val ok = ffiResultVecLocalUtxoT.ok!!
when {
err > 0 -> {
throw FfiException(err)

View File

@@ -0,0 +1,32 @@
package org.bitcoindevkit.bdk.wallet
import org.bitcoindevkit.bdk.FfiException
import org.bitcoindevkit.bdk.LibBase
import org.bitcoindevkit.bdk.LibJna
import org.slf4j.Logger
import org.slf4j.LoggerFactory
class VecTxDetailsResult(private val ffiResultVecTransactionDetailsT: LibJna.FfiResult_Vec_TransactionDetails_t.ByValue) :
LibBase() {
private val log: Logger = LoggerFactory.getLogger(VecTxDetailsResult::class.java)
fun value(): Array<LibJna.TransactionDetails_t.ByReference> {
val err = ffiResultVecTransactionDetailsT.err!!
val ok = ffiResultVecTransactionDetailsT.ok!!
when {
err > 0 -> {
throw FfiException(err)
}
else -> {
val first = ok.ptr!!
return first.toArray(ok.len!!.toInt()) as Array<LibJna.TransactionDetails_t.ByReference>
}
}
}
protected fun finalize() {
libJna.free_vectxdetails_result(ffiResultVecTransactionDetailsT)
log.debug("$ffiResultVecTransactionDetailsT freed")
}
}

View File

@@ -48,4 +48,9 @@ class Wallet constructor(
val longResult = UInt64Result(libJna.balance(wallet))
return longResult.value()
}
fun listTransactionDetails(): Array<LibJna.TransactionDetails_t.ByReference> {
val vecTxDetailsResult = VecTxDetailsResult(libJna.list_transactions((wallet)))
return vecTxDetailsResult.value()
}
}

View File

@@ -12,14 +12,14 @@ class WalletResult constructor(private val ffiResultOpaqueWalletPtrT: LibJna.Ffi
private val log: Logger = LoggerFactory.getLogger(WalletResult::class.java)
fun value(): LibJna.OpaqueWallet_t {
val err = ffiResultOpaqueWalletPtrT.err
val err = ffiResultOpaqueWalletPtrT.err!!
val ok = ffiResultOpaqueWalletPtrT.ok
when {
err > 0 -> {
throw FfiException(err)
}
else -> {
return ok
return ok!!
}
}
}

View File

@@ -105,4 +105,26 @@ abstract class LibTest : LibBase() {
//log.debug("balance from kotlin: $balance")
assertTrue(balance > 0)
}
@Test
fun walletTxDetails() {
val wallet = Wallet(desc, change, blockchainConfig, databaseConfig)
wallet.sync()
val txDetails = wallet.listTransactionDetails()
assertTrue(txDetails.isNotEmpty())
txDetails.iterator().forEach {
//log.debug("txDetails.txid: ${it.txid}")
assertNotNull(it.txid)
//log.debug("txDetails.timestamp: ${it.timestamp}")
assertTrue(it.timestamp!! > 0)
//log.debug("txDetails.received: ${it.received}")
//log.debug("txDetails.sent: ${it.sent}")
assertTrue(it.received!! > 0 || it.sent!! > 0)
//log.debug("txDetails.fees: ${it.fees}")
assertTrue(it.fees!! > 0)
//log.debug("txDetails.fees: ${it.height}")
assertTrue(it.height!! >= -1)
}
}
}