Skip to content

fix(display): floor wallet balances to whole spendable minor units - #695

Merged
islandbitcoin merged 3 commits into
mainfrom
fix/display-floor-balances
Aug 15, 2026
Merged

islandbitcoin merged 3 commits into
mainfrom
fix/display-floor-balances

Conversation

@bobodread876

Copy link
Copy Markdown
Collaborator

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 IBEX insufficient balance rejection. 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, toSpendableBalance in app/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 typed WalletAmount<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; NaN passes 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 the spendableBalance concept #689 introduces in max-send-amount.ts.

Where it's applied (balance displays only)

  • Home screen wallet cards — wallet-overview.tsx (formatBalance(): cash + BTC cards, both the wallet-unit and display-currency strings)
  • Amount input header — amount-input-screen-ui.tsx (balanceText, both branches)
  • Send flow, wallet picker ("From") — ChooseWallet.tsx (selected row + picker modal rows)
  • Send flow, details screen — send-bitcoin-details-screen.tsx (balance shown in the two amount+fee-exceeds-balance error messages)
  • Send flow, shared insufficient-balance error box — send-bitcoin-details-extra-info.tsx (floors the balance handed back by isValidAmount at the point of display)
  • Send flow, confirmation screen — send-bitcoin-confirmation-screen.tsx (setWalletText() wallet rows; the amount-exceed error strings reuse these texts)
  • Conversion / swap — conversion-details-screen.tsx + useSwap.ts (formatted from/to balances and fee-exceeds-balance error strings)
  • Cashout — CashoutFromWallet.tsx (single render sink for the details + confirmation screens)
  • Settings warnings — account-screen.tsx, account/settings/delete.tsx, advanced-mode-toggle.tsx (balance shown in delete-account / beginner-mode warnings)

What deliberately does NOT floor

  • Validation and send logic is untouched: 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's max-send-amount.ts), and the > 0 gates on the settings warnings. Where one MoneyAmount feeds both display and validation, the floor is applied only inside the display expression.
  • Non-balance amounts keep their formatting: fees, transaction rows/history, entered amounts, limits, success screens.
  • Flashcard balances (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 > 0 gates 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 in amount-input-screen-ui.tsx (the floor lives inside the balanceText useMemo, which #689 doesn't modify) and leaves usdBalanceMoneyAmount/btcBalanceMoneyAmount in send-bitcoin-details-screen.tsx untouched 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.
  • Full suite: 57 suites / 323 tests green (baseline on main: 55 / 314). yarn tsc:check clean; 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

Dread and others added 3 commits August 14, 2026 23:37
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
islandbitcoin merged commit b1bc922 into main Aug 15, 2026
9 checks passed
@islandbitcoin
islandbitcoin deleted the fix/display-floor-balances branch August 15, 2026 07:46
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]>
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.

Displayed balance can exceed spendable balance (fractional-cent USD wallets) — manual full-balance sends fail at IBEX

2 participants