fix(balance): sum unrounded token values for the total (iOS parity) - #1228
Merged
Conversation
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.
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.
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
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.totalBalancecomputedset.map { it.balance.rounded() }.sum()— it rounded each token to cents before summing (sum(round(x))). That drifts fromround(sum(x))by up to a penny once the sub-cent remainders accumulate.iOS is
round(sum):Session.totalBalance→ExchangedFiat.total(rate:)sums each token's unrounded native value and rounds only at display. Android's ownaggregateAppreciationalready sums unrounded (which is why appreciation matched). So the per-token round intotalBalancewas 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-unusedroundedimport.This single computed property backs both the v1 (
BalanceScreenContent) and v2 (WalletScreenContent) balance headers, so both are fixed.Test Plan
:apps:flipcash:shared:tokenscompiles.