Skip to content

CP-13209: Add health score, safety checks & zero LTV handling#3634

Merged
onghwan merged 20 commits intomainfrom
CP-13209
Mar 19, 2026
Merged

CP-13209: Add health score, safety checks & zero LTV handling#3634
onghwan merged 20 commits intomainfrom
CP-13209

Conversation

@onghwan
Copy link
Contributor

@onghwan onghwan commented Mar 11, 2026

Description

Ticket: CP-13209

  • Add health score card to deposit, withdraw, and borrow forms showing current → projected score transition
  • Cap withdraw max amount to maintain safe health factor (≥ 1.02) for both Aave and Benqi
  • Detect zero-LTV collateral assets on Aave and display blocking error with disabled Next button on borrow/withdraw screens
  • Show ∞ for health score when max repay clears all debt; use compact notation (e.g., 1.5M) for very large scores
  • Add collateral health pre-check warning before toggling collateral that could risk liquidation
  • Extract reusable health score hooks (useAaveHealthScore, useBenqiHealthScore)
  • Refactor select collateral screen into shared SelectCollateralBase component for Aave and Benqi
  • Polish borrow UI: protocol selector, asset logos, spend limit layout
  • Fix repay navigation and hide empty sort/filter controls on deposit tab

Screenshots/Videos

ScreenRecording_03-12-2026.13-14-14_1.MP4
ScreenRecording_03-18-2026.12-40-12_1.MP4

Testing

iOS: 7773
Android: 7774

  1. Health score on deposit/withdraw

    • Have an active borrow on Aave or Benqi
    • Go to Deposit → select an asset that is used as collateral → verify health score card appears and shows current → projected as you change the amount
    • Go to Withdraw → verify same behavior, and "Available to withdraw" is capped below full balance
    • Go to Deposit → select an asset that is NOT used as collateral → verify health score stays unchanged regardless of the amount entered
    • Go to Withdraw → select an asset that is NOT used as collateral → verify health score stays unchanged, and "Available to withdraw" equals the full deposited balance (no health-based cap)
  2. Withdraw max safety

    • Withdraw max on a collateral asset → verify health factor stays above 1.0 and transaction succeeds on-chain
    • Withdraw max on a non-collateral asset → verify the full balance is withdrawable and transaction succeeds on-chain
  3. Zero LTV collateral blocking

    • Have a deposited asset with LTV=0 (e.g., LINK.e, DAI.e) enabled as collateral on Aave
    • Go to Borrow → verify red error message is shown and Next button is disabled
    • Go to Withdraw with active debt → verify same blocking error
    • Go to Withdraw without any debt → verify no error, withdraw succeeds normally
  4. Max repay health score

    • Borrow a single token → go to Repay → tap Max → verify health score shows ∞
    • Borrow multiple tokens → repay max of one → verify health score shows a finite improved value
  5. Collateral toggle pre-check

    • With active debt, try to disable collateral on an asset → verify warning is shown if it would risk liquidation
  6. Non-collateral asset health score isolation

    • Have an active borrow and multiple deposits, some used as collateral and some not
    • Deposit screen (non-collateral asset): enter various amounts → verify the health score card shows current score only with no arrow/transition (current score = projected score)
    • Withdraw screen (non-collateral asset): enter various amounts → verify the health score card shows current score only with no arrow/transition
    • Switch to a collateral asset in the same flow → verify health score now updates with current → projected as expected
    • Repeat the above for both Aave and Benqi protocols

Checklist

  • I have performed a self-review of my code
  • I have verified the code works
  • I have included screenshots / videos of android and ios
  • I have added testing steps
  • I have added/updated necessary unit tests
  • I have updated the documentation

@onghwan onghwan changed the title CP-13209: Add health score to deposit/withdraw and collateral pre-check CP-13209: Add health score, safety checks & zero LTV handling Mar 12, 2026
@onghwan onghwan marked this pull request as ready for review March 12, 2026 17:19
Copilot AI review requested due to automatic review settings March 12, 2026 17:19
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR enhances the DeFi Market (Aave/Benqi) UX and safety checks by adding health-score projection/alerts, tightening withdraw/borrow constraints, and making several UI/layout refinements across deposit/borrow/repay flows.

Changes:

  • Add projected health score UI (and blocking errors) to borrow/deposit/withdraw amount selection flows.
  • Add collateral-disable confirmation alerts based on projected health score; fetch protocol-specific collateral/ltv factors where needed.
  • Refactor collateral selection screen into protocol-specific content components and apply assorted UI tweaks (logos, dropdowns, list headers, icons).

Reviewed changes

Copilot reviewed 43 out of 44 changed files in this pull request and generated 8 comments.

Show a summary per file
File Description
packages/k2-alpine/src/components/TokenUnitInput/SendTokenUnitInputWidget.tsx Add maxAmountZeroMessage prop to customize “max=0” error messaging.
packages/k2-alpine/src/assets/icons/expand_more.svg Update expand-more icon path.
packages/core-mobile/app/utils/formatNumber/formatNumber.ts Allow passing a custom formatter for “normal” magnitude numbers.
packages/core-mobile/app/new/features/defiMarket/utils/healthRisk.ts Use formatNumber for large (>=100) health score formatting.
packages/core-mobile/app/new/features/defiMarket/utils/collateralHealthAlert.ts Add helper to show a confirm/blocking alert when disabling collateral harms health score.
packages/core-mobile/app/new/features/defiMarket/utils/borrow.ts Add Aave ltv + Benqi collateralFactor; reduce duplicate scaling via WAD_SCALE; extend Benqi data fetch via multicall.
packages/core-mobile/app/new/features/defiMarket/utils/benqiBorrowPositionsSummary.ts Use WAD_SCALE constant for health score math.
packages/core-mobile/app/new/features/defiMarket/utils/benqiBorrowPositionsSummary.test.ts Update mocks for new Benqi borrow-data shape (collateralFactor).
packages/core-mobile/app/new/features/defiMarket/utils/aaveBorrowPositionsSummary.test.ts Update mocks for new Aave borrow-data shape (ltv).
packages/core-mobile/app/new/features/defiMarket/types.ts Extend market/borrow types with baseLTVasCollateral, ltv, and collateralFactor.
packages/core-mobile/app/new/features/defiMarket/screens/repay/RepaySelectAmountScreen.tsx Simplify navigation dismissal using dismissAll().
packages/core-mobile/app/new/features/defiMarket/screens/DepositTabScreen.tsx Improve list refresh behavior with stable keying + hide dropdowns when empty; minor spacing tweaks.
packages/core-mobile/app/new/features/defiMarket/screens/DepositDetailScreen.tsx Pass theme-based border color to logos and fix memo deps.
packages/core-mobile/app/new/features/defiMarket/screens/borrow/SelectCollateralScreen.tsx Refactor to protocol-specific content components.
packages/core-mobile/app/new/features/defiMarket/hooks/benqi/useBenqiWithdraw.ts Invalidate Benqi borrow-data query after withdraw confirmation.
packages/core-mobile/app/new/features/defiMarket/hooks/benqi/useBenqiSetCollateral.ts Invalidate Benqi borrow-data query after collateral toggle settles.
packages/core-mobile/app/new/features/defiMarket/hooks/benqi/useBenqiHealthScore.ts New hook for current/projected Benqi health score calculations.
packages/core-mobile/app/new/features/defiMarket/hooks/aave/useAaveZeroLtvCollateral.ts New hook to detect Aave “LTV=0 collateral enabled” situations that can block actions.
packages/core-mobile/app/new/features/defiMarket/hooks/aave/useAaveWithdraw.ts Invalidate Aave borrow/reserves queries after withdraw confirmation.
packages/core-mobile/app/new/features/defiMarket/hooks/aave/useAaveSetCollateral.ts Invalidate Aave borrow-data query after collateral toggle settles.
packages/core-mobile/app/new/features/defiMarket/hooks/aave/useAaveHealthScore.ts New hook for current/projected Aave health factor calculations.
packages/core-mobile/app/new/features/defiMarket/hooks/aave/useAaveAvailableMarkets.ts Populate baseLTVasCollateral in available-market objects.
packages/core-mobile/app/new/features/defiMarket/consts.ts Add WAD_SCALE BigInt constant (1e18).
packages/core-mobile/app/new/features/defiMarket/components/withdraw/BenqiSelectAmountForm.tsx Add max-safe-withdraw and health score projection to Benqi withdraw flow; customize labels/messages.
packages/core-mobile/app/new/features/defiMarket/components/withdraw/AaveSelectAmountForm.tsx Add max-safe-withdraw, health score projection, and Aave zero-LTV blocking to Aave withdraw flow.
packages/core-mobile/app/new/features/defiMarket/components/SelectAmountFormBase.tsx Show projected health score card and optional blocking error; support custom balance label + max=0 message.
packages/core-mobile/app/new/features/defiMarket/components/repay/RepaySelectAmountFormBase.tsx Improve “full repay” health-score handling; show transition (current → projected).
packages/core-mobile/app/new/features/defiMarket/components/HealthScoreCard.tsx Show current→projected transition and compute colors locally.
packages/core-mobile/app/new/features/defiMarket/components/deposit/BenqiErc20SelectAmountForm.tsx Add Benqi health score projection to deposit flow.
packages/core-mobile/app/new/features/defiMarket/components/deposit/BenqiAvaxSelectAmountForm.tsx Add Benqi health score projection to AVAX deposit flow.
packages/core-mobile/app/new/features/defiMarket/components/deposit/AaveErc20SelectAmountForm.tsx Add Aave health score projection; use underlying address consistently for allowance check.
packages/core-mobile/app/new/features/defiMarket/components/deposit/AaveAvaxSelectAmountForm.tsx Add Aave health score projection for AVAX deposit flow.
packages/core-mobile/app/new/features/defiMarket/components/DefiMarketLogo.tsx Add configurable border styling for protocol logos.
packages/core-mobile/app/new/features/defiMarket/components/DefiMarketAssetLogo.tsx Route border styling through to protocol logo; adjust sizing for border.
packages/core-mobile/app/new/features/defiMarket/components/BorrowProtocolSelector.tsx Layout tweaks for the borrow protocol selector trigger.
packages/core-mobile/app/new/features/defiMarket/components/borrow/SelectCollateralBase.tsx Extract shared collateral selection list UI into a reusable base component.
packages/core-mobile/app/new/features/defiMarket/components/borrow/BorrowTabContent.tsx Hide dropdowns when empty and adjust header spacing.
packages/core-mobile/app/new/features/defiMarket/components/borrow/BorrowSelectAmountFormBase.tsx Add optional blocking error + show current→projected health transition.
packages/core-mobile/app/new/features/defiMarket/components/borrow/BenqiSelectCollateralContent.tsx Implement Benqi collateral toggle with health-impact confirmation and toggling state.
packages/core-mobile/app/new/features/defiMarket/components/borrow/BenqiSelectAmountForm.tsx Use shared USD→token conversion util + WAD_SCALE and update health math.
packages/core-mobile/app/new/features/defiMarket/components/borrow/AaveSelectCollateralContent.tsx Implement Aave collateral toggle with health-impact confirmation.
packages/core-mobile/app/new/features/defiMarket/components/borrow/AaveSelectAmountForm.tsx Use shared USD→token conversion util + add Aave zero-LTV blocking.
packages/core-mobile/app/new/features/defiMarket/abis/benqiComptroller.ts Add markets() ABI entry for collateral factor lookup.
packages/core-mobile/app/new/features/approval/components/SpendLimits/SpendLimitOptions.tsx Adjust dropdown trigger layout and display value formatting.
Comments suppressed due to low confidence (1)

packages/core-mobile/app/new/features/defiMarket/utils/borrow.ts:214

  • The “Re-export for backward compatibility” comment no longer matches the code (the export was removed). Either restore the re-export or delete/update this comment to avoid confusion for future maintainers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings March 13, 2026 13:18
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 43 out of 44 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

packages/core-mobile/app/new/features/defiMarket/utils/borrow.ts:214

  • This file still contains the comment // Re-export for backward compatibility, but the actual re-export has been removed. Please either restore the re-export (if still needed) or delete the stale comment to avoid misleading future changes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@onghwan onghwan marked this pull request as draft March 13, 2026 14:03
@onghwan onghwan marked this pull request as ready for review March 13, 2026 15:01
Copilot AI review requested due to automatic review settings March 13, 2026 15:01
@onghwan onghwan marked this pull request as draft March 13, 2026 15:02
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 43 out of 44 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

packages/core-mobile/app/new/features/defiMarket/utils/borrow.ts:214

  • There’s now a dangling // Re-export for backward compatibility comment at the end of the file, but the re-export itself was removed. Either restore the export (if consumers still rely on it) or remove the comment to avoid confusion.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@onghwan onghwan marked this pull request as ready for review March 13, 2026 15:47
Copilot AI review requested due to automatic review settings March 13, 2026 15:47
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 43 out of 44 changed files in this pull request and generated 3 comments.

Comments suppressed due to low confidence (1)

packages/core-mobile/app/new/features/defiMarket/utils/borrow.ts:214

  • The file still has a // Re-export for backward compatibility comment, but the re-export was removed. Either restore the re-export (if external call sites still rely on it) or remove/update the comment to avoid implying compatibility that no longer exists.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copilot AI review requested due to automatic review settings March 16, 2026 14:46
Copy link
Contributor

@ruijialin-avalabs ruijialin-avalabs left a comment

Choose a reason for hiding this comment

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

🚢

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 44 out of 45 changed files in this pull request and generated 2 comments.

Comments suppressed due to low confidence (1)

packages/core-mobile/app/new/features/defiMarket/utils/borrow.ts:214

  • The file ends with // Re-export for backward compatibility but the re-export was removed. Either restore the re-export (if external callers rely on it) or remove/update this comment to avoid misleading future maintainers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Contributor

@B0Y3R-AVA B0Y3R-AVA left a comment

Choose a reason for hiding this comment

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

dev tested ✅

Copilot AI review requested due to automatic review settings March 19, 2026 02:40
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 44 out of 45 changed files in this pull request and generated 4 comments.

Comments suppressed due to low confidence (1)

packages/core-mobile/app/new/features/defiMarket/utils/borrow.ts:214

  • The file still says "Re-export for backward compatibility" but the actual re-export was removed. Either restore the intended re-export or remove/update this comment to avoid misleading future readers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@onghwan onghwan merged commit 84121e5 into main Mar 19, 2026
8 checks passed
@onghwan onghwan deleted the CP-13209 branch March 19, 2026 02:52
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.

4 participants