import React, { useState } from 'react' import BigNumber from 'bignumber.js' import Button from '../Button' import { formatCurrency } from 'utils/formatters' import useCreditManagerStore from 'stores/useCreditManagerStore' import useWalletStore from 'stores/useWalletStore' import useCreditAccountPositions from 'hooks/useCreditAccountPositions' import { getTokenDecimals, getTokenSymbol } from 'utils/tokens' import FundAccount from './FundAccount' import useTokenPrices from 'hooks/useTokenPrices' import useAccountStats from 'hooks/useAccountStats' import useMarkets from 'hooks/useMarkets' import ContainerSecondary from 'components/ContainerSecondary' const CreditManager = () => { const [isFund, setIsFund] = useState(false) const address = useWalletStore((s) => s.address) const selectedAccount = useCreditManagerStore((s) => s.selectedAccount) const { data: positionsData, isLoading: isLoadingPositions } = useCreditAccountPositions( selectedAccount ?? '' ) const { data: tokenPrices } = useTokenPrices() const { data: marketsData } = useMarkets() const accountStats = useAccountStats() const getTokenTotalUSDValue = (amount: string, denom: string) => { // early return if prices are not fetched yet if (!tokenPrices) return 0 return ( BigNumber(amount) .div(10 ** getTokenDecimals(denom)) .toNumber() * tokenPrices[denom] ) } if (!address) { return (
You must have a connected wallet
) } return (
{isFund ? (

Fund Account

) : (
)}
{isFund ? ( ) : ( <>
Total Position:
{formatCurrency(accountStats?.totalPosition ?? 0)}
Total Liabilities:
{formatCurrency(accountStats?.totalDebt ?? 0)}

Balances

{isLoadingPositions ? (
Loading...
) : ( <>
Asset
Value
Size
APY
{positionsData?.coins.map((coin) => (
{getTokenSymbol(coin.denom)}
{formatCurrency(getTokenTotalUSDValue(coin.amount, coin.denom))}
{BigNumber(coin.amount) .div(10 ** getTokenDecimals(coin.denom)) .toNumber() .toLocaleString(undefined, { maximumFractionDigits: getTokenDecimals(coin.denom), })}
-
))} {positionsData?.debts.map((coin) => (
{getTokenSymbol(coin.denom)}
-{formatCurrency(getTokenTotalUSDValue(coin.amount, coin.denom))}
- {BigNumber(coin.amount) .div(10 ** getTokenDecimals(coin.denom)) .toNumber() .toLocaleString(undefined, { maximumFractionDigits: 6, })}
-{(Number(marketsData?.[coin.denom].borrow_rate) * 100).toFixed(1)}%
))} )}
)}
) } export default CreditManager