Skip to content

Conversation

@jvsena42
Copy link
Member

@jvsena42 jvsena42 commented Jul 29, 2025

Related to #229

Description

  • Error message improvements
  • Max amount specific error message
  • Improvement on max amount and quarter button logic
  • Improvement on loading logic
  • Layout logic cleanup

Preview

transfer_tested.mp4

QA Notes

Tested

  • Normal Success flow
  • Max amount
  • 25%
  • Advanced
  • Error value higher than max amount
  • Error liquidity

@jvsena42 jvsena42 self-assigned this Jul 29, 2025
@jvsena42 jvsena42 mentioned this pull request Jul 29, 2025
3 tasks
@jvsena42
Copy link
Member Author

I'll do the TransferViewModel clean up and Unit tests in other branch to don't obfuscate the fixes from this one

@jvsena42 jvsena42 requested a review from ovitrif July 29, 2025 16:40
@jvsena42 jvsena42 marked this pull request as ready for review July 29, 2025 16:55
@jvsena42 jvsena42 changed the title Refactor/state handling improvement State handling improvement Jul 29, 2025
@jvsena42 jvsena42 changed the title State handling improvement SpendingAmountScreen state handling improvement Jul 29, 2025
Copy link
Collaborator

@ovitrif ovitrif left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice work!

Found only an issue which needs to be addressed IMHO, related to the max amount fee calculation

Comment on lines 236 to 240
val totalFeeFromAvailableAmount = lightningRepo.calculateTotalFee(amountSats = onChainBalance)
.getOrDefault(DEFAULT_TX_FEE.toULong())

// Get the max available balance discounting onChain fee
val availableAmount = onChainBalance - totalFeeFromAvailableAmount
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always get NodeException.InsufficientFunds exception when trying to calculateTotalFee for the entire onChainBalance.

That's the reason I added walletRepo.getMaxSendAmount(), which is not perfect either (I got sometimes failure for the onChainBalance - 1000sat buffer solution), but still much less error-prone:

suspend fun getMaxSendAmount(): ULong = withContext(bgDispatcher) {
val totalOnchainSats = balanceState.value.totalOnchainSats
if (totalOnchainSats == 0uL) {
return@withContext 0uL
}
try {
val minFeeBuffer = 1000uL
val amountSats = (totalOnchainSats - minFeeBuffer).coerceAtLeast(0uL)
val fee = lightningRepo.calculateTotalFee(amountSats).getOrThrow()
val maxSendable = (totalOnchainSats - fee).coerceAtLeast(0uL)
return@withContext maxSendable
} catch (_: Throwable) {
Logger.debug("Could not calculate max send amount, using as fallback 90% of total", context = TAG)
val fallbackMax = (totalOnchainSats.toDouble() * 0.9).toULong()
return@withContext fallbackMax
}
}

Much recommended to use the getMaxSendAmount() here as well, that way we won't ALWAYS end up with the DEFAULT_TX_FEE.

PS. I informed @coreyphillips about this issue but it's not the highest priority thus looking into it will be delayed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much better, though I still get error: Invalid request format when selecting the max amount

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, because Invalid request format comes from bitkit-core's http call to Blocktank staging.

@jvsena42 jvsena42 requested a review from ovitrif July 30, 2025 11:25
@jvsena42 jvsena42 enabled auto-merge July 30, 2025 11:28
@jvsena42
Copy link
Member Author

This PR fixes the bug introduced here

Copy link
Collaborator

@ovitrif ovitrif left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice refactor, the changed code works good!

Added an remark for improvement, feel free to address outside of this PR though.

Tested

  • Normal Success flow
  • Max amount
  • 25%
  • Error value higher than max amount

Comment on lines +138 to +147
if (sats > _spendingUiState.value.maxAllowedToSend) {
setTransferEffect(
TransferEffect.ToastError(
title = context.getString(R.string.lightning__spending_amount__error_max__title),
description = context.getString(
R.string.lightning__spending_amount__error_max__description
).replace("{amount}", _spendingUiState.value.maxAllowedToSend.toString()),
)
)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in the RN app, the amount will not update if it would exceed the max limit.

I think we can easily do the same here with this code right after the effect:

_spendingUiState.update { it.copy(overrideSats = it.satsAmount) }
return

@jvsena42 jvsena42 merged commit c47a1f9 into master Jul 30, 2025
3 checks passed
@jvsena42 jvsena42 deleted the refactor/state-handling-improvement branch July 30, 2025 13:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants