fix(assets-controller): increase stale and gc time for accounts api request#9591
Merged
Conversation
Kriys94
force-pushed
the
fix/AccountsAPIcache
branch
from
July 22, 2026 09:31
2b040cb to
2202087
Compare
Kriys94
marked this pull request as ready for review
July 22, 2026 09:33
Kriys94
force-pushed
the
fix/AccountsAPIcache
branch
3 times, most recently
from
July 22, 2026 09:59
0ed5ee7 to
e0e7950
Compare
Contributor
|
@metamaskbot publish-preview |
bergarces
previously approved these changes
Jul 23, 2026
Contributor
|
This is already the case for the v6 endpoint right? I have noticed that subsequent calls to it won't trigger a network request. |
salimtb
previously approved these changes
Jul 23, 2026
Contributor
|
i just tested this on the client and works as expected |
Contributor
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
Contributor
Author
Yeah that was the case for v6, event for v5. Actually, since we put |
Kriys94
force-pushed
the
fix/AccountsAPIcache
branch
from
July 23, 2026 08:45
e0e7950 to
1659296
Compare
salimtb
approved these changes
Jul 23, 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.
Explanation
Current behavior:

After PR:

Balance refreshes triggered on-demand go through
AssetsController.getAssets(..., { forceUpdate: true }), which flows down toAccountsApiDataSource.fetchas TanStack QueryfetchOptions. Previously theforceUpdatepath used{ staleTime: 0, gcTime: 0 }. These values has been added via #9265 but it was mainly to fix a websocket issue.That was too aggressive:
staleTime: 0marks the query as stale immediately, so every forced refresh refetches — even two identical requests fired microseconds apart.gcTime: 0evicts the cache entry the instant the query goes inactive (which, for imperativefetchQuery, is right after it resolves). Because the forced fetch uses the same queryKey as the 30s polling subscription, it not only bypasses the cache but also tears down the entry the poll would have reused.In practice almost every internal trigger calls
getAssetswithforceUpdate: true(unlock, account switch, network change, tx confirmation, price refresh, etc.), and several of these fire near-simultaneously. With0/0each trigger produced its own Accounts API request, causing bursts of duplicatemultiaccount/balancescalls.This PR changes the
forceUpdatepath to{ staleTime: 200, gcTime: 200 }. The small 200ms window lets a burst of near-simultaneous forced refreshes de-duplicate into a single Accounts API request while keeping the data effectively fresh (well within a user-perceptible refresh). KeepinggcTime >= staleTimealso ensures the entry survives its freshness window instead of being evicted immediately.References
N/A
Checklist
Note
Low Risk
Narrow change to query cache options for forced balance fetches; balances may share a single response within ~100ms, which is an intentional tradeoff for fewer API calls.
Overview
Forced balance refreshes (
getAssetswithforceUpdate: true→AccountsApiDataSource.fetch) no longer pass TanStack Query{ staleTime: 0, gcTime: 0 }. They now usestaleTimeandgcTimeof 100ms, so near-simultaneous triggers (unlock, account/network switch, tx confirmation, etc.) collapse to onemultiaccount/balancesrequest instead of a burst of duplicates, while still avoiding the default long-lived cache.The unit test and changelog are updated to match the new short-lived cache window (replacing the “fully bypass cache” expectation from #9265).
Reviewed by Cursor Bugbot for commit 1659296. Bugbot is set up for automated code reviews on this repo. Configure here.