-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
fix(perps): Resolve 429 errors and improve session performance cp-7.59.0 #22242
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
… market metadata - Updated caching mechanism for market metadata to improve efficiency and reduce redundant API calls. - Changed error messages for better clarity when fetching market metadata fails. - Refactored referral and builder fee approval processes to utilize session caching, ensuring they are only set once per session. - Improved tests to reflect changes in error handling and caching logic. This refactor aims to streamline the provider's performance and enhance user experience by minimizing unnecessary API requests.
|
CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Remove redundant checks after ensureReady in updatePositionTPSL
The updatePositionTPSL() method still explicitly calls ensureBuilderFeeApproval() and ensureReferralSet() after calling ensureReady(). This is inconsistent with the session-based caching optimization implemented in this PR. According to the PR description and the changes in placeOrder() (where these calls were removed), builder fee approval and referral setup should only happen once during ensureReady() initialization, not per-operation. These redundant calls in updatePositionTPSL() defeat the purpose of session-based caching and will still make unnecessary API calls on every TP/SL update. These lines should be removed to match the pattern used in placeOrder().
app/components/UI/Perps/controllers/providers/HyperLiquidProvider.ts#L2428-L2433
metamask-mobile/app/components/UI/Perps/controllers/providers/HyperLiquidProvider.ts
Lines 2428 to 2433 in c1d380b
| const userAddress = await this.walletService.getUserAddressWithDefault(); | |
| // Extract DEX name for API calls (main DEX = null) | |
| const { dex: dexName } = parseAssetName(coin); | |
| // Fetch current price for this asset's DEX |
- Changed referral code setup to be blocking, ensuring proper attribution while logging errors to Sentry for retries in the next session. - Introduced a new method to generate user-specific session cache keys for better organization. - Enhanced error messages for clarity and added context to error logging throughout the referral process. - Updated comments for better understanding of caching behavior and error handling. These changes aim to streamline the referral process and improve overall reliability in user sessions.
app/components/UI/Perps/controllers/providers/HyperLiquidProvider.ts
Outdated
Show resolved
Hide resolved
…val processes - Updated referral code setup to block initialization for better attribution, ensuring users can trade immediately even if setup fails. - Improved caching logic for builder fee approvals, now only caching successful approvals to avoid redundant checks. - Enhanced comments for clarity on caching behavior and error handling. These changes aim to streamline user sessions and improve overall reliability in the HyperLiquidProvider.
app/components/UI/Perps/controllers/providers/HyperLiquidProvider.ts
Outdated
Show resolved
Hide resolved
- Updated caching behavior to only store successful referral states, preventing redundant checks and allowing detection of external referral changes. - Enhanced comments for clarity on caching semantics and error handling. - Adjusted session cache updates to streamline the referral process and improve reliability. These changes aim to optimize user sessions and ensure accurate referral tracking in the HyperLiquidProvider.
app/components/UI/Perps/controllers/providers/HyperLiquidProvider.ts
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Cache Clearing Gap: Network Toggle Misuses Data
Session caches are not cleared when toggling between testnet and mainnet, which can cause cached data from one network to be incorrectly used on another network. The toggleTestnet() method should clear referralCheckCache, builderFeeCheckCache, and cachedMetaByDex since:
- Builder fee approval is network-specific (different builder addresses for testnet vs mainnet)
- Referral state is network-specific (different referral codes)
- Meta responses contain network-specific market data
This can lead to:
- Using mainnet builder fee approval cache on testnet
- Using testnet referral state on mainnet
- Using wrong market metadata for the current network
The fix should add cache clearing calls similar to the disconnect() method:
this.referralCheckCache.clear();
this.builderFeeCheckCache.clear();
this.cachedMetaByDex.clear();app/components/UI/Perps/controllers/providers/HyperLiquidProvider.ts#L5309-L5325
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
- Added `maxRetries` and `connectionTimeout` parameters to the reconnect configuration in `HyperLiquidClientService`. - Updated corresponding test to reflect these new options.
|



Description
This PR fixes 429 rate limit errors and improves Perps trading performance through two complementary changes:
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:
Benefits:
Solution 2: Session-Based Caching (TAT-2022)
Moved repeated API calls to once-per-session initialization:
Benefits:
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
Screenshots/Recordings
Backend performance optimization (no UI changes).
Simulator.Screen.Recording.-.iPhone.16.Pro.-.2025-11-05.at.11.57.28.mp4
Demo on iOS/Android showing functionality still works correctly.
Pre-merge author checklist
Pre-merge reviewer checklist
Technical Implementation Details
For reviewers: Implementation overview
Files Modified:
Transport Architecture (TAT-1974):
HyperLiquidClientService.ts- Dual transport creation and injectionHyperLiquidClientService.test.ts- Transport configuration testsSession-Based Caching (TAT-2022):
3.
HyperLiquidProvider.ts- Session cache implementation and initialization flow4.
HyperLiquidProvider.test.ts- Updated test expectationsTest Coverage:
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.
getCachedMeta()and replaces prior market cache; used inplaceOrder,editOrder,closePositions,getMarkets,getMaxLeverage,getAvailableHip3Dexs, etc.ensureReady()and clears ondisconnect().{ dex, skipFilters, skipCache }and updates internal callers.HttpTransportforExchangeClient/InfoClient,WebSocketTransportforSubscriptionClient.@nktkas/hyperliquidto^0.25.9(lockfile updated).Written by Cursor Bugbot for commit b85d046. This will update automatically on new commits. Configure here.