2022-12-07 13:33:35 -08:00
|
|
|
import MaxAmountButton from '@components/shared/MaxAmountButton'
|
|
|
|
import mangoStore from '@store/mangoStore'
|
|
|
|
import { useTranslation } from 'next-i18next'
|
2023-01-05 20:10:25 -08:00
|
|
|
import { floorToDecimal } from 'utils/numbers'
|
2022-12-07 13:33:35 -08:00
|
|
|
import { useTokenMax } from './useTokenMax'
|
|
|
|
|
|
|
|
const MaxSwapAmount = ({
|
|
|
|
setAmountIn,
|
|
|
|
useMargin,
|
|
|
|
}: {
|
|
|
|
setAmountIn: (x: string) => void
|
|
|
|
useMargin: boolean
|
|
|
|
}) => {
|
|
|
|
const { t } = useTranslation('common')
|
|
|
|
const mangoAccountLoading = mangoStore((s) => s.mangoAccount.initialLoad)
|
|
|
|
const {
|
|
|
|
amount: tokenMax,
|
|
|
|
amountWithBorrow,
|
|
|
|
decimals,
|
|
|
|
} = useTokenMax(useMargin)
|
|
|
|
|
|
|
|
if (mangoAccountLoading) return null
|
|
|
|
|
2023-01-05 21:26:54 -08:00
|
|
|
const maxBalanceValue = floorToDecimal(
|
|
|
|
tokenMax.toNumber(),
|
|
|
|
decimals
|
|
|
|
).toFixed()
|
|
|
|
const maxBorrowValue = floorToDecimal(
|
|
|
|
amountWithBorrow.toNumber(),
|
|
|
|
decimals
|
|
|
|
).toFixed()
|
|
|
|
|
2022-12-07 13:33:35 -08:00
|
|
|
return (
|
|
|
|
<div className="flex flex-wrap justify-end pl-6 text-xs">
|
|
|
|
<MaxAmountButton
|
|
|
|
className="mb-0.5"
|
|
|
|
label="Bal"
|
2023-01-05 21:26:54 -08:00
|
|
|
onClick={() => setAmountIn(maxBalanceValue)}
|
|
|
|
value={maxBalanceValue}
|
2022-12-07 13:33:35 -08:00
|
|
|
/>
|
|
|
|
{useMargin ? (
|
|
|
|
<MaxAmountButton
|
|
|
|
className="mb-0.5 ml-2"
|
|
|
|
label={t('max')}
|
2023-01-05 21:26:54 -08:00
|
|
|
onClick={() => setAmountIn(maxBorrowValue)}
|
|
|
|
value={maxBorrowValue}
|
2022-12-07 13:33:35 -08:00
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default MaxSwapAmount
|