mango-v4-ui/components/account/AccountPage.tsx

539 lines
19 KiB
TypeScript
Raw Normal View History

2022-09-01 10:33:29 -07:00
import {
HealthType,
toUiDecimalsForQuote,
} from '@blockworks-foundation/mango-v4'
import { useTranslation } from 'next-i18next'
import { serverSideTranslations } from 'next-i18next/serverSideTranslations'
import { useEffect, useMemo, useState } from 'react'
import AccountActions from './AccountActions'
import DepositModal from '../modals/DepositModal'
import WithdrawModal from '../modals/WithdrawModal'
2022-09-12 08:53:57 -07:00
import mangoStore, { PerformanceDataItem } from '@store/mangoStore'
2022-10-25 19:57:28 -07:00
import { formatFixedDecimals } from '../../utils/numbers'
2022-09-01 10:33:29 -07:00
import FlipNumbers from 'react-flip-numbers'
2022-10-10 14:15:35 -07:00
import dynamic from 'next/dynamic'
const SimpleAreaChart = dynamic(
() => import('@components/shared/SimpleAreaChart'),
{ ssr: false }
)
2022-09-01 10:33:29 -07:00
import { COLORS } from '../../styles/colors'
import { useTheme } from 'next-themes'
import { IconButton } from '../shared/Button'
2022-09-06 21:36:35 -07:00
import {
ArrowsPointingOutIcon,
ChevronRightIcon,
} from '@heroicons/react/20/solid'
2022-09-01 10:33:29 -07:00
import { Transition } from '@headlessui/react'
import AccountTabs from './AccountTabs'
import SheenLoader from '../shared/SheenLoader'
import AccountChart from './AccountChart'
2022-11-18 09:09:39 -08:00
import useMangoAccount from '../../hooks/useMangoAccount'
2022-10-05 22:11:28 -07:00
import Change from '../shared/Change'
import Tooltip from '@components/shared/Tooltip'
2022-09-22 21:00:42 -07:00
import { IS_ONBOARDED_KEY } from 'utils/constants'
2022-09-21 21:25:24 -07:00
import { useWallet } from '@solana/wallet-adapter-react'
import useLocalStorageState from 'hooks/useLocalStorageState'
import AccountOnboardingTour from '@components/tours/AccountOnboardingTour'
2022-11-20 16:50:25 -08:00
import dayjs from 'dayjs'
2022-09-01 10:33:29 -07:00
export async function getStaticProps({ locale }: { locale: string }) {
return {
props: {
...(await serverSideTranslations(locale, [
'common',
'close-account',
'trade',
])),
},
}
}
const AccountPage = () => {
const { t } = useTranslation('common')
2022-09-21 21:25:24 -07:00
const { connected } = useWallet()
const group = mangoStore.getState().group
2022-09-21 21:25:24 -07:00
const { mangoAccount } = useMangoAccount()
2022-09-01 10:33:29 -07:00
const actions = mangoStore((s) => s.actions)
const loadPerformanceData = mangoStore(
(s) => s.mangoAccount.stats.performance.loading
)
const performanceData = mangoStore(
(s) => s.mangoAccount.stats.performance.data
)
const totalInterestData = mangoStore(
(s) => s.mangoAccount.stats.interestTotals.data
)
const [showDepositModal, setShowDepositModal] = useState<boolean>(false)
const [showWithdrawModal, setShowWithdrawModal] = useState<boolean>(false)
const [chartToShow, setChartToShow] = useState<
'account-value' | 'cumulative-interest-value' | 'pnl' | ''
>('')
const [oneDayPerformanceData, setOneDayPerformanceData] = useState<
PerformanceDataItem[]
>([])
const [showExpandChart, setShowExpandChart] = useState<boolean>(false)
const { theme } = useTheme()
2022-09-22 21:00:42 -07:00
const tourSettings = mangoStore((s) => s.settings.tours)
const [isOnBoarded] = useLocalStorageState(IS_ONBOARDED_KEY)
2022-09-01 10:33:29 -07:00
const leverage = useMemo(() => {
if (!group || !mangoAccount) return 0
const liabsValue = mangoAccount
.getLiabsValue(group, HealthType.init)!
.toNumber()
2022-10-06 19:10:12 -07:00
const totalCollateral = mangoAccount
.getAssetsValue(group, HealthType.init)!
2022-10-06 19:10:12 -07:00
.toNumber()
2022-10-25 19:57:28 -07:00
if (isNaN(liabsValue / totalCollateral)) {
return 0
} else return liabsValue / totalCollateral
2022-10-07 13:02:11 -07:00
}, [mangoAccount, group])
2022-09-01 10:33:29 -07:00
useEffect(() => {
if (mangoAccount) {
const pubKey = mangoAccount.publicKey.toString()
actions.fetchAccountPerformance(pubKey, 1)
actions.fetchAccountInterestTotals(pubKey)
}
}, [actions, mangoAccount])
useEffect(() => {
2022-10-05 22:11:28 -07:00
if (mangoAccount && performanceData.length && !chartToShow) {
2022-09-01 10:33:29 -07:00
setOneDayPerformanceData(performanceData)
}
2022-10-05 22:11:28 -07:00
}, [mangoAccount, performanceData, chartToShow])
2022-09-01 10:33:29 -07:00
const onHoverMenu = (open: boolean, action: string) => {
if (
(!open && action === 'onMouseEnter') ||
(open && action === 'onMouseLeave')
) {
setShowExpandChart(!open)
}
}
const handleShowAccountValueChart = () => {
setChartToShow('account-value')
setShowExpandChart(false)
}
const handleHideChart = () => {
const set = mangoStore.getState().set
set((s) => {
s.mangoAccount.stats.performance.data = oneDayPerformanceData
})
setChartToShow('')
}
2022-11-20 16:50:25 -08:00
const accountValue = useMemo(() => {
if (!group || !mangoAccount) return 0.0
return toUiDecimalsForQuote(mangoAccount.getEquity(group)!.toNumber())
}, [group, mangoAccount])
2022-09-01 10:33:29 -07:00
const { accountPnl, accountValueChange } = useMemo(() => {
2022-11-20 16:50:25 -08:00
if (accountValue && performanceData.length) {
2022-09-01 10:33:29 -07:00
return {
accountPnl: performanceData[performanceData.length - 1].pnl,
2022-11-20 16:50:25 -08:00
accountValueChange: accountValue - performanceData[0].account_equity,
2022-09-01 10:33:29 -07:00
}
}
return { accountPnl: 0, accountValueChange: 0 }
2022-11-20 16:50:25 -08:00
}, [accountValue, performanceData])
2022-09-01 10:33:29 -07:00
2022-10-04 22:12:52 -07:00
const oneDayPnlChange = useMemo(() => {
if (accountPnl && oneDayPerformanceData.length) {
2022-11-19 03:28:06 -08:00
const startDayPnl = oneDayPerformanceData[0].pnl
2022-11-20 01:08:12 -08:00
return accountPnl - startDayPnl
2022-10-04 22:12:52 -07:00
}
return 0.0
}, [accountPnl, oneDayPerformanceData])
2022-09-01 10:33:29 -07:00
const interestTotalValue = useMemo(() => {
if (totalInterestData.length) {
return totalInterestData.reduce(
(a, c) => a + c.borrow_interest_usd + c.deposit_interest_usd,
0
)
}
return 0.0
2022-09-01 10:33:29 -07:00
}, [totalInterestData])
2022-10-05 03:16:32 -07:00
const oneDayInterestChange = useMemo(() => {
if (oneDayPerformanceData.length && mangoAccount) {
2022-11-19 03:28:06 -08:00
const startDayInterest =
oneDayPerformanceData[0].borrow_interest_cumulative_usd +
oneDayPerformanceData[0].deposit_interest_cumulative_usd
const latest = oneDayPerformanceData.length - 1
const endDayInterest =
oneDayPerformanceData[latest].borrow_interest_cumulative_usd +
oneDayPerformanceData[latest].deposit_interest_cumulative_usd
return endDayInterest - startDayInterest
2022-10-05 03:16:32 -07:00
}
return 0.0
}, [oneDayPerformanceData, mangoAccount])
2022-10-05 03:16:32 -07:00
2022-09-01 10:33:29 -07:00
const maintHealth = useMemo(() => {
return group && mangoAccount
? mangoAccount.getHealthRatioUi(group, HealthType.maint)
: 0
2022-10-07 13:02:11 -07:00
}, [mangoAccount, group])
2022-09-01 10:33:29 -07:00
2022-10-05 03:16:32 -07:00
const handleChartToShow = (
2022-10-06 05:08:29 -07:00
chartName: 'pnl' | 'cumulative-interest-value'
2022-10-05 03:16:32 -07:00
) => {
2022-10-06 05:08:29 -07:00
if (
(chartName === 'cumulative-interest-value' && interestTotalValue > 1) ||
interestTotalValue < -1
) {
setChartToShow(chartName)
}
if (chartName === 'pnl' && performanceData.length > 4) {
setChartToShow(chartName)
}
2022-10-03 15:45:46 -07:00
}
2022-11-20 16:50:25 -08:00
const latestAccountData = useMemo(() => {
if (!accountValue || !performanceData.length) return []
const latestIndex = performanceData.length - 1
return [
{
account_equity: accountValue,
time: dayjs(Date.now()).toISOString(),
borrow_interest_cumulative_usd:
performanceData[latestIndex].borrow_interest_cumulative_usd,
deposit_interest_cumulative_usd:
performanceData[latestIndex].deposit_interest_cumulative_usd,
pnl: performanceData[latestIndex].pnl,
spot_value: performanceData[latestIndex].spot_value,
transfer_balance: performanceData[latestIndex].transfer_balance,
},
]
}, [accountValue, performanceData])
2022-09-01 10:33:29 -07:00
return !chartToShow ? (
<>
2022-10-25 04:22:24 -07:00
<div className="flex flex-wrap items-center justify-between border-b-0 border-th-bkg-3 px-6 py-3 md:border-b">
2022-09-19 20:12:34 -07:00
<div className="flex items-center space-x-6">
2022-09-21 21:25:24 -07:00
<div id="account-step-three">
2022-09-19 20:12:34 -07:00
<Tooltip
maxWidth="20rem"
2022-09-25 17:37:04 -07:00
placement="bottom-start"
2022-09-19 20:12:34 -07:00
content="The value of your assets (deposits) minus the value of your liabilities (borrows)."
2022-10-05 03:25:47 -07:00
delay={250}
2022-09-19 20:12:34 -07:00
>
2022-11-20 03:13:44 -08:00
<p className="tooltip-underline mb-2 text-base">
2022-10-07 15:27:36 -07:00
{t('account-value')}
</p>
2022-09-19 20:12:34 -07:00
</Tooltip>
2022-11-20 03:13:44 -08:00
<div className="mb-2 flex items-center text-5xl font-bold text-th-fgd-1">
{group && mangoAccount ? (
2022-09-01 10:33:29 -07:00
<FlipNumbers
height={48}
width={32}
play
delay={0.05}
duration={1}
2022-11-20 16:50:25 -08:00
numbers={formatFixedDecimals(accountValue, true)}
2022-09-01 10:33:29 -07:00
/>
) : (
<FlipNumbers
height={48}
width={32}
play
delay={0.05}
duration={1}
2022-10-07 04:47:15 -07:00
numbers={'$0.00'}
2022-09-01 10:33:29 -07:00
/>
)}
</div>
2022-11-20 03:13:44 -08:00
<div className="flex items-center space-x-1.5">
2022-11-20 01:08:12 -08:00
<Change change={accountValueChange} isCurrency />
<p className="text-th-fgd-4">{t('today')}</p>
</div>
2022-09-01 10:33:29 -07:00
</div>
{!loadPerformanceData ? (
mangoAccount && performanceData.length ? (
<div
className="relative flex items-end"
onMouseEnter={() =>
onHoverMenu(showExpandChart, 'onMouseEnter')
}
onMouseLeave={() =>
onHoverMenu(showExpandChart, 'onMouseLeave')
}
>
<SimpleAreaChart
color={
accountValueChange >= 0
? COLORS.GREEN[theme]
: COLORS.RED[theme]
}
2022-11-20 16:50:25 -08:00
data={performanceData.concat(latestAccountData)}
2022-09-01 10:33:29 -07:00
height={88}
name="accountValue"
width={180}
xKey="time"
yKey="account_equity"
/>
<Transition
appear={true}
className="absolute right-2 bottom-2"
show={showExpandChart}
enter="transition ease-in duration-300"
enterFrom="opacity-0 scale-75"
enterTo="opacity-100 scale-100"
leave="transition ease-out duration-200"
leaveFrom="opacity-100"
leaveTo="opacity-0"
>
<IconButton
className="text-th-fgd-3"
hideBg
onClick={() => handleShowAccountValueChart()}
>
2022-09-06 21:36:35 -07:00
<ArrowsPointingOutIcon className="h-5 w-5" />
2022-09-01 10:33:29 -07:00
</IconButton>
</Transition>
</div>
) : null
) : (
<SheenLoader>
<div className="h-[88px] w-[180px] rounded-md bg-th-bkg-2" />
</SheenLoader>
)}
</div>
2022-10-25 04:22:24 -07:00
<div className="mt-3 mb-1 lg:mt-0 lg:mb-0">
<AccountActions />
</div>
2022-09-01 10:33:29 -07:00
</div>
<div className="grid grid-cols-5 border-b border-th-bkg-3">
2022-10-05 03:29:11 -07:00
<div className="col-span-5 flex border-t border-th-bkg-3 py-3 pl-6 md:border-t-0 lg:col-span-1">
2022-09-21 21:25:24 -07:00
<div id="account-step-four">
<Tooltip
2022-09-19 20:12:34 -07:00
maxWidth="20rem"
2022-09-25 17:37:04 -07:00
placement="bottom-start"
2022-10-05 03:25:47 -07:00
delay={250}
content={
<div className="flex-col space-y-2 text-sm">
2022-09-19 20:12:34 -07:00
<p className="text-xs">
Health describes how close your account is to liquidation.
The lower your account health is the more likely you are to
get liquidated when prices fluctuate.
</p>
<p className="text-xs font-bold text-th-fgd-1">
Your account health is {maintHealth}%
</p>
<p className="text-xs">
<span className="font-bold text-th-fgd-1">Scenario:</span>{' '}
If the prices of all your liabilities increase by{' '}
{maintHealth}%, even for just a moment, some of your
liabilities will be liquidated.
2022-09-19 20:12:34 -07:00
</p>
<p className="text-xs">
<span className="font-bold text-th-fgd-1">Scenario:</span>{' '}
If the value of your total collateral decreases by{' '}
{((1 - 1 / ((maintHealth || 0) / 100 + 1)) * 100).toFixed(
2
)}
% , some of your liabilities will be liquidated.
</p>
<p className="text-xs">
These are examples. A combination of events can also lead to
liquidation.
2022-09-19 20:12:34 -07:00
</p>
</div>
}
>
<p className="tooltip-underline text-sm text-th-fgd-3 xl:text-base">
{t('health')}
</p>
</Tooltip>
<p className="mt-1 text-2xl font-bold text-th-fgd-1 lg:text-xl xl:text-2xl">
{maintHealth}%
</p>
2022-09-13 04:27:04 -07:00
</div>
2022-09-01 10:33:29 -07:00
</div>
<div className="col-span-5 flex border-t border-th-bkg-3 py-3 pl-6 lg:col-span-1 lg:border-l lg:border-t-0">
2022-09-21 21:25:24 -07:00
<div id="account-step-five">
2022-09-19 20:12:34 -07:00
<Tooltip
content="The value of collateral you have to open new trades or borrows. When your free collateral reaches $0 you won't be able to make withdrawals."
2022-09-19 20:12:34 -07:00
maxWidth="20rem"
2022-10-06 05:08:29 -07:00
placement="bottom"
2022-10-05 03:25:47 -07:00
delay={250}
2022-09-19 20:12:34 -07:00
>
<p className="tooltip-underline text-sm text-th-fgd-3 xl:text-base">
2022-09-19 20:12:34 -07:00
{t('free-collateral')}
</p>
</Tooltip>
<p className="mt-1 mb-0.5 text-2xl font-bold text-th-fgd-1 lg:text-xl xl:text-2xl">
{group && mangoAccount
2022-09-13 04:27:04 -07:00
? formatFixedDecimals(
toUiDecimalsForQuote(
mangoAccount.getCollateralValue(group)!.toNumber()
2022-09-13 04:27:04 -07:00
),
true
)
: (0).toFixed(2)}
</p>
<span className="text-xs font-normal text-th-fgd-4">
<Tooltip
content="Total value of collateral for trading and borrowing (including unsettled PnL)."
maxWidth="20rem"
2022-10-06 05:08:29 -07:00
placement="bottom"
2022-10-05 03:25:47 -07:00
delay={250}
>
2022-10-05 03:25:47 -07:00
<span className="tooltip-underline">Total</span>:
<span className="ml-1 font-mono text-th-fgd-2">
{group && mangoAccount
? formatFixedDecimals(
toUiDecimalsForQuote(
mangoAccount
.getAssetsValue(group, HealthType.init)!
.toNumber()
),
true
)
: `$${(0).toFixed(2)}`}
</span>
</Tooltip>
</span>
2022-09-13 04:27:04 -07:00
</div>
2022-09-01 10:33:29 -07:00
</div>
<div className="col-span-5 flex border-t border-th-bkg-3 py-3 pl-6 lg:col-span-1 lg:border-l lg:border-t-0">
2022-11-13 19:21:45 -08:00
<div id="account-step-six">
<Tooltip
2022-10-06 19:10:12 -07:00
content="Total position size divided by total collateral."
maxWidth="20rem"
2022-10-06 05:08:29 -07:00
placement="bottom"
2022-10-05 03:25:47 -07:00
delay={250}
>
<p className="tooltip-underline text-sm text-th-fgd-3 xl:text-base">
{t('leverage')}
</p>
</Tooltip>
<p className="mt-1 text-2xl font-bold text-th-fgd-1 lg:text-xl xl:text-2xl">
{leverage.toFixed(2)}x
</p>
2022-09-13 04:27:04 -07:00
</div>
2022-09-01 10:33:29 -07:00
</div>
2022-10-03 15:45:46 -07:00
<button
2022-10-06 05:08:29 -07:00
className={`col-span-5 flex items-center justify-between border-t border-th-bkg-3 py-3 pl-6 pr-4 lg:col-span-1 lg:border-l lg:border-t-0 ${
2022-10-03 15:45:46 -07:00
performanceData.length > 4
? 'default-transition cursor-pointer md:hover:bg-th-bkg-2'
2022-10-06 05:08:29 -07:00
: 'cursor-default'
2022-10-03 15:45:46 -07:00
}`}
onClick={() => handleChartToShow('pnl')}
>
2022-11-13 19:21:45 -08:00
<div id="account-step-seven" className="flex flex-col items-start">
2022-09-19 20:12:34 -07:00
<Tooltip
content="The amount your account has made or lost."
2022-10-06 05:08:29 -07:00
placement="bottom"
delay={250}
2022-09-19 20:12:34 -07:00
>
2022-10-06 05:08:29 -07:00
<p className="tooltip-underline inline text-sm text-th-fgd-3 xl:text-base">
{t('pnl')}
2022-10-03 15:45:46 -07:00
</p>
2022-09-19 20:12:34 -07:00
</Tooltip>
2022-10-06 05:08:29 -07:00
<p className="mt-1 mb-0.5 text-left text-2xl font-bold text-th-fgd-1 lg:text-xl xl:text-2xl">
2022-09-01 10:33:29 -07:00
{formatFixedDecimals(accountPnl, true)}
</p>
2022-11-19 03:28:06 -08:00
<div className="flex space-x-1">
2022-11-20 01:08:12 -08:00
<Change change={oneDayPnlChange} isCurrency size="small" />
2022-11-19 03:28:06 -08:00
<p className="text-xs text-th-fgd-4">{t('today')}</p>
</div>
2022-09-01 10:33:29 -07:00
</div>
{performanceData.length > 4 ? (
2022-10-06 05:08:29 -07:00
<ChevronRightIcon className="h-6 w-6" />
2022-09-01 10:33:29 -07:00
) : null}
2022-10-03 15:45:46 -07:00
</button>
<button
2022-10-06 05:08:29 -07:00
className={`col-span-5 flex items-center justify-between border-t border-th-bkg-3 py-3 pl-6 pr-4 text-left lg:col-span-1 lg:border-l lg:border-t-0 ${
interestTotalValue > 1 || interestTotalValue < -1
2022-10-03 15:45:46 -07:00
? 'default-transition cursor-pointer md:hover:bg-th-bkg-2'
2022-10-06 05:08:29 -07:00
: 'cursor-default'
2022-10-03 15:45:46 -07:00
}`}
onClick={() => handleChartToShow('cumulative-interest-value')}
>
2022-11-13 19:21:45 -08:00
<div id="account-step-eight">
2022-09-19 20:12:34 -07:00
<Tooltip
content="The value of interest earned (deposits) minus interest paid (borrows)."
maxWidth="20rem"
2022-09-25 17:37:04 -07:00
placement="bottom-end"
2022-10-06 05:08:29 -07:00
delay={250}
2022-09-19 20:12:34 -07:00
>
2022-10-06 05:08:29 -07:00
<p className="tooltip-underline text-sm text-th-fgd-3 xl:text-base">
{t('total-interest-earned')}
2022-09-19 20:12:34 -07:00
</p>
</Tooltip>
2022-10-06 05:08:29 -07:00
<p className="mt-1 mb-0.5 text-2xl font-bold text-th-fgd-1 lg:text-xl xl:text-2xl">
2022-09-01 10:33:29 -07:00
{formatFixedDecimals(interestTotalValue, true)}
</p>
2022-11-19 03:28:06 -08:00
<div className="flex space-x-1">
<Change change={oneDayInterestChange} isCurrency size="small" />
<p className="text-xs text-th-fgd-4">{t('today')}</p>
</div>
2022-09-01 10:33:29 -07:00
</div>
{interestTotalValue > 1 || interestTotalValue < -1 ? (
2022-10-06 05:08:29 -07:00
<ChevronRightIcon className="-mt-0.5 h-6 w-6" />
2022-09-01 10:33:29 -07:00
) : null}
2022-10-03 15:45:46 -07:00
</button>
2022-09-01 10:33:29 -07:00
</div>
<AccountTabs />
2022-09-01 10:33:29 -07:00
{showDepositModal ? (
<DepositModal
isOpen={showDepositModal}
onClose={() => setShowDepositModal(false)}
/>
) : null}
{showWithdrawModal ? (
<WithdrawModal
isOpen={showWithdrawModal}
onClose={() => setShowWithdrawModal(false)}
/>
) : null}
{!tourSettings?.account_tour_seen && isOnBoarded && connected ? (
2022-09-21 21:25:24 -07:00
<AccountOnboardingTour />
) : null}
2022-09-01 10:33:29 -07:00
</>
) : (
2022-09-19 16:57:58 -07:00
<div className="p-6">
{chartToShow === 'account-value' ? (
<AccountChart
chartToShow="account-value"
data={performanceData}
hideChart={handleHideChart}
mangoAccount={mangoAccount!}
yKey="account_equity"
/>
) : chartToShow === 'pnl' ? (
<AccountChart
chartToShow="pnl"
data={performanceData}
hideChart={handleHideChart}
mangoAccount={mangoAccount!}
yKey="pnl"
/>
) : (
<AccountChart
chartToShow="cumulative-interest-value"
data={performanceData.map((d) => ({
interest_value:
d.borrow_interest_cumulative_usd +
d.deposit_interest_cumulative_usd,
time: d.time,
}))}
hideChart={handleHideChart}
mangoAccount={mangoAccount!}
yKey="interest_value"
/>
)}
</div>
2022-09-01 10:33:29 -07:00
)
}
export default AccountPage