Skip to content

Commit

Permalink
fix(fee): allow missing fee config values to be updated
Browse files Browse the repository at this point in the history
  • Loading branch information
theborakompanioni committed Oct 11, 2023
1 parent 1123af9 commit 06ab774
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
15 changes: 10 additions & 5 deletions src/components/Send/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<FeeConfigSectionKey>()
const [showFeeConfigModal, setShowFeeConfigModal] = useState(false)

Expand All @@ -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(
Expand Down Expand Up @@ -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))
})
Expand Down Expand Up @@ -628,7 +633,7 @@ export default function Send({ wallet }: SendProps) {
)}
</>
</rb.Fade>
{!isLoading && !feeConfigValues && (
{maxFeesConfigMissing && (
<rb.Alert className="slashed-zeroes" variant="danger">
{t('send.taker_error_message_max_fees_config_missing')}
</rb.Alert>
Expand Down
26 changes: 20 additions & 6 deletions src/context/ServiceConfigContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ export interface ServiceConfigUpdate {
value: string
}

export interface ServiceConfigValue {
key: ConfigKey
value: string | null
}

type LoadConfigValueProps = {
signal?: AbortSignal
key: ConfigKey
Expand All @@ -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
Expand All @@ -55,14 +60,23 @@ const fetchConfigValues = async ({
wallet: MinimalWalletContext
configKeys: ConfigKey[]
}) => {
const fetches: Promise<ServiceConfigUpdate>[] = configKeys.map((configKey) => {
const fetches: Promise<ServiceConfigValue>[] = 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
})
})

Expand Down Expand Up @@ -95,7 +109,7 @@ const pushConfigValues = async ({
}

export interface ServiceConfigContextEntry {
loadConfigValueIfAbsent: (props: LoadConfigValueProps) => Promise<ServiceConfigUpdate>
loadConfigValueIfAbsent: (props: LoadConfigValueProps) => Promise<ServiceConfigValue>
refreshConfigValues: (props: RefreshConfigValuesProps) => Promise<ServiceConfig>
updateConfigValues: (props: UpdateConfigValuesProps) => Promise<ServiceConfig>
}
Expand Down Expand Up @@ -138,15 +152,15 @@ const ServiceConfigProvider = ({ children }: PropsWithChildren<{}>) => {
return {
key,
value: serviceConfig.current[key.section][key.field],
} as ServiceConfigUpdate
} as ServiceConfigValue
}
}

return refreshConfigValues({ signal, keys: [key] }).then((conf) => {
return {
key,
value: conf[key.section][key.field],
} as ServiceConfigUpdate
} as ServiceConfigValue
})
},
[refreshConfigValues],
Expand Down

0 comments on commit 06ab774

Please sign in to comment.