Commit cf57bcc
fix(perps): Resolve 429 errors and improve session performance cp-7.59.0 (#22242)
## **Description**
This PR fixes 429 rate limit errors and improves Perps trading
performance through two complementary changes:
1. **Dual Transport Architecture**: Separates HTTP and WebSocket
transports to match their intended use cases
2. **Session-Based Caching**: Eliminates redundant API calls during user
sessions
### What is the reason for the change?
**Problem 1: 429 Rate Limit Errors After SDK Upgrade (TAT-1974)**
HyperLiquid SDK v0.25.9 defaulted all operations to WebSocket transport,
causing 429 errors during normal trading. Write operations (orders,
cancellations) were exhausting WebSocket rate limits that should be
reserved for real-time subscriptions.
**Problem 2: Redundant API Calls (TAT-2022)**
Builder fee approval, referral setup, and market metadata were called
repeatedly during each session, adding unnecessary latency and rate
limit pressure.
### What is the improvement/solution?
**Solution 1: Dual Transport Architecture (TAT-1974)**
Separated HTTP and WebSocket transports to match their intended use:
- **HTTP transport**: Request/response operations (orders, queries,
account data)
- **WebSocket transport**: Real-time subscriptions only (price feeds,
position updates)
**Benefits:**
- Eliminates 429 errors (separate rate limit pools)
- Improved reliability (write operations don't compete with
subscriptions)
**Solution 2: Session-Based Caching (TAT-2022)**
Moved repeated API calls to once-per-session initialization:
- **Builder fee approval**: Called once during initialization instead of
per-order
- **Referral setup**: Non-blocking fire-and-forget pattern
- **Market metadata**: Single shared cache for all operations
**Benefits:**
- Reduced API calls (1 vs many per session)
- Lower latency (no per-order network overhead)
- Improved reliability (referral failures don't block orders)
## **Changelog**
CHANGELOG entry: Fixed 429 rate limit errors by separating
HTTP/WebSocket transports and improved Perps trading performance through
session-based caching
## **Related issues**
Fixes: https://consensyssoftware.atlassian.net/browse/TAT-1974
Fixes: https://consensyssoftware.atlassian.net/browse/TAT-2022
## **Manual testing steps**
```gherkin
Feature: Dual transport architecture and session-based caching for Perps trading
Scenario: Write operations use HTTP transport
Given user opens Perps and initializes connection
When user places multiple orders rapidly
Then orders should complete successfully without 429 errors
And WebSocket subscriptions should remain active for price updates
Scenario: Session caching reduces redundant API calls
Given user opens Perps and initializes connection
When user places multiple orders
Then builder fee approval should only be called once
And referral setup should happen in background (non-blocking)
And meta responses should be cached and reused
When user disconnects or switches account
Then caches should be cleared and reinitialized
```
## **Screenshots/Recordings**
Backend performance optimization (no UI changes).
https://github.com/user-attachments/assets/6b5014e0-2c3f-41c2-b1ce-69ef4171128d
Demo on iOS/Android showing functionality still works correctly.
## **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.
## **Technical Implementation Details**
<details>
<summary>For reviewers: Implementation overview</summary>
### Files Modified:
**Transport Architecture (TAT-1974):**
1. `HyperLiquidClientService.ts` - Dual transport creation and injection
2. `HyperLiquidClientService.test.ts` - Transport configuration tests
**Session-Based Caching (TAT-2022):**
3. `HyperLiquidProvider.ts` - Session cache implementation and
initialization flow
4. `HyperLiquidProvider.test.ts` - Updated test expectations
### Test Coverage:
- **Transport Architecture**: 32 tests passing
- **Session Caching**: 244 tests passing
- **Total**: 276 tests passing
</details>
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> Splits SDK transport into HTTP (Info/Exchange) and WebSocket
(Subscription), adds session-scoped caching for meta, referral, and
builder-fee approval, refactors provider to use cached meta across
methods, updates tests, and bumps SDK.
>
> - **Perps Backend (HyperLiquidProvider.ts)**
> - Introduces shared `getCachedMeta()` and replaces prior market cache;
used in `placeOrder`, `editOrder`, `closePositions`, `getMarkets`,
`getMaxLeverage`, `getAvailableHip3Dexs`, etc.
> - Adds session caches for referral and builder-fee approval;
initializes once in `ensureReady()` and clears on `disconnect()`.
> - Refactors market fetching API to `{ dex, skipFilters, skipCache }`
and updates internal callers.
> - Improves meta validation and error messaging; switches
TP/SL/referral to non-blocking behavior where applicable.
> - Adds HIP‑3 balance handling helpers and post-order
rebalance/rollback flow (uses cached meta and session state).
> - **SDK Client Service (HyperLiquidClientService.ts)**
> - Implements dual transports: `HttpTransport` for
`ExchangeClient`/`InfoClient`, `WebSocketTransport` for
`SubscriptionClient`.
> - Updates logging and disconnect to close only WebSocket transport;
exposes connection state helpers.
> - **Tests**
> - Updates provider tests for new error messages (e.g., "Invalid meta
response"), non-blocking referral, and session-cached builder-fee
approval (verifies single approval across orders).
> - Updates client service tests to validate dual transports, config,
and disconnect behavior.
> - **Dependencies**
> - Bumps `@nktkas/hyperliquid` to `^0.25.9` (lockfile updated).
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
b85d046. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
---------
Co-authored-by: Nicholas Smith <nick.smith@consensys.net>1 parent 7e7938a commit cf57bcc
File tree
6 files changed
+435
-236
lines changed- app/components/UI/Perps
- controllers/providers
- services
6 files changed
+435
-236
lines changedLines changed: 40 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2616 | 2616 | | |
2617 | 2617 | | |
2618 | 2618 | | |
2619 | | - | |
| 2619 | + | |
2620 | 2620 | | |
2621 | 2621 | | |
2622 | 2622 | | |
| |||
2634 | 2634 | | |
2635 | 2635 | | |
2636 | 2636 | | |
2637 | | - | |
| 2637 | + | |
2638 | 2638 | | |
2639 | 2639 | | |
2640 | 2640 | | |
| |||
4266 | 4266 | | |
4267 | 4267 | | |
4268 | 4268 | | |
4269 | | - | |
| 4269 | + | |
| 4270 | + | |
4270 | 4271 | | |
4271 | 4272 | | |
4272 | 4273 | | |
4273 | 4274 | | |
4274 | 4275 | | |
4275 | 4276 | | |
4276 | 4277 | | |
4277 | | - | |
4278 | | - | |
4279 | | - | |
4280 | | - | |
4281 | | - | |
4282 | | - | |
| 4278 | + | |
| 4279 | + | |
| 4280 | + | |
| 4281 | + | |
| 4282 | + | |
| 4283 | + | |
| 4284 | + | |
| 4285 | + | |
| 4286 | + | |
| 4287 | + | |
| 4288 | + | |
| 4289 | + | |
4283 | 4290 | | |
4284 | 4291 | | |
4285 | 4292 | | |
| |||
4450 | 4457 | | |
4451 | 4458 | | |
4452 | 4459 | | |
4453 | | - | |
| 4460 | + | |
4454 | 4461 | | |
4455 | 4462 | | |
4456 | 4463 | | |
| |||
4483 | 4490 | | |
4484 | 4491 | | |
4485 | 4492 | | |
4486 | | - | |
4487 | | - | |
| 4493 | + | |
| 4494 | + | |
| 4495 | + | |
4488 | 4496 | | |
4489 | 4497 | | |
4490 | 4498 | | |
| |||
4574 | 4582 | | |
4575 | 4583 | | |
4576 | 4584 | | |
| 4585 | + | |
| 4586 | + | |
| 4587 | + | |
| 4588 | + | |
| 4589 | + | |
4577 | 4590 | | |
4578 | 4591 | | |
4579 | 4592 | | |
| |||
4675 | 4688 | | |
4676 | 4689 | | |
4677 | 4690 | | |
| 4691 | + | |
| 4692 | + | |
| 4693 | + | |
| 4694 | + | |
| 4695 | + | |
4678 | 4696 | | |
4679 | 4697 | | |
4680 | 4698 | | |
| |||
4993 | 5011 | | |
4994 | 5012 | | |
4995 | 5013 | | |
| 5014 | + | |
| 5015 | + | |
| 5016 | + | |
| 5017 | + | |
| 5018 | + | |
4996 | 5019 | | |
4997 | 5020 | | |
4998 | 5021 | | |
| |||
5073 | 5096 | | |
5074 | 5097 | | |
5075 | 5098 | | |
| 5099 | + | |
| 5100 | + | |
| 5101 | + | |
| 5102 | + | |
| 5103 | + | |
5076 | 5104 | | |
5077 | 5105 | | |
5078 | 5106 | | |
| |||
0 commit comments