Skip to content

Commit 3c7ea34

Browse files
committed
fix: skip health score calc for non-collateral assets
1 parent 19a3efd commit 3c7ea34

File tree

8 files changed

+51
-16
lines changed

8 files changed

+51
-16
lines changed

packages/core-mobile/app/new/features/defiMarket/components/deposit/AaveAvaxSelectAmountForm.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,13 @@ export const AaveAvaxSelectAmountForm = ({
4545
})
4646

4747
const { data: borrowData } = useAaveBorrowData(WAVAX_ADDRESS as Address)
48+
const isUsedAsCollateral = market.usageAsCollateralEnabledOnUser === true
49+
4850
const { currentHealthScore, calculateHealthScore } = useAaveHealthScore({
4951
borrowData,
5052
tokenDecimals: asset.token.decimals,
51-
direction: 'deposit'
53+
direction: 'deposit',
54+
isUsedAsCollateral
5255
})
5356

5457
const validateAmount = useCallback(

packages/core-mobile/app/new/features/defiMarket/components/deposit/AaveErc20SelectAmountForm.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,13 @@ export const AaveErc20SelectAmountForm = ({
5959

6060
const underlyingAssetAddress = market.asset.contractAddress as Address
6161
const { data: borrowData } = useAaveBorrowData(underlyingAssetAddress)
62+
const isUsedAsCollateral = market.usageAsCollateralEnabledOnUser === true
63+
6264
const { currentHealthScore, calculateHealthScore } = useAaveHealthScore({
6365
borrowData,
6466
tokenDecimals: market.asset.decimals,
65-
direction: 'deposit'
67+
direction: 'deposit',
68+
isUsedAsCollateral
6669
})
6770

6871
const validateAmount = useCallback(

packages/core-mobile/app/new/features/defiMarket/components/deposit/BenqiAvaxSelectAmountForm.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,12 @@ export const BenqiAvaxSelectAmountForm = ({
4646
const { data: borrowData } = useBenqiBorrowData(
4747
market.asset.mintTokenAddress as Address
4848
)
49+
const isUsedAsCollateral = market.usageAsCollateralEnabledOnUser === true
50+
4951
const { currentHealthScore, calculateHealthScore } = useBenqiHealthScore({
5052
borrowData,
51-
direction: 'deposit'
53+
direction: 'deposit',
54+
isUsedAsCollateral
5255
})
5356

5457
const validateAmount = useCallback(

packages/core-mobile/app/new/features/defiMarket/components/deposit/BenqiErc20SelectAmountForm.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,12 @@ export const BenqiErc20SelectAmountForm = ({
5656
const { data: borrowData } = useBenqiBorrowData(
5757
market.asset.mintTokenAddress as Address
5858
)
59+
const isUsedAsCollateral = market.usageAsCollateralEnabledOnUser === true
60+
5961
const { currentHealthScore, calculateHealthScore } = useBenqiHealthScore({
6062
borrowData,
61-
direction: 'deposit'
63+
direction: 'deposit',
64+
isUsedAsCollateral
6265
})
6366

6467
const validateAmount = useCallback(

packages/core-mobile/app/new/features/defiMarket/components/withdraw/AaveSelectAmountForm.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,23 +83,28 @@ export const WithdrawAaveSelectAmountForm = ({
8383
const underlyingAssetAddress = (market.asset.contractAddress ??
8484
WAVAX_ADDRESS) as Address
8585
const { data: borrowData } = useAaveBorrowData(underlyingAssetAddress)
86+
const isUsedAsCollateral = market.usageAsCollateralEnabledOnUser === true
87+
8688
const { currentHealthScore, calculateHealthScore } = useAaveHealthScore({
8789
borrowData,
8890
tokenDecimals: market.asset.decimals,
89-
direction: 'withdraw'
91+
direction: 'withdraw',
92+
isUsedAsCollateral
9093
})
9194

9295
const { blockingError: zeroLtvError } = useAaveZeroLtvCollateral()
9396
const hasDebt = borrowData !== undefined && borrowData.totalDebtUSD > 0n
9497
const blockingError = hasDebt ? zeroLtvError : undefined
9598

96-
// Max safe withdraw: keep health factor >= 1.01 (liquidation threshold-based)
99+
// Max safe withdraw: keep health factor >= 1.02 (liquidation threshold-based)
100+
// Only applies when this asset is used as collateral and user has debt
97101
const maxWithdrawAmount = useMemo(() => {
98102
if (
99103
!borrowData ||
100104
borrowData.totalDebtUSD === 0n ||
101105
!borrowData.tokenPriceUSD ||
102-
!borrowData.liquidationThreshold
106+
!borrowData.liquidationThreshold ||
107+
!isUsedAsCollateral
103108
) {
104109
return tokenBalance
105110
}
@@ -123,7 +128,7 @@ export const WithdrawAaveSelectAmountForm = ({
123128
market.asset.symbol
124129
)
125130
return maxUnit.lt(tokenBalance) ? maxUnit : tokenBalance
126-
}, [borrowData, tokenBalance, market.asset])
131+
}, [borrowData, tokenBalance, market.asset, isUsedAsCollateral])
127132

128133
const validateAmount = useCallback(
129134
async (amt: TokenUnit) => {

packages/core-mobile/app/new/features/defiMarket/components/withdraw/BenqiSelectAmountForm.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,23 @@ export const WithdrawBenqiSelectAmountForm = ({
4040
const { data: borrowData } = useBenqiBorrowData(
4141
market.asset.mintTokenAddress as Address
4242
)
43+
const isUsedAsCollateral = market.usageAsCollateralEnabledOnUser === true
44+
4345
const { currentHealthScore, calculateHealthScore } = useBenqiHealthScore({
4446
borrowData,
45-
direction: 'withdraw'
47+
direction: 'withdraw',
48+
isUsedAsCollateral
4649
})
4750

4851
// Max safe withdraw: keep health factor at same level as borrow max
49-
// maxWithdrawUSD = liquidity * 10^WAD / collateralFactor
52+
// Only applies when this asset is used as collateral and user has debt
5053
const maxWithdrawAmount = useMemo(() => {
5154
if (
5255
!borrowData ||
5356
borrowData.totalDebtUSD === 0n ||
5457
!borrowData.tokenPriceUSD ||
55-
!borrowData.collateralFactor
58+
!borrowData.collateralFactor ||
59+
!isUsedAsCollateral
5660
) {
5761
return tokenBalance
5862
}
@@ -73,7 +77,7 @@ export const WithdrawBenqiSelectAmountForm = ({
7377
market.asset.symbol
7478
)
7579
return maxUnit.lt(tokenBalance) ? maxUnit : tokenBalance
76-
}, [borrowData, tokenBalance, market.asset])
80+
}, [borrowData, tokenBalance, market.asset, isUsedAsCollateral])
7781

7882
const validateAmount = useCallback(
7983
async (amt: TokenUnit) => {

packages/core-mobile/app/new/features/defiMarket/hooks/aave/useAaveHealthScore.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ import { AaveBorrowData } from '../../types'
77
export function useAaveHealthScore({
88
borrowData,
99
tokenDecimals,
10-
direction
10+
direction,
11+
isUsedAsCollateral = true
1112
}: {
1213
borrowData: AaveBorrowData | undefined
1314
tokenDecimals: number
1415
direction: 'deposit' | 'withdraw'
16+
isUsedAsCollateral?: boolean
1517
}): {
1618
currentHealthScore: number | undefined
1719
calculateHealthScore: (amount: TokenUnit) => number | undefined
@@ -24,6 +26,8 @@ export function useAaveHealthScore({
2426
const calculateHealthScore = useCallback(
2527
(amount: TokenUnit): number | undefined => {
2628
if (!borrowData) return undefined
29+
if (!isUsedAsCollateral) return currentHealthScore
30+
2731
const {
2832
totalCollateralUSD,
2933
totalDebtUSD,
@@ -47,7 +51,13 @@ export function useAaveHealthScore({
4751
(totalDebtUSD * 10000n)
4852
return Number(formatUnits(newHealthFactor, WAD))
4953
},
50-
[borrowData, tokenDecimals, direction]
54+
[
55+
borrowData,
56+
tokenDecimals,
57+
direction,
58+
isUsedAsCollateral,
59+
currentHealthScore
60+
]
5161
)
5262

5363
return { currentHealthScore, calculateHealthScore }

packages/core-mobile/app/new/features/defiMarket/hooks/benqi/useBenqiHealthScore.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import { BenqiBorrowData } from '../../types'
66

77
export function useBenqiHealthScore({
88
borrowData,
9-
direction
9+
direction,
10+
isUsedAsCollateral = true
1011
}: {
1112
borrowData: BenqiBorrowData | undefined
1213
direction: 'deposit' | 'withdraw'
14+
isUsedAsCollateral?: boolean
1315
}): {
1416
currentHealthScore: number | undefined
1517
calculateHealthScore: (amount: TokenUnit) => number | undefined
@@ -25,6 +27,8 @@ export function useBenqiHealthScore({
2527
const calculateHealthScore = useCallback(
2628
(amount: TokenUnit): number | undefined => {
2729
if (!borrowData) return undefined
30+
if (!isUsedAsCollateral) return currentHealthScore
31+
2832
const { liquidity, totalDebtUSD, tokenPriceUSD, collateralFactor } =
2933
borrowData
3034
if (totalDebtUSD === 0n) return Infinity
@@ -41,7 +45,7 @@ export function useBenqiHealthScore({
4145
((newLiquidity + totalDebtUSD) * WAD_SCALE) / totalDebtUSD
4246
return Number(formatUnits(newHealth, WAD))
4347
},
44-
[borrowData, direction]
48+
[borrowData, direction, isUsedAsCollateral, currentHealthScore]
4549
)
4650

4751
return { currentHealthScore, calculateHealthScore }

0 commit comments

Comments
 (0)