Skip to content

Commit 00a8108

Browse files
committed
refactor: extract waitForUsableChannels logic from canSend
1 parent 385b655 commit 00a8108

2 files changed

Lines changed: 7 additions & 17 deletions

File tree

app/src/main/java/to/bitkit/repositories/LightningRepo.kt

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ class LightningRepo @Inject constructor(
984984
}
985985
}
986986

987-
private suspend fun waitForUsableChannels() {
987+
suspend fun waitForUsableChannels() {
988988
if (lightningService.channels?.any { it.isUsable } == true) return
989989

990990
Logger.info("Waiting for usable channels before sending payment", context = TAG)
@@ -1214,23 +1214,10 @@ class LightningRepo @Inject constructor(
12141214
}
12151215
}
12161216

1217-
suspend fun canSend(amountSats: ULong) = withContext(bgDispatcher) {
1217+
fun canSend(amountSats: ULong): Boolean {
12181218
val state = _lightningState.value
1219-
if (!state.nodeLifecycleState.canRun()) return@withContext false
1220-
1221-
if (state.channels.none { it.isUsable }) {
1222-
syncState()
1223-
val ready = withTimeoutOrNull(CHANNELS_USABLE_TIMEOUT_MS) {
1224-
_lightningState.first { it.channels.any { ch -> ch.isUsable } }
1225-
}
1226-
if (ready == null) {
1227-
Logger.warn("Timeout waiting for usable channels in canSend", context = TAG)
1228-
return@withContext false
1229-
}
1230-
}
1231-
1232-
return@withContext _lightningState.value.channels
1233-
.totalNextOutboundHtlcLimitSats() >= amountSats
1219+
if (!state.nodeLifecycleState.canRun()) return false
1220+
return state.channels.totalNextOutboundHtlcLimitSats() >= amountSats
12341221
}
12351222

12361223
fun getNodeId(): String? =

app/src/main/java/to/bitkit/viewmodels/AppViewModel.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ class AppViewModel @Inject constructor(
984984
)
985985
return@takeIf false
986986
}
987+
lightningRepo.waitForUsableChannels()
987988
val canSend = lightningRepo.canSend(lnInv.amountSatoshis.coerceAtLeast(1u))
988989
if (!canSend) {
989990
val nodeState = lightningRepo.lightningState.value.nodeLifecycleState
@@ -1426,6 +1427,7 @@ class AppViewModel @Inject constructor(
14261427
val quickPayHandled = handleQuickPayIfApplicable(amountSats = invoice.amountSatoshis, invoice = invoice)
14271428
if (quickPayHandled) return
14281429

1430+
lightningRepo.waitForUsableChannels()
14291431
if (!lightningRepo.canSend(invoice.amountSatoshis)) {
14301432
val maxSendLightning = walletRepo.balanceState.value.maxSendLightningSats
14311433
val shortfall = invoice.amountSatoshis.safe() - maxSendLightning.safe()
@@ -1474,6 +1476,7 @@ class AppViewModel @Inject constructor(
14741476
val isFixed = data.isFixedAmount()
14751477
val displaySats = data.minSendableSat()
14761478

1479+
lightningRepo.waitForUsableChannels()
14771480
if (!lightningRepo.canSend(displaySats.coerceAtLeast(1u))) {
14781481
toast(
14791482
type = Toast.ToastType.WARNING,

0 commit comments

Comments
 (0)