Skip to content

Commit

Permalink
Refactor error typecheck function
Browse files Browse the repository at this point in the history
  • Loading branch information
kpyszkowski committed Aug 13, 2024
1 parent 56950f3 commit bd10ec7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
14 changes: 8 additions & 6 deletions dapp/src/constants/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConnectionErrorData } from "#/types"
import { ACTION_FLOW_TYPES, ConnectionErrorData } from "#/types"

export const CONNECTION_ERRORS: Record<string, ConnectionErrorData> = {
REJECTED: {
Expand All @@ -25,16 +25,18 @@ export const CONNECTION_ERRORS: Record<string, ConnectionErrorData> = {
},
}

const ACTION_FORM_ERRORS = {
export const TOKEN_FORM_ERRORS = {
REQUIRED: "Please enter an amount.",
EXCEEDED_VALUE:
"The amount exceeds your current wallet balance. Add more funds to your wallet or lower the deposit amount.",
INSUFFICIENT_VALUE: (minValue: string) =>
`The amount is below the minimum required deposit of ${minValue} BTC.`,
}

export const STAKE_FORM_ERRORS = ACTION_FORM_ERRORS
export const UNSTAKE_FORM_ERRORS = {
...ACTION_FORM_ERRORS,
EXCEEDED_VALUE: "Your Acre balance is insufficient.",
export const ACTION_FORM_ERRORS = {
[ACTION_FLOW_TYPES.STAKE]: TOKEN_FORM_ERRORS,
[ACTION_FLOW_TYPES.UNSTAKE]: {
...TOKEN_FORM_ERRORS,
EXCEEDED_VALUE: "Your Acre balance is insufficient.",
},
}
21 changes: 9 additions & 12 deletions dapp/src/utils/forms.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { STAKE_FORM_ERRORS, UNSTAKE_FORM_ERRORS } from "#/constants"
import { ACTION_FORM_ERRORS, TOKEN_FORM_ERRORS } from "#/constants"
import { ACTION_FLOW_TYPES, ActionFlowType, CurrencyType } from "#/types"
import { getCurrencyByType } from "./currency"
import { fixedPointNumberToString } from "./numbers"
Expand All @@ -16,36 +16,33 @@ export function validateTokenAmount(
minValue: bigint,
currency: CurrencyType,
): string | undefined {
const TOKEN_FORM_ERRORS =
actionType === ACTION_FLOW_TYPES.STAKE
? STAKE_FORM_ERRORS
: UNSTAKE_FORM_ERRORS
const ERRORS_BY_ACTION_TYPE = ACTION_FORM_ERRORS[actionType]

if (value === undefined) return TOKEN_FORM_ERRORS.REQUIRED
if (value === undefined) return ERRORS_BY_ACTION_TYPE.REQUIRED

const { decimals } = getCurrencyByType(currency)

const isMaximumValueExceeded = value > maxValue
const isMinimumValueFulfilled = value >= minValue

if (isMaximumValueExceeded) return TOKEN_FORM_ERRORS.EXCEEDED_VALUE
if (isMaximumValueExceeded) return ERRORS_BY_ACTION_TYPE.EXCEEDED_VALUE
if (!isMinimumValueFulfilled)
return TOKEN_FORM_ERRORS.INSUFFICIENT_VALUE(
return ERRORS_BY_ACTION_TYPE.INSUFFICIENT_VALUE(
fixedPointNumberToString(minValue, decimals),
)

return undefined
}

type GetTokenAmountErrorKeyReturnType = keyof typeof STAKE_FORM_ERRORS | null
type GetTokenAmountErrorKeyReturnType = keyof typeof TOKEN_FORM_ERRORS | null
export const getTokenAmountErrorKey = (
errorMessage: string,
): GetTokenAmountErrorKeyReturnType => {
const errorKeys = Object.keys(STAKE_FORM_ERRORS)
const errorKeys = Object.keys(ACTION_FORM_ERRORS)
const errorKeyValuePairs = [
...new Set([
...Object.entries(STAKE_FORM_ERRORS),
...Object.entries(UNSTAKE_FORM_ERRORS),
...Object.entries(ACTION_FORM_ERRORS[ACTION_FLOW_TYPES.STAKE]),
...Object.entries(ACTION_FORM_ERRORS[ACTION_FLOW_TYPES.UNSTAKE]),
]),
]
const errorKey =
Expand Down

0 comments on commit bd10ec7

Please sign in to comment.