import BigNumber from 'bignumber.js' import React, { useRef, useState } from 'react' import ContainerSecondary from 'components/ContainerSecondary' import FundAccountModal from 'components/FundAccountModal' import WithdrawModal from 'components/WithdrawModal' import useAccountStats from 'hooks/useAccountStats' import useCreditAccountPositions from 'hooks/useCreditAccountPositions' import useMarkets from 'hooks/useMarkets' import useTokenPrices from 'hooks/useTokenPrices' import useCreditManagerStore from 'stores/useCreditManagerStore' import useWalletStore from 'stores/useWalletStore' import { formatCurrency } from 'utils/formatters' import { getTokenDecimals, getTokenSymbol } from 'utils/tokens' import { chain } from 'utils/chains' import Button from 'components/Button' const CreditManager = () => { const [showFundWalletModal, setShowFundWalletModal] = useState(false) const [showWithdrawModal, setShowWithdrawModal] = useState(false) // recreate modals and reset state whenever ref changes const modalId = useRef(0) 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 (
Total Position:
{formatCurrency( BigNumber(accountStats?.totalPosition ?? 0) .dividedBy(10 ** chain.stakeCurrency.coinDecimals) .toNumber(), )}
Total Liabilities:
{formatCurrency( BigNumber(accountStats?.totalDebt ?? 0) .dividedBy(10 ** chain.stakeCurrency.coinDecimals) .toNumber(), )}

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)}%
))} )}
setShowFundWalletModal(false)} /> setShowWithdrawModal(false)} />
) } export default CreditManager