import { HealthType, toNativeDecimals } from '@blockworks-foundation/mango-v4' import { ArrowRightIcon } from '@heroicons/react/solid' import { PublicKey } from '@solana/web3.js' import { useTranslation } from 'next-i18next' import { useMemo } from 'react' import mangoStore from '../../store/state' const HealthImpact = ({ uiAmount, isDeposit, mintPk, }: { uiAmount: number isDeposit?: boolean mintPk: PublicKey }) => { const { t } = useTranslation('common') const mangoAccount = mangoStore((s) => s.mangoAccount.current) const currentHealth = useMemo(() => { if (!mangoAccount) return 0 return mangoAccount.getHealthRatioUi(HealthType.maint) }, [mangoAccount]) const projectedHealth = useMemo(() => { const group = mangoStore.getState().group if (!group || !mangoAccount) return 0 const amount = group.toNativeDecimals(uiAmount, mintPk).toNumber(); const projectedHealth = mangoAccount .simHealthRatioWithTokenPositionChanges(group, [ { mintPk, tokenAmount: isDeposit ? amount : amount * -1 }, ], HealthType.maint) .toNumber() return projectedHealth > 100 ? 100 : projectedHealth < 0 ? 0 : projectedHealth }, [mangoAccount, mintPk, uiAmount, isDeposit]) return (

{t('health-impact')}

{currentHealth}%

15 ? 'text-th-orange' : projectedHealth <= 15 ? 'text-th-red' : 'text-th-green' } > {projectedHealth.toFixed(2)}%{' '} = currentHealth ? 'text-th-green' : 'text-th-red' }`} > ({(projectedHealth - currentHealth).toFixed(2)}%)

) } export default HealthImpact