diff --git a/src/components/Earn.tsx b/src/components/Earn.tsx index c934b69d..e6ff46e5 100644 --- a/src/components/Earn.tsx +++ b/src/components/Earn.tsx @@ -6,8 +6,14 @@ import { TFunction } from 'i18next' import { useSettings } from '../context/SettingsContext' import { CurrentWallet, useCurrentWalletInfo, useReloadCurrentWalletInfo, WalletInfo } from '../context/WalletContext' import { useServiceInfo, useReloadServiceInfo, Offer } from '../context/ServiceInfoContext' -import { factorToPercentage, isAbsoluteOffer, isRelativeOffer, isValidNumber, percentageToFactor } from '../utils' -import { JM_DUST_THRESHOLD } from '../constants/jm' +import { + calcOfferMinsizeMax, + factorToPercentage, + isAbsoluteOffer, + isRelativeOffer, + isValidNumber, + percentageToFactor, +} from '../utils' import { OFFER_FEE_ABS_MIN, OFFER_FEE_REL_MAX, @@ -220,20 +226,9 @@ const EarnForm = ({ }: EarnFormProps) => { const { t } = useTranslation() - const maxAvailableBalanceInJar = useMemo(() => { - return Math.max( - 0, - Math.max( - ...Object.values(walletInfo?.balanceSummary.accountBalances || []).map( - (it) => it.calculatedAvailableBalanceInSats, - ), - ), - ) - }, [walletInfo]) - const offerMinsizeMax = useMemo(() => { - return Math.max(0, maxAvailableBalanceInJar - JM_DUST_THRESHOLD) - }, [maxAvailableBalanceInJar]) + return walletInfo === undefined ? 0 : calcOfferMinsizeMax(walletInfo) + }, [walletInfo]) const validate = (values: EarnFormValues) => { const errors = {} as FormikErrors @@ -277,15 +272,7 @@ const EarnForm = ({ } return ( - + {(props) => { const { handleSubmit, setFieldValue, handleBlur, values, touched, errors, isSubmitting } = props const minsizeField = props.getFieldProps('minsize') diff --git a/src/utils.ts b/src/utils.ts index 9a2db657..a8e2292f 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,4 +1,6 @@ -import { AmountSats, OfferType, WalletFileName } from './libs/JmWalletApi' +import { JM_DUST_THRESHOLD } from './constants/jm' +import type { WalletInfo } from './context/WalletContext' +import type { AmountSats, OfferType, WalletFileName } from './libs/JmWalletApi' const BTC_FORMATTER = new Intl.NumberFormat('en-US', { minimumIntegerDigits: 1, @@ -135,3 +137,19 @@ export const setIntervalDebounced = ( ) })() } + +const calcMaxAvailableBalanceInJar = (walletInfo: WalletInfo) => { + return Math.max( + 0, + Math.max( + ...Object.values(walletInfo.balanceSummary.accountBalances || []).map( + (it) => it.calculatedAvailableBalanceInSats, + ), + ), + ) +} + +export const calcOfferMinsizeMax = (walletInfo: WalletInfo) => { + const maxAvailableBalanceInJar = calcMaxAvailableBalanceInJar(walletInfo) + return Math.max(0, maxAvailableBalanceInJar - JM_DUST_THRESHOLD) +}