Skip to content

feat(perps): setup perps controller dir#7654

Merged
abretonc7s merged 9 commits into
mainfrom
perps/setup
Jan 19, 2026
Merged

feat(perps): setup perps controller dir#7654
abretonc7s merged 9 commits into
mainfrom
perps/setup

Conversation

@abretonc7s

@abretonc7s abretonc7s commented Jan 19, 2026

Copy link
Copy Markdown
Contributor

Explanation

This PR introduces the initial package scaffolding for @metamask/perps-controller, a new controller for perpetual trading functionality in MetaMask.

Current state: Perpetual trading (perps) functionality currently lives in the metamask-mobile repository. To enable code sharing across platforms and follow the established controller pattern, we need to migrate this functionality to the core monorepo.

Solution: This is PR 1 of a migration series that creates the package structure and ownership setup:

  • Creates the packages/perps-controller/ directory with standard monorepo scaffolding
  • Sets up dual ESM/CJS exports via ts-bridge
  • Configures Jest testing with initial placeholder test
  • Adds CODEOWNERS entry for @MetaMask/perps team
  • Adds teams.json mapping for team-perps
  • Updates root tsconfig.build.json with package reference

This minimal scaffolding approach allows us to:

  1. Establish package ownership early
  2. Validate the build/test infrastructure
  3. Enable incremental migrations in subsequent PRs

References

  • Related to metamask-mobile perps controller migration effort
  • Part of the broader initiative to centralize controller logic in the core monorepo

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note: No breaking changes introduced. This is a new package with no existing consumers.


Note

Adds new package scaffolding for perpetuals.

  • New packages/perps-controller with PerpsController (extends BaseController), empty PerpsControllerState, messenger types, and index exports
  • Tooling: dual ESM/CJS build via ts-bridge, jest.config.js with placeholder tests, typedoc.json, LICENSE, CHANGELOG.md, package README
  • Monorepo wiring: updates README.md (package list + graph), tsconfig.json and tsconfig.build.json references, .github/CODEOWNERS, teams.json, and yarn.lock

Written by Cursor Bugbot for commit 1b80fb3. This will update automatically on new commits. Configure here.

@abretonc7s abretonc7s requested a review from a team as a code owner January 19, 2026 00:26
@cryptodev-2s

Copy link
Copy Markdown
Contributor

@abretonc7s an example you could follow for the init of the controller that we have recently reviewed and that uses all of our recommendations #7587

@abretonc7s

Copy link
Copy Markdown
Contributor Author

@abretonc7s an example you could follow for the init of the controller that we have recently reviewed and that uses all of our recommendations #7587

7a95554

@abretonc7s abretonc7s enabled auto-merge January 19, 2026 13:25

@cryptodev-2s cryptodev-2s left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some minor changes lefts, otherwise looks good to me!
Would mind also running the following command yarn update-readme-content https://github.com/MetaMask/core/blob/main/docs/processes/package-migration-process-guide.md#core to update the root readme and dependency graph

Comment thread packages/perps-controller/CHANGELOG.md Outdated
Comment thread tsconfig.build.json
Comment thread .gitignore Outdated
abretonc7s and others added 2 commits January 19, 2026 21:42
Co-authored-by: cryptodev-2s <109512101+cryptodev-2s@users.noreply.github.com>
Co-authored-by: cryptodev-2s <109512101+cryptodev-2s@users.noreply.github.com>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Comment thread tsconfig.build.json

@cryptodev-2s cryptodev-2s left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@abretonc7s abretonc7s added this pull request to the merge queue Jan 19, 2026
Merged via the queue into main with commit 3d88d12 Jan 19, 2026
298 checks passed
@abretonc7s abretonc7s deleted the perps/setup branch January 19, 2026 13:57
github-merge-queue Bot pushed a commit that referenced this pull request Mar 3, 2026
…7941)

## Explanation

Syncs `PerpsController` from Mobile into Core as
`@metamask/perps-controller` for npm publishing.

**Architecture (latest):** Cross-controller communication uses the
standard `@metamask/messenger` pattern — `messenger.call()` and
`messenger.subscribe()`. Typed `PerpsControllerAllowedActions` /
`PerpsControllerAllowedEvents` define the contract. The only DI
remaining is `PerpsPlatformDependencies.rewards` (RewardsController is
not yet in Core) and platform-specific adapters (logger, metrics,
tracer, stream manager, etc.).

This addresses Mark Stacey's concern from the ADR-42 review:
PerpsController uses the same messenger architecture as all other Core
controllers, with no special DI bridging for controller-to-controller
calls.

**Messenger actions used:**

| Action | Controller |
|--------|-----------|
| `AccountTreeController:getAccountsFromSelectedAccountGroup` |
AccountTreeController |
| `KeyringController:getState` | KeyringController |
| `KeyringController:signTypedMessage` | KeyringController |
| `NetworkController:getState` | NetworkController |
| `NetworkController:getNetworkClientById` | NetworkController |
| `NetworkController:findNetworkClientIdByChainId` | NetworkController |
| `TransactionController:addTransaction` | TransactionController |
| `AuthenticationController:getBearerToken` | AuthenticationController |
| `RemoteFeatureFlagController:getState` | RemoteFeatureFlagController |

**Messenger events subscribed:**
- `RemoteFeatureFlagController:stateChange`
- `AccountTreeController:selectedAccountGroupChange`

**Approach:** Mobile remains the source of truth. A sync script
(`validate-core-sync.sh`) copies controller source to this package,
verifies build + lint, and writes a `.sync-state.json` with commit
hashes and source checksum for conflict detection. This is a
transitional mechanism that goes away when Mobile folds into Core as
`apps/mobile`.

**Key components:**
- `PerpsController` — main controller with state management, messenger
integration, and multi-provider orchestration
- `HyperLiquidProvider` / `MYXProvider` — DEX-specific provider
implementations
- `AggregatedPerpsProvider` — multi-provider aggregation layer
- `ProviderRouter` — routes operations to the appropriate provider
- `SubscriptionMultiplexer` — real-time WebSocket data aggregation
- 13 services: Trading, MarketData, Account, Deposit, Eligibility,
FeatureFlagConfiguration, HyperLiquidClient, HyperLiquidSubscription,
HyperLiquidWallet, MYXClient, DataLake, RewardsIntegration,
TradingReadinessCache
- `PerpsPlatformDependencies` — platform-agnostic injection interface
(rewards, logger, metrics, tracer, stream manager, feature flags, market
data formatters, cache invalidator)

**CI / Testing:** A minimal placeholder test
(`tests/placeholder.test.ts`) is included to satisfy Core's CI
requirement. Full unit tests remain in Mobile (27 suites, 1295 tests
passing) and will be migrated when development moves fully to Core.

## References

- Builds on #7654 (initial `perps-controller` package scaffolding)
- Supersedes #7749 (closed — first full migration attempt)
- Related: #7841 (draft — preview publishing test)
- Mobile messenger migration:
[metamask-mobile@feat/perps/di-refactor-remove-controller-deps](https://github.com/MetaMask/metamask-mobile/tree/feat/perps/di-refactor-remove-controller-deps)
- Mobile preparation:
[metamask-mobile#26064](MetaMask/metamask-mobile#26064)
(ESLint alignment + `validate-core-sync.sh`)
- Mobile isolation:
[metamask-mobile#25813](MetaMask/metamask-mobile#25813)
(isolated perps code, converted imports)
- Architecture position: [PerpsController Location — Position v2
(Post-DI)](https://consensyssoftware.atlassian.net/wiki/spaces/PRDC/pages/400956981249/PerpsController+package+Location+Position+v2+Post-DI)

## Checklist

- [x] I've updated the test suite for new or updated code as appropriate
- Placeholder test included for CI. Full unit tests remain in Mobile and
will be migrated when development moves fully to Core.
- [x] I've updated documentation (JSDoc, Markdown, etc.) for new or
updated code as appropriate
- [x] I've communicated my changes to consumers by [updating changelogs
for packages I've
changed](https://github.com/MetaMask/core/tree/main/docs/processes/updating-changelogs.md)
- [ ] I've introduced [breaking
changes](https://github.com/MetaMask/core/tree/main/docs/processes/breaking-changes.md)
in this PR and have prepared draft pull requests for clients and
consumer packages to resolve them
- No breaking changes — messenger migration is internal; existing public
API is preserved

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **High Risk**
> Large change that introduces/rewires perps trading flows, WebSocket
subscriptions, feature-flagged provider switching, and transaction
submission via `TransactionController`, increasing risk of runtime, data
consistency, and funds-impacting regressions. Testing/coverage is
intentionally reduced to a placeholder in Core, so validation relies on
Mobile’s test suite.
> 
> **Overview**
> Replaces the stub `PerpsController` with the full synced
implementation, expanding controller state/metadata and exposing a broad
messenger API for perps operations (orders, positions, funding,
eligibility, deposits/withdrawals, provider/network switching, and
live-data subscriptions).
> 
> Adds multi-provider support (HyperLiquid default, MYX behind a feature
flag, and an `AggregatedPerpsProvider` mode), plus background
market/user-data preloading and stream-pausing helpers to reduce UI
races during trading operations.
> 
> Updates packaging for publishing: adds a sync provenance file, expands
runtime/dev dependencies for provider + cross-controller integrations,
adjusts Jest coverage to only include `tests/placeholder.test.ts`, and
removes the prior controller unit test while updating the changelog to
document the full initial release contents.
> 
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
2fbf70d. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Co-authored-by: Nick Gambino <35090461+gambinish@users.noreply.github.com>
Co-authored-by: Nicholas Gambino <nicholas.gambino@consensys.net>
Co-authored-by: Nick Gambino <ngambino0192@gmail.com>
Co-authored-by: Elliot Winkler <elliot.winkler@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants