Skip to content

Commit 25b823b

Browse files
committed
fix: refresh paykit on channel events
1 parent 2920a97 commit 25b823b

2 files changed

Lines changed: 51 additions & 2 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ class AppViewModel @Inject constructor(
460460
private suspend fun handleChannelReady(event: Event.ChannelReady) {
461461
transferRepo.syncTransferStates()
462462
walletRepo.syncBalances()
463+
refreshPublicPaykitEndpointsIfEnabled()
463464
notifyChannelReady(event)
464465
}
465466

@@ -476,6 +477,7 @@ class AppViewModel @Inject constructor(
476477
}
477478
transferRepo.syncTransferStates()
478479
walletRepo.syncBalances()
480+
refreshPublicPaykitEndpointsIfEnabled()
479481
}
480482

481483
private suspend fun createTransferForCounterpartyClose(channelId: String, isForceClose: Boolean) {

app/src/test/java/to/bitkit/viewmodels/AppViewModelSendFlowTest.kt

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import org.junit.Test
1212
import org.lightningdevkit.ldknode.Event
1313
import org.mockito.kotlin.any
1414
import org.mockito.kotlin.anyOrNull
15+
import org.mockito.kotlin.clearInvocations
1516
import org.mockito.kotlin.mock
1617
import org.mockito.kotlin.verify
1718
import org.mockito.kotlin.whenever
@@ -83,6 +84,8 @@ class AppViewModelSendFlowTest : BaseUnitTest() {
8384
private val formatMoneyValue = mock<FormatMoneyValue>()
8485

8586
private val balanceState = MutableStateFlow(BalanceState())
87+
private val settingsData = MutableStateFlow(SettingsData())
88+
private val walletState = MutableStateFlow(WalletState())
8689
private val nodeEvents = MutableSharedFlow<Event>()
8790

8891
private val timedSheetManager = mock<TimedSheetManager>()
@@ -100,9 +103,9 @@ class AppViewModelSendFlowTest : BaseUnitTest() {
100103
whenever(lightningRepo.lightningState).thenReturn(MutableStateFlow(LightningState()))
101104
whenever(lightningRepo.nodeEvents).thenReturn(nodeEvents)
102105
whenever(walletRepo.balanceState).thenReturn(balanceState)
103-
whenever(walletRepo.walletState).thenReturn(MutableStateFlow(WalletState()))
106+
whenever(walletRepo.walletState).thenReturn(walletState)
104107
whenever(walletRepo.walletExists()).thenReturn(true)
105-
whenever(settingsStore.data).thenReturn(flowOf(SettingsData()))
108+
whenever(settingsStore.data).thenReturn(settingsData)
106109
whenever(cacheStore.data).thenReturn(flowOf(AppCacheData()))
107110
whenever(transferRepo.activeTransfers).thenReturn(flowOf(emptyList()))
108111
whenever(timedSheetManager.currentSheet).thenReturn(MutableStateFlow(null))
@@ -310,6 +313,44 @@ class AppViewModelSendFlowTest : BaseUnitTest() {
310313
assertNull(pendingContactPaymentContext(paymentHash))
311314
}
312315

316+
@Test
317+
fun `channel ready refreshes public Paykit endpoints when sharing enabled`() = test {
318+
enablePublicPaykitSharing()
319+
advanceUntilIdle()
320+
clearInvocations(publicPaykitRepo)
321+
322+
nodeEvents.emit(
323+
Event.ChannelReady(
324+
channelId = "testChannelId",
325+
userChannelId = "testUserChannelId",
326+
counterpartyNodeId = null,
327+
fundingTxo = null,
328+
),
329+
)
330+
advanceUntilIdle()
331+
332+
verify(publicPaykitRepo).syncCurrentPublishedEndpoints()
333+
}
334+
335+
@Test
336+
fun `channel closed refreshes public Paykit endpoints when sharing enabled`() = test {
337+
enablePublicPaykitSharing()
338+
advanceUntilIdle()
339+
clearInvocations(publicPaykitRepo)
340+
341+
nodeEvents.emit(
342+
Event.ChannelClosed(
343+
channelId = "testChannelId",
344+
userChannelId = "testUserChannelId",
345+
counterpartyNodeId = null,
346+
reason = null,
347+
),
348+
)
349+
advanceUntilIdle()
350+
351+
verify(publicPaykitRepo).syncCurrentPublishedEndpoints()
352+
}
353+
313354
@Test
314355
fun `amount change clears confirmedWarnings`() = test {
315356
setUnifiedState(amount = 1000u)
@@ -363,6 +404,12 @@ class AppViewModelSendFlowTest : BaseUnitTest() {
363404
assertEquals(0L, sut.sendUiState.value.lastLightningFee)
364405
}
365406

407+
private suspend fun enablePublicPaykitSharing() {
408+
settingsData.value = SettingsData(sharesPublicPaykitEndpoints = true)
409+
walletState.value = WalletState(onchainAddress = "bc1qtest")
410+
whenever { publicPaykitRepo.syncCurrentPublishedEndpoints() }.thenReturn(Result.success(Unit))
411+
}
412+
366413
@Suppress("UNCHECKED_CAST")
367414
private fun setPendingContactPaymentContext(paymentHash: String, publicKey: String) {
368415
val field = AppViewModel::class.java.getDeclaredField("pendingContactPaymentContexts")

0 commit comments

Comments
 (0)