feat: refetch defi positions if they are still loading - #9711
Conversation
|
@metamaskbot publish-preview |
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
| */ | ||
| function delay(ms: number): Promise<void> { | ||
| return new Promise((resolve) => setTimeout(resolve, ms)); | ||
| } |
There was a problem hiding this comment.
We have eslint rules to stop us from using the setTimeout from timers/promises (probably because it won't transpile nicely).
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
|
@metamaskbot publish-preview |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ 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 cb83555. Configure here.
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
There was a problem hiding this comment.
Uses 5 seconds and 5 attempts as default, but it's configurable via the same feature flag used to determine if v2 is enabled.
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |

Explanation
When loading DeFi positions from an address. Accounts API v6 tends to not return DeFi positions and, instead, return an indicator that positions are loading for that account. That prevents the user from seeing the positions the first time they load the wallet.
With this changes, when DeFi positions are fetched, we will continue refetching (up to 5 times, with 5s intervals) until the positions are properly loaded. In the meantime, the user will see a loading spinner.
When it takes more than one attempt to fetch positions, it does capture an error with sentry using the messenger, but it does not throw.
Retry settings are controlled from the feature flag.
PRs with breaking change applied:
MetaMask/metamask-extension#45200
MetaMask/metamask-mobile#34265
refetch.defi.until.available.mp4
References
Fixes: https://consensyssoftware.atlassian.net/browse/ASSETS-3835
Checklist
Note
Medium Risk
Breaking messenger wiring plus changed fetch semantics (all-or-nothing state updates and polling) affect DeFi tab loading; mitigated by feature-flag tunability and extensive tests.
Overview
DeFiPositionsControllerV2.fetchDeFiPositionsnow polls when any selected account returnsprocessingDefiPositions, instead of skipping those accounts and optionally updating others. State is written only when every selected account is ready; mixed ready/processing responses keep prior positions. Between attempts the balances TanStack cache is invalidated and later attempts usestaleTime: 0so polls do not reuse stale “still processing” snapshots.Concurrency: calls for the same account set and
vsCurrencyshare one in-flight promise (usable as a loading signal); different selection or fiat starts a separate fetch while earlier polls can continue.forceRefreshonly bypasses cache on the first attempt.Tuning & ops: poll
maxAttempts/pollIntervalcome from thedefiControllerV2remote feature flag via newgetProcessingPollConfig(defaults 5 attempts, 5s). Sentry getsDeFiPositionsV2FetchAttemptswhen success required multiple tries andDeFiPositionsV2ProcessingPollExhaustedwhen the limit is hit while still processing.Breaking: integrators must allow and delegate
RemoteFeatureFlagController:getStateon the DeFi V2 messenger;@metamask/remote-feature-flag-controlleris added toassets-controllers.Reviewed by Cursor Bugbot for commit abbcfa6. Bugbot is set up for automated code reviews on this repo. Configure here.