mango-v4-ui/components/swap/MaxSwapAmount.tsx

56 lines
1.5 KiB
TypeScript
Raw Normal View History

import MaxAmountButton from '@components/shared/MaxAmountButton'
import mangoStore from '@store/mangoStore'
2023-01-23 19:04:05 -08:00
import Decimal from 'decimal.js'
import { useTranslation } from 'next-i18next'
import { floorToDecimal } from 'utils/numbers'
2023-09-06 06:06:57 -07:00
import { TokenMaxResults } from './useTokenMax'
const MaxSwapAmount = ({
setAmountIn,
useMargin,
2023-09-06 06:06:57 -07:00
maxAmount,
}: {
setAmountIn: (x: string) => void
useMargin: boolean
2023-09-06 06:06:57 -07:00
maxAmount: (useMargin: boolean) => TokenMaxResults
}) => {
const { t } = useTranslation('common')
const mangoAccountLoading = mangoStore((s) => s.mangoAccount.initialLoad)
2024-01-01 04:46:08 -08:00
const {
amount: spotMax,
amountWithBorrow: leverageMax,
decimals,
} = maxAmount(useMargin)
if (mangoAccountLoading) return null
2023-01-23 19:04:05 -08:00
const setMax = (value: Decimal) => {
setAmountIn(floorToDecimal(value, decimals).toFixed())
2023-01-23 19:04:05 -08:00
}
2023-01-05 21:26:54 -08:00
return (
<div className="flex flex-wrap justify-end pl-6 text-xs">
2024-01-01 04:46:08 -08:00
{spotMax.lt(leverageMax) || (spotMax.eq(leverageMax) && !useMargin) ? (
<MaxAmountButton
className="mb-0.5"
decimals={decimals}
2023-08-10 16:45:40 -07:00
label={useMargin ? t('bal') : t('max')}
2024-01-01 04:46:08 -08:00
onClick={() => setMax(spotMax)}
value={spotMax}
/>
) : null}
{useMargin ? (
<MaxAmountButton
className="mb-0.5 ml-2"
2023-01-23 19:04:05 -08:00
decimals={decimals}
label={t('max')}
2024-01-01 04:46:08 -08:00
onClick={() => setMax(leverageMax)}
value={leverageMax}
/>
) : null}
</div>
)
}
export default MaxSwapAmount