fix(earn-controller): reduce redundant/eager staking API calls on unlock - #9804
Open
juanmigdr wants to merge 3 commits into
Open
fix(earn-controller): reduce redundant/eager staking API calls on unlock#9804juanmigdr wants to merge 3 commits into
juanmigdr wants to merge 3 commits into
Conversation
Wallet unlock was triggering ~14 staking/lending API calls, including 3 exact duplicates and 4 calls for the Hoodi testnet that most users never need. This addresses both, without changing any consumer-facing behavior for real account switches or explicit Hoodi requests: - Skip the AccountTreeController:selectedAccountGroupChange refresh when the resolved address is unchanged from the last refresh (this event fires again right after init() during startup hydration with the same address, producing 3 wasted duplicate calls). - Only eagerly prefetch pooled staking data for Ethereum Mainnet on startup/network change. Hoodi is still fully supported via explicit chainId calls, it is just no longer unconditionally prefetched.
juanmigdr
temporarily deployed
to
default-branch
August 7, 2026 16:43 — with
GitHub Actions
Inactive
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
Investigating why wallet unlock in MetaMask Mobile fired so many staking/lending API requests (~14 in one capture, all within ~1.5s), a runtime log trace of
EarnController's trigger points confirmed two sources of waste that this PR fixes:AccountTreeController:selectedAccountGroupChangefires again ~570ms afterinit()during startup hydration, resolving to the same addressinit()already refreshed — re-triggeringrefreshEarnEligibility+refreshPooledStakes+refreshLendingPositionswith no new information. Fixed by tracking the last address that was actually refreshed and skipping the group-change refresh when the resolved address hasn't changed. Real account switches are unaffected, since the address differs in that case.#supportedPooledStakingChainsunconditionally includesChainId.HOODIalongsideChainId.ETHEREUM, soinit()andNetworkController:networkDidChangealways eagerly fetch a full Hoodi staking dataset (stakes/vault/vault-apys/vault-apy-averages) for every user, most of whom never use it. Fixed by only eagerly prefetching Ethereum Mainnet; Hoodi remains fully supported for any caller that explicitly passeschainId: ChainId.HOODI(e.g. a user actually viewing a Hoodi-network staking screen), it's just no longer prefetched unconditionally.Net effect measured via the runtime log trace: 14 calls → 7 calls on a typical unlock, with no change in behavior for account switches or explicit Hoodi usage.
References
https://consensyssoftware.atlassian.net/browse/ASSETS-3857
Checklists
CHANGELOG.md, etc.) for the affected packagesNote
Low Risk
Behavioral optimization with no public API changes; Hoodi still works on explicit chainId, and account switches still trigger refreshes.
Overview
Cuts redundant earn/staking API traffic on wallet unlock by tightening when
EarnControllerrefreshes data.Account group changes: Tracks
#lastRefreshedAddress(set in#refreshEarnPortfolioand before account-group refreshes). OnAccountTreeController:selectedAccountGroupChange, it skipsrefreshEarnEligibility,refreshPooledStakes, andrefreshLendingPositionswhen the resolved EVM address matches the last refreshed address—avoiding duplicate work afterinit()during startup hydration. Real switches still refresh when the address changes.Eager prefetch:
#supportedPooledStakingChainsis now only[ChainId.ETHEREUM]instead of ETH + Hoodi, soinit()and network-change paths no longer prefetch Hoodi testnet staking for every user. Hoodi remains available via explicitchainIdon refresh APIs.Tests and changelog updated accordingly (fewer setup-time API calls, new no-op test for unchanged address).
Reviewed by Cursor Bugbot for commit ea13341. Bugbot is set up for automated code reviews on this repo. Configure here.