From 06ab7746a5dcfd332cf606467774eca4eb31d737 Mon Sep 17 00:00:00 2001 From: theborakompanioni Date: Wed, 11 Oct 2023 22:05:17 +0200 Subject: [PATCH] fix(fee): allow missing fee config values to be updated --- src/components/Send/index.tsx | 15 ++++++++++----- src/context/ServiceConfigContext.tsx | 26 ++++++++++++++++++++------ 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/src/components/Send/index.tsx b/src/components/Send/index.tsx index 436625c3..36d4cec2 100644 --- a/src/components/Send/index.tsx +++ b/src/components/Send/index.tsx @@ -83,6 +83,11 @@ export default function Send({ wallet }: SendProps) { const [destinationIsReusedAddress, setDestinationIsReusedAddress] = useState(false) const [feeConfigValues, reloadFeeConfigValues] = useFeeConfigValues() + const maxFeesConfigMissing = useMemo( + () => + feeConfigValues && (feeConfigValues.max_cj_fee_abs === undefined || feeConfigValues.max_cj_fee_rel === undefined), + [feeConfigValues], + ) const [activeFeeConfigModalSection, setActiveFeeConfigModalSection] = useState() const [showFeeConfigModal, setShowFeeConfigModal] = useState(false) @@ -91,12 +96,12 @@ export default function Send({ wallet }: SendProps) { const isOperationDisabled = useMemo( () => - !feeConfigValues || + maxFeesConfigMissing || isCoinjoinInProgress || isMakerRunning || isRescanningInProgress || waitForUtxosToBeSpent.length > 0, - [feeConfigValues, isCoinjoinInProgress, isMakerRunning, isRescanningInProgress, waitForUtxosToBeSpent], + [maxFeesConfigMissing, isCoinjoinInProgress, isMakerRunning, isRescanningInProgress, waitForUtxosToBeSpent], ) const [isInitializing, setIsInitializing] = useState(!isOperationDisabled) const isLoading = useMemo( @@ -285,10 +290,10 @@ export default function Send({ wallet }: SendProps) { signal: abortCtrl.signal, key: { section: 'POLICY', field: 'minimum_makers' }, }) - .then((data) => { + .then((data) => (data.value !== null ? parseInt(data.value, 10) : JM_MINIMUM_MAKERS_DEFAULT)) + .then((minimumMakers) => { if (abortCtrl.signal.aborted) return - const minimumMakers = parseInt(data.value, 10) setMinNumCollaborators(minimumMakers) setNumCollaborators(initialNumCollaborators(minimumMakers)) }) @@ -628,7 +633,7 @@ export default function Send({ wallet }: SendProps) { )} - {!isLoading && !feeConfigValues && ( + {maxFeesConfigMissing && ( {t('send.taker_error_message_max_fees_config_missing')} diff --git a/src/context/ServiceConfigContext.tsx b/src/context/ServiceConfigContext.tsx index f06775cd..8bf15835 100644 --- a/src/context/ServiceConfigContext.tsx +++ b/src/context/ServiceConfigContext.tsx @@ -23,6 +23,11 @@ export interface ServiceConfigUpdate { value: string } +export interface ServiceConfigValue { + key: ConfigKey + value: string | null +} + type LoadConfigValueProps = { signal?: AbortSignal key: ConfigKey @@ -40,7 +45,7 @@ type UpdateConfigValuesProps = { wallet?: MinimalWalletContext } -const configReducer = (state: ServiceConfig, obj: ServiceConfigUpdate): ServiceConfig => { +const configReducer = (state: ServiceConfig, obj: ServiceConfigValue): ServiceConfig => { const data = { ...state } data[obj.key.section] = { ...data[obj.key.section], [obj.key.field]: obj.value } return data @@ -55,14 +60,23 @@ const fetchConfigValues = async ({ wallet: MinimalWalletContext configKeys: ConfigKey[] }) => { - const fetches: Promise[] = configKeys.map((configKey) => { + const fetches: Promise[] = configKeys.map((configKey) => { return Api.postConfigGet({ ...wallet, signal }, { section: configKey.section, field: configKey.field }) .then((res) => (res.ok ? res.json() : Api.Helper.throwError(res))) .then((data: JmConfigData) => { return { key: configKey, value: data.configvalue, - } as ServiceConfigUpdate + } as ServiceConfigValue + }) + .catch((e) => { + if (e instanceof Api.JmApiError && e.response.status === 409) { + return { + key: configKey, + value: null, + } as ServiceConfigValue + } + throw e }) }) @@ -95,7 +109,7 @@ const pushConfigValues = async ({ } export interface ServiceConfigContextEntry { - loadConfigValueIfAbsent: (props: LoadConfigValueProps) => Promise + loadConfigValueIfAbsent: (props: LoadConfigValueProps) => Promise refreshConfigValues: (props: RefreshConfigValuesProps) => Promise updateConfigValues: (props: UpdateConfigValuesProps) => Promise } @@ -138,7 +152,7 @@ const ServiceConfigProvider = ({ children }: PropsWithChildren<{}>) => { return { key, value: serviceConfig.current[key.section][key.field], - } as ServiceConfigUpdate + } as ServiceConfigValue } } @@ -146,7 +160,7 @@ const ServiceConfigProvider = ({ children }: PropsWithChildren<{}>) => { return { key, value: conf[key.section][key.field], - } as ServiceConfigUpdate + } as ServiceConfigValue }) }, [refreshConfigValues],