Skip to content

Commit

Permalink
refactor: util function calcOfferMinsizeMax
Browse files Browse the repository at this point in the history
  • Loading branch information
theborakompanioni committed Oct 30, 2024
1 parent 00dffa0 commit a2cbc66
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
35 changes: 11 additions & 24 deletions src/components/Earn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<EarnFormValues>
Expand Down Expand Up @@ -277,15 +272,7 @@ const EarnForm = ({
}

return (
<Formik
initialValues={initialValues}
validate={validate}
onSubmit={onSubmit}
validateOnMount={true}
initialTouched={{
minsize: true,
}}
>
<Formik initialValues={initialValues} validate={validate} onSubmit={onSubmit}>
{(props) => {
const { handleSubmit, setFieldValue, handleBlur, values, touched, errors, isSubmitting } = props
const minsizeField = props.getFieldProps<AmountValue>('minsize')
Expand Down
20 changes: 19 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
}

0 comments on commit a2cbc66

Please sign in to comment.