mars-v2-frontend/src/hooks/data/useBalances.tsx
Linkie Link 6efa380a98
Ux and details update (#77)
* fix: added the metadata base

* tidy: refactor the config structure

* fix: fixed the denom and fetching shenanigans

* fix: get rid of injective

* fix: removed logs

* tidy: updated dependencies

* fix: replaced static fallbacks with networkConfig.asset.base

* fix: remove memo

* tidy: added TODO comment

* fix: fixed the accountDetails

* fix: fixed the wallet connector suffix
2022-12-23 12:23:00 +01:00

34 lines
1.2 KiB
TypeScript

import { useEffect, useState } from 'react'
import { useCreditAccountPositions, useMarkets, useTokenPrices } from 'hooks/queries'
import { useAccountDetailsStore, useNetworkConfigStore } from 'stores'
import { formatBalances } from 'utils/balances'
export const useBalances = () => {
const [balanceData, setBalanceData] = useState<PositionsData[]>()
const { data: marketsData } = useMarkets()
const { data: tokenPrices } = useTokenPrices()
const selectedAccount = useAccountDetailsStore((s) => s.selectedAccount)
const whitelistedAssets = useNetworkConfigStore((s) => s.assets.whitelist)
const { data: positionsData, isLoading: isLoadingPositions } = useCreditAccountPositions(
selectedAccount ?? '',
)
useEffect(() => {
const balances =
positionsData?.coins && tokenPrices
? formatBalances(positionsData.coins, tokenPrices, false, whitelistedAssets)
: []
const debtBalances =
positionsData?.debts && tokenPrices
? formatBalances(positionsData.debts, tokenPrices, true, whitelistedAssets, marketsData)
: []
setBalanceData([...balances, ...debtBalances])
}, [positionsData, marketsData, tokenPrices, whitelistedAssets])
return balanceData
}