mars-v2-frontend/src/hooks/useLendingMarketAssetsTableData.ts

62 lines
2.2 KiB
TypeScript
Raw Normal View History

2023-06-15 12:34:12 +00:00
import { useMemo } from 'react'
import useCurrentAccountLends from 'hooks/useCurrentAccountLends'
import useDepositEnabledMarkets from 'hooks/useDepositEnabledMarkets'
import useDisplayCurrencyPrice from 'hooks/useDisplayCurrencyPrice'
2023-06-15 12:34:12 +00:00
import useMarketLiquidities from 'hooks/useMarketLiquidities'
import { byDenom } from 'utils/array'
import { getAssetByDenom } from 'utils/assets'
import { BN } from 'utils/helpers'
2023-06-15 12:34:12 +00:00
function useLendingMarketAssetsTableData(): {
accountLentAssets: LendingMarketTableData[]
2023-06-15 12:34:12 +00:00
availableAssets: LendingMarketTableData[]
allAssets: LendingMarketTableData[]
2023-06-15 12:34:12 +00:00
} {
const markets = useDepositEnabledMarkets()
const accountLentAmounts = useCurrentAccountLends()
2023-06-15 12:34:12 +00:00
const { data: marketLiquidities } = useMarketLiquidities()
const { convertAmount } = useDisplayCurrencyPrice()
return useMemo(() => {
const accountLentAssets: LendingMarketTableData[] = [],
2023-06-15 12:34:12 +00:00
availableAssets: LendingMarketTableData[] = []
markets.forEach(
({ denom, cap, liquidityRate, liquidationThreshold, maxLtv, borrowEnabled }) => {
const asset = getAssetByDenom(denom) as Asset
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: cap.used,
accountLentValue,
accountLentAmount,
marketLiquidityAmount,
marketDepositCap: cap.max,
marketLiquidityRate: liquidityRate,
marketLiquidationThreshold: liquidationThreshold,
marketMaxLtv: maxLtv,
borrowEnabled,
}
;(lendingMarketAsset.accountLentValue ? accountLentAssets : availableAssets).push(
lendingMarketAsset,
)
},
)
2023-06-15 12:34:12 +00:00
return {
accountLentAssets,
availableAssets,
allAssets: [...accountLentAssets, ...availableAssets],
}
}, [markets, marketLiquidities, accountLentAmounts, convertAmount])
2023-06-15 12:34:12 +00:00
}
export default useLendingMarketAssetsTableData