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 (