Repository navigation
fix(amounts): floor percentage quick-buttons to the spendable balance - #697
Merged
Merged
Conversation
The conversion and cashout percentage quick-buttons Math.round-ed the raw fractional-cent balance (flash#480), so on a wallet holding 109.9346 cents tapping 100% filled 110 cents — more than is spendable — driving cashout into the max-amount error and conversion into a swap that can't be filled. Extract percentageOfBalance next to toSpendableBalance: floor the balance to whole minor units first, then floor the percentage result, so every quick-set amount is a whole, sendable minor unit <= the spendable balance. At 100% this matches toSpendableBalance exactly — the same rule as the MAX chip (#689) and the balance displays (#695). Fixes #696 Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_015fAxSGEsL2LsMx1uCjAzHH
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The rule
Every percentage quick-set amount must be ≤ the spendable balance. The API serves USD cash-wallet balances in fractional cents (flash#480), and the quick-buttons
Math.round-ed the raw balance — rounding up past what is spendable.New helper
percentageOfBalance(balance, percentage)inapp/types/amounts.ts, next totoSpendableBalance(#695): floor the balance to whole minor units first, then floor the percentage result. Every quick-set lands on a whole, sendable minor unit, and 100% returns exactly the spendable balance — the same rule as the MAX chip (#689) and the balance displays (#695).Both flows fixed
app/screens/topup-cashout-flow/CashoutDetails.tsx(setAmountToBalancePercentage, 50%/100% buttons)Math.round((usdBalance.amount * percentage) / 100)percentageOfBalance(usdBalance.amount, percentage)app/screens/conversion-flow/conversion-details-screen.tsx(setAmountToBalancePercentage, 25/50/75 + top chip)Math.round((fromBalance * percentage) / 100)percentageOfBalance(fromBalance, percentage)Exact numbers on the issue's wallet, 109.9346 cents held:
Small-balance overshoot in conversion is also fixed: on 1.9¢ held, the top chip (passes 99 for USD) previously set
Math.round(1.9 × 0.99) = 2¢— more than both the spendable and the raw balance; now 0¢.Also corrected the stale
toSpendableBalancedocblock line ("never use to build send amounts") — the MAX chip has used it for fills since #689; the real rule is never for validation, which this PR does not touch (validation still compares against the raw balance).Deliberately untouched
PercentageAmount.tsx:51— the conversion top chip passes90(BTC, labeled 90%) /99(USD, labeled 100%). These values predate fractional balances (Sept 2024, breez-liquid migration) and look like deliberate swap-fee headroom; raising 99 → 100 is a product decision out of scope here. With this fix the chip can no longer overshoot regardless.max-send-amount.ts,amount-input-screen.tsxMAX paths — already floor correctly (feat(send): max amount button on the amount screen #689).quick-zap-modal.tsxpresets — fixed sat amounts, not balance-derived.utils/breez-sdk/migration.ts— integer-sat math, not a user-facing button.Tests
New
__tests__/utils/percentage-of-balance.spec.ts(10 tests, house style ofto-spendable-balance.spec.ts): the exact issue numbers (109.9346 @ 100% → 109, @ 50% → 54 not 55), small-balance overshoot (1.9 @ 99 → 0), integer passthrough, zero, sub-cent residue, NaN propagation, 100% ≡toSpendableBalance, and a sweep asserting no button percentage ever exceeds the spendable balance.yarn test: 64 suites / 430 tests passedyarn tsc:check: cleanconversion-details-screen.tsxleft alone)Fixes #696
🤖 Generated with Claude Code
https://claude.ai/code/session_015fAxSGEsL2LsMx1uCjAzHH