diff --git a/dapp/src/constants/errors.ts b/dapp/src/constants/errors.ts index faf34d39d..320b53195 100644 --- a/dapp/src/constants/errors.ts +++ b/dapp/src/constants/errors.ts @@ -1,4 +1,4 @@ -import { ConnectionErrorData } from "#/types" +import { ACTION_FLOW_TYPES, ConnectionErrorData } from "#/types" export const CONNECTION_ERRORS: Record = { REJECTED: { @@ -25,7 +25,7 @@ export const CONNECTION_ERRORS: Record = { }, } -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.", @@ -33,8 +33,10 @@ const ACTION_FORM_ERRORS = { `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.", + }, } diff --git a/dapp/src/utils/forms.ts b/dapp/src/utils/forms.ts index 2bae57bc6..8c67d11c2 100644 --- a/dapp/src/utils/forms.ts +++ b/dapp/src/utils/forms.ts @@ -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" @@ -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 =