MetaMask / MetaMask/metamask-extension

[State Sync Redesign 2.3] Simplify `getState()` to iterate `controllersByName`

Open
#40,054 0 comments 0 reactions 0 assignees View on GitHub
INVALID-ISSUE-TEMPLATE team-extension-platform
Dominant language
TypeScript
Stars
13.2k
Forks
5.6k
Avg merge
2d 5h
Merged PRs (30d)
451

Description

`getState()` currently calls `memStore.getFlatState()`. Replace with direct iteration over `controllersByName`, merging `controller.state` for each. Also update Sentry's `getSentryAppState` in `background.js`.

## Steps

1. Implement `getState()` as `Object.fromEntries(controllersByName.entries().map(...))`
2. Decide: return flat (for backward compat during migration) or controller-keyed
3. Update `setupSentryGetStateGlobal` in `background.js` to use new `getState()`

## Additional consumers identified (PoC)

The PoC branch (`jongsun/poc/sync-state-redesign`) identified additional flat-state consumers that depend on `getState()` returning flat state:

- **`_getMetaMaskState()`**: Used by `getIsSmartTransaction` and `selectAllEnabledNetworkClientIds` selectors. These selectors expect `{ metamask: flatState }` shape.
- **`createEip1193MethodMiddleware`**: Receives `metamaskState: this.getState()` as a one-time snapshot. See #40182.
- **`controller-init` `getFlatState`**: `transaction-controller-init.ts` passes `getFlatState()` to smart transaction helpers. See #40181.
- **`createCancelTransaction` / `createSpeedUpTransaction`**: Return `this.getState()` to UI callers. With per-controller subscriptions, these no longer need to return state.
- **`_trackTransactionFailure`**: Reads `metamaskState.accounts` — can be replaced with direct `accountTrackerController.state.accounts`.

## Sub-issues

- #40181 — Remove flat state from `controller-init` smart transaction helpers
- #40182 — Remove flat state from `createEip1193MethodMiddleware`

## Files

- `app/scripts/metamask-controller.js`
- `app/scripts/background.js`

## Risk

Low — lazy read, not subscription-based.

## Dependencies

None

## Context

Part of [State Sync Redesign — Epic 2: Background cleanup].

Contributor guide

Open the contributing guide

Research direction

Start in app/scripts/metamask-controller.js with getState(), controllersByName, and the listed flat-state consumers; then inspect setupSentryGetStateGlobal in app/scripts/background.js. Review sub-issues #40181 and #40182 before choosing the migrated state shape. Done means getState and Sentry use the agreed shape without breaking the identified consumers.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
backend
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.