Repository navigation
fix(display): floor wallet balances to whole spendable minor units - #695
Merged
Merged
Conversation
The API reports USD cash-wallet balances in fractional cents (109.9346 = $1.099346 held at IBEX), and round-to-nearest display formatting showed users money they could not send — typing the displayed balance got an IBEX insufficient-balance rejection, and post-MAX residue showed $0.01. Add toSpendableBalance (app/types/amounts.ts): floor a wallet balance to whole minor units BEFORE display-currency conversion. Typed to WalletAmount<WalletCurrency> so it cannot be misapplied to display-currency amounts. Math.floor keeps displayed <= actual for negative amounts; NaN passes through. Applied at balance displays only: home wallet cards, amount-input header, send-flow wallet picker / details+confirmation error strings / shared insufficient-balance box, conversion+swap balances, cashout from- wallet row, settings balance warnings. Validation, send amounts, fees, transaction rows, and entered amounts are deliberately unchanged. Fixes #690 Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_015fAxSGEsL2LsMx1uCjAzHH
Code-review fixes for #695: - The max-amount-exceeded error formatted the raw fractional-cent wallet balance: with 109.9346 cents held, the cashout screen's balance row shows "$1.09" (floored) while a user typing $1.10 was told the maximum is "$1.10" — the exact amount just rejected, the #690 symptom. Format a display-side floored max instead; the greaterThan validation and the CashoutDetails Next-button gate keep using the raw balance. Adds a moneyAmountIsWalletAmount guard (moneyAmountIsCurrencyType cannot narrow its false branch on the non-union MoneyAmount type) plus regression specs mirroring wallet-overview-spendable-balance.spec.tsx. - ChooseWallet: replace the self-referential toBtcMoneyAmount(btcWallet.balance || btcWallet?.balance) read with toBtcMoneyAmount(btcWallet?.balance) — the fallback re-read the same property and the unguarded first access contradicted the optional chain. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_015fAxSGEsL2LsMx1uCjAzHH
…ssion Review fix for PR #695: the "still validates against the raw balance" test used the identity conversion, under which integer number-pad entries can never land in the (floor(max), rawMax] gap — switching validation to the floored displayMaxAmount left it green. Use a rate-based display<->USD conversion (1 display cent = 1.005 USD cents) so the entered $1.09 converts to 109.545 USD cents inside (109, 109.9346]: raw validation permits it, floored validation rejects it. Verified by mutation: pointing the greaterThan check at displayMaxAmount now fails this test. Added an over-max companion assertion so the no-error expectation cannot pass vacuously. Co-Authored-By: Claude Fable 5 <[email protected]> Claude-Session: https://claude.ai/code/session_015fAxSGEsL2LsMx1uCjAzHH
islandbitcoin
added a commit
that referenced
this pull request
Aug 15, 2026
…#697) 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 Claude-Session: https://claude.ai/code/session_015fAxSGEsL2LsMx1uCjAzHH Co-authored-by: Dread <[email protected]> Co-authored-by: Claude Fable 5 <[email protected]>
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.
Problem
The API reports USD cash-wallet balances in fractional cents (e.g.
109.9346= $1.099346 held at IBEX, since flash#480). Balance displays round to nearest, so a wallet holding $1.099346 shows $1.10 — and a user who types that displayed amount gets an IBEXinsufficient balancerejection. After a MAX drain, a 0.9346-cent residue displays as "$0.01" though nothing is spendable.The rule
Displayed balance = format(convert(floor(balance in wallet minor units))).
One helper,
toSpendableBalanceinapp/types/amounts.ts, floors a wallet balance to whole minor units (cents / sats) before display-currency conversion — never in display units, so e.g. a JMD conversion of the floored cent amount stays untouched. It is typedWalletAmount<T extends WalletCurrency>, so it cannot be applied to display-currency amounts by mistake.Math.floor(toward −∞) keeps the invariant displayed ≤ actual even for negative amounts;NaNpasses through so missing balances keep their existing rendering. BTC sat balances are integers, so flooring them is a deliberate no-op (applied for uniformity wherever BTC and USD balances are formatted side by side). The name matches thespendableBalanceconcept #689 introduces inmax-send-amount.ts.Where it's applied (balance displays only)
wallet-overview.tsx(formatBalance(): cash + BTC cards, both the wallet-unit and display-currency strings)amount-input-screen-ui.tsx(balanceText, both branches)ChooseWallet.tsx(selected row + picker modal rows)send-bitcoin-details-screen.tsx(balance shown in the two amount+fee-exceeds-balance error messages)send-bitcoin-details-extra-info.tsx(floors the balance handed back byisValidAmountat the point of display)send-bitcoin-confirmation-screen.tsx(setWalletText()wallet rows; the amount-exceed error strings reuse these texts)conversion-details-screen.tsx+useSwap.ts(formatted from/to balances and fee-exceeds-balance error strings)CashoutFromWallet.tsx(single render sink for the details + confirmation screens)account-screen.tsx,account/settings/delete.tsx,advanced-mode-toggle.tsx(balance shown in delete-account / beginner-mode warnings)What deliberately does NOT floor
isValidAmount, the amount+fee > balance comparisons (details, confirmation, swap), percentage buttons,sendAll/MAX (that's feat(send): max amount button on the amount screen #689'smax-send-amount.ts), and the> 0gates on the settings warnings. Where one MoneyAmount feeds both display and validation, the floor is applied only inside the display expression.Flashcard.tsx, the home-screen card row): an external card balance in integral sats, not a Flash wallet balance — left as-is.Sub-cent residue displays as $0.00
A drained wallet holding 0.9346 unspendable cents shows $0.00 (not a special zero/empty state). That's the honest value — nothing is sendable — and keeps this fix presentation-only. The settings-warning
> 0gates still fire on residue, so a deleting user with residue sees a "$0.00" warning; acceptable, and changing those gates would be a logic change out of scope here.Merge order
Merge after #689 (
feat/max-amount-button). This PR deliberately avoids the JSX #689 touches inamount-input-screen-ui.tsx(the floor lives inside thebalanceTextuseMemo, which #689 doesn't modify) and leavesusdBalanceMoneyAmount/btcBalanceMoneyAmountinsend-bitcoin-details-screen.tsxuntouched for its MAX logic — a trivial rebase is expected.Tests
__tests__/utils/to-spendable-balance.spec.ts— floors fractional cents (109.9346 → 109), sub-cent residue → 0, integer passthrough, BTC no-op, NaN/undefined/null tolerance, negative floors toward −∞ (documented), input not mutated.__tests__/components/wallet-overview-spendable-balance.spec.tsx— renders the home-screen cash card with a fractional-cent balance and asserts "$1.09" (never "$1.10"), and residue renders "$0.00" (never "$0.01"). Verified the spec fails without the floor.yarn tsc:checkclean; eslint clean on all changed lines (remaining warnings in touched files are pre-existing debt on untouched lines). No i18n strings added.Fixes #690
🤖 Generated with Claude Code
https://claude.ai/code/session_015fAxSGEsL2LsMx1uCjAzHH