Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
a409579
fix: let send reach spark before the node runs
jvsena42 Dec 3, 2026
cc01758
chore: name the changelog fragment after the pr
jvsena42 Dec 3, 2026
b337747
fix: show the node wait on recipient steps and cap amount at spark
jvsena42 Dec 3, 2026
272067d
fix: show the node id wait on recipient steps
jvsena42 Dec 3, 2026
1064562
fix: keep the sync overlay off a submitted send
jvsena42 Dec 3, 2026
1b89d0f
fix: accept amounts spark can pay above the cached limit
jvsena42 Dec 3, 2026
cf58265
fix: cap the amount pad at spark's balance before the node runs
jvsena42 Dec 3, 2026
90fddb0
fix: wait for the node before rejecting a typed invoice
jvsena42 Dec 3, 2026
652c67b
fix: skip the typed invoice node wait once the node runs
jvsena42 Dec 3, 2026
fa15097
fix: keep the routing fee reserve for spark amounts
jvsena42 Dec 3, 2026
7c9ab36
fix: cancel typed invoice validation when leaving it
jvsena42 Dec 3, 2026
54c3525
fix: show coming soon at once while the node starts
jvsena42 Dec 3, 2026
e5584b2
fix: quote the max routing fee from spark's live limit
jvsena42 Dec 3, 2026
37f8ec4
fix: keep typed invoices pending through a queued node start
jvsena42 Dec 3, 2026
ce90643
Merge remote-tracking branch 'origin/master' into codex/rc4-send-android
piotr-iohk Dec 4, 2026
c03deaa
fix: quote the visible Lightning limit and validate Spark Max in the …
piotr-iohk Dec 4, 2026
c7c0c1f
fix: wait for a slow or queued node start before checking channels
jvsena42 Dec 4, 2026
f467ac0
fix: wait for reconnecting channels on a typed invoice
jvsena42 Dec 4, 2026
4ca8c91
fix: hold continue for any in-flight max fee quote
jvsena42 Dec 4, 2026
e5826ce
refactor: share the recipient route list across both checks
jvsena42 Dec 4, 2026
3b46d03
fix: refresh channels before checking for a reconnect
jvsena42 Dec 4, 2026
a157048
perf: refresh node state once for an invoice with usable channels
jvsena42 Dec 4, 2026
02dd9d1
fix: cap amount entry at spark until a channel is usable
jvsena42 Dec 4, 2026
abf849f
fix: keep the overlay off a send confirm submitted
jvsena42 Dec 4, 2026
c1eef62
docs: fix the spark payment and recipient step comments
jvsena42 Dec 4, 2026
f77fd47
fix: keep confirm's progress up after spark debits a submitted send
jvsena42 Dec 4, 2026
a56b714
fix: poll channels while they reconnect behind amount
jvsena42 Dec 4, 2026
a1a137b
fix: show a wait toast for a payment waiting outside send
jvsena42 Dec 4, 2026
3ee4ade
fix: stop polling channels once a send is submitted
jvsena42 Dec 4, 2026
04244cd
fix: dismiss the wait toast in the coroutine that shows it
jvsena42 Dec 4, 2026
f3e88d5
fix: finish the wait toast before the scan continues
jvsena42 Dec 4, 2026
6c6e88c
fix: drop the time estimate from the node wait toast
jvsena42 Dec 4, 2026
546e800
fix: drop the manual entry request when back leaves it
jvsena42 Dec 4, 2026
488b7de
fix: check fresh savings before refusing an address while node is down
jvsena42 Dec 4, 2026
78481b0
fix: treat savings as cached until the started node syncs
jvsena42 Dec 4, 2026
b156c2b
fix: wait for fresh savings before replacing the send state
jvsena42 Dec 4, 2026
5ef6500
fix: re-read savings after the lightning wait of a unified request
jvsena42 Dec 4, 2026
187e507
fix: ignore max quotes from an amount screen already left
jvsena42 Dec 4, 2026
31b8a8a
fix: derive savings on refusal when sync ended before publishing
jvsena42 Dec 4, 2026
2b5c549
fix: wait for a running sync before refusing on savings
jvsena42 Dec 4, 2026
0022954
fix: refuse a below-minimum request before the savings wait
jvsena42 Dec 4, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions app/src/main/java/to/bitkit/repositories/LightningRepo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3133,6 +3133,23 @@ class LightningRepo @Inject constructor(
/** Whether Spark alone can pay [amountSats], which it does whenever the node cannot. */
fun canSendWithSpark(amountSats: ULong): Boolean = sparkRepo.canSend(amountSats)

/**
* Waits, up to [NODE_RUN_WAIT_TIMEOUT], until the node runs while it is starting, stopping or has a start queued,
* so a caller that then checks channels sees the running node rather than a start the channel wait gives up on.
* Returns at once when the node runs or no start is coming.
*/
suspend fun awaitPendingNodeStart() {
val changes = combine(_lightningState, pendingStartCount) { _, _ -> }
withTimeoutOrNull(NODE_RUN_WAIT_TIMEOUT) {
changes.first {
val lifecycle = _lightningState.value.nodeLifecycleState
val isStopping = lifecycle is NodeLifecycleState.Stopping
val isComing = lifecycle.canRun() || isStopping || pendingStartCount.value > 0
lifecycle.isRunning() || !isComing
}
}
}

/** Whether Spark's whole balance covers a withdrawal debiting [debitSats], its fees included. */
fun canWithdrawWithSpark(debitSats: ULong): Boolean = sparkRepo.canWithdraw(debitSats)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,10 @@ fun SendAmountScreen(
}
}

LaunchedEffect(uiState.decodedInvoice, uiState.payMethod) {
LaunchedEffect(uiState.decodedInvoice, uiState.payMethod, balances.maxSendLightningSats) {
if (uiState.payMethod == SendMethod.LIGHTNING && uiState.decodedInvoice != null) {
currentOnEvent(SendEvent.EstimateMaxRoutingFee)
val quote = SendEvent.EstimateMaxRoutingFee(uiState.decodedInvoice.bolt11, balances.maxSendLightningSats)
currentOnEvent(quote)
}
}

Expand All @@ -194,6 +195,7 @@ fun SendAmountScreen(
amountInputViewModel = amountInputViewModel,
syncNodeVariant = syncNodeVariant,
currencies = currencies,
balances = balances,
onBack = {
onEvent(SendEvent.AmountReset)
onBack()
Expand Down Expand Up @@ -463,7 +465,7 @@ private fun SendAmountNodeRunning(

PrimaryButton(
text = stringResource(R.string.common__continue),
enabled = uiState.isAmountInputValid && !uiState.isFundingSourceLoading,
enabled = uiState.isAmountInputValid && !uiState.isFundingSourceLoading && !uiState.awaitsSparkMaxFee,
isLoading = uiState.isLoading,
onClick = onContinue,
textStyle = if (uiState.paykitUsesUsdt) AppTextStyles.BodySSB.copy(letterSpacing = 0.sp) else null,
Expand Down
157 changes: 142 additions & 15 deletions app/src/main/java/to/bitkit/ui/sheets/SendSheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
Expand Down Expand Up @@ -57,6 +58,7 @@ import to.bitkit.repositories.ConnectivityState
import to.bitkit.repositories.PaykitPaymentRequest
import to.bitkit.repositories.PaykitSubscriptionId
import to.bitkit.repositories.SparkState
import to.bitkit.ui.LocalBalances
import to.bitkit.ui.components.ConnectionIssuesView
import to.bitkit.ui.components.SyncNodeVariant
import to.bitkit.ui.components.SyncNodeView
Expand Down Expand Up @@ -109,20 +111,53 @@ import kotlin.time.Duration.Companion.seconds

private const val HARDWARE_SEND_FALLBACK_SATS_PER_VBYTE = 3uL

/** A peer reconnecting makes a channel usable again without any node event, so the overlay polls. */
/** A peer reconnecting makes a channel usable again without any node event, so the sheet polls. */
private val CHANNELS_REFRESH_INTERVAL = 1.seconds

/** Whether Spark can pay the scanned Lightning invoice's own amount, so an amountless one still waits for the node. */
/** Whether Spark can pay a Lightning invoice's amount: its own, or for an amountless one the amount entered. */
internal fun sparkCanPayInvoice(payMethod: SendMethod, invoiceSats: ULong?, sparkState: SparkState): Boolean =
payMethod == SendMethod.LIGHTNING && invoiceSats != null && invoiceSats > 0uL && sparkState.canSend(invoiceSats)

/**
* The amount Spark would pay for the scanned invoice: its own amount, or for an amountless one the amount entered, so
* the review of an amountless invoice Spark can pay goes ahead without the node too.
*/
internal fun sendInvoiceSats(invoiceSats: ULong?, enteredSats: ULong): ULong? =
invoiceSats?.takeIf { it > 0uL } ?: enteredSats.takeIf { invoiceSats != null && it > 0uL }

/**
* Whether the amount of an amountless Lightning invoice can be entered without the node: Spark can send, so an amount
* within its balance pays without channels. An LNURL payment still waits for the node.
*/
internal fun sparkCanTakeAmount(
payMethod: SendMethod,
invoiceSats: ULong?,
isLnurl: Boolean,
sparkState: SparkState,
): Boolean = payMethod == SendMethod.LIGHTNING && invoiceSats == 0uL && !isLnurl &&
sparkState.isUsable && sparkState.sendableSats > 0uL

/** Until a channel is usable only Spark pays, so Available stays at Spark's limit while channels reconnect. */
internal fun lightningAmountLimit(cachedMax: ULong, sparkMax: ULong?, hasUsableChannels: Boolean): ULong = when {
sparkMax == null -> cachedMax
hasUsableChannels -> maxOf(cachedMax, sparkMax)
else -> sparkMax
}

/** Whether Spark can pay a Quick Pay invoice; an LNURL one still waits for the node. */
internal fun sparkCanPayQuickPay(data: QuickPayData, sparkState: SparkState): Boolean =
data is QuickPayData.Bolt11 && sparkCanPayInvoice(SendMethod.LIGHTNING, data.sats, sparkState)

/** Whether a send can go ahead now: the node runs, a hardware wallet signs it, or Spark pays it without the node. */
internal fun canSendWithoutWaiting(isNodeRunning: Boolean, isHardwareWallet: Boolean, sparkCanPay: Boolean): Boolean =
isNodeRunning || isHardwareWallet || sparkCanPay
/**
* Whether a send can go ahead now: the node runs, a hardware wallet signs it, or Spark pays it without the node. A send
* already submitted keeps its progress, since Spark's balance can drop below it before the result arrives.
*/
internal fun canSendWithoutWaiting(
isNodeRunning: Boolean,
isHardwareWallet: Boolean,
sparkCanPay: Boolean,
isSubmitted: Boolean = false,
): Boolean = isNodeRunning || isHardwareWallet || sparkCanPay || isSubmitted

/**
* A Lightning payment Spark can pay goes ahead without the node. Otherwise the node must run, and with channels a
Expand All @@ -140,6 +175,52 @@ internal fun waitsForNode(
return hasAnyChannels && !hasUsableChannels
}

/** The steps that pick a recipient, which never wait for the node. Coming Soon is Contact's Paykit-off fallback. */
private val recipientRoutes = setOf(
SendRoute.Recipient::class,
SendRoute.Address::class,
SendRoute.ContactSelect::class,
SendRoute.QrScanner::class,
SendRoute.ComingSoon::class,
)

/**
* Whether [route] comes before a payment is decoded: choosing, scanning or typing a recipient. Nothing there needs the
* node, and a Lightning invoice Spark can pay must reach it, so the sync overlay waits for the next step.
*/
internal fun isRecipientStep(route: SendRoute): Boolean = route::class in recipientRoutes

internal fun NavDestination.isRecipientStep(): Boolean = recipientRoutes.any { hasRoute(it) }

/**
* A Max quote from an Amount screen already left (still composed while it animates out) must not reprice the amount
* Continue accepted, or it can disable Confirm without a word.
*/
internal fun forwardsAmountEvent(event: SendEvent, isAmountActive: Boolean): Boolean =
event !is SendEvent.EstimateMaxRoutingFee || isAmountActive

/** Whether [this] comes after a payment was submitted, where waiting for the node no longer applies. */
internal fun NavDestination.isPastReview(): Boolean =
hasRoute<SendRoute.Pending>() || hasRoute<SendRoute.Success>() || hasRoute<SendRoute.Error>() ||
hasRoute<SendRoute.WithdrawError>() || hasRoute<SendRoute.QuickPay>()

/** A send Confirm submitted keeps its progress visible: Spark's balance can drop before the result route arrives. */
internal fun isPastSubmission(isPastReview: Boolean, isSendSwipeRunning: Boolean): Boolean =
isPastReview || isSendSwipeRunning

/**
* Whether the sheet polls channels: while its overlay waits for a usable one, and while a running node's channels
* reconnect behind Amount, whose Spark cap lifts only once the list shows a usable channel. A submitted send has
* nothing left that reads them.
*/
internal fun pollsChannels(
Comment thread
jvsena42 marked this conversation as resolved.
Resolved
isNodeRunning: Boolean,
isPastSubmission: Boolean,
shouldShowSyncOverlay: Boolean,
hasAnyChannels: Boolean,
hasUsableChannels: Boolean,
): Boolean = isNodeRunning && !isPastSubmission && (shouldShowSyncOverlay || hasAnyChannels && !hasUsableChannels)

internal fun NavDestination.isPaymentReview(): Boolean =
hasRoute<SendRoute.Confirm>() || hasRoute<SendRoute.PinCheck>() ||
hasRoute<SendRoute.RecoveryPinCheck>() || hasRoute<SendRoute.HardwareSign>()
Expand Down Expand Up @@ -187,26 +268,52 @@ fun SendSheet(
val isOffline by remember { derivedStateOf { connectivityState != ConnectivityState.CONNECTED } }
val lightningState by walletViewModel.lightningState.collectAsStateWithLifecycle()
val sendUiState by appViewModel.sendUiState.collectAsStateWithLifecycle()
val isWaitingForNode by appViewModel.isSendWaitingForNode.collectAsStateWithLifecycle()
val isSendSwipeRunning by appViewModel.isSendSwipeRunning.collectAsStateWithLifecycle()
val contacts by appViewModel.pubkyContacts.collectAsStateWithLifecycle()
var routingCacheResetAttempted by rememberSaveable(startDestination) { mutableStateOf(false) }

val hasAnyChannels = lightningState.channels.isNotEmpty()
val syncNodeVariant = if (hasAnyChannels) SyncNodeVariant.LIGHTNING else SyncNodeVariant.BITCOIN
val sparkState by walletViewModel.sparkState.collectAsStateWithLifecycle()
val sparkCanPay = sparkCanPayInvoice(sendUiState.payMethod, sendUiState.decodedInvoice?.amountSatoshis, sparkState)
val invoiceSats = sendUiState.decodedInvoice?.amountSatoshis
val sparkCanPay = sparkCanPayInvoice(
sendUiState.payMethod,
sendInvoiceSats(invoiceSats, sendUiState.amount),
sparkState,
)
val sparkTakesAmount = sparkCanTakeAmount(
payMethod = sendUiState.payMethod,
invoiceSats = invoiceSats,
isLnurl = sendUiState.lnurl != null,
sparkState = sparkState,
)
val navController = rememberNavController()
val entry by navController.currentBackStackEntryAsState()
// Until navigation settles the entry is null, and the sheet shows its start destination
val isRecipientStep = entry?.destination?.isRecipientStep() ?: isRecipientStep(startDestination)
val isAmountStep = entry?.destination?.hasRoute<SendRoute.Amount>() ?: (startDestination == SendRoute.Amount)
// A Spark payment can be submitted before the node runs, and its balance then no longer covers it
val isPastReview = entry?.destination?.isPastReview() == true
val shouldShowSyncOverlay = run {
if (preparingRequest != null || sendUiState.paykitUsesUsdt) return@run false
if (sendUiState.hardwareWalletId != null) return@run false
if (sendUiState.hardwareWalletId != null || isPastSubmission(isPastReview, isSendSwipeRunning)) return@run false
waitsForNode(
isNodeRunning = lightningState.nodeLifecycleState.isRunning(),
hasAnyChannels = hasAnyChannels,
hasUsableChannels = lightningState.channels.any { it.isUsable },
sparkCanPay = sparkCanPay,
sparkCanPay = sparkCanPay || isAmountStep && sparkTakesAmount,
Comment thread
jvsena42 marked this conversation as resolved.
Resolved
)
}

val lifecycle = LocalLifecycleOwner.current.lifecycle
val isWaitingForUsableChannel = shouldShowSyncOverlay && lightningState.nodeLifecycleState.isRunning()
val isWaitingForUsableChannel = pollsChannels(
isNodeRunning = lightningState.nodeLifecycleState.isRunning(),
isPastSubmission = isPastSubmission(isPastReview, isSendSwipeRunning),
shouldShowSyncOverlay = shouldShowSyncOverlay,
hasAnyChannels = hasAnyChannels,
hasUsableChannels = lightningState.channels.any { it.isUsable },
)
LaunchedEffect(isWaitingForUsableChannel) {
if (!isWaitingForUsableChannel) return@LaunchedEffect
lifecycle.repeatOnLifecycle(Lifecycle.State.STARTED) {
Expand Down Expand Up @@ -239,8 +346,6 @@ fun SendSheet(
.imePadding()
.testTag("SendSheet"),
) {
val navController = rememberNavController()
val entry by navController.currentBackStackEntryAsState()
val reviewing = entry?.destination?.isPaymentReview() == true
LaunchedEffect(reviewing, lifecycle, appViewModel) {
if (!reviewing) return@LaunchedEffect
Expand Down Expand Up @@ -410,9 +515,15 @@ fun SendSheet(
}
composableWithDefaultTransitions<SendRoute.Address> {
val uiState by appViewModel.sendUiState.collectAsStateWithLifecycle()
DisposableEffect(Unit) { onDispose { appViewModel.cancelAddressValidation() } }
Comment thread
jvsena42 marked this conversation as resolved.
Resolved
val back = {
appViewModel.onAddressBack()
navController.popBackStack()
}
BackHandler(onBack = { back() })
SendAddressScreen(
uiState = uiState,
onBack = { navController.popBackStack() },
onBack = { back() },
onEvent = { appViewModel.setSendEvent(it) },
)
}
Expand Down Expand Up @@ -465,19 +576,33 @@ fun SendSheet(
val lightningState by walletViewModel.lightningState.collectAsStateWithLifecycle()
SendAmountScreen(
uiState = uiState,
nodeLifecycleState = if (uiState.hardwareWalletId != null) {
// Spark takes an amountless invoice's amount without the node, as a hardware wallet does
nodeLifecycleState = if (uiState.hardwareWalletId != null || sparkTakesAmount) {
NodeLifecycleState.Running
Comment thread
ovitrif marked this conversation as resolved.
Resolved
} else {
lightningState.nodeLifecycleState
},
canGoBack = startDestination != SendRoute.Amount || uiState.isPaymentRequest,
syncNodeVariant = syncNodeVariant,
balances = LocalBalances.current.let { balances ->
balances.copy(
maxSendLightningSats = lightningAmountLimit(
cachedMax = balances.maxSendLightningSats,
sparkMax = sparkState.sendableSats.takeIf { sparkTakesAmount },
hasUsableChannels = lightningState.nodeLifecycleState.isRunning() &&
lightningState.channels.any { it.isUsable },
),
Comment thread
jvsena42 marked this conversation as resolved.
Resolved
)
},
Comment thread
ovitrif marked this conversation as resolved.
Resolved
onBack = {
if (!navController.popBackStack()) {
appViewModel.hideSheet()
}
},
onEvent = { appViewModel.setSendEvent(it) }
onEvent = { event ->
val isAmountActive = navController.currentDestination?.hasRoute<SendRoute.Amount>() == true
if (forwardsAmountEvent(event, isAmountActive)) appViewModel.setSendEvent(event)
}
)
}
composableWithDefaultTransitions<SendRoute.QrScanner> {
Expand Down Expand Up @@ -582,6 +707,7 @@ fun SendSheet(
isNodeRunning = lightningState.nodeLifecycleState.isRunning(),
isHardwareWallet = uiState.hardwareWalletId != null,
sparkCanPay = sparkCanPay,
isSubmitted = isSendSwipeRunning,
),
canAutoStart = !isOffline && !shouldShowSyncOverlay,
syncNodeVariant = syncNodeVariant,
Expand Down Expand Up @@ -957,7 +1083,8 @@ fun SendSheet(
}

AnimatedVisibility(
visible = shouldShowSyncOverlay && !isOffline,
// A recipient step shows it only while a scanned payment waits for the node
visible = (if (isRecipientStep) isWaitingForNode else shouldShowSyncOverlay) && !isOffline,
Comment thread
jvsena42 marked this conversation as resolved.
Resolved
enter = fadeIn(),
exit = fadeOut(),
) {
Expand Down
Loading