Skip to content

fix: validate DexScreener response before caching (M7)#95

Open
0x-SquidSol wants to merge 1 commit intodcccrypto:mainfrom
0x-SquidSol:fix/m7-dexscreener-cache-validation-order
Open

fix: validate DexScreener response before caching (M7)#95
0x-SquidSol wants to merge 1 commit intodcccrypto:mainfrom
0x-SquidSol:fix/m7-dexscreener-cache-validation-order

Conversation

@0x-SquidSol
Copy link
Copy Markdown
Contributor

@0x-SquidSol 0x-SquidSol commented Apr 9, 2026

Summary

  • DexScreener responses were cached before validation (empty pairs, low liquidity, invalid price)
  • A single bad 200 OK response suppressed fresh fetches for the full 10s cache TTL
  • Move cache write to after all validation gates pass
  • Bad responses now allow immediate retry; good responses still cached for rate-limit protection

Test plan

  • All 133 tests pass

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced price data validation to occur before caching. Only valid price responses meeting liquidity and validity requirements are now cached, preventing invalid price data from being served during the cache period.

DexScreener responses were cached before validation. A 200 OK response
with empty pairs, zero liquidity, or invalid price was cached for 10s,
suppressing fresh fetches even if DexScreener would return valid data
on retry.

Move cache write to after all validation gates pass. Only responses
that produce a valid price (non-null pairs, above MIN_LIQUIDITY_USD,
finite positive priceUsd) are now cached. Bad responses allow immediate
retry on the next call.

Safe for rate limits: BM2 in-flight deduplication prevents concurrent
requests, and the 5s push rate limit caps query frequency.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 9, 2026

📝 Walkthrough

Walkthrough

Updated OracleService._fetchDexScreenerPriceInternal() to validate DexScreener responses before caching. Previously validation occurred after caching; now invalid responses are rejected before insertion into dexScreenerCache, preventing invalid data from occupying cache during TTL windows.

Changes

Cohort / File(s) Summary
DexScreener Cache Validation
src/services/oracle.ts
Reordered validation logic in _fetchDexScreenerPriceInternal() to validate DexScreener responses (priceUsd existence, MIN_LIQUIDITY_USD threshold, finite positive price) before caching rather than after, ensuring only validated data occupies the dexScreenerCache.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 checks before storing away
Before the cache holds any price today,
We validate each number, precise and true,
No invalid data shall sneak through!
Clean cache, clean trades—hooray! 🥕✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: validate DexScreener response before caching (M7)' accurately and concisely describes the main change: moving validation before cache insertion to prevent invalid data from being cached.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/services/oracle.ts (1)

95-96: Use insertion-time timestamp for cache freshness.

Line 140 stores fetchedAt: now, but now is captured before the HTTP call (Line 95). Slow responses shrink effective TTL and reduce rate-limit protection. Prefer capturing fetchedAt immediately before cache insertion.

Suggested patch
-      dexScreenerCache.delete(mint);
-      dexScreenerCache.set(mint, { data: json, fetchedAt: now });
+      const fetchedAt = Date.now();
+      dexScreenerCache.delete(mint);
+      dexScreenerCache.set(mint, { data: json, fetchedAt });

Also applies to: 140-140

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/services/oracle.ts` around lines 95 - 96, The cache uses a timestamp
captured before the HTTP call (const now = Date.now() near
dexScreenerCache.get(mint)) which can make fetchedAt stale for slow requests;
update the code to compute the insertion timestamp (e.g., const fetchedAt =
Date.now()) immediately before inserting into dexScreenerCache.set(...) and
store that fetchedAt instead of the earlier now value so TTL/rate-limit logic
uses the actual cache insertion time; adjust any references to fetchedAt/now in
the same function (where you build the cache entry for mint) accordingly.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/services/oracle.ts`:
- Around line 95-96: The cache uses a timestamp captured before the HTTP call
(const now = Date.now() near dexScreenerCache.get(mint)) which can make
fetchedAt stale for slow requests; update the code to compute the insertion
timestamp (e.g., const fetchedAt = Date.now()) immediately before inserting into
dexScreenerCache.set(...) and store that fetchedAt instead of the earlier now
value so TTL/rate-limit logic uses the actual cache insertion time; adjust any
references to fetchedAt/now in the same function (where you build the cache
entry for mint) accordingly.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 623f628e-e1ae-47d4-a342-6b290a196c36

📥 Commits

Reviewing files that changed from the base of the PR and between 3280971 and f143c2b.

📒 Files selected for processing (1)
  • src/services/oracle.ts

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant