fix(perps): add spotMeta caching to reduce API calls on HIP-3 markets #25492
+368
−17
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.


Description
fix(perps): add spotMeta caching to reduce API calls on HIP-3 markets
This PR adds session-based caching for HyperLiquid's global
spotMetaendpoint to avoid redundant API calls during HIP-3 operations.Context
Following the rate limiting incident where excessive API calls triggered HyperLiquid's rate limits (2000 msg/min), this is a defensive improvement to reduce unnecessary network requests.
Problem
The
spotMetaAPI (which returns token metadata like USDC/USDH indices) was being called multiple times per trading session:getUsdcTokenId()- called during transfersisUsdhCollateralDex()- called to check collateral typeswapUsdcToUsdh()- called during HIP-3 USDH swapsEach call was making a fresh API request, even though the data (token indices) doesn't change during a session.
Solution
cachedSpotMetaproperty for session-based caching (no TTL - token indices are stable)getCachedSpotMeta()method that returns cached data or fetches onceensureReadyForTrading()when HIP-3 is enabled (non-blocking)disconnect()to ensure fresh state on reconnect/account switchDesign Decisions
spotMetais a global endpoint returning all tokensgetCachedMeta(),getCachedPerpDexs()Changelog
CHANGELOG entry: Fixed excessive API calls on HIP-3 markets by caching spot metadata
Related issues
Fixes: add spotMeta caching to reduce API calls on HIP-3 markets
Manual testing steps
Screenshots/Recordings
N/A - Internal optimization, no UI changes
Before
Multiple
spotMetaAPI calls per session (one per HIP-3 operation)After
Single
spotMetaAPI call per session, cached for subsequent operationsPre-merge author checklist
Pre-merge reviewer checklist
Note
Medium Risk
Touches perps trading setup and HIP-3 order/transfer paths by introducing session caching and prefetching of
spotMeta; bugs here could affect order placement after reconnect or when token metadata is unexpectedly stale.Overview
Reduces HyperLiquid API traffic during HIP-3 trading by introducing a session cache for
spotMeta(viagetCachedSpotMeta()), prefetching it inensureReadyForTrading(), and reusing it ingetUsdcTokenId(),isUsdhCollateralDex(), andswapUsdcToUsdh(); the cache is cleared ondisconnect().Improves error reporting by updating
ensureError()to convertnull/undefinedinto a consistent descriptiveError, and updates provider tests/mocks to match theSpotMetaResponseshape (tokenindexanduniverse).Written by Cursor Bugbot for commit a7d00a0. This will update automatically on new commits. Configure here.