fix: guard RpcDataSource against invalid decimals to prevent NaN bala…#8781
Merged
Conversation
…nces
Two fixes that together stop entries like
`eip155:4326/erc20:0x000…000: { amount: "NaN" }` from leaking into
`assetsBalance`.
- `#collectMetadataForBalances`: only reuse existing native metadata when
it has finite `decimals` (via new `#hasValidDecimals` helper). Empty
`name`/`symbol` are still accepted, and `decimals: 0` is valid. Bad
state metadata (e.g. `{ decimals: null, name: '', symbol: '', type:
'native' }`) is replaced by the chain-status stub so the pipeline
resolves a usable `decimals`.
- `#convertToHumanReadable`: defensively return `'0'` when `decimals`
isn't a finite non-negative number or when `rawBalance` can't be
parsed. Defaulting decimals to a fixed value (e.g. 18) would silently
produce wrong amounts; `'0'` matches the existing safe fallback used
in the error path.
Adds 7 tests covering the bug payload (`decimals: null`), `NaN`
decimals, negative decimals, non-numeric raw balance, and the
`decimals: 0` / missing `name`/`symbol` valid cases.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 7e44e16. Configure here.
This update introduces a new helper method, `#pickValidDecimals`, to ensure that the first valid `decimals` value is selected from metadata sources, preventing stale `decimals: NaN` entries from shadowing valid values. The `#handleBalanceUpdate` method has been updated to utilize this helper, ensuring accurate balance calculations. Additionally, the changelog has been updated to reflect these changes and their implications for balance handling.
…lances This update enhances the `#hasValidDecimals` guard to ensure that only finite and non-negative `decimals` values are accepted for balance calculations. It prevents stale metadata with negative decimals from affecting balance resolution, ensuring that the chain-status stub is used correctly. Additionally, tests have been added to verify this behavior, and the changelog has been updated accordingly.
This update refines the `#hasValidDecimals` method by consolidating the validation logic for asset metadata decimals into a single line. This change enhances code readability while maintaining the existing functionality that ensures only valid, non-negative decimals are accepted.
bergarces
approved these changes
May 13, 2026
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.

…nces
Two fixes that together stop entries like
eip155:4326/erc20:0x000…000: { amount: "NaN" }from leaking intoassetsBalance.#collectMetadataForBalances: only reuse existing native metadata when it has finitedecimals(via new#hasValidDecimalshelper). Emptyname/symbolare still accepted, anddecimals: 0is valid. Bad state metadata (e.g.{ decimals: null, name: '', symbol: '', type: 'native' }) is replaced by the chain-status stub so the pipeline resolves a usabledecimals.#convertToHumanReadable: defensively return'0'whendecimalsisn't a finite non-negative number or whenrawBalancecan't be parsed. Defaulting decimals to a fixed value (e.g. 18) would silently produce wrong amounts;'0'matches the existing safe fallback used in the error path.Adds 7 tests covering the bug payload (
decimals: null),NaNdecimals, negative decimals, non-numeric raw balance, and thedecimals: 0/ missingname/symbolvalid cases.Explanation
References
Checklist
Note
Medium Risk
Touches balance formatting and native metadata selection in
RpcDataSource, which can affect emittedassetsBalance/assetsInfovalues across chains and accounts. Changes are defensive and well-tested but could alter behavior for edge-case metadata payloads.Overview
Prevents
RpcDataSourcefrom emitting{ amount: "NaN" }(or silently wrong'0') balances when native asset metadata in state contains invaliddecimals.Native metadata reuse is now gated by a new
#hasValidDecimals(finite, non-negative;0allowed), otherwise falling back to the chain-status stub, and decimals selection no longer uses??—a new#pickValidDecimalsskipsNaN/negative values so stale state can’t shadow valid pipeline metadata.#convertToHumanReadablenow returns'0'whendecimalsis invalid or the raw balance is unparsable, and the PR adds focused unit tests covering null/NaN/negative decimals and invalid raw balances (plus validdecimals: 0cases).Reviewed by Cursor Bugbot for commit 37b8e8c. Bugbot is set up for automated code reviews on this repo. Configure here.