MetaMask / MetaMask/metamask-extension

[`MetamaskController` B.5] Complete `ProviderEngineFactory` extraction

Open
#41,751 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

for-migration INVALID-ISSUE-TEMPLATE mig-epic-66-child team-extension-platform
Dominant language
TypeScript
Stars
13.2k
Forks
5.6k
Avg merge
2d 5h
Merged PRs (30d)
451

Description

**Sub-epic:** [#41737 — `MetamaskController` decomposition: extension-specific modules](https://github.com/MetaMask/metamask-extension/issues/41737)
**Files:**
- `app/scripts/lib/provider-engine/index.ts` (new)
- `app/scripts/lib/provider-engine/provider-engine.test.ts` (new)
- `app/scripts/metamask-controller.js`

**Size:** L

## Problem

Provider-engine construction lives inline on `MetamaskController` across three method bodies: `setupProviderEngineEip1193`, `setupProviderEngineCaip`, and `setupCommonMiddlewareHooks`. Extract them into a single `ProviderEngineFactory` under `provider-engine/` so the middleware stack is independently constructible, testable, and consumable by `connection-manager` (B.2) via dependency injection.

An earlier exploration sketched the factory shape (`ProviderEngineDependencies` type and method signatures) in [commit `1329be94ef`](https://github.com/MetaMask/metamask-extension/commit/1329be94ef); use it as an implementation hint when shaping the new module, but the extraction itself starts from the current `MetamaskController` method bodies on `main`.

## Solution

1. Create `app/scripts/lib/provider-engine/` with `index.ts` exporting `ProviderEngineFactory` and a `ProviderEngineDependencies` type.
2. Port each of the three method bodies into the factory, preserving middleware ordering.
3. Rewrite `this.fooController.bar(...)` as `messenger.call(...)` where an action exists; remaining references become typed fields on `ProviderEngineDependencies`.
4. Update `MetamaskController`'s call sites to delegate to the factory.
5. Delete the three method definitions from `metamask-controller.js`.
6. If `index.ts` becomes unwieldy during the port (~900+ lines), split into `eip1193.ts` / `caip.ts` / `common-hooks.ts`.
7. Add unit tests for middleware composition.

## Acceptance criteria

- [ ] `app/scripts/lib/provider-engine/index.ts` exists with `buildEip1193Engine` and `buildCaipEngine` fully bodied (not throwing)
- [ ] `MetamaskController`'s `setupProviderEngineEip1193`, `setupProviderEngineCaip`, and `setupCommonMiddlewareHooks` definitions deleted; call sites delegate to the factory
- [ ] `connection-manager` (B.2) consumes the factory via injected dep, not via a `MetamaskController` method reference
- [ ] `yarn test` green, `tsc` green, extension dev build boots

## Dependencies

**B.2** — needs `ConnectionManager` caller interface stable.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the three provider-engine methods in app/scripts/metamask-controller.js and review commit 1329be94ef for the proposed factory shape. Create and test app/scripts/lib/provider-engine/index.ts, preserving middleware ordering, then update the controller call sites and remove the old definitions. Run the provider-engine tests, yarn test, tsc, and the extension dev build to verify completion.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
api, backend
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.