diff --git a/components/forms/CreateTxForm/Fields/FieldCommission.tsx b/components/forms/CreateTxForm/Fields/FieldCommission.tsx new file mode 100644 index 00000000..e15a6f0f --- /dev/null +++ b/components/forms/CreateTxForm/Fields/FieldCommission.tsx @@ -0,0 +1,94 @@ +import { FormControl, FormField, FormItem, FormLabel, FormMessage } from "@/components/ui/form"; +import { Input } from "@/components/ui/input"; +import { prettyFieldName } from "@/lib/form"; +import * as z from "zod"; +import type { FieldProps } from "./types"; + +const isFieldCommission = (fieldName: string) => fieldName === "commission"; + +export const getFieldCommission = (fieldName: string) => + isFieldCommission(fieldName) ? FieldCommission : null; + +export const getFieldCommissionSchema = (fieldName: string) => + isFieldCommission(fieldName) + ? z.object({ + rate: z.coerce + .number({ invalid_type_error: "Must be a number", required_error: "Required" }) + .positive("Must be positive") + .transform((value) => { + try { + return String(value); + } catch (error) { + return value; + } + }), + maxRate: z.coerce + .number({ invalid_type_error: "Must be a number", required_error: "Required" }) + .positive("Must be positive") + .transform((value) => { + try { + return String(value); + } catch (error) { + return value; + } + }), + maxChangeRate: z.coerce + .number({ invalid_type_error: "Must be a number", required_error: "Required" }) + .positive("Must be positive") + .transform((value) => { + try { + return String(value); + } catch (error) { + return value; + } + }), + }) + : null; + +export default function FieldCommission({ form, fieldFormName }: FieldProps) { + const prettyLabel = prettyFieldName(fieldFormName); + + return ( + <> + ( + + {`${prettyLabel} Rate`} + + + + + + )} + /> + ( + + {`${prettyLabel} Max Rate`} + + + + + + )} + /> + ( + + {`${prettyLabel} Max Change Rate`} + + + + + + )} + /> + + ); +} diff --git a/components/forms/CreateTxForm/Fields/index.ts b/components/forms/CreateTxForm/Fields/index.ts index 62413a5a..193c24bd 100644 --- a/components/forms/CreateTxForm/Fields/index.ts +++ b/components/forms/CreateTxForm/Fields/index.ts @@ -1,6 +1,7 @@ export * from "./FieldAddress"; export * from "./FieldAmount"; export * from "./FieldBoolean"; +export * from "./FieldCommission"; export * from "./FieldDescription"; export * from "./FieldNumber"; export * from "./FieldString"; diff --git a/lib/form.ts b/lib/form.ts index 53b2e5e8..ac6a7b52 100644 --- a/lib/form.ts +++ b/lib/form.ts @@ -5,6 +5,8 @@ import { getFieldAmountSchema, getFieldBoolean, getFieldBooleanSchema, + getFieldCommission, + getFieldCommissionSchema, getFieldDescription, getFieldDescriptionSchema, getFieldNumber, @@ -33,6 +35,7 @@ export const getField = (fieldName: string) => getFieldBoolean(fieldName) || getFieldTimeoutHeight(fieldName) || getFieldDescription(fieldName) || + getFieldCommission(fieldName) || null; const getFieldSchema = (fieldName: string, schemaInput: FieldSchemaInput) => @@ -43,6 +46,7 @@ const getFieldSchema = (fieldName: string, schemaInput: FieldSchemaInput) => getFieldBooleanSchema(fieldName) || getFieldTimeoutHeightSchema(fieldName) || getFieldDescriptionSchema(fieldName) || + getFieldCommissionSchema(fieldName) || null; export const getMsgSchema = (fieldNames: readonly string[], schemaInput: FieldSchemaInput) => {