fix: rebuild L2 headers per page and surface API errors in pagination - #100
Open
erik-polymarket wants to merge 1 commit into
Open
fix: rebuild L2 headers per page and surface API errors in pagination#100erik-polymarket wants to merge 1 commit into
erik-polymarket wants to merge 1 commit into
Conversation
Cursor pagination loops (getTrades, getOpenOrders, getPreMigrationOrders,
getEarningsForUserForDay, getUserEarningsAndMarketsConfig) built L2 auth
headers once before the loop and reused them for every page. Since the
HMAC timestamp is part of the header, long pagination runs could send
stale auth on later pages. Move header creation inside each loop so a
fresh timestamp/signature is generated per page.
Also, on an API error the HTTP helper returns { error, status }; the loops
spread response.data and threw a misleading 'undefined is not iterable',
hiding the real error. Add extractPage() to detect error-shaped or
malformed pages and throw an ApiError carrying the actual message/status.
Closes #99
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fixes two related bugs in the cursor-pagination loops reported in #99.
1. Stale L2 auth headers.
getTrades,getOpenOrders,getPreMigrationOrders,getEarningsForUserForDay, andgetUserEarningsAndMarketsConfigbuilt the L2 auth headers once before the loop and reused them for every page. The HMAC timestamp is part of the header, so a long pagination run could send stale authentication on later pages. Header creation now happens inside each loop iteration, so every page request gets a fresh timestamp/signature.2. Masked API errors. On an API error the HTTP helper returns
{ error, status }. The loops then didnext_cursor = response.next_cursor/results = [...results, ...response.data], spreading a missingdatafield and throwing a misleadingTypeError: undefined is not iterable— hiding the real error. A new privateextractPage()helper detects error-shaped or malformed pages and throws anApiErrorcarrying the actual API message and status.Note: when
throwOnErroris enabled,this.getalready throws before the loop sees the error. When it is disabled, an array-returning pagination method cannot hand back an{ error, status }object, so surfacing anApiErroris the correct way to expose the failure instead of crashing with a bogusTypeError.Testing
New
tests/client/pagination.test.tscovers both fixes with delayed page responses:POLY_TIMESTAMP/POLY_SIGNATUREacross pages){ error, status }surfaces as anApiErrorwith the real message/status (notis not iterable)dataarray throws a descriptiveApiErrorpnpm test→ 303 passed (21 files) ·pnpm lintclean ·pnpm buildclean.Closes #99
Note
Medium Risk
Touches L2-authenticated fetch paths used for trades, orders, and rewards; behavior changes on long paginated runs and when the API returns errors mid-loop, but the fixes align with intended auth and error semantics.
Overview
Fixes cursor-pagination for L2-authenticated list endpoints (
getTrades,getOpenOrders,getPreMigrationOrders,getEarningsForUserForDay,getUserEarningsAndMarketsConfig).Stale L2 auth: L2 headers are built inside each loop iteration so every page gets a fresh HMAC timestamp/signature instead of reusing headers from before the loop.
Pagination errors: A new private
extractPage()validates each page before appending. Error-shaped{ error, status }responses and pages missing adataarray throwApiErrorwith the real message/status instead of failing withundefined is not iterablewhenthrowOnErroris off.Adds
tests/client/pagination.test.tsfor per-page header regeneration and mid-pagination error handling.Reviewed by Cursor Bugbot for commit cc28852. Bugbot is set up for automated code reviews on this repo. Configure here.