Skip to content

fix(amounts): floor percentage quick-buttons to the spendable balance - #697

Merged
islandbitcoin merged 1 commit into
mainfrom
fix/percentage-buttons-spendable
Aug 15, 2026
Merged

islandbitcoin merged 1 commit into
mainfrom
fix/percentage-buttons-spendable

Conversation

@islandbitcoin

Copy link
Copy Markdown
Contributor

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) in app/types/amounts.ts, next to toSpendableBalance (#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

Site Before After
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:

Tap Before After
Cashout 100% 110¢ — unsendable, max-amount error 109¢ = spendable balance
Cashout 50% 55¢ (rounded up past half the spendable 109¢) 54¢
Conversion 75% 82¢ 81¢
Conversion 25% 27¢ 27¢ (unchanged, was already ≤)

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 toSpendableBalance docblock 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 passes 90 (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.tsx MAX paths — already floor correctly (feat(send): max amount button on the amount screen #689).
  • quick-zap-modal.tsx presets — 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 of to-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 passed
  • yarn tsc:check: clean
  • eslint on changed files: clean (two pre-existing findings on untouched lines of conversion-details-screen.tsx left alone)

Fixes #696

🤖 Generated with Claude Code

https://claude.ai/code/session_015fAxSGEsL2LsMx1uCjAzHH

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
@islandbitcoin
islandbitcoin merged commit 5ef7a30 into main Aug 15, 2026
9 checks passed
@islandbitcoin
islandbitcoin deleted the fix/percentage-buttons-spendable branch August 15, 2026 16:30
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.

Percentage buttons (conversion / cashout '100%') round the raw balance up to an unsendable amount

1 participant