2023-10-18 07:38:24 +00:00
|
|
|
import useSWR from 'swr'
|
2023-07-11 19:01:14 +00:00
|
|
|
|
2023-10-27 16:24:13 +00:00
|
|
|
import useBorrowEnabledMarkets from 'hooks/useBorrowEnabledMarkets'
|
2023-09-19 13:39:14 +00:00
|
|
|
import useCurrentAccountDebts from 'hooks/useCurrentAccountDebts'
|
2023-07-11 19:01:14 +00:00
|
|
|
import useMarketBorrowings from 'hooks/useMarketBorrowings'
|
|
|
|
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'
|
|
|
|
|
2023-10-18 07:38:24 +00:00
|
|
|
export default function useBorrowMarketAssetsTableData(suspense = true) {
|
|
|
|
const markets = useBorrowEnabledMarkets()
|
2023-07-11 19:01:14 +00:00
|
|
|
const accountDebts = useCurrentAccountDebts()
|
|
|
|
const { data: borrowData } = useMarketBorrowings()
|
|
|
|
const { data: marketDeposits } = useMarketDeposits()
|
|
|
|
const { data: marketLiquidities } = useMarketLiquidities()
|
|
|
|
|
2023-10-18 07:38:24 +00:00
|
|
|
return useSWR(
|
|
|
|
'borrowMarketAssetsTableData',
|
|
|
|
async (): Promise<{
|
|
|
|
accountBorrowedAssets: BorrowMarketTableData[]
|
|
|
|
availableAssets: BorrowMarketTableData[]
|
|
|
|
allAssets: BorrowMarketTableData[]
|
|
|
|
}> => {
|
|
|
|
const accountBorrowedAssets: BorrowMarketTableData[] = [],
|
|
|
|
availableAssets: BorrowMarketTableData[] = []
|
|
|
|
|
|
|
|
markets.forEach(({ denom, liquidityRate, liquidationThreshold, maxLtv }) => {
|
|
|
|
const asset = getAssetByDenom(denom) as Asset
|
|
|
|
const borrow = borrowData.find((borrow) => borrow.denom === denom)
|
|
|
|
const marketDepositAmount = BN(marketDeposits.find(byDenom(denom))?.amount ?? 0)
|
|
|
|
const marketLiquidityAmount = BN(marketLiquidities.find(byDenom(denom))?.amount ?? 0)
|
|
|
|
|
|
|
|
const debt = accountDebts?.find((debt) => debt.denom === denom)
|
|
|
|
if (!borrow) return
|
|
|
|
|
|
|
|
const borrowMarketAsset: BorrowMarketTableData = {
|
|
|
|
...borrow,
|
|
|
|
asset,
|
|
|
|
debt: debt?.amount,
|
|
|
|
marketDepositAmount,
|
|
|
|
marketLiquidityAmount,
|
|
|
|
marketLiquidityRate: liquidityRate,
|
|
|
|
marketLiquidationThreshold: liquidationThreshold,
|
|
|
|
marketMaxLtv: maxLtv,
|
|
|
|
}
|
|
|
|
;(borrowMarketAsset.debt ? accountBorrowedAssets : availableAssets).push(borrowMarketAsset)
|
|
|
|
})
|
|
|
|
|
|
|
|
return {
|
|
|
|
accountBorrowedAssets,
|
|
|
|
availableAssets,
|
|
|
|
allAssets: [...accountBorrowedAssets, ...availableAssets],
|
2023-07-11 19:01:14 +00:00
|
|
|
}
|
2023-10-18 07:38:24 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
suspense,
|
|
|
|
},
|
|
|
|
)
|
2023-07-11 19:01:14 +00:00
|
|
|
}
|