Skip to content

feat: badDebt warning#161

Merged
antoncoding merged 3 commits intomasterfrom
feat/bad-debt
Oct 13, 2025
Merged

feat: badDebt warning#161
antoncoding merged 3 commits intomasterfrom
feat/bad-debt

Conversation

@antoncoding
Copy link
Copy Markdown
Owner

@antoncoding antoncoding commented Oct 13, 2025

Summary by CodeRabbit

  • New Features

    • Shows a market warning when realized bad debt exceeds a threshold.
    • Exposes realized bad debt and total bad debt metrics (underlying and USD).
    • Adds WalletConnect as a supported wallet option.
  • Chores

    • Shortened block-number cache duration to refresh data more frequently.

@vercel
Copy link
Copy Markdown
Contributor

vercel Bot commented Oct 13, 2025

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

Project Deployment Preview Comments Updated (UTC)
monarch Ready Ready Preview Comment Oct 13, 2025 4:49am

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Oct 13, 2025

📝 Walkthrough

Walkthrough

Adds realized and total bad-debt fields to market GraphQL and types, ensures subgraph transformation supplies a default realizedBadDebt, surfaces realizedBadDebt into warnings logic (introducing an internal BAD_DEBT check), updates hooks to accept full Market, shortens a cache TTL, and enables WalletConnect in wallet config.

Changes

Cohort / File(s) Summary
Debt types & GraphQL
src/utils/types.ts, src/graphql/morpho-api-queries.ts
Adds Market.realizedBadDebt to types and extends MarketFields with realizedBadDebt { underlying; usd } and badDebt { underlying; usd } in GraphQL.
Subgraph transform
src/data-sources/subgraph/market.ts
Ensures transformed market includes realizedBadDebt.underlying default ('0') and removes a market-specific debug log.
Warnings & logic
src/utils/warnings.ts
Replaces inline market typings with Market, removes public bad_debt_realized warning entry, adds internal BAD_DEBT constant and conditional logic to append it when realizedBadDebt.underlying exceeds threshold relative to supply.
Hooks (warnings)
src/hooks/useMarketWarnings.ts
Updates signatures to accept full Market and adds market.realizedBadDebt.underlying into dependency tracking.
Cache tuning
src/hooks/useUserPositionsSummaryData.ts
Reduces block numbers query gcTime from 30 minutes to 3 minutes.
Wallet config
src/store/createWagmiConfig.ts
Adds walletConnectWallet to RainbowKit wallets array when running in a browser.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant UI as UI
  participant GQL as GraphQL API
  participant Subgraph as Subgraph Transform
  participant Warnings as Warnings Util

  UI->>GQL: request markets (includes badDebt, realizedBadDebt)
  GQL-->>UI: returns markets with debt fields
  UI->>Subgraph: transform market (ensure realizedBadDebt.underlying default '0')
  Subgraph-->>UI: market detail
  UI->>Warnings: getMarketWarningsWithDetail(market: Market)
  alt realizedBadDebt.underlying > 0 and realizedBadDebt*1000 > supply
    Warnings-->>UI: warnings + BAD_DEBT
  else
    Warnings-->>UI: warnings (no BAD_DEBT)
  end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Poem

Realized debt joins the fold,
Warnings heed when numbers grow bold.
Hooks widened, TTLs slim,
WalletConnect joins on a whim.
Small edits, steady and told.

Pre-merge checks and finishing touches

✅ 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 clearly indicates a new feature introducing a bad debt warning, which reflects the bulk of changes in this pull request around realized and total bad debt tracking and warning logic.
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
✨ 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 feat/bad-debt

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

🧹 Nitpick comments (1)
src/data-sources/subgraph/market.ts (1)

268-272: Use existing badDebtRealizations data
The subgraph already returns badDebtRealizations in your liquidation query. Instead of hardcoding '0' in realizedBadDebt, mirror the parsing logic from src/data-sources/subgraph/market-liquidations.ts. If you must defer, add a comment explaining why this stays defaulted.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between b6c418a and f7eb386.

📒 Files selected for processing (7)
  • src/data-sources/subgraph/market.ts (1 hunks)
  • src/graphql/morpho-api-queries.ts (2 hunks)
  • src/hooks/useMarketWarnings.ts (3 hunks)
  • src/hooks/useUserPositionsSummaryData.ts (1 hunks)
  • src/store/createWagmiConfig.ts (2 hunks)
  • src/utils/types.ts (1 hunks)
  • src/utils/warnings.ts (5 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
src/hooks/useMarketWarnings.ts (1)
src/utils/types.ts (1)
  • Market (260-307)
src/utils/warnings.ts (1)
src/utils/types.ts (2)
  • WarningWithDetail (81-86)
  • Market (260-307)
🔇 Additional comments (2)
src/hooks/useMarketWarnings.ts (2)

9-12: LGTM: Type signature simplified.

Accepting the full Market type is cleaner than Pick<Market, ...> and makes the function signature easier to maintain. The change aligns with the addition of realizedBadDebt to the Market type.

Also applies to: 30-34


21-21: Dependency addition looks correct.

Adding market.realizedBadDebt.underlying ensures the warnings recompute when bad debt changes. The granular property access is appropriate here.

Comment thread src/utils/warnings.ts Outdated
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

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 8ef5b5e and 3d4674b.

📒 Files selected for processing (1)
  • src/utils/warnings.ts (5 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
src/utils/warnings.ts (1)
src/utils/types.ts (2)
  • WarningWithDetail (81-86)
  • Market (260-307)

Comment thread src/utils/warnings.ts
Comment on lines +184 to +199
// Append bad debt warnings
try {
const badDebtUnderlying = market.realizedBadDebt.underlying;
if (badDebtUnderlying != null) {
const badDebt = BigInt(badDebtUnderlying);
if (badDebt > 0n) {
// only push the bad debt error is it's > 10BPS
const supplyAssets = BigInt(market.state.supplyAssets);
if (badDebt * 1000n > supplyAssets) {
result.push(BAD_DEBT);
}
}
}
} catch {
// ignore invalid BigInt values (e.g., decimal strings like "0.00")
}
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot Oct 13, 2025

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Fix bad-debt parsing

realizedBadDebt.underlying shows up as decimal text (e.g. "0.05"). BigInt throws, we hit the catch, and the warning never fires even though debt exists. Parse as a float (or other decimal helper) and guard before comparing; same for supplyAssets.

-    const badDebtUnderlying = market.realizedBadDebt.underlying;
-    if (badDebtUnderlying != null) {
-      const badDebt = BigInt(badDebtUnderlying);
-      if (badDebt > 0n) {
-        // only push the bad debt error is it's > 10BPS
-        const supplyAssets = BigInt(market.state.supplyAssets);
-        if (badDebt * 1000n > supplyAssets) {
-          result.push(BAD_DEBT);
-        }
-      }
-    }
+    const badDebtValue = Number(market.realizedBadDebt?.underlying ?? '0');
+    const supplyValue = Number(market.state?.supplyAssets ?? '0');
+    if (
+      Number.isFinite(badDebtValue) &&
+      Number.isFinite(supplyValue) &&
+      badDebtValue > 0 &&
+      badDebtValue * 1000 > supplyValue
+    ) {
+      result.push(BAD_DEBT);
+    }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
// Append bad debt warnings
try {
const badDebtUnderlying = market.realizedBadDebt.underlying;
if (badDebtUnderlying != null) {
const badDebt = BigInt(badDebtUnderlying);
if (badDebt > 0n) {
// only push the bad debt error is it's > 10BPS
const supplyAssets = BigInt(market.state.supplyAssets);
if (badDebt * 1000n > supplyAssets) {
result.push(BAD_DEBT);
}
}
}
} catch {
// ignore invalid BigInt values (e.g., decimal strings like "0.00")
}
// Append bad debt warnings
try {
const badDebtValue = Number(market.realizedBadDebt?.underlying ?? '0');
const supplyValue = Number(market.state?.supplyAssets ?? '0');
if (
Number.isFinite(badDebtValue) &&
Number.isFinite(supplyValue) &&
badDebtValue > 0 &&
badDebtValue * 1000 > supplyValue
) {
result.push(BAD_DEBT);
}
} catch {
// ignore invalid BigInt values (e.g., decimal strings like "0.00")
}
🤖 Prompt for AI Agents
In src/utils/warnings.ts around lines 184 to 199, replace the current BigInt
parsing/try-catch with numeric/decimal-safe parsing: parse
realizedBadDebt.underlying and market.state.supplyAssets as decimals (e.g.,
parseFloat or a Decimal/Big library) only proceed if both parse to finite
numbers, compute the ratio (badDebt / supplyAssets) and push BAD_DEBT when the
ratio > 0.001 (10 bps); if you need integer precision use a scaled integer
approach (multiply decimals to an agreed scale before comparing) and remove the
silent BigInt swallow so numeric parsing errors are handled by the guard checks
instead.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

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

The field should return string, expect to be integer.
Does your comment still hold?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@antoncoding, you're right—if realizedBadDebt.underlying is an integer string (like "1234567890"), then BigInt() is the correct parser. My original comment assumed decimal text, which was incorrect.

The current implementation looks fine. The try-catch guards against malformed data, but if the API always returns valid integer strings, it should work as intended.

If you want to verify the catch block isn't hiding real issues, you could temporarily log any errors during development, but otherwise the code is sound.

🐰

@antoncoding antoncoding merged commit ccfeaf9 into master Oct 13, 2025
4 checks passed
@antoncoding antoncoding deleted the feat/bad-debt branch October 13, 2025 05:09
This was referenced Dec 7, 2025
@coderabbitai coderabbitai Bot mentioned this pull request Jan 9, 2026
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