|
| 1 | +import { valueToBigNumber } from '@aave/math-utils'; |
| 2 | +import { SxProps, Theme } from '@mui/system'; |
| 3 | +import { createContext, ReactNode, useContext } from 'react'; |
| 4 | +import { BorrowCapMaxedTooltip } from 'src/components/infoTooltips/BorrowCapMaxedTooltip'; |
| 5 | +import { DebtCeilingMaxedTooltip } from 'src/components/infoTooltips/DebtCeilingMaxedTooltip'; |
| 6 | +import { SupplyCapMaxedTooltip } from 'src/components/infoTooltips/SupplyCapMaxedTooltip'; |
| 7 | +import { BorrowCapWarning } from 'src/components/transactions/Warnings/BorrowCapWarning'; |
| 8 | +import { DebtCeilingWarning } from 'src/components/transactions/Warnings/DebtCeilingWarning'; |
| 9 | +import { SupplyCapWarning } from 'src/components/transactions/Warnings/SupplyCapWarning'; |
| 10 | + |
| 11 | +import { ReserveWithId } from './app-data-provider/useAppDataProvider'; |
| 12 | + |
| 13 | +type WarningDisplayProps = { |
| 14 | + supplyCap?: AssetCapData; |
| 15 | + borrowCap?: AssetCapData; |
| 16 | + debtCeiling?: AssetCapData; |
| 17 | + icon?: boolean; |
| 18 | + sx?: SxProps<Theme>; |
| 19 | +}; |
| 20 | + |
| 21 | +export type AssetCapData = { |
| 22 | + percentUsed: number; |
| 23 | + isMaxed: boolean; |
| 24 | +}; |
| 25 | + |
| 26 | +export type AssetCapHookData = AssetCapData & { |
| 27 | + determineWarningDisplay: (props: WarningDisplayProps) => JSX.Element | null; |
| 28 | + displayMaxedTooltip: (props: WarningDisplayProps) => JSX.Element | null; |
| 29 | +}; |
| 30 | + |
| 31 | +export type AssetCapUsageData = { |
| 32 | + reserve: ReserveWithId; |
| 33 | + supplyCap: AssetCapHookData; |
| 34 | + borrowCap: AssetCapHookData; |
| 35 | + debtCeiling: AssetCapHookData; |
| 36 | +}; |
| 37 | + |
| 38 | +const getAssetCapData = (asset: ReserveWithId): AssetCapUsageData => { |
| 39 | + const { supplyCapUsage, supplyCapReached } = getSupplyCapData(asset); |
| 40 | + const { borrowCapUsage, borrowCapReached } = getBorrowCapData(asset); |
| 41 | + const { debtCeilingUsage, debtCeilingReached } = getDebtCeilingData(asset); |
| 42 | + /* |
| 43 | + Aggregated Data |
| 44 | + */ |
| 45 | + const assetCapUsageData: AssetCapUsageData = { |
| 46 | + reserve: asset, |
| 47 | + supplyCap: { |
| 48 | + percentUsed: supplyCapUsage, |
| 49 | + isMaxed: supplyCapReached, |
| 50 | + // percentUsed: 99.9, |
| 51 | + // isMaxed: true, |
| 52 | + determineWarningDisplay: ({ supplyCap, icon, ...rest }) => |
| 53 | + supplyCap ? <SupplyCapWarning supplyCap={supplyCap} icon={icon} {...rest} /> : null, |
| 54 | + displayMaxedTooltip: ({ supplyCap }) => |
| 55 | + supplyCap ? <SupplyCapMaxedTooltip supplyCap={supplyCap} /> : null, |
| 56 | + }, |
| 57 | + borrowCap: { |
| 58 | + percentUsed: borrowCapUsage, |
| 59 | + isMaxed: borrowCapReached, |
| 60 | + // percentUsed: 98.5, |
| 61 | + // isMaxed: false, |
| 62 | + determineWarningDisplay: ({ borrowCap, icon, ...rest }) => |
| 63 | + borrowCap ? <BorrowCapWarning borrowCap={borrowCap} icon={icon} {...rest} /> : null, |
| 64 | + displayMaxedTooltip: ({ borrowCap }) => |
| 65 | + borrowCap ? <BorrowCapMaxedTooltip borrowCap={borrowCap} /> : null, |
| 66 | + }, |
| 67 | + debtCeiling: { |
| 68 | + percentUsed: debtCeilingUsage, |
| 69 | + isMaxed: debtCeilingReached, |
| 70 | + // percentUsed: 99.994, |
| 71 | + // isMaxed: true, |
| 72 | + determineWarningDisplay: ({ debtCeiling, icon, ...rest }) => |
| 73 | + debtCeiling ? <DebtCeilingWarning debtCeiling={debtCeiling} icon={icon} {...rest} /> : null, |
| 74 | + displayMaxedTooltip: ({ debtCeiling }) => |
| 75 | + debtCeiling ? <DebtCeilingMaxedTooltip debtCeiling={debtCeiling} /> : null, |
| 76 | + }, |
| 77 | + }; |
| 78 | + |
| 79 | + return assetCapUsageData; |
| 80 | +}; |
| 81 | + |
| 82 | +/* |
| 83 | + Asset Caps Context |
| 84 | +*/ |
| 85 | +export const AssetCapsSDKContext = createContext({} as AssetCapUsageData); |
| 86 | + |
| 87 | +/* |
| 88 | + Asset Caps Provider Component |
| 89 | +*/ |
| 90 | +export const AssetCapsProvider = ({ |
| 91 | + children, |
| 92 | + asset, |
| 93 | +}: { |
| 94 | + children: ReactNode; |
| 95 | + asset: ReserveWithId; |
| 96 | +}): JSX.Element | null => { |
| 97 | + // Return if no reserve is provided |
| 98 | + if (!asset) { |
| 99 | + console.warn('<AssetCapsProvider /> was not given a valid reserve asset to parse'); |
| 100 | + return null; |
| 101 | + } |
| 102 | + |
| 103 | + const providerValue = getAssetCapData(asset); |
| 104 | + |
| 105 | + return ( |
| 106 | + <AssetCapsSDKContext.Provider value={providerValue}>{children}</AssetCapsSDKContext.Provider> |
| 107 | + ); |
| 108 | +}; |
| 109 | + |
| 110 | +/* |
| 111 | + useAssetCaspsSDKContext hook |
| 112 | +*/ |
| 113 | +export const useAssetCapsSDK = () => { |
| 114 | + const context = useContext(AssetCapsSDKContext); |
| 115 | + |
| 116 | + if (context === undefined) { |
| 117 | + throw new Error( |
| 118 | + 'useAssetCaps() can only be used inside of <AssetCapsProvider />, ' + |
| 119 | + 'please declare it at a higher level.' |
| 120 | + ); |
| 121 | + } |
| 122 | + |
| 123 | + return context; |
| 124 | +}; |
| 125 | + |
| 126 | +export { AssetCapsProvider as AssetCapsProviderSDK }; |
| 127 | + |
| 128 | +/** |
| 129 | + * Calculates % of totalLiquidity / supplyCap. |
| 130 | + * @param asset ReserveWithId |
| 131 | + * @returns { supplyCapUsage: number, supplyCapReached: boolean } |
| 132 | + */ |
| 133 | +export const getSupplyCapData = (asset: ReserveWithId) => { |
| 134 | + const total = valueToBigNumber(asset?.supplyInfo?.total.value ?? '0'); |
| 135 | + const cap = valueToBigNumber(asset?.supplyInfo?.supplyCap.amount.value ?? '0'); |
| 136 | + |
| 137 | + const rawUsage = cap.isZero() ? 0 : total.dividedBy(cap).multipliedBy(100).toNumber(); |
| 138 | + |
| 139 | + return { |
| 140 | + supplyCapUsage: Number.isFinite(rawUsage) ? rawUsage : 0, |
| 141 | + supplyCapReached: asset?.supplyInfo?.supplyCapReached || rawUsage >= 99.99, |
| 142 | + }; |
| 143 | +}; |
| 144 | + |
| 145 | +/** |
| 146 | + * Calculates borrow cap usage and % of totalDebt / borrowCap. |
| 147 | + * @param asset ReserveWithId |
| 148 | + * @returns { borrowCapUsage: number, borrowCapReached: boolean } |
| 149 | + */ |
| 150 | +export const getBorrowCapData = (asset: ReserveWithId) => { |
| 151 | + const totalDebt = valueToBigNumber(asset?.borrowInfo?.total.amount.value ?? '0'); |
| 152 | + const cap = valueToBigNumber(asset?.borrowInfo?.borrowCap.amount.value ?? '0'); |
| 153 | + |
| 154 | + const rawUsage = cap.isZero() ? 0 : totalDebt.dividedBy(cap).multipliedBy(100).toNumber(); |
| 155 | + |
| 156 | + const borrowCapReached = asset?.borrowInfo?.borrowCapReached || rawUsage >= 99.99; |
| 157 | + |
| 158 | + return { |
| 159 | + borrowCapUsage: Number.isFinite(rawUsage) ? rawUsage : 0, |
| 160 | + borrowCapReached: borrowCapReached ?? false, |
| 161 | + }; |
| 162 | +}; |
| 163 | + |
| 164 | +/** |
| 165 | + * Calculates debt ceiling usage and % of isolationModeTotalDebt / debtCeiling. |
| 166 | + * @param asset |
| 167 | + * @returns {debtCeilingUsage: number, debtCeilingReached: boolean} |
| 168 | + */ |
| 169 | +export const getDebtCeilingData = (asset: ReserveWithId) => { |
| 170 | + const totalBorrows = valueToBigNumber( |
| 171 | + asset?.isolationModeConfig?.totalBorrows.amount.value ?? '0' |
| 172 | + ); |
| 173 | + const debtCeilingCap = valueToBigNumber( |
| 174 | + asset?.isolationModeConfig?.debtCeiling.amount.value ?? '0' |
| 175 | + ); |
| 176 | + const rawUsage = debtCeilingCap.isZero() |
| 177 | + ? 0 |
| 178 | + : totalBorrows.dividedBy(debtCeilingCap).multipliedBy(100).toNumber(); |
| 179 | + |
| 180 | + return { |
| 181 | + debtCeilingUsage: Number.isFinite(rawUsage) ? rawUsage : 0, |
| 182 | + debtCeilingReached: rawUsage >= 99.99, |
| 183 | + }; |
| 184 | +}; |
0 commit comments