Skip to content

Commit

Permalink
Update Kotlin client
Browse files Browse the repository at this point in the history
  • Loading branch information
ssantos21 committed May 22, 2024
1 parent 815a904 commit 1b3e08d
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 9 deletions.
19 changes: 12 additions & 7 deletions clients/kotlin/src/main/kotlin/BroadcastBackupTransaction.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ class BroadcastBackupTransaction: CliktCommand(help = "Broadcast a backup transa

val backupTxs = appContext.sqliteManager.getBackupTxs(statechainId)

val backupTx = if (backupTxs.isEmpty()) null else backupTxs.maxByOrNull { it.txN }

if (backupTx == null) {
throw Exception("There is no backup transaction for the statechain id $statechainId")
}
// val backupTx = if (backupTxs.isEmpty()) null else backupTxs.maxByOrNull { it.txN }

val coinsWithStatechainId = wallet.coins.filter { it.statechainId == statechainId }

Expand All @@ -49,8 +45,17 @@ class BroadcastBackupTransaction: CliktCommand(help = "Broadcast a backup transa

val coin = coinsWithStatechainId.sortedBy { it.locktime }.first()

if (coin.status != CoinStatus.CONFIRMED) {
throw Exception("Coin status must be CONFIRMED to broadcast the backup transaction. The current status is ${coin.status}")
if (coin.status != CoinStatus.CONFIRMED && coin.status != CoinStatus.IN_TRANSFER) {
throw Exception("Coin status must be CONFIRMED or IN_TRANSFER to transfer it. The current status is ${coin.status}");
}

var backupTx: BackupTx? = null

try {
backupTx = latestBackupTxPaysToUserPubkey(backupTxs, coin, wallet.network)
} catch (e: Exception) {
println("Error: ${e.message}")
return
}

var feeRateSatsPerByte = feeRate
Expand Down
1 change: 1 addition & 0 deletions clients/kotlin/src/main/kotlin/ListStatecoins.kt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ListStatecoins: CliktCommand(help = "List all wallet' statecoins") {
put("status", coin.status.toString())
put("deposit_address", coin.aggregatedAddress ?: "Empty")
put("statechain_address", coin.address ?: "Empty")
put("locktime", coin.locktime.toString())
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions clients/kotlin/src/main/kotlin/Withdraw.kt
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ class Withdraw: CliktCommand(help = "Withdraw funds from a statecoin to a BTC ad
throw Exception("coin.amount is None")
}

if (coin.status != CoinStatus.CONFIRMED) {
throw Exception("Coin status must be CONFIRMED to withdraw it. The current status is ${coin.status}")
if (coin.status != CoinStatus.CONFIRMED && coin.status != CoinStatus.IN_TRANSFER) {
throw Exception("Coin status must be CONFIRMED or IN_TRANSFER to transfer it. The current status is ${coin.status}");
}

val signedTx = Transaction.create(
Expand Down
30 changes: 30 additions & 0 deletions clients/kotlin/src/main/kotlin/mercurylib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,13 @@ internal interface UniffiLib : Library {
uniffi_out_err: UniffiRustCallStatus,
): Byte

fun uniffi_mercurylib_fn_func_latest_backup_tx_pays_to_user_pubkey(
`backupTxs`: RustBuffer.ByValue,
`coin`: RustBuffer.ByValue,
`network`: RustBuffer.ByValue,
uniffi_out_err: UniffiRustCallStatus,
): RustBuffer.ByValue

fun uniffi_mercurylib_fn_func_new_backup_transaction(
`encodedUnsignedTx`: RustBuffer.ByValue,
`signatureHex`: RustBuffer.ByValue,
Expand Down Expand Up @@ -1182,6 +1189,8 @@ internal interface UniffiLib : Library {

fun uniffi_mercurylib_checksum_func_is_enclave_pubkey_part_of_coin(): Short

fun uniffi_mercurylib_checksum_func_latest_backup_tx_pays_to_user_pubkey(): Short

fun uniffi_mercurylib_checksum_func_new_backup_transaction(): Short

fun uniffi_mercurylib_checksum_func_sign_message(): Short
Expand Down Expand Up @@ -1276,6 +1285,9 @@ private fun uniffiCheckApiChecksums(lib: UniffiLib) {
if (lib.uniffi_mercurylib_checksum_func_is_enclave_pubkey_part_of_coin() != 37041.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_mercurylib_checksum_func_latest_backup_tx_pays_to_user_pubkey() != 19689.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_mercurylib_checksum_func_new_backup_transaction() != 56642.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
Expand Down Expand Up @@ -4394,6 +4406,24 @@ fun `isEnclavePubkeyPartOfCoin`(
)
}

@Throws(MercuryException::class)
fun `latestBackupTxPaysToUserPubkey`(
`backupTxs`: List<BackupTx>,
`coin`: Coin,
`network`: kotlin.String,
): BackupTx {
return FfiConverterTypeBackupTx.lift(
uniffiRustCallWithError(MercuryException) { _status ->
UniffiLib.INSTANCE.uniffi_mercurylib_fn_func_latest_backup_tx_pays_to_user_pubkey(
FfiConverterSequenceTypeBackupTx.lower(`backupTxs`),
FfiConverterTypeCoin.lower(`coin`),
FfiConverterString.lower(`network`),
_status,
)
},
)
}

@Throws(MercuryException::class)
fun `newBackupTransaction`(
`encodedUnsignedTx`: kotlin.String,
Expand Down
30 changes: 30 additions & 0 deletions lib/out-kotlin/com/mercurylayer/mercurylib.kt
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,13 @@ internal interface UniffiLib : Library {
uniffi_out_err: UniffiRustCallStatus,
): Byte

fun uniffi_mercurylib_fn_func_latest_backup_tx_pays_to_user_pubkey(
`backupTxs`: RustBuffer.ByValue,
`coin`: RustBuffer.ByValue,
`network`: RustBuffer.ByValue,
uniffi_out_err: UniffiRustCallStatus,
): RustBuffer.ByValue

fun uniffi_mercurylib_fn_func_new_backup_transaction(
`encodedUnsignedTx`: RustBuffer.ByValue,
`signatureHex`: RustBuffer.ByValue,
Expand Down Expand Up @@ -1182,6 +1189,8 @@ internal interface UniffiLib : Library {

fun uniffi_mercurylib_checksum_func_is_enclave_pubkey_part_of_coin(): Short

fun uniffi_mercurylib_checksum_func_latest_backup_tx_pays_to_user_pubkey(): Short

fun uniffi_mercurylib_checksum_func_new_backup_transaction(): Short

fun uniffi_mercurylib_checksum_func_sign_message(): Short
Expand Down Expand Up @@ -1276,6 +1285,9 @@ private fun uniffiCheckApiChecksums(lib: UniffiLib) {
if (lib.uniffi_mercurylib_checksum_func_is_enclave_pubkey_part_of_coin() != 37041.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_mercurylib_checksum_func_latest_backup_tx_pays_to_user_pubkey() != 19689.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
if (lib.uniffi_mercurylib_checksum_func_new_backup_transaction() != 56642.toShort()) {
throw RuntimeException("UniFFI API checksum mismatch: try cleaning and rebuilding your project")
}
Expand Down Expand Up @@ -4394,6 +4406,24 @@ fun `isEnclavePubkeyPartOfCoin`(
)
}

@Throws(MercuryException::class)
fun `latestBackupTxPaysToUserPubkey`(
`backupTxs`: List<BackupTx>,
`coin`: Coin,
`network`: kotlin.String,
): BackupTx {
return FfiConverterTypeBackupTx.lift(
uniffiRustCallWithError(MercuryException) { _status ->
UniffiLib.INSTANCE.uniffi_mercurylib_fn_func_latest_backup_tx_pays_to_user_pubkey(
FfiConverterSequenceTypeBackupTx.lower(`backupTxs`),
FfiConverterTypeCoin.lower(`coin`),
FfiConverterString.lower(`network`),
_status,
)
},
)
}

@Throws(MercuryException::class)
fun `newBackupTransaction`(
`encodedUnsignedTx`: kotlin.String,
Expand Down

0 comments on commit 1b3e08d

Please sign in to comment.