Skip to content

Commit 95c90e9

Browse files
authored
docs(perps): perps controller isolation update (MetaMask#25959)
<!-- Please submit this PR as a draft initially. Do not mark it as "Ready for review" until the template has been completely filled out, and PR status checks have passed at least once. --> ## **Description** <!-- Write a short description of the changes included in this pull request, also include relevant motivation and context. Have in mind the following questions: 1. What is the reason for the change? 2. What is the improvement/solution? --> Update perps docs to refer to new ADR-42 arch for perps. ## **Changelog** <!-- If this PR is not End-User-Facing and should not show up in the CHANGELOG, you can choose to either: 1. Write `CHANGELOG entry: null` 2. Label with `no-changelog` If this PR is End-User-Facing, please write a short User-Facing description in the past tense like: `CHANGELOG entry: Added a new tab for users to see their NFTs` `CHANGELOG entry: Fixed a bug that was causing some NFTs to flicker` (This helps the Release Engineer do their job more quickly and accurately) --> CHANGELOG entry: null ## **Related issues** Fixes: ## **Manual testing steps** ```gherkin Feature: my feature name Scenario: user [verb for user action] Given [describe expected initial app state] When user [verb for user action] Then [describe expected outcome] ``` ## **Screenshots/Recordings** <!-- If applicable, add screenshots and/or recordings to visualize the before and after of your change. --> ### **Before** <!-- [screenshots/recordings] --> ### **After** <!-- [screenshots/recordings] --> ## **Pre-merge author checklist** - [x] I've followed [MetaMask Contributor Docs](https://github.com/MetaMask/contributor-docs) and [MetaMask Mobile Coding Standards](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/CODING_GUIDELINES.md). - [x] I've completed the PR template to the best of my ability - [x] I've included tests if applicable - [x] I've documented my code using [JSDoc](https://jsdoc.app/) format if applicable - [x] I've applied the right labels on the PR (see [labeling guidelines](https://github.com/MetaMask/metamask-mobile/blob/main/.github/guidelines/LABELING_GUIDELINES.md)). Not required for external contributors. ## **Pre-merge reviewer checklist** - [ ] I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed). - [ ] I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots. <!-- CURSOR_SUMMARY --> --- > [!NOTE] > **Low Risk** > Documentation-only changes (path updates, structure clarification, and deletion of a migration doc) with no runtime or behavioral impact. > > **Overview** > Updates Perps docs to reflect the controller/UI split introduced by ADR-042, pointing architectural references and “related files” to `app/controllers/perps/*` instead of the former UI-layer controller paths. > > Expands `perps-architecture.md` with the new controller directory structure and multi-provider architecture notes (HyperLiquid + feature-flagged MYX via `AggregatedPerpsProvider`/routing), and removes the now-redundant `perps-controller-migration.md` document. > > <sup>Written by [Cursor Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit 82c04d3. This will update automatically on new commits. Configure [here](https://cursor.com/dashboard?tab=bugbot).</sup> <!-- /CURSOR_SUMMARY -->
1 parent 31a8e1a commit 95c90e9

6 files changed

Lines changed: 90 additions & 239 deletions

File tree

docs/perps/hyperliquid/order-types-reference.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ const availableBalance = state.marginSummary.accountValue;
408408
409409
**Codebase**:
410410
411-
- `app/components/UI/Perps/controllers/providers/HyperLiquidProvider.ts:765` - Production orders
411+
- `app/controllers/perps/providers/HyperLiquidProvider.ts:765` - Production orders
412412
- `app/components/UI/Perps/utils/hyperLiquidAdapter.ts:64-71` - Adapter patterns
413413
- `app/components/UI/Perps/Debug/HIP3DebugView.tsx:500,717` - Debug/testing
414414

docs/perps/perps-architecture.md

Lines changed: 84 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
The Perps feature enables perpetual futures trading in MetaMask Mobile. This document provides a high-level architectural overview of the codebase structure, key patterns, and references to detailed documentation.
66

7-
**Location**: `app/components/UI/Perps/`
7+
**Locations**:
8+
9+
- **Controller**: `app/controllers/perps/` — business logic, providers, services, types (portable, no mobile-specific imports)
10+
- **UI**: `app/components/UI/Perps/` — React components, hooks, views, contexts
811

912
## Quick Navigation
1013

@@ -49,21 +52,54 @@ graph TD
4952

5053
## Directory Structure
5154

55+
### Controller (`app/controllers/perps/`)
56+
57+
The controller is isolated from mobile-specific code and published as `@metamask/perps-controller`.
58+
See [ADR-042](https://github.com/MetaMask/core/blob/main/docs/adr/ADR-042-perps-controller-location.md) for the architectural decision.
59+
60+
```
61+
app/controllers/perps/
62+
├── PerpsController.ts - Main controller (state, lifecycle, provider mgmt)
63+
├── index.ts - Package entry point
64+
├── perpsErrorCodes.ts - Error code definitions
65+
├── selectors.ts - Redux state selectors
66+
├── providers/ - Protocol-specific implementations
67+
│ ├── HyperLiquidProvider.ts
68+
│ ├── MYXProvider.ts
69+
│ └── AggregatedPerpsProvider.ts
70+
├── services/ - Business logic services
71+
│ ├── AccountService.ts
72+
│ ├── TradingService.ts
73+
│ ├── MarketDataService.ts
74+
│ ├── EligibilityService.ts
75+
│ ├── DepositService.ts
76+
│ ├── DataLakeService.ts
77+
│ ├── RewardsIntegrationService.ts
78+
│ ├── FeatureFlagConfigurationService.ts
79+
│ ├── HyperLiquidClientService.ts
80+
│ ├── HyperLiquidSubscriptionService.ts
81+
│ ├── HyperLiquidWalletService.ts
82+
│ ├── MYXClientService.ts
83+
│ ├── TradingReadinessCache.ts
84+
│ └── ServiceContext.ts
85+
├── routing/ - Provider routing logic
86+
├── aggregation/ - Multi-provider aggregation
87+
├── types/ - TypeScript type definitions
88+
├── constants/ - Configuration values
89+
└── utils/ - Pure utility functions
90+
```
91+
92+
### UI (`app/components/UI/Perps/`)
93+
5294
```
53-
/Perps
95+
app/components/UI/Perps/
5496
├── components/ - Reusable UI components
5597
├── Views/ - Main screen-level components
5698
├── hooks/ - React hooks for data access and logic
5799
│ └── stream/ - WebSocket subscription hooks (real-time data)
58-
├── controllers/ - Business logic and Redux state
59-
│ └── providers/ - Protocol-specific implementations
60100
├── providers/ - React context providers
61-
├── services/ - External integrations (WebSocket, HTTP, wallet)
62-
├── utils/ - Pure utility functions
63-
├── types/ - TypeScript type definitions
64-
├── constants/ - Configuration values
65-
├── contexts/ - React contexts
66101
├── selectors/ - Redux selectors by domain
102+
├── contexts/ - React contexts
67103
├── styles/ - Shared style utilities
68104
├── Debug/ - Developer tools
69105
├── animations/ - Rive animation files
@@ -185,23 +221,35 @@ React hooks organized by category:
185221
- `usePerpsDepositStatus` - Deposit status tracking
186222
- `usePerpsWithdrawStatus` - Withdrawal status tracking
187223

188-
### Controllers
224+
### Controller (`app/controllers/perps/`)
189225

190-
Business logic and Redux state management:
226+
Business logic and Redux state management. Isolated from mobile-specific code — uses `PerpsPlatformDependencies` for dependency injection of platform-specific services (logging, metrics, feature flags, etc.).
191227

192-
- **PerpsController** (`controllers/PerpsController.ts`) - Main controller managing providers, orders, positions, market data
193-
- **PerpsProvider** (`controllers/providers/HyperLiquidProvider.ts`) - HyperLiquid protocol implementation
194-
- **Selectors** (`controllers/selectors.ts`) - Redux state selectors
195-
- **Error Codes** (`controllers/perpsErrorCodes.ts`) - Error code definitions
228+
- **PerpsController** (`app/controllers/perps/PerpsController.ts`) - Main controller managing providers, orders, positions, market data
229+
- **HyperLiquidProvider** (`app/controllers/perps/providers/HyperLiquidProvider.ts`) - HyperLiquid protocol implementation
230+
- **MYXProvider** (`app/controllers/perps/providers/MYXProvider.ts`) - MYX protocol implementation
231+
- **AggregatedPerpsProvider** (`app/controllers/perps/providers/AggregatedPerpsProvider.ts`) - Multi-protocol aggregation via `ProviderRouter`
232+
- **Selectors** (`app/controllers/perps/selectors.ts`) - Redux state selectors
233+
- **Error Codes** (`app/controllers/perps/perpsErrorCodes.ts`) - Error code definitions
196234

197-
### Services
235+
### Services (`app/controllers/perps/services/`)
198236

199-
External integrations and infrastructure:
237+
Business logic services instantiated with platform dependencies:
200238

201-
- **HyperLiquidClientService** - HTTP client for REST API
239+
- **AccountService** - Account state, balances, withdrawals
240+
- **TradingService** - Order placement, cancellation, position management
241+
- **MarketDataService** - Market info, candles, funding rates
242+
- **EligibilityService** - User eligibility and geo-blocking
243+
- **DepositService** - Deposit flow with transaction confirmation
244+
- **DataLakeService** - Historical data queries
245+
- **RewardsIntegrationService** - Rewards program integration
246+
- **FeatureFlagConfigurationService** - Remote feature flag configuration (HIP-3, geo-blocking)
247+
- **HyperLiquidClientService** - HTTP client for HyperLiquid REST API
202248
- **HyperLiquidSubscriptionService** - WebSocket subscription management
203-
- **HyperLiquidWalletService** - Wallet operations
204-
- **PerpsConnectionManager** - Connection lifecycle orchestration (singleton)
249+
- **HyperLiquidWalletService** - Wallet operations and signing
250+
- **MYXClientService** - HTTP client for MYX protocol
251+
- **TradingReadinessCache** - Cached trading readiness state
252+
- **PerpsConnectionManager** (`app/components/UI/Perps/services/`) - Connection lifecycle orchestration (mobile-specific singleton, stays in UI layer)
205253

206254
### Providers
207255

@@ -526,13 +574,27 @@ The codebase maintains high quality standards:
526574

527575
## Protocol Integration
528576

529-
Currently integrated with HyperLiquid protocol:
577+
Multi-protocol architecture with provider abstraction:
578+
579+
### HyperLiquid (primary)
530580

531581
- **REST API** - Account queries, order placement, market data
532582
- **WebSocket** - Real-time prices, order fills, position updates
533583
- **Wallet Integration** - Ethereum signing for orders
534584

535-
**See [hyperliquid/](./hyperliquid/) directory for protocol-specific documentation.**
585+
### MYX (feature-flagged)
586+
587+
- MYX protocol support via `MYXProvider` and `MYXClientService`
588+
- Enabled via `perpsMyxProviderEnabled` remote feature flag with version gating
589+
- When enabled alongside HyperLiquid, uses `AggregatedPerpsProvider` with `ProviderRouter`
590+
591+
### Multi-Protocol Architecture
592+
593+
- **`ProviderRouter`** (`routing/`) - Routes operations to the correct provider based on market
594+
- **`AggregatedPerpsProvider`** - Wraps multiple providers behind the `PerpsProvider` interface
595+
- **`SubscriptionMultiplexer`** - Merges WebSocket subscriptions from multiple providers
596+
597+
**See [hyperliquid/](./hyperliquid/) directory for HyperLiquid-specific documentation.**
536598

537599
## Migration Notes
538600

docs/perps/perps-controller-migration.md

Lines changed: 0 additions & 211 deletions
This file was deleted.

docs/perps/perps-feature-flags.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ The `minimumVersion` field ensures features only activate on compatible app vers
263263
- **Tests:** `app/components/UI/Perps/selectors/featureFlags/index.test.ts`
264264
- **Version validation:** `app/util/remoteFeatureFlag/index.ts`
265265
- **Controller init:** `app/core/Engine/controllers/remote-feature-flag-controller-init.ts`
266-
- **Configuration service:** `app/components/UI/Perps/controllers/services/FeatureFlagConfigurationService.ts`
266+
- **Configuration service:** `app/controllers/perps/services/FeatureFlagConfigurationService.ts`
267267

268268
---
269269

docs/perps/perps-metametrics-reference.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ track(MetaMetricsEvents.PERPS_UI_INTERACTION, {
4242

4343
### 2. Controller Tracking (Transactions)
4444

45-
**Location:** `app/components/UI/Perps/controllers/PerpsController.ts`
45+
**Location:** `app/controllers/perps/PerpsController.ts`
4646

4747
```typescript
4848
import MetaMetrics from '../../../../core/Analytics/MetaMetrics';
@@ -549,5 +549,5 @@ usePerpsEventTracking({
549549
- **Event Tracking Hook**: `app/components/UI/Perps/hooks/usePerpsEventTracking.ts`
550550
- **Events**: `app/core/Analytics/MetaMetrics.events.ts`
551551
- **Properties & Values**: `app/components/UI/Perps/constants/eventNames.ts`
552-
- **Controller**: `app/components/UI/Perps/controllers/PerpsController.ts`
553-
- **Trading Service**: `app/components/UI/Perps/controllers/services/TradingService.ts`
552+
- **Controller**: `app/controllers/perps/PerpsController.ts`
553+
- **Trading Service**: `app/controllers/perps/services/TradingService.ts`

0 commit comments

Comments
 (0)