mango-v4-ui/components/shared/HealthImpact.tsx

92 lines
2.7 KiB
TypeScript
Raw Normal View History

import { HealthType, toNativeDecimals } from '@blockworks-foundation/mango-v4'
2022-08-17 05:09:40 -07:00
import { ArrowRightIcon } from '@heroicons/react/solid'
import { PublicKey } from '@solana/web3.js'
2022-08-17 05:09:40 -07:00
import { useTranslation } from 'next-i18next'
import { useMemo } from 'react'
2022-08-20 11:17:57 -07:00
import mangoStore from '../../store/mangoStore'
2022-08-17 05:09:40 -07:00
const HealthImpact = ({
uiAmount,
2022-08-17 16:54:56 -07:00
isDeposit,
mintPk,
2022-08-17 05:09:40 -07:00
}: {
uiAmount: number
2022-08-17 16:54:56 -07:00
isDeposit?: boolean
mintPk: PublicKey
2022-08-17 05:09:40 -07:00
}) => {
const { t } = useTranslation('common')
const mangoAccount = mangoStore((s) => s.mangoAccount.current)
const currentMaintHealth = useMemo(() => {
2022-08-17 05:09:40 -07:00
if (!mangoAccount) return 0
return mangoAccount.getHealthRatioUi(HealthType.maint)
}, [mangoAccount])
const maintProjectedHealth = useMemo(() => {
2022-08-17 05:09:40 -07:00
const group = mangoStore.getState().group
if (!group || !mangoAccount) return 0
const projectedHealth =
mangoAccount.simHealthRatioWithTokenPositionUiChanges(
2022-08-20 11:17:57 -07:00
group,
[{ mintPk, uiTokenAmount: isDeposit ? uiAmount : uiAmount * -1 }],
2022-08-20 11:17:57 -07:00
HealthType.maint
)
2022-08-17 05:09:40 -07:00
return projectedHealth > 100
? 100
: projectedHealth < 0
? 0
: projectedHealth
}, [mangoAccount, mintPk, uiAmount, isDeposit])
2022-08-17 05:09:40 -07:00
const initProjectedHealth = useMemo(() => {
const group = mangoStore.getState().group
if (!group || !mangoAccount) return 0
const projectedHealth =
mangoAccount.simHealthRatioWithTokenPositionUiChanges(
group,
[{ mintPk, uiTokenAmount: isDeposit ? uiAmount : uiAmount * -1 }],
HealthType.init
)
return projectedHealth > 100
? 100
: projectedHealth < 0
? 0
: projectedHealth
}, [mangoAccount, mintPk, uiAmount, isDeposit])
2022-08-17 05:09:40 -07:00
return (
2022-08-24 17:53:31 -07:00
<div className="flex justify-between">
<p>{t('health-impact')}</p>
<div className="flex items-center space-x-2">
<p className="text-th-fgd-1">{currentMaintHealth}%</p>
2022-08-25 06:00:42 -07:00
<ArrowRightIcon className="h-4 w-4 text-th-fgd-4" />
2022-08-24 17:53:31 -07:00
<p
className={
maintProjectedHealth < 50 && maintProjectedHealth > 15
? 'text-th-orange'
: maintProjectedHealth <= 15
? 'text-th-red'
: 'text-th-green'
}
>
{maintProjectedHealth.toFixed(2)}%{' '}
<span
className={`text-xs ${
maintProjectedHealth >= currentMaintHealth
? 'text-th-green'
: 'text-th-red'
}`}
>
2022-08-24 17:53:31 -07:00
({maintProjectedHealth >= currentMaintHealth ? '+' : ''}
{(maintProjectedHealth - currentMaintHealth).toFixed(2)}%)
</span>
</p>
</div>
2022-08-17 05:09:40 -07:00
</div>
)
}
export default HealthImpact