import { INITIAL_ANIMATION_SETTINGS } from '@components/settings/AnimationSettings' import useLocalStorageState from 'hooks/useLocalStorageState' import useMangoAccount from 'hooks/useMangoAccount' import useMangoGroup from 'hooks/useMangoGroup' import { useTranslation } from 'next-i18next' import { ANIMATION_SETTINGS_KEY, USDC_MINT } from 'utils/constants' import FlipNumbers from 'react-flip-numbers' import Button from '@components/shared/Button' import { formatCurrencyValue } from 'utils/numbers' import { useEffect, useMemo, useState } from 'react' import YourBorrowsTable from './YourBorrowsTable' import AssetsBorrowsTable from './AssetsBorrowsTable' import { ArrowDownRightIcon, ArrowUpLeftIcon } from '@heroicons/react/20/solid' import { useWallet } from '@solana/wallet-adapter-react' import BorrowRepayModal from '@components/modals/BorrowRepayModal' import CreateAccountModal from '@components/modals/CreateAccountModal' import TabButtons from '@components/shared/TabButtons' import { useViewport } from 'hooks/useViewport' import { breakpoints } from 'utils/theme' import FormatNumericValue from '@components/shared/FormatNumericValue' import useBanksWithBalances from 'hooks/useBanksWithBalances' import { PublicKey } from '@solana/web3.js' import { getMaxWithdrawForBank } from '@components/swap/useTokenMax' const BorrowPage = () => { const { t } = useTranslation(['common', 'borrow']) const { group } = useMangoGroup() const { mangoAccount, mangoAccountAddress } = useMangoAccount() const [activeTab, setActiveTab] = useState('borrow:your-borrows') const [showBorrowModal, setShowBorrowModal] = useState(false) const [showRepayModal, setShowRepayModal] = useState(false) const [showCreateAccountModal, setShowCreateAccountModal] = useState(false) const { connected } = useWallet() const { width } = useViewport() const fullWidthTabs = width ? width < breakpoints.sm : false const banks = useBanksWithBalances() const handleBorrowModal = () => { if (!connected || mangoAccount) { setShowBorrowModal(true) } else { setShowCreateAccountModal(true) } } const [animationSettings] = useLocalStorageState( ANIMATION_SETTINGS_KEY, INITIAL_ANIMATION_SETTINGS, ) const filteredBanks = useMemo(() => { if (banks.length) { return banks.filter((b) => b.borrowedAmount > 0) } return [] }, [banks]) const borrowValue = useMemo(() => { if (!filteredBanks.length) return 0 return filteredBanks.reduce( (a, c) => a + Math.abs(c.borrowedAmount) * c.bank.uiPrice, 0, ) }, [filteredBanks]) useEffect(() => { if (mangoAccountAddress && !borrowValue) { setActiveTab('borrow:assets-to-borrow') } }, [borrowValue, mangoAccountAddress]) const [collateralRemaining, collateralRemainingRatio] = useMemo(() => { if (mangoAccount && group) { const usdcBank = group.getFirstBankByMint(new PublicKey(USDC_MINT)) const remaining = getMaxWithdrawForBank( group, usdcBank, mangoAccount, true, ).toNumber() if (borrowValue) { const total = borrowValue + remaining const ratio = (remaining / total) * 100 return [remaining, ratio] } return [remaining, 100] } return [0, 0] }, [borrowValue, mangoAccount, group]) return (

{t('borrow:current-borrow-value')}

{animationSettings['number-scroll'] ? ( group && mangoAccount ? ( ) : ( ) ) : ( )}

{t('borrow:available-to-borrow')}

setActiveTab(v)} showBorders values={[ ['borrow:your-borrows', 0], ['borrow:assets-to-borrow', 0], ]} />
{activeTab === 'borrow:your-borrows' ? ( ) : ( )} {showBorrowModal ? ( setShowBorrowModal(false)} /> ) : null} {showRepayModal ? ( setShowRepayModal(false)} /> ) : null} {showCreateAccountModal ? ( setShowCreateAccountModal(false)} /> ) : null}
) } export default BorrowPage