|
| 1 | +import { calculateHealthFactorFromBalancesBigUnits, valueToBigNumber } from '@aave/math-utils'; |
| 2 | +import { Trans } from '@lingui/macro'; |
| 3 | +import { Typography } from '@mui/material'; |
| 4 | +import { useEffect, useState } from 'react'; |
| 5 | +import { Warning } from 'src/components/primitives/Warning'; |
| 6 | +import { ExtendedFormattedUser } from 'src/hooks/app-data-provider/useAppDataProvider'; |
| 7 | +import { useAssetCaps } from 'src/hooks/useAssetCaps'; |
| 8 | +import { useModalContext } from 'src/hooks/useModal'; |
| 9 | +import { useZeroLTVBlockingWithdraw } from 'src/hooks/useZeroLTVBlockingWithdraw'; |
| 10 | + |
| 11 | +import { GasEstimationError } from '../FlowCommons/GasEstimationError'; |
| 12 | +import { ModalWrapperProps } from '../FlowCommons/ModalWrapper'; |
| 13 | +import { TxSuccessView } from '../FlowCommons/Success'; |
| 14 | +import { DetailsHFLine, DetailsNumberLine, TxModalDetails } from '../FlowCommons/TxModalDetails'; |
| 15 | +import { IsolationModeWarning } from '../Warnings/IsolationModeWarning'; |
| 16 | +import { CollateralChangeActions } from './CollateralChangeActions'; |
| 17 | + |
| 18 | +export type CollateralChangeModalContentProps = { |
| 19 | + underlyingAsset: string; |
| 20 | +}; |
| 21 | + |
| 22 | +export enum ErrorType { |
| 23 | + DO_NOT_HAVE_SUPPLIES_IN_THIS_CURRENCY, |
| 24 | + CAN_NOT_USE_THIS_CURRENCY_AS_COLLATERAL, |
| 25 | + CAN_NOT_SWITCH_USAGE_AS_COLLATERAL_MODE, |
| 26 | + ZERO_LTV_WITHDRAW_BLOCKED, |
| 27 | +} |
| 28 | + |
| 29 | +export const CollateralChangeModalContent = ({ |
| 30 | + poolReserve, |
| 31 | + userReserve, |
| 32 | + isWrongNetwork, |
| 33 | + symbol, |
| 34 | + user, |
| 35 | +}: ModalWrapperProps & { user: ExtendedFormattedUser }) => { |
| 36 | + const { gasLimit, mainTxState: collateralChangeTxState, txError } = useModalContext(); |
| 37 | + const { debtCeiling } = useAssetCaps(); |
| 38 | + |
| 39 | + const [collateralEnabled, setCollateralEnabled] = useState( |
| 40 | + userReserve.usageAsCollateralEnabledOnUser |
| 41 | + ); |
| 42 | + |
| 43 | + // Health factor calculations |
| 44 | + const usageAsCollateralModeAfterSwitch = !userReserve.usageAsCollateralEnabledOnUser; |
| 45 | + const currenttotalCollateralMarketReferenceCurrency = valueToBigNumber( |
| 46 | + user.totalCollateralMarketReferenceCurrency |
| 47 | + ); |
| 48 | + |
| 49 | + // Messages |
| 50 | + const showEnableIsolationModeMsg = !poolReserve.isIsolated && usageAsCollateralModeAfterSwitch; |
| 51 | + const showDisableIsolationModeMsg = !poolReserve.isIsolated && !usageAsCollateralModeAfterSwitch; |
| 52 | + const showEnterIsolationModeMsg = poolReserve.isIsolated && usageAsCollateralModeAfterSwitch; |
| 53 | + const showExitIsolationModeMsg = poolReserve.isIsolated && !usageAsCollateralModeAfterSwitch; |
| 54 | + |
| 55 | + const totalCollateralAfterSwitchETH = currenttotalCollateralMarketReferenceCurrency[ |
| 56 | + usageAsCollateralModeAfterSwitch ? 'plus' : 'minus' |
| 57 | + ](userReserve.underlyingBalanceMarketReferenceCurrency); |
| 58 | + |
| 59 | + const healthFactorAfterSwitch = calculateHealthFactorFromBalancesBigUnits({ |
| 60 | + collateralBalanceMarketReferenceCurrency: totalCollateralAfterSwitchETH, |
| 61 | + borrowBalanceMarketReferenceCurrency: user.totalBorrowsMarketReferenceCurrency, |
| 62 | + currentLiquidationThreshold: user.currentLiquidationThreshold, |
| 63 | + }); |
| 64 | + |
| 65 | + const assetsBlockingWithdraw = useZeroLTVBlockingWithdraw(); |
| 66 | + |
| 67 | + // error handling |
| 68 | + let blockingError: ErrorType | undefined = undefined; |
| 69 | + if (assetsBlockingWithdraw.length > 0 && !assetsBlockingWithdraw.includes(poolReserve.symbol)) { |
| 70 | + blockingError = ErrorType.ZERO_LTV_WITHDRAW_BLOCKED; |
| 71 | + } else if (valueToBigNumber(userReserve.underlyingBalance).eq(0)) { |
| 72 | + blockingError = ErrorType.DO_NOT_HAVE_SUPPLIES_IN_THIS_CURRENCY; |
| 73 | + } else if ( |
| 74 | + (!userReserve.usageAsCollateralEnabledOnUser && |
| 75 | + poolReserve.reserveLiquidationThreshold === '0') || |
| 76 | + poolReserve.reserveLiquidationThreshold === '0' |
| 77 | + ) { |
| 78 | + blockingError = ErrorType.CAN_NOT_USE_THIS_CURRENCY_AS_COLLATERAL; |
| 79 | + } else if ( |
| 80 | + userReserve.usageAsCollateralEnabledOnUser && |
| 81 | + user.totalBorrowsMarketReferenceCurrency !== '0' && |
| 82 | + healthFactorAfterSwitch.lte('1') |
| 83 | + ) { |
| 84 | + blockingError = ErrorType.CAN_NOT_SWITCH_USAGE_AS_COLLATERAL_MODE; |
| 85 | + } |
| 86 | + |
| 87 | + // error render handling |
| 88 | + const BlockingError: React.FC = () => { |
| 89 | + switch (blockingError) { |
| 90 | + case ErrorType.DO_NOT_HAVE_SUPPLIES_IN_THIS_CURRENCY: |
| 91 | + return <Trans>You do not have supplies in this currency</Trans>; |
| 92 | + case ErrorType.CAN_NOT_USE_THIS_CURRENCY_AS_COLLATERAL: |
| 93 | + return <Trans>You can not use this currency as collateral</Trans>; |
| 94 | + case ErrorType.CAN_NOT_SWITCH_USAGE_AS_COLLATERAL_MODE: |
| 95 | + return ( |
| 96 | + <Trans> |
| 97 | + You can not switch usage as collateral mode for this currency, because it will cause |
| 98 | + collateral call |
| 99 | + </Trans> |
| 100 | + ); |
| 101 | + case ErrorType.ZERO_LTV_WITHDRAW_BLOCKED: |
| 102 | + return ( |
| 103 | + <Trans> |
| 104 | + Assets with zero LTV ({assetsBlockingWithdraw.join(', ')}) must be withdrawn or disabled |
| 105 | + as collateral to perform this action |
| 106 | + </Trans> |
| 107 | + ); |
| 108 | + default: |
| 109 | + return null; |
| 110 | + } |
| 111 | + }; |
| 112 | + |
| 113 | + // Effect to handle changes in collateral mode after switch as polling is fetching reserve state different after successful tx |
| 114 | + useEffect(() => { |
| 115 | + if (collateralChangeTxState.success) { |
| 116 | + setCollateralEnabled(usageAsCollateralModeAfterSwitch); |
| 117 | + } |
| 118 | + }, [collateralChangeTxState.success, collateralEnabled]); |
| 119 | + |
| 120 | + if (collateralChangeTxState.success) |
| 121 | + return <TxSuccessView collateral={collateralEnabled} symbol={poolReserve.symbol} />; |
| 122 | + |
| 123 | + return ( |
| 124 | + <> |
| 125 | + {showEnableIsolationModeMsg && ( |
| 126 | + <Warning severity="warning" icon={false} sx={{ mb: 3 }}> |
| 127 | + <Trans> |
| 128 | + Enabling this asset as collateral increases your borrowing power and Health Factor. |
| 129 | + However, it can get liquidated if your health factor drops below 1. |
| 130 | + </Trans> |
| 131 | + </Warning> |
| 132 | + )} |
| 133 | + |
| 134 | + {showDisableIsolationModeMsg && ( |
| 135 | + <Warning severity="warning" icon={false} sx={{ mb: 3 }}> |
| 136 | + <Trans> |
| 137 | + Disabling this asset as collateral affects your borrowing power and Health Factor. |
| 138 | + </Trans> |
| 139 | + </Warning> |
| 140 | + )} |
| 141 | + |
| 142 | + {showEnterIsolationModeMsg && <IsolationModeWarning asset={poolReserve.symbol} />} |
| 143 | + |
| 144 | + {showExitIsolationModeMsg && ( |
| 145 | + <Warning severity="info" icon={false} sx={{ mb: 3 }}> |
| 146 | + <Trans>You will exit isolation mode and other tokens can now be used as collateral</Trans> |
| 147 | + </Warning> |
| 148 | + )} |
| 149 | + |
| 150 | + {poolReserve.isIsolated && debtCeiling.determineWarningDisplay({ debtCeiling })} |
| 151 | + |
| 152 | + <TxModalDetails gasLimit={gasLimit}> |
| 153 | + <DetailsNumberLine |
| 154 | + symbol={poolReserve.symbol} |
| 155 | + iconSymbol={poolReserve.iconSymbol} |
| 156 | + description={<Trans>Supply balance</Trans>} |
| 157 | + value={userReserve.underlyingBalance} |
| 158 | + /> |
| 159 | + <DetailsHFLine |
| 160 | + visibleHfChange={true} |
| 161 | + healthFactor={user.healthFactor} |
| 162 | + futureHealthFactor={healthFactorAfterSwitch.toString(10)} |
| 163 | + /> |
| 164 | + </TxModalDetails> |
| 165 | + |
| 166 | + {blockingError !== undefined && ( |
| 167 | + <Typography variant="helperText" color="error.main"> |
| 168 | + <BlockingError /> |
| 169 | + </Typography> |
| 170 | + )} |
| 171 | + |
| 172 | + {txError && <GasEstimationError txError={txError} />} |
| 173 | + |
| 174 | + <CollateralChangeActions |
| 175 | + symbol={symbol} |
| 176 | + poolReserve={poolReserve} |
| 177 | + usageAsCollateral={usageAsCollateralModeAfterSwitch} |
| 178 | + isWrongNetwork={isWrongNetwork} |
| 179 | + blocked={blockingError !== undefined} |
| 180 | + /> |
| 181 | + </> |
| 182 | + ); |
| 183 | +}; |
0 commit comments