Skip to content

Commit fc3d19e

Browse files
committed
fix(receive): dont request liquidity for 0 LN channels
1 parent 75ba763 commit fc3d19e

File tree

2 files changed

+20
-25
lines changed

2 files changed

+20
-25
lines changed

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

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ import kotlinx.coroutines.withContext
1717
import kotlinx.datetime.Clock
1818
import org.lightningdevkit.ldknode.BalanceDetails
1919
import org.lightningdevkit.ldknode.Event
20-
import org.lightningdevkit.ldknode.Txid
2120
import to.bitkit.data.AppDb
2221
import to.bitkit.data.CacheStore
2322
import to.bitkit.data.SettingsStore
2423
import to.bitkit.data.entities.TagMetadataEntity
2524
import to.bitkit.data.keychain.Keychain
2625
import to.bitkit.di.BgDispatcher
2726
import to.bitkit.env.Env
27+
import to.bitkit.ext.filterOpen
2828
import to.bitkit.ext.nowTimestamp
2929
import to.bitkit.ext.toHex
3030
import to.bitkit.ext.totalNextOutboundHtlcLimitSats
@@ -422,6 +422,8 @@ class WalletRepo @Inject constructor(
422422
if (coreService.checkGeoStatus() == true) return@withContext Result.success(false)
423423

424424
val channels = lightningRepo.lightningState.value.channels
425+
if (channels.filterOpen().isEmpty()) return@withContext Result.success(false)
426+
425427
val inboundBalanceSats = channels.sumOf { it.inboundCapacityMsat / 1000u }
426428

427429
Result.success((_walletState.value.bip21AmountSats ?: 0uL) >= inboundBalanceSats)
@@ -461,27 +463,6 @@ class WalletRepo @Inject constructor(
461463
}
462464
}
463465

464-
suspend fun searchInvoiceByPaymentHash(paymentHash: String): Result<TagMetadataEntity> = withContext(bgDispatcher) {
465-
return@withContext try {
466-
val invoiceTag =
467-
db.tagMetadataDao().searchByPaymentHash(paymentHash = paymentHash) ?: return@withContext Result.failure(
468-
Exception("Invoice not found")
469-
)
470-
Result.success(invoiceTag)
471-
} catch (e: Throwable) {
472-
Logger.error("searchInvoice error", e, context = TAG)
473-
Result.failure(e)
474-
}
475-
}
476-
477-
suspend fun deleteInvoice(txId: Txid) = withContext(bgDispatcher) {
478-
try {
479-
db.tagMetadataDao().deleteByPaymentHash(paymentHash = txId)
480-
} catch (e: Throwable) {
481-
Logger.error("deleteInvoice error", e, context = TAG)
482-
}
483-
}
484-
485466
suspend fun deleteAllInvoices() = withContext(bgDispatcher) {
486467
try {
487468
db.tagMetadataDao().deleteAll()

app/src/test/java/to/bitkit/repositories/WalletRepoTest.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,14 +402,16 @@ class WalletRepoTest : BaseUnitTest() {
402402
whenever(coreService.checkGeoStatus()).thenReturn(false)
403403
val testChannels = listOf(
404404
mock<ChannelDetails> {
405-
on { inboundCapacityMsat } doReturn 500_000u // 500 sats
405+
on { inboundCapacityMsat } doReturn 500_000u
406+
on { isChannelReady } doReturn true
406407
},
407408
mock<ChannelDetails> {
408-
on { inboundCapacityMsat } doReturn 300_000u // 300 sats
409+
on { inboundCapacityMsat } doReturn 300_000u
410+
on { isChannelReady } doReturn true
409411
}
410412
)
411413
whenever(lightningRepo.lightningState).thenReturn(MutableStateFlow(LightningState(channels = testChannels)))
412-
sut.updateBip21Invoice(amountSats = 1000uL) // 1000 sats
414+
sut.updateBip21Invoice(amountSats = 1000uL)
413415

414416
// When
415417
val result = sut.shouldRequestAdditionalLiquidity()
@@ -419,6 +421,18 @@ class WalletRepoTest : BaseUnitTest() {
419421
assertTrue(result.getOrThrow())
420422
}
421423

424+
@Test
425+
fun `should not request additional liquidity for 0 channels`() = test {
426+
whenever(coreService.checkGeoStatus()).thenReturn(false)
427+
whenever(lightningRepo.lightningState).thenReturn(MutableStateFlow(LightningState()))
428+
sut.updateBip21Invoice(amountSats = 1000uL)
429+
430+
val result = sut.shouldRequestAdditionalLiquidity()
431+
432+
assertTrue(result.isSuccess)
433+
assertFalse(result.getOrThrow())
434+
}
435+
422436
@Test
423437
fun `shouldRequestAdditionalLiquidity should return false when amount is less than inbound capacity`() = test {
424438
// Given

0 commit comments

Comments
 (0)