mars-v2-frontend/src/hooks/useLendingMarketAssetsTableData.ts
Linkie Link 85533cdea3
Deposit Cap and Utilization Fix (#654)
* fix: fixed the deposit cap and total supplied / utilization rate

* fix: fixed build

* fix: fixed build

* fix: avoid deposit cap usage over 100%

* refactor market data apy/ltv

* fix: fixed the withdraw from vaults modal

* tidy: refactor

---------

Co-authored-by: Bob van der Helm <34470358+bobthebuidlr@users.noreply.github.com>
2023-11-26 11:43:37 +01:00

62 lines
2.2 KiB
TypeScript

import { useMemo } from 'react'
import useCurrentAccountLends from 'hooks/useCurrentAccountLends'
import useDepositEnabledMarkets from 'hooks/useDepositEnabledMarkets'
import useDisplayCurrencyPrice from 'hooks/useDisplayCurrencyPrice'
import useMarketDeposits from 'hooks/useMarketDeposits'
import useMarketLiquidities from 'hooks/useMarketLiquidities'
import { byDenom } from 'utils/array'
import { getAssetByDenom } from 'utils/assets'
import { BN } from 'utils/helpers'
function useLendingMarketAssetsTableData(): {
accountLentAssets: LendingMarketTableData[]
availableAssets: LendingMarketTableData[]
allAssets: LendingMarketTableData[]
} {
const markets = useDepositEnabledMarkets()
const accountLentAmounts = useCurrentAccountLends()
const { data: marketLiquidities } = useMarketLiquidities()
const { data: marketDeposits } = useMarketDeposits()
const { convertAmount } = useDisplayCurrencyPrice()
return useMemo(() => {
const accountLentAssets: LendingMarketTableData[] = [],
availableAssets: LendingMarketTableData[] = []
markets.forEach(({ denom, cap, ltv, apy, borrowEnabled }) => {
const asset = getAssetByDenom(denom) as Asset
const marketDepositAmount = BN(marketDeposits.find(byDenom(denom))?.amount ?? 0)
const marketLiquidityAmount = BN(marketLiquidities.find(byDenom(denom))?.amount ?? 0)
const accountLentAmount = accountLentAmounts.find(byDenom(denom))?.amount
const accountLentValue = accountLentAmount
? convertAmount(asset, accountLentAmount)
: undefined
const lendingMarketAsset: LendingMarketTableData = {
asset,
marketDepositAmount,
accountLentValue,
accountLentAmount,
marketLiquidityAmount,
apy,
ltv,
borrowEnabled,
cap,
}
;(lendingMarketAsset.accountLentValue ? accountLentAssets : availableAssets).push(
lendingMarketAsset,
)
})
return {
accountLentAssets,
availableAssets,
allAssets: [...accountLentAssets, ...availableAssets],
}
}, [markets, marketLiquidities, accountLentAmounts, marketDeposits, convertAmount])
}
export default useLendingMarketAssetsTableData