Skip to content

Commit

Permalink
refactor(renterd): typescript strict mode for config feature
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Sep 20, 2024
1 parent d710788 commit 93b720f
Show file tree
Hide file tree
Showing 28 changed files with 785 additions and 547 deletions.
8 changes: 4 additions & 4 deletions apps/renterd/components/Config/ConfigStats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ import { useApp } from '../../contexts/app'
import { useEstimatedSpending } from './useEstimatedSpending'

export function ConfigStats() {
const { autopilot } = useApp()
const { isAutopilotEnabled } = useApp()
const { redundancyMultiplier, storageTB, configViewMode } = useConfig()

const { canEstimate, estimatedSpendingPerMonth, estimatedSpendingPerTB } =
const { estimatedSpendingPerMonth, estimatedSpendingPerTB } =
useEstimatedSpending()

const perMonth = useSiacoinFiat({ sc: estimatedSpendingPerMonth })
const perTB = useSiacoinFiat({ sc: estimatedSpendingPerTB })

if (autopilot.status !== 'on') {
if (!isAutopilotEnabled) {
return null
}

return !canEstimate ? (
return !estimatedSpendingPerMonth || !estimatedSpendingPerTB || !storageTB ? (
<Text size="12" font="mono" weight="medium">
{configViewMode === 'advanced'
? 'Enter expected storage, period, and allowance values to estimate monthly spending.'
Expand Down
6 changes: 3 additions & 3 deletions apps/renterd/components/Config/ShouldPinSwitch.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import { FieldSwitch, Text, Tooltip } from '@siafoundation/design-system'
import { UseFormReturn } from 'react-hook-form'
import { SettingsData } from '../../contexts/config/types'
import { AllFieldValues } from '../../contexts/config/types'
import { Fields } from '../../contexts/config/fields'

export function ShouldPinSwitch({
name,
form,
fields,
}: {
name: keyof SettingsData
form: UseFormReturn<SettingsData>
name: keyof AllFieldValues
form: UseFormReturn<AllFieldValues>
fields: Fields
}) {
return (
Expand Down
8 changes: 4 additions & 4 deletions apps/renterd/components/Config/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export function Config() {
name="allowanceMonthPinned"
form={form}
fields={fields}
currency={pinnedCurrency || undefined}
currency={pinnedCurrency || ''}
/>
) : (
<PinnedCurrencyWarning
Expand Down Expand Up @@ -154,7 +154,7 @@ export function Config() {
name="maxStoragePriceTBMonthPinned"
form={form}
fields={fields}
currency={pinnedCurrency || undefined}
currency={pinnedCurrency || ''}
/>
) : (
<PinnedCurrencyWarning
Expand Down Expand Up @@ -189,7 +189,7 @@ export function Config() {
name="maxUploadPriceTBPinned"
form={form}
fields={fields}
currency={pinnedCurrency || undefined}
currency={pinnedCurrency || ''}
/>
) : (
<PinnedCurrencyWarning
Expand Down Expand Up @@ -224,7 +224,7 @@ export function Config() {
name="maxDownloadPriceTBPinned"
form={form}
fields={fields}
currency={pinnedCurrency || undefined}
currency={pinnedCurrency || ''}
/>
) : (
<PinnedCurrencyWarning
Expand Down
22 changes: 6 additions & 16 deletions apps/renterd/components/Config/useEstimatedSpending.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,27 @@
import { useApp } from '../../contexts/app'
import { useConfig } from '../../contexts/config'
import BigNumber from 'bignumber.js'
import { useMemo } from 'react'

export function useEstimatedSpending() {
const { isAutopilotEnabled } = useApp()
const { form } = useConfig()
const allowanceMonth = form.watch('allowanceMonth')
const storageTB = form.watch('storageTB')
const canEstimate = useMemo(() => {
if (!isAutopilotEnabled) {
return false
}
return !!allowanceMonth?.gt(0) && !!storageTB?.gt(0)
}, [isAutopilotEnabled, allowanceMonth, storageTB])

const estimatedSpendingPerMonth = useMemo(() => {
if (!canEstimate) {
return new BigNumber(0)
if (!allowanceMonth?.gt(0)) {
return undefined
}
return allowanceMonth
}, [canEstimate, allowanceMonth])
}, [allowanceMonth])

const estimatedSpendingPerTB = useMemo(() => {
if (!canEstimate) {
return new BigNumber(0)
if (!estimatedSpendingPerMonth?.gt(0) || !storageTB?.gt(0)) {
return undefined
}
const totalCostPerMonthTB = estimatedSpendingPerMonth.div(storageTB)
return totalCostPerMonthTB
}, [canEstimate, estimatedSpendingPerMonth, storageTB])
}, [estimatedSpendingPerMonth, storageTB])

return {
canEstimate,
estimatedSpendingPerMonth,
estimatedSpendingPerTB,
}
Expand Down
138 changes: 138 additions & 0 deletions apps/renterd/contexts/config/assertValues.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import {
AutopilotSubmitValues,
AutopilotFieldValues,
UploadFieldValues,
PinnedFieldValues,
GougingFieldValues,
AllFieldValues,
UploadSubmitValues,
PinnedSubmitValues,
GougingSubmitValues,
AllSubmitValues,
} from './types'

export function assertSubmitValuesAutopilot(
values: AutopilotFieldValues
): AutopilotSubmitValues {
if (values.allowanceMonth === undefined) {
throw new Error('allowanceMonth is required')
}
if (values.downloadTBMonth === undefined) {
throw new Error('downloadTBMonth is required')
}
if (values.storageTB === undefined) {
throw new Error('storageTB is required')
}
if (values.uploadTBMonth === undefined) {
throw new Error('uploadTBMonth is required')
}

return {
...values,
amountHosts: values.amountHosts,
periodWeeks: values.periodWeeks,
allowanceMonth: values.allowanceMonth,
downloadTBMonth: values.downloadTBMonth,
maxConsecutiveScanFailures: values.maxConsecutiveScanFailures,
maxDowntimeHours: values.maxDowntimeHours,
renewWindowWeeks: values.renewWindowWeeks,
storageTB: values.storageTB,
uploadTBMonth: values.uploadTBMonth,
}
}

export function assertSubmitValuesUpload(
values: UploadFieldValues
): UploadSubmitValues {
return {
...values,
defaultContractSet: values.defaultContractSet,
minShards: values.minShards,
totalShards: values.totalShards,
}
}

export function assertSubmitValuesPinned(
values: PinnedFieldValues
): PinnedSubmitValues {
if (values.pinnedThreshold === undefined) {
throw new Error('pinnedThreshold is required')
}
if (values.allowanceMonthPinned === undefined) {
throw new Error('allowanceMonthPinned is required')
}
if (values.maxStoragePriceTBMonthPinned === undefined) {
throw new Error('maxStoragePriceTBMonthPinned is required')
}
if (values.maxUploadPriceTBPinned === undefined) {
throw new Error('maxUploadPriceTBPinned is required')
}
if (values.maxDownloadPriceTBPinned === undefined) {
throw new Error('maxDownloadPriceTBPinned is required')
}
return {
...values,
pinnedThreshold: values.pinnedThreshold,
allowanceMonthPinned: values.allowanceMonthPinned,
maxStoragePriceTBMonthPinned: values.maxStoragePriceTBMonthPinned,
maxUploadPriceTBPinned: values.maxUploadPriceTBPinned,
maxDownloadPriceTBPinned: values.maxDownloadPriceTBPinned,
}
}

export function assertSubmitValuesGouging(
values: GougingFieldValues
): GougingSubmitValues {
if (values.maxRPCPriceMillion === undefined) {
throw new Error('maxRPCPriceMillion is required')
}
if (values.maxStoragePriceTBMonth === undefined) {
throw new Error('maxStoragePriceTBMonth is required')
}
if (values.maxContractPrice === undefined) {
throw new Error('maxContractPrice is required')
}
if (values.maxDownloadPriceTB === undefined) {
throw new Error('maxDownloadPriceTB is required')
}
if (values.maxUploadPriceTB === undefined) {
throw new Error('maxUploadPriceTB is required')
}
if (values.hostBlockHeightLeeway === undefined) {
throw new Error('hostBlockHeightLeeway is required')
}
if (values.minPriceTableValidityMinutes === undefined) {
throw new Error('minPriceTableValidityMinutes is required')
}
if (values.minAccountExpiryDays === undefined) {
throw new Error('minAccountExpiryDays is required')
}
if (values.minMaxEphemeralAccountBalance === undefined) {
throw new Error('minMaxEphemeralAccountBalance is required')
}
if (values.migrationSurchargeMultiplier === undefined) {
throw new Error('migrationSurchargeMultiplier is required')
}
return {
...values,
maxRPCPriceMillion: values.maxRPCPriceMillion,
maxStoragePriceTBMonth: values.maxStoragePriceTBMonth,
maxContractPrice: values.maxContractPrice,
maxDownloadPriceTB: values.maxDownloadPriceTB,
maxUploadPriceTB: values.maxUploadPriceTB,
hostBlockHeightLeeway: values.hostBlockHeightLeeway,
minPriceTableValidityMinutes: values.minPriceTableValidityMinutes,
minAccountExpiryDays: values.minAccountExpiryDays,
minMaxEphemeralAccountBalance: values.minMaxEphemeralAccountBalance,
migrationSurchargeMultiplier: values.migrationSurchargeMultiplier,
}
}

export function assertSubmitValuesAll(values: AllFieldValues): AllSubmitValues {
return {
...assertSubmitValuesAutopilot(values),
...assertSubmitValuesUpload(values),
...assertSubmitValuesPinned(values),
...assertSubmitValuesGouging(values),
}
}
Loading

0 comments on commit 93b720f

Please sign in to comment.