Skip to content

fix(perps): retry candle data fetch on too-many-requests errors cp-7.72.0#28161

Closed
michalconsensys wants to merge 3 commits intomainfrom
fix/perps/too-many-requests-chart
Closed

fix(perps): retry candle data fetch on too-many-requests errors cp-7.72.0#28161
michalconsensys wants to merge 3 commits intomainfrom
fix/perps/too-many-requests-chart

Conversation

@michalconsensys
Copy link
Copy Markdown
Contributor

@michalconsensys michalconsensys commented Mar 31, 2026

Description

When the HyperLiquid API returns "too many requests" rate-limiting errors during candle data initialization, the chart would remain permanently empty with no recovery path. This PR adds retry logic so the chart self-heals after being rate-limited.

Changes:

  • CandleStreamChannel.ts: Propagates onError callback through the connect → WebSocket subscription flow so consumers can react to initialization failures. Removes the early return in fetchMoreHistory when no cached data exists, allowing history fetches even after a failed initial load (falls back to Date.now() as the anchor timestamp).
  • PerpsMarketDetailsView.tsx: Adds a useRefetchCandleDataOnError hook that detects "too many requests" errors and retries fetchMoreHistory up to 12 times with 5-second intervals, stopping as soon as candles are loaded.

Changelog

CHANGELOG entry: Fixed an issue where the Perps chart would remain empty after being rate-limited by the data provider

Related issues

Partially resolves: #28141

Manual testing steps

Feature: Perps chart recovery after rate limiting

  Scenario: chart recovers from too-many-requests errors
    Given the user is on the Perps Market Details view
    And the HyperLiquid API is returning "too many requests" errors for candle data

    When the rate limit is lifted within 60 seconds
    Then the chart should automatically populate with candle data
    And no user intervention should be required

  Scenario: chart loads normally without rate limiting
    Given the user is on the Perps Market Details view
    And the HyperLiquid API is responding normally

    When the chart loads
    Then candle data should display immediately
    And no retry behavior should be triggered

Screenshots/Recordings

N/A — no UI changes, behavior-only fix.

Before

Chart stays permanently empty after a "too many requests" error.

After

Screen.Recording.2026-03-31.at.13.44.51.mov

Chart automatically retries and recovers once the rate limit lifts.

Pre-merge author checklist

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.

Note

Medium Risk
Touches the candle streaming/retry path used to render Perps charts; incorrect error propagation or retry timing could cause extra network load or continued empty charts, but scope is limited to this data flow.

Overview
Prevents the Perps chart from getting stuck empty after candle subscription initialization hits a "too many requests" rate-limit error.

CandleStreamChannel now propagates subscription onError back to subscribers and allows fetchHistoricalCandles to run even when cache is empty (anchoring with Date.now()), enabling recovery after a failed initial load. PerpsMarketDetailsView wires in a new useRefetchCandleDataOnError hook that retries fetchMoreHistory (up to 12 attempts with 5s delay) until candles arrive, and adds unit tests covering retry/stop conditions.

Written by Cursor Bugbot for commit 20ff264. This will update automatically on new commits. Configure here.

Propagate onError from CandleStreamChannel subscriptions and add
retry logic in PerpsMarketDetailsView that re-fetches candle history
up to 12 times with 5s delays when rate-limited, preventing empty
chart states.
@michalconsensys michalconsensys self-assigned this Mar 31, 2026
@github-actions
Copy link
Copy Markdown
Contributor

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.

@metamaskbot metamaskbot added the team-perps Perps team label Mar 31, 2026
Extract useRefetchCandleDataOnError into its own hook file for
testability and add post-sleep cancellation checks. Add 8 hook tests
covering retry, max-retries, unmount cleanup, and case-insensitive
matching. Update CandleStreamChannel tests for the new fetchMoreHistory
Date.now fallback and add 4 tests for onError propagation.
@github-actions github-actions bot added size-M and removed size-S labels Mar 31, 2026
The real sleep from @walletconnect/utils is already a plain
setTimeout wrapper, so jest.useFakeTimers() controls it directly.
@michalconsensys michalconsensys marked this pull request as ready for review March 31, 2026 12:41
@michalconsensys michalconsensys requested a review from a team as a code owner March 31, 2026 12:42
@github-actions
Copy link
Copy Markdown
Contributor

🔍 Smart E2E Test Selection

⏭️ Smart E2E selection skipped - draft PR

All E2E tests pre-selected.

View GitHub Actions results

@michalconsensys michalconsensys changed the title fix(perps): retry candle data fetch on too-many-requests errors fix(perps): retry candle data fetch on too-many-requests errors cp-7.72.0 Mar 31, 2026
Copy link
Copy Markdown

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.

},
onError: (error: Error) => {
// Log initialization failure
onError?.(error);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First subscriber's onError called twice on error

Medium Severity

The connect() error handler calls onError?.(error) directly on line 284 (new code), and then the existing subscriber loop on lines 296–301 also calls subscriber.onError?.(error) for all matching subscribers — including the first subscriber whose onError is the same function reference passed to connect(). This results in the first subscriber's onError being invoked twice per error event, causing duplicate Logger.error() reports to Sentry in usePerpsLiveCandles.

Additional Locations (1)
Fix in Cursor Fix in Web

@sonarqubecloud
Copy link
Copy Markdown

@github-actions
Copy link
Copy Markdown
Contributor

E2E Fixture Validation — Schema is up to date
17 value mismatches detected (expected — fixture represents an existing user).
View details

@github-actions github-actions bot locked and limited conversation to collaborators Apr 1, 2026
@michalconsensys
Copy link
Copy Markdown
Contributor Author

Closed in favour of #28176

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Perps rate limit issue when discovering 5-10 markets

2 participants