Skip to content

fix(earn-controller): reduce redundant/eager staking API calls on unlock - #9804

Open
juanmigdr wants to merge 3 commits into
mainfrom
juan/reduce-earn-controller-duplicate-fetches
Open

fix(earn-controller): reduce redundant/eager staking API calls on unlock#9804
juanmigdr wants to merge 3 commits into
mainfrom
juan/reduce-earn-controller-duplicate-fetches

Conversation

@juanmigdr

@juanmigdr juanmigdr commented Aug 7, 2026

Copy link
Copy Markdown
Member

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:

  1. 3 duplicate calls: AccountTreeController:selectedAccountGroupChange fires again ~570ms after init() during startup hydration, resolving to the same address init() already refreshed — re-triggering refreshEarnEligibility + refreshPooledStakes + refreshLendingPositions with 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.
  2. 4 calls for the Hoodi testnet on every unlock: #supportedPooledStakingChains unconditionally includes ChainId.HOODI alongside ChainId.ETHEREUM, so init() and NetworkController:networkDidChange always 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 passes chainId: 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

  • I've updated the test suite for the affected packages
  • I've updated the documentation (comments, CHANGELOG.md, etc.) for the affected packages
  • I've highlighted breaking changes using the "BREAKING" label (N/A — no consumer-facing API changes)

Note

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 EarnController refreshes data.

Account group changes: Tracks #lastRefreshedAddress (set in #refreshEarnPortfolio and before account-group refreshes). On AccountTreeController:selectedAccountGroupChange, it skips refreshEarnEligibility, refreshPooledStakes, and refreshLendingPositions when the resolved EVM address matches the last refreshed address—avoiding duplicate work after init() during startup hydration. Real switches still refresh when the address changes.

Eager prefetch: #supportedPooledStakingChains is now only [ChainId.ETHEREUM] instead of ETH + Hoodi, so init() and network-change paths no longer prefetch Hoodi testnet staking for every user. Hoodi remains available via explicit chainId on 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.

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
juanmigdr requested review from a team as code owners August 7, 2026 16:43
@juanmigdr juanmigdr added the area-performance Issues relating to slowness of app, cpu usage, and/or blank screens. label Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-performance Issues relating to slowness of app, cpu usage, and/or blank screens.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant