11package to.bitkit.repositories
22
33import com.google.firebase.messaging.FirebaseMessaging
4+ import com.synonym.bitkitcore.FeeRates
45import com.synonym.bitkitcore.LightningInvoice
56import com.synonym.bitkitcore.Scanner
67import com.synonym.bitkitcore.createWithdrawCallbackUrl
@@ -87,7 +88,7 @@ class LightningRepo @Inject constructor(
8788 waitTimeout : Duration = 1.minutes,
8889 operation : suspend () -> Result <T >,
8990 ): Result <T > = withContext(bgDispatcher) {
90- Logger .debug (" Operation called: $operationName " , context = TAG )
91+ Logger .verbose (" Operation called: $operationName " , context = TAG )
9192
9293 if (_lightningState .value.nodeLifecycleState.isRunning()) {
9394 return @withContext executeOperation(operationName, operation)
@@ -106,7 +107,7 @@ class LightningRepo @Inject constructor(
106107 }
107108
108109 // Otherwise, wait for it to transition to running state
109- Logger .debug (" Waiting for node runs to execute $operationName " , context = TAG )
110+ Logger .verbose (" Waiting for node runs to execute $operationName " , context = TAG )
110111 _lightningState .first { it.nodeLifecycleState.isRunning() }
111112 Logger .debug(" Operation executed: $operationName " , context = TAG )
112113 true
@@ -480,28 +481,18 @@ class LightningRepo @Inject constructor(
480481 Result .success(paymentId)
481482 }
482483
483- /* *
484- * Sends bitcoin to an on-chain address
485- *
486- * @param address The bitcoin address to send to
487- * @param sats The amount in satoshis to send
488- * @param speed The desired transaction speed determining the fee rate. If null, the user's default speed is used.
489- * @param utxosToSpend Manually specify UTXO's to spend if not null.
490- * @return A `Result` with the `Txid` of sent transaction, or an error if the transaction fails
491- * or the fee rate cannot be retrieved.
492- */
493-
494484 suspend fun sendOnChain (
495485 address : Address ,
496486 sats : ULong ,
497487 speed : TransactionSpeed ? = null,
498488 utxosToSpend : List <SpendableUtxo >? = null,
489+ feeRates : FeeRates ? = null,
499490 isTransfer : Boolean = false,
500491 channelId : String? = null,
501492 ): Result <Txid > =
502493 executeWhenNodeRunning(" Send on-chain" ) {
503494 val transactionSpeed = speed ? : settingsStore.data.first().defaultTransactionSpeed
504- val satsPerVByte = getFeeRateForSpeed(transactionSpeed).getOrThrow().toUInt()
495+ val satsPerVByte = getFeeRateForSpeed(transactionSpeed, feeRates ).getOrThrow().toUInt()
505496
506497 // if utxos are manually specified, use them, otherwise run auto coin select if enabled
507498 val finalUtxosToSpend = utxosToSpend ? : determineUtxosToSpend(
@@ -531,33 +522,35 @@ class LightningRepo @Inject constructor(
531522 Result .success(txId)
532523 }
533524
534- private suspend fun determineUtxosToSpend (
525+ suspend fun determineUtxosToSpend (
535526 sats : ULong ,
536527 satsPerVByte : UInt ,
537- ): List <SpendableUtxo >? {
538- return runCatching {
528+ ): List <SpendableUtxo >? = withContext(bgDispatcher) {
529+ return @withContext runCatching {
539530 val settings = settingsStore.data.first()
540531 if (settings.coinSelectAuto) {
541532 val coinSelectionPreference = settings.coinSelectPreference
542533
543534 val allSpendableUtxos = lightningService.listSpendableOutputs().getOrThrow()
544535
545536 if (coinSelectionPreference == CoinSelectionPreference .Consolidate ) {
546- Logger .info (" Consolidating by spending all ${allSpendableUtxos.size} UTXOs" , context = TAG )
547- return allSpendableUtxos
537+ Logger .debug (" Consolidating by spending all ${allSpendableUtxos.size} UTXOs" , context = TAG )
538+ return @withContext allSpendableUtxos
548539 }
549540
550541 val coinSelectionAlgorithm = coinSelectionPreference.toCoinSelectAlgorithm().getOrThrow()
551542
552- Logger .info (" Selecting UTXOs with algorithm: $coinSelectionAlgorithm for sats: $sats " , context = TAG )
553- Logger .debug (" All spendable UTXOs: $allSpendableUtxos " , context = TAG )
543+ Logger .debug (" Selecting UTXOs with algorithm: $coinSelectionAlgorithm for sats: $sats " , context = TAG )
544+ Logger .verbose (" All spendable UTXOs: $allSpendableUtxos " , context = TAG )
554545
555546 lightningService.selectUtxosWithAlgorithm(
556547 targetAmountSats = sats,
557548 algorithm = coinSelectionAlgorithm,
558549 satsPerVByte = satsPerVByte,
559550 utxos = allSpendableUtxos,
560- ).getOrThrow()
551+ ).onSuccess {
552+ Logger .debug(" Selected ${it.size} UTXOs" , context = TAG )
553+ }.getOrThrow()
561554 } else {
562555 null // let ldk-node handle utxos
563556 }
@@ -579,10 +572,11 @@ class LightningRepo @Inject constructor(
579572 address : Address ? = null,
580573 speed : TransactionSpeed ? = null,
581574 utxosToSpend : List <SpendableUtxo >? = null,
575+ feeRates : FeeRates ? = null,
582576 ): Result <ULong > = withContext(bgDispatcher) {
583577 return @withContext try {
584578 val transactionSpeed = speed ? : settingsStore.data.first().defaultTransactionSpeed
585- val satsPerVByte = getFeeRateForSpeed(transactionSpeed).getOrThrow().toUInt()
579+ val satsPerVByte = getFeeRateForSpeed(transactionSpeed, feeRates ).getOrThrow().toUInt()
586580
587581 val addressOrDefault = address ? : cacheStore.data.first().onchainAddress
588582
@@ -600,9 +594,12 @@ class LightningRepo @Inject constructor(
600594 }
601595 }
602596
603- suspend fun getFeeRateForSpeed (speed : TransactionSpeed ): Result <ULong > = withContext(bgDispatcher) {
597+ suspend fun getFeeRateForSpeed (
598+ speed : TransactionSpeed ,
599+ feeRates : FeeRates ? = null,
600+ ): Result <ULong > = withContext(bgDispatcher) {
604601 return @withContext runCatching {
605- val fees = coreService.blocktank.getFees().getOrThrow()
602+ val fees = feeRates ? : coreService.blocktank.getFees().getOrThrow()
606603 val satsPerVByte = fees.getSatsPerVByteFor(speed)
607604 satsPerVByte.toULong()
608605 }.onFailure { e ->
0 commit comments