Skip to content

fix(balance): sum unrounded token values for the total (iOS parity) - #1228

Merged
bmc08gt merged 2 commits into
code/cashfrom
fix/balance-sum-rounding-parity
Aug 12, 2026
Merged

fix(balance): sum unrounded token values for the total (iOS parity)#1228
bmc08gt merged 2 commits into
code/cashfrom
fix/balance-sum-rounding-parity

Conversation

@bmc08gt

@bmc08gt bmc08gt commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator

Problem

A 1¢ discrepancy in the wallet total between iOS and Android, even though every per-token balance and the appreciation matched.

Root cause

SelectTokenViewModel.State.totalBalance computed set.map { it.balance.rounded() }.sum() — it rounded each token to cents before summing (sum(round(x))). That drifts from round(sum(x)) by up to a penny once the sub-cent remainders accumulate.

iOS is round(sum): Session.totalBalanceExchangedFiat.total(rate:) sums each token's unrounded native value and rounds only at display. Android's own aggregateAppreciation already sums unrounded (which is why appreciation matched). So the per-token round in totalBalance was the lone divergence.

Fix

Sum the unrounded per-token balances (set.map { it.balance }.sum()) and let display formatting round the total — matching iOS and our appreciation aggregation. One line; also removes the now-unused rounded import.

This single computed property backs both the v1 (BalanceScreenContent) and v2 (WalletScreenContent) balance headers, so both are fixed.

Test Plan

  • :apps:flipcash:shared:tokens compiles.
  • On device: a multi-token balance whose sub-cent remainders sum past a cent now matches iOS to the penny; single-token and whole-cent balances are unchanged.

The wallet total was `sum(round(each token))` — it rounded every token to cents
and then summed, which drifts the total by up to a penny versus `round(sum)`.
iOS (ExchangedFiat.total) sums the unrounded native values and rounds only at
display, and our own aggregateAppreciation already sums unrounded — so the total
was the lone divergence, showing a 1¢ difference from iOS even when every
per-token balance and the appreciation matched.

Sum the unrounded per-token balances and let display formatting round the total.
@github-actions github-actions Bot added area: tokens Token accounts, balances, token info type: fix Bug fix labels Aug 12, 2026
Two tokens each worth $0.014 display as $0.01 apiece (sum-of-rounded = $0.02)
but truly sum to $0.028 -> $0.03. Locks in the round(sum) aggregation so the
per-token-rounding regression can't come back.
@bmc08gt
bmc08gt merged commit 1b9f9a9 into code/cash Aug 12, 2026
3 checks passed
@bmc08gt
bmc08gt deleted the fix/balance-sum-rounding-parity branch August 12, 2026 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: tokens Token accounts, balances, token info type: fix Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant