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'
import useMarketDeposits from 'hooks/useMarketDeposits'
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 { data: marketDeposits } = useMarketDeposits()
2023-06-15 12:34:12 +00:00
const { convertAmount } = useDisplayCurrencyPrice()
return useMemo(() => {
const accountLentAssets: LendingMarketTableData[] = [],
2023-06-15 12:34:12 +00:00
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,
)
})
2023-06-15 12:34:12 +00:00
return {
accountLentAssets,
availableAssets,
allAssets: [...accountLentAssets, ...availableAssets],
}
}, [markets, marketLiquidities, accountLentAmounts, marketDeposits, convertAmount])
2023-06-15 12:34:12 +00:00
}
export default useLendingMarketAssetsTableData