Commit e2ca72f
chore(runway): cherry-pick fix(perps): add spotMeta caching to reduce API calls on HIP-3 markets (#25511)
- fix(perps): add spotMeta caching to reduce API calls on HIP-3 markets
cp-7.63.0 (#25493)
## **Description**
**fix(perps): add spotMeta caching to reduce API calls on HIP-3
markets**
This PR adds session-based caching for HyperLiquid's global `spotMeta`
endpoint to avoid redundant API calls during HIP-3 operations.
### Context
Following a 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 `spotMeta` API (which returns token metadata like USDC/USDH indices)
was being called multiple times per trading session:
- `getUsdcTokenId()` - called during transfers
- `isUsdhCollateralDex()` - called to check collateral type
- `swapUsdcToUsdh()` - called during HIP-3 USDH swaps
Each call was making a fresh API request, even though the data (token
indices) doesn't change during a session.
### Solution
- Added `cachedSpotMeta` property for session-based caching (no TTL -
token indices are stable)
- Added `getCachedSpotMeta()` method that returns cached data or fetches
once
- Pre-fetch spotMeta in `ensureReadyForTrading()` when HIP-3 is enabled
(non-blocking)
- Cache invalidated on `disconnect()` to ensure fresh state on
reconnect/account switch
### Design Decisions
- **Global cache** (not per-DEX): `spotMeta` is a global endpoint
returning all tokens
- **Session-based** (no TTL): Token indices don't change during a
session
- **Graceful fallback**: If pre-fetch fails, methods fetch on-demand
- Follows existing patterns: `getCachedMeta()`, `getCachedPerpDexs()`
## **Changelog**
CHANGELOG entry: Fixed excessive API calls on HIP-3 markets by caching
spot metadata
## **Related issues**
Fixes: N/A (Defensive improvement following rate limiting incident)
## **Manual testing steps**
```gherkin
Feature: SpotMeta caching for HIP-3 operations
Scenario: User places order on HIP-3 DEX (SILVER)
Given user has connected wallet with USDC balance
And user is on a HIP-3 enabled DEX (e.g., SILVER)
When user places an order
Then order should succeed
And spotMeta API should only be called once per session (check debug logs)
Scenario: User disconnects and reconnects
Given user has placed orders (spotMeta is cached)
When user disconnects wallet
And user reconnects wallet
Then spotMeta cache should be cleared
And next HIP-3 operation should fetch fresh spotMeta
```
## **Screenshots/Recordings**
N/A - Internal optimization, no UI changes
### **Before**
Multiple `spotMeta` API calls per session (one per HIP-3 operation)
### **After**
Single `spotMeta` API call per session, cached for subsequent operations
## **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]
> **Medium Risk**
> Touches core Perps trading/connectivity paths (HIP-3 collateral
checks, transfers, connection lifecycle), so caching or error-wrapping
regressions could impact order placement or diagnostics despite being
largely additive and well-tested.
>
> **Overview**
> Reduces redundant HyperLiquid API calls by adding a session-scoped
`spotMeta` cache in `HyperLiquidProvider` and routing HIP-3 collateral
checks/USDC↔USDH flows through it, with cache cleared on disconnect.
>
> Standardizes error handling by expanding `ensureError` (better
undefined/null handling + optional context) and updating Perps
connection/provider/stream logging and Sentry capture to use it.
>
> Introduces a global singleton signing readiness cache
(`TradingReadinessCache`/`PerpsSigningCache`) with in-flight locks to
prevent repeated signing prompts, plus comprehensive unit tests and
updated HyperLiquid provider test mocks/types (`SpotMetaResponse`).
>
> <sup>Written by [Cursor
Bugbot](https://cursor.com/dashboard?tab=bugbot) for commit
90f96a1. This will update automatically
on new commits. Configure
[here](https://cursor.com/dashboard?tab=bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
[b5c386c](b5c386c)
---------
Co-authored-by: Arthur Breton <arthur.breton@consensys.net>
Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
Co-authored-by: abretonc7s <107169956+abretonc7s@users.noreply.github.com>1 parent c6e1142 commit e2ca72f
10 files changed
Lines changed: 1260 additions & 73 deletions
File tree
- app
- components/UI/Perps
- controllers/providers
- providers
- services
- types
- util
Lines changed: 12 additions & 7 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
236 | 236 | | |
237 | 237 | | |
238 | 238 | | |
239 | | - | |
240 | | - | |
| 239 | + | |
| 240 | + | |
241 | 241 | | |
| 242 | + | |
242 | 243 | | |
243 | 244 | | |
244 | 245 | | |
| |||
6024 | 6025 | | |
6025 | 6026 | | |
6026 | 6027 | | |
6027 | | - | |
| 6028 | + | |
| 6029 | + | |
6028 | 6030 | | |
6029 | 6031 | | |
6030 | 6032 | | |
| |||
6114 | 6116 | | |
6115 | 6117 | | |
6116 | 6118 | | |
6117 | | - | |
| 6119 | + | |
| 6120 | + | |
6118 | 6121 | | |
6119 | 6122 | | |
6120 | 6123 | | |
| |||
6224 | 6227 | | |
6225 | 6228 | | |
6226 | 6229 | | |
6227 | | - | |
6228 | | - | |
| 6230 | + | |
| 6231 | + | |
6229 | 6232 | | |
| 6233 | + | |
6230 | 6234 | | |
6231 | 6235 | | |
6232 | 6236 | | |
| |||
6246 | 6250 | | |
6247 | 6251 | | |
6248 | 6252 | | |
6249 | | - | |
| 6253 | + | |
| 6254 | + | |
6250 | 6255 | | |
6251 | 6256 | | |
6252 | 6257 | | |
| |||
Lines changed: 53 additions & 12 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| 57 | + | |
57 | 58 | | |
58 | 59 | | |
59 | 60 | | |
| |||
288 | 289 | | |
289 | 290 | | |
290 | 291 | | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
291 | 296 | | |
292 | 297 | | |
293 | 298 | | |
| |||
889 | 894 | | |
890 | 895 | | |
891 | 896 | | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
| 915 | + | |
| 916 | + | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
892 | 926 | | |
893 | 927 | | |
894 | 928 | | |
| |||
1082 | 1116 | | |
1083 | 1117 | | |
1084 | 1118 | | |
1085 | | - | |
1086 | | - | |
| 1119 | + | |
1087 | 1120 | | |
1088 | | - | |
| 1121 | + | |
1089 | 1122 | | |
1090 | 1123 | | |
1091 | 1124 | | |
| |||
1104 | 1137 | | |
1105 | 1138 | | |
1106 | 1139 | | |
1107 | | - | |
1108 | | - | |
| 1140 | + | |
1109 | 1141 | | |
1110 | 1142 | | |
1111 | | - | |
| 1143 | + | |
1112 | 1144 | | |
1113 | 1145 | | |
1114 | 1146 | | |
| |||
1216 | 1248 | | |
1217 | 1249 | | |
1218 | 1250 | | |
1219 | | - | |
| 1251 | + | |
| 1252 | + | |
| 1253 | + | |
| 1254 | + | |
1220 | 1255 | | |
1221 | 1256 | | |
1222 | 1257 | | |
| |||
1234 | 1269 | | |
1235 | 1270 | | |
1236 | 1271 | | |
1237 | | - | |
1238 | | - | |
| 1272 | + | |
1239 | 1273 | | |
1240 | 1274 | | |
1241 | 1275 | | |
| |||
1277 | 1311 | | |
1278 | 1312 | | |
1279 | 1313 | | |
| 1314 | + | |
1280 | 1315 | | |
1281 | 1316 | | |
1282 | 1317 | | |
| |||
1373 | 1408 | | |
1374 | 1409 | | |
1375 | 1410 | | |
1376 | | - | |
| 1411 | + | |
| 1412 | + | |
| 1413 | + | |
| 1414 | + | |
1377 | 1415 | | |
1378 | 1416 | | |
1379 | 1417 | | |
| |||
1699 | 1737 | | |
1700 | 1738 | | |
1701 | 1739 | | |
1702 | | - | |
| 1740 | + | |
1703 | 1741 | | |
1704 | 1742 | | |
1705 | 1743 | | |
| |||
1708 | 1746 | | |
1709 | 1747 | | |
1710 | 1748 | | |
1711 | | - | |
| 1749 | + | |
1712 | 1750 | | |
1713 | 1751 | | |
1714 | 1752 | | |
| |||
6457 | 6495 | | |
6458 | 6496 | | |
6459 | 6497 | | |
| 6498 | + | |
| 6499 | + | |
6460 | 6500 | | |
| 6501 | + | |
6461 | 6502 | | |
6462 | 6503 | | |
6463 | 6504 | | |
| |||
Lines changed: 25 additions & 16 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
100 | 100 | | |
101 | 101 | | |
102 | 102 | | |
103 | | - | |
| 103 | + | |
104 | 104 | | |
105 | 105 | | |
106 | 106 | | |
| |||
128 | 128 | | |
129 | 129 | | |
130 | 130 | | |
131 | | - | |
| 131 | + | |
132 | 132 | | |
133 | 133 | | |
134 | 134 | | |
| |||
166 | 166 | | |
167 | 167 | | |
168 | 168 | | |
169 | | - | |
170 | | - | |
171 | | - | |
172 | | - | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
173 | 176 | | |
174 | 177 | | |
175 | 178 | | |
| |||
185 | 188 | | |
186 | 189 | | |
187 | 190 | | |
188 | | - | |
189 | | - | |
190 | | - | |
191 | | - | |
192 | | - | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
193 | 199 | | |
194 | 200 | | |
195 | 201 | | |
| |||
198 | 204 | | |
199 | 205 | | |
200 | 206 | | |
201 | | - | |
202 | | - | |
203 | | - | |
204 | | - | |
205 | | - | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
206 | 215 | | |
207 | 216 | | |
208 | 217 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
7 | 8 | | |
8 | 9 | | |
9 | 10 | | |
| |||
398 | 399 | | |
399 | 400 | | |
400 | 401 | | |
401 | | - | |
| 402 | + | |
402 | 403 | | |
403 | 404 | | |
404 | 405 | | |
| |||
1269 | 1270 | | |
1270 | 1271 | | |
1271 | 1272 | | |
1272 | | - | |
1273 | | - | |
1274 | | - | |
| 1273 | + | |
| 1274 | + | |
| 1275 | + | |
| 1276 | + | |
| 1277 | + | |
| 1278 | + | |
1275 | 1279 | | |
1276 | 1280 | | |
1277 | 1281 | | |
| |||
Lines changed: 19 additions & 22 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
560 | 560 | | |
561 | 561 | | |
562 | 562 | | |
563 | | - | |
564 | | - | |
565 | | - | |
566 | | - | |
567 | | - | |
568 | | - | |
569 | | - | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
570 | 572 | | |
571 | | - | |
572 | | - | |
573 | | - | |
574 | | - | |
575 | | - | |
576 | | - | |
577 | | - | |
578 | | - | |
579 | | - | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
580 | 577 | | |
581 | 578 | | |
582 | | - | |
| 579 | + | |
583 | 580 | | |
584 | 581 | | |
585 | 582 | | |
586 | | - | |
| 583 | + | |
587 | 584 | | |
588 | 585 | | |
589 | 586 | | |
590 | | - | |
591 | | - | |
592 | | - | |
| 587 | + | |
593 | 588 | | |
594 | 589 | | |
595 | 590 | | |
| |||
811 | 806 | | |
812 | 807 | | |
813 | 808 | | |
814 | | - | |
| 809 | + | |
| 810 | + | |
| 811 | + | |
815 | 812 | | |
816 | 813 | | |
817 | 814 | | |
| |||
0 commit comments