Skip to content

Commit

Permalink
extract health
Browse files Browse the repository at this point in the history
  • Loading branch information
vidvidvid committed Dec 2, 2024
1 parent 1a08ce6 commit 27c2106
Show file tree
Hide file tree
Showing 11 changed files with 576 additions and 386 deletions.
54 changes: 36 additions & 18 deletions packages/ui/app/_components/dialogs/manage/BorrowTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import StatusAlerts from './StatusAlerts';
import TransactionStepsHandler from './TransactionStepsHandler';
import ResultHandler from '../../ResultHandler';
import MemoizedUtilizationStats from '../../UtilizationStats';
import { useHealth } from '@ui/hooks/market/useHealth';
import { useEffect } from 'react';

interface BorrowTabProps {
maxAmount: bigint;
Expand All @@ -29,19 +31,14 @@ interface BorrowTabProps {
const BorrowTab = ({ maxAmount, isLoadingMax, totalStats }: BorrowTabProps) => {
const {
selectedMarketData,
amount,
setAmount,
currentUtilizationPercentage,
handleUtilization,
hfpStatus,
resetTransactionSteps,
chainId,
normalizedHealthFactor,
normalizedPredictedHealthFactor,
amountAsBInt,
isLoadingPredictedHealthFactor,
isLoadingUpdatedAssets,
updatedValues
updatedValues,
comptrollerAddress,
minBorrowAmount,
maxBorrowAmount,
setPredictionAmount
} = useManageDialogContext();

const {
Expand All @@ -50,8 +47,30 @@ const BorrowTab = ({ maxAmount, isLoadingMax, totalStats }: BorrowTabProps) => {
transactionSteps,
isPolling,
borrowLimits,
isUnderMinBorrow
} = useBorrow();
isUnderMinBorrow,
amount,
setAmount,
utilizationPercentage,
handleUtilization,
amountAsBInt
} = useBorrow({
selectedMarketData,
chainId,
comptrollerAddress,
minBorrowAmount,
maxBorrowAmount
});

const { isLoadingPredictedHealthFactor, healthFactor, hfpStatus } = useHealth(
{
comptrollerAddress,
cToken: selectedMarketData.cToken,
activeTab: 'borrow',
amount: amountAsBInt,
exchangeRate: selectedMarketData.exchangeRate,
decimals: selectedMarketData.underlyingDecimals
}
);

const isDisabled =
!amount ||
Expand All @@ -60,20 +79,19 @@ const BorrowTab = ({ maxAmount, isLoadingMax, totalStats }: BorrowTabProps) => {
hfpStatus === HFPStatus.CRITICAL ||
hfpStatus === HFPStatus.UNKNOWN;

const healthFactor = {
current: normalizedHealthFactor ?? '0',
predicted: normalizedPredictedHealthFactor ?? '0'
};
useEffect(() => {
setPredictionAmount(amountAsBInt);
}, [amountAsBInt]);

return (
<div className="space-y-4 pt-4">
<Amount
amount={amount}
handleInput={setAmount}
handleInput={(val?: string) => setAmount(val ?? '')}
isLoading={isLoadingMax || isPolling}
max={formatUnits(maxAmount, selectedMarketData.underlyingDecimals)}
symbol={selectedMarketData.underlyingSymbol}
currentUtilizationPercentage={currentUtilizationPercentage}
currentUtilizationPercentage={utilizationPercentage}
handleUtilization={handleUtilization}
/>

Expand Down
47 changes: 30 additions & 17 deletions packages/ui/app/_components/dialogs/manage/RepayTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import StatusAlerts from './StatusAlerts';
import TransactionStepsHandler from './TransactionStepsHandler';
import ResultHandler from '../../ResultHandler';
import MemoizedUtilizationStats from '../../UtilizationStats';
import { useHealth } from '@ui/hooks/market/useHealth';
import { useEffect } from 'react';

interface RepayTabProps {
maxAmount: bigint;
Expand All @@ -27,42 +29,53 @@ interface RepayTabProps {
const RepayTab = ({ maxAmount, isLoadingMax, totalStats }: RepayTabProps) => {
const {
selectedMarketData,
amount,
setAmount,
currentUtilizationPercentage,
handleUtilization,
hfpStatus,
resetTransactionSteps,
chainId,
normalizedHealthFactor,
normalizedPredictedHealthFactor,
amountAsBInt,
isLoadingUpdatedAssets,
updatedValues
updatedValues,
comptrollerAddress,
setPredictionAmount
} = useManageDialogContext();

const {
isWaitingForIndexing,
repayAmount,
transactionSteps,
isPolling,
currentBorrowAmountAsFloat
} = useRepay();
currentBorrowAmountAsFloat,
amount,
setAmount,
utilizationPercentage,
handleUtilization,
amountAsBInt
} = useRepay({
maxAmount,
selectedMarketData,
chainId
});

const healthFactor = {
current: normalizedHealthFactor ?? '0',
predicted: normalizedPredictedHealthFactor ?? '0'
};
const { healthFactor, hfpStatus } = useHealth({
comptrollerAddress,
cToken: selectedMarketData.cToken,
activeTab: 'repay',
amount: amountAsBInt,
exchangeRate: selectedMarketData.exchangeRate,
decimals: selectedMarketData.underlyingDecimals
});

useEffect(() => {
setPredictionAmount(amountAsBInt);
}, [amountAsBInt]);

return (
<div className="space-y-4 pt-4">
<Amount
amount={amount}
handleInput={setAmount}
handleInput={(val?: string) => setAmount(val ?? '')}
isLoading={isLoadingMax || isPolling}
max={formatUnits(maxAmount, selectedMarketData.underlyingDecimals)}
symbol={selectedMarketData.underlyingSymbol}
currentUtilizationPercentage={currentUtilizationPercentage}
currentUtilizationPercentage={utilizationPercentage}
handleUtilization={handleUtilization}
/>

Expand Down
31 changes: 19 additions & 12 deletions packages/ui/app/_components/dialogs/manage/SupplyTab.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo } from 'react';
import { useEffect, useMemo } from 'react';

import { formatUnits } from 'viem';

Expand Down Expand Up @@ -38,17 +38,13 @@ const SupplyTab = ({
}: SupplyTabProps) => {
const {
selectedMarketData,
amount,
setAmount,
currentUtilizationPercentage,
handleUtilization,
resetTransactionSteps,
chainId,
amountAsBInt,
comptrollerAddress,
updatedValues,
isLoadingUpdatedAssets,
refetchUsedQueries
refetchUsedQueries,
setPredictionAmount
} = useManageDialogContext();

const {
Expand All @@ -65,13 +61,24 @@ const SupplyTab = ({
isWaitingForIndexing,
supplyAmount,
transactionSteps: supplyTxSteps,
isPolling
isPolling,
amount,
setAmount,
utilizationPercentage,
handleUtilization,
amountAsBInt
} = useSupply({
maxAmount,
enableCollateral
enableCollateral,
selectedMarketData,
comptrollerAddress,
chainId
});

// Combine both sets of transaction steps
useEffect(() => {
setPredictionAmount(amountAsBInt);
}, [amountAsBInt]);

const combinedTransactionSteps = useMemo(() => {
return [...supplyTxSteps, ...collateralTxSteps];
}, [supplyTxSteps, collateralTxSteps]);
Expand All @@ -92,11 +99,11 @@ const SupplyTab = ({

<Amount
amount={amount}
handleInput={setAmount}
handleInput={(val?: string) => setAmount(val ?? '')}
isLoading={isLoadingMax || isPolling}
max={formatUnits(maxAmount, selectedMarketData.underlyingDecimals)}
symbol={selectedMarketData.underlyingSymbol}
currentUtilizationPercentage={currentUtilizationPercentage}
currentUtilizationPercentage={utilizationPercentage}
handleUtilization={handleUtilization}
/>

Expand Down
56 changes: 36 additions & 20 deletions packages/ui/app/_components/dialogs/manage/WithdrawTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import StatusAlerts from './StatusAlerts';
import TransactionStepsHandler from './TransactionStepsHandler';
import ResultHandler from '../../ResultHandler';
import MemoizedUtilizationStats from '../../UtilizationStats';
import { useHealth } from '@ui/hooks/market/useHealth';
import { useEffect } from 'react';

interface WithdrawTabProps {
maxAmount: bigint;
Expand All @@ -31,25 +33,40 @@ const WithdrawTab = ({
}: WithdrawTabProps) => {
const {
selectedMarketData,
amount,
setAmount,
currentUtilizationPercentage,
handleUtilization,
hfpStatus,
resetTransactionSteps,
chainId,
normalizedHealthFactor,
normalizedPredictedHealthFactor,
amountAsBInt,
isLoadingPredictedHealthFactor,
updatedValues,
isLoadingUpdatedAssets
isLoadingUpdatedAssets,
comptrollerAddress,
setPredictionAmount
} = useManageDialogContext();

const { isWaitingForIndexing, withdrawAmount, transactionSteps, isPolling } =
useWithdraw({
maxAmount
});
const {
isWaitingForIndexing,
withdrawAmount,
transactionSteps,
isPolling,
amount,
setAmount,
utilizationPercentage,
handleUtilization,
amountAsBInt
} = useWithdraw({
maxAmount,
selectedMarketData,
chainId
});

const { isLoadingPredictedHealthFactor, healthFactor, hfpStatus } = useHealth(
{
comptrollerAddress,
cToken: selectedMarketData.cToken,
activeTab: 'withdraw',
amount: amountAsBInt,
exchangeRate: selectedMarketData.exchangeRate,
decimals: selectedMarketData.underlyingDecimals
}
);

const isDisabled =
!amount ||
Expand All @@ -58,21 +75,20 @@ const WithdrawTab = ({
hfpStatus === HFPStatus.CRITICAL ||
hfpStatus === HFPStatus.UNKNOWN;

const healthFactor = {
current: normalizedHealthFactor ?? '0',
predicted: normalizedPredictedHealthFactor ?? '0'
};
useEffect(() => {
setPredictionAmount(amountAsBInt);
}, [amountAsBInt]);

return (
<div className="space-y-4 pt-4">
<Amount
amount={amount}
handleInput={setAmount}
handleInput={(val?: string) => setAmount(val ?? '')}
isLoading={isLoadingMax || isPolling}
max={formatUnits(maxAmount, selectedMarketData.underlyingDecimals)}
symbol={selectedMarketData.underlyingSymbol}
hintText="Max Withdraw"
currentUtilizationPercentage={currentUtilizationPercentage}
currentUtilizationPercentage={utilizationPercentage}
handleUtilization={handleUtilization}
/>

Expand Down
Loading

0 comments on commit 27c2106

Please sign in to comment.