Skip to content

refactor: remove dup fetching position keys#312

Merged
antoncoding merged 1 commit intomasterfrom
fix/vault-read
Jan 20, 2026
Merged

refactor: remove dup fetching position keys#312
antoncoding merged 1 commit intomasterfrom
fix/vault-read

Conversation

@antoncoding
Copy link
Copy Markdown
Owner

@antoncoding antoncoding commented Jan 20, 2026

Summary by CodeRabbit

  • Bug Fixes

    • Improved handling of missing market data from the Morpho API—the system now gracefully returns empty or null results instead of throwing errors
    • Enhanced fallback logic to use Subgraph data only when the Morpho API actually fails
    • Increased resilience to incomplete or missing API responses across multiple data sources
  • Style

    • Minor formatting improvements in component styling

✏️ Tip: You can customize this high-level summary in your review settings.

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Jan 20, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Review Updated (UTC)
monarch Ready Ready Preview, Comment Jan 20, 2026 3:05am

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jan 20, 2026

📝 Walkthrough

Walkthrough

The changes add graceful null handling for GraphQL NOT_FOUND errors across the Morpho API data layer. The core fetcher now returns null instead of throwing on NOT_FOUND, and callers guard against falsy responses with early returns or empty defaults. GraphQL response types are relaxed to optional fields.

Changes

Cohort / File(s) Summary
Core Fetcher
src/data-sources/morpho-api/fetchers.ts
Return type changed to Promise<T | null>. Detects NOT_FOUND errors and returns null; preserves throw behavior for other errors.
Market Data Handlers
src/data-sources/morpho-api/market.ts, src/data-sources/morpho-api/historical.ts, src/data-sources/morpho-api/market-{borrowers,borrows,liquidations,suppliers,supplies}.ts
Added early returns when fetch result is falsy (null/undefined). GraphQL response types updated to optional fields (data?, items?, pageInfo?). Existing mapping and error paths unchanged.
Other Data Handlers
src/data-sources/morpho-api/positions.ts, src/data-sources/morpho-api/prices.ts, src/data-sources/morpho-api/transactions.ts, src/data-sources/morpho-api/{v2-vaults,vaults}.ts
Added guards for falsy responses, returning empty arrays or null before processing. Made response types optional. Error re-throw behavior refined in positions.ts.
Hook Logic
src/hooks/useUserPositions.ts
Added apiError flag; fallback to Subgraph now only triggers when Morpho API is attempted AND returns no markets AND errors.
UI Refactor
src/features/positions/components/supplied-markets-detail.tsx
Collapsed multi-line conditional ternaries into single-line expressions. No logic changes.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Title check ⚠️ Warning Title mentions 'remove dup fetching position keys' but changes span Morpho API response handling across 13+ files, making it incomplete relative to the actual scope. Title should reflect the main change: handling missing/NOT_FOUND API responses gracefully. Consider: 'refactor: handle NOT_FOUND responses in Morpho API fetchers' or similar.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch fix/vault-read

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.

Actionable comments posted: 1

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
src/hooks/useUserPositions.ts (1)

64-89: Logic bug: Subgraph fallback broken for non-Morpho-API networks.

When supportsMorphoApi(network) is false, the code skips the Morpho block entirely. apiError stays false and markets stays []. The condition on line 79 (markets.length === 0 && apiError) is never true, so Subgraph is never called.

The previous behavior likely fell back to Subgraph when markets.length === 0 regardless of error state.

Proposed fix
       // If Morpho API failed or not supported, try Subgraph
-      if (markets.length === 0 && apiError) {
+      if (markets.length === 0 && (apiError || !supportsMorphoApi(network))) {
         try {
           console.log(`Attempting to fetch positions via Subgraph for network ${network}`);
           markets = await fetchSubgraphUserPositionMarkets(user, network);
🤖 Fix all issues with AI agents
In `@src/data-sources/morpho-api/fetchers.ts`:
- Around line 23-29: The NOT_FOUND check currently inspects err.status which
doesn't exist, so update the guard to examine the message field instead: in the
notFoundError computation (the variable named notFoundError that iterates
(result as any).errors), change the predicate to check
err.message?.includes('NOT_FOUND') (and adjust the inline type from { status?:
string } to { message?: string } if present) so the branch that logs "Morpho API
return Not Found error" and returns null will correctly trigger for NOT_FOUND
errors.
🧹 Nitpick comments (1)
src/data-sources/morpho-api/vaults.ts (1)

34-41: Type updated, but errors field no longer checked.

The errors field is still in the type (line 40) but no longer checked in the function. This is fine since the fetcher now handles GraphQL errors centrally, though you could remove errors from the type for clarity.

Comment thread src/data-sources/morpho-api/fetchers.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