From 9452bfe1f6382ee7f7e98513d03838e0392c8582 Mon Sep 17 00:00:00 2001 From: WRadoslaw Date: Wed, 20 Mar 2024 13:46:44 +0100 Subject: [PATCH] Validate first input against user balance in sell scenario --- .../components/ChangeNowModal/steps/FormStep.tsx | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/atlas/src/components/ChangeNowModal/steps/FormStep.tsx b/packages/atlas/src/components/ChangeNowModal/steps/FormStep.tsx index dbf238af9c..98908d9326 100644 --- a/packages/atlas/src/components/ChangeNowModal/steps/FormStep.tsx +++ b/packages/atlas/src/components/ChangeNowModal/steps/FormStep.tsx @@ -14,6 +14,8 @@ import { FormField } from '@/components/_inputs/FormField' import { Input } from '@/components/_inputs/Input' import { TokenInput, TokenInputProps } from '@/components/_inputs/TokenInput' import { Spinner } from '@/components/_loaders/Spinner' +import { hapiBnToTokenNumber } from '@/joystream-lib/utils' +import { useSubscribeAccountBalance } from '@/providers/joystream' import { useSnackbar } from '@/providers/snackbars' import { square } from '@/styles' import { @@ -23,6 +25,7 @@ import { changeNowService, } from '@/utils/ChangeNowService' import { SentryLogger } from '@/utils/logs' +import { formatSmallDecimal } from '@/utils/number' type CurrencyInputValues = { amount: number @@ -46,10 +49,13 @@ type FormStepProps = { export const FormStep = ({ setPrimaryButtonProps, onSubmit, type, initialValues }: FormStepProps) => { const { displaySnackbar } = useSnackbar() + const { accountBalance } = useSubscribeAccountBalance() const [isLoadingRate, setIsLoadingRate] = useState<'to' | 'from' | null>(null) const debouncedExchangeEstimation = useRef Promise > | null>(null) + const isSellingJoy = type === 'sell' + const { data } = useQuery('changenow-currency', () => changeNowService.getAvailableCurrencies(), { onSuccess: (data) => { const currencyOptions = data.map((curr) => ({ @@ -179,6 +185,12 @@ export const FormStep = ({ setPrimaryButtonProps, onSubmit, type, initialValues if (!value.currency) { return 'Please choose currency' } + + if (isSellingJoy && accountBalance) { + if (value.amount > hapiBnToTokenNumber(accountBalance)) { + return 'Amount exceeds your balance' + } + } }, }} render={({ field: { value, onChange } }) => { @@ -285,7 +297,7 @@ export const FormStep = ({ setPrimaryButtonProps, onSubmit, type, initialValues /> {from.currency && to.currency && ( - Estimated rate: 1 {from.currency.toUpperCase()} ~ {to.amount / from.amount || 'N/A'}{' '} + Estimated rate: 1 {from.currency.toUpperCase()} ~ {formatSmallDecimal(to.amount / from.amount) || 'N/A'}{' '} {to.currency.toUpperCase()} )}