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