-
-
Notifications
You must be signed in to change notification settings - Fork 275
fix: custom token balances #7879
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
fix: custom token balances #7879
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
| const balanceKey = `${chainId}-${checksummedAccount}-${tokenAddress}`; | ||
| const hasBalance = balanceKeys.has(balanceKey); | ||
| if (!hasBalance) { | ||
| chainsWithMissingTokens.add(chainId); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fallback detection defeated by API fetcher's zero-fill entries
High Severity
The balanceKeys set is built from all of result.balances, which includes zero-fill entries that the AccountsApiBalanceFetcher adds (via getUserTokens / this.state.tokenBalances) for tokens the API didn't return. After the first successful RPC fallback stores a real balance in state, every subsequent poll sees the custom token in balanceKeys (as a zero entry), so chainsWithMissingTokens is never populated and RPC fallback never triggers again. The custom token balance then reverts to 0x0 permanently.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great catch, tokens with aggregators: ["Dynamic"] are treated as custom tokens and are not returned by the Accounts API balance endpoint. ApiBalanceFetcher zero-fills these tokens to maintain a consistent token list.
Because of this, result.balances contains synthetic entries for custom tokens, and using it to detect missing balances prevents RPC fallback from triggering after the first successful fetch. The fallback decision must be derived from API-returned balances prior to zero-fill.


Explanation
At this moment the custom token balances on chains like Base and BNB are not showing correct token amounts on MetaMask token section. I traced the error to
TokenBalancesControllerwhere AccountsAPI fetcher returns balances but misses custom tokens. The controller now detects this and triggers an RPC fallback for those specific tokensThis change fixes the problem that I was facing on Base and BNB chains
References
This fixes the problem that I faced and submitted the ticket #74726250 to Technical Support
Checklist
Note
Medium Risk
Touches token-balance aggregation and fetcher fallback logic, which can affect balance correctness and RPC/API call volume across chains/accounts.
Overview
Fixes missing custom token balances on Accounts API-supported chains by detecting when the API response omits balances for tokens present in controller state and re-adding those chains to
remainingChainsso the RPC fetcher can backfill them (scoped to only the accounts actually queried, respectingqueryAllAccounts).Adds a focused unit test covering multi-account vs selected-account behavior for custom token balance fetching, updates the
nockimport style in tests, and documents the fix in the package changelog.Written by Cursor Bugbot for commit 33db82d. This will update automatically on new commits. Configure here.