diff --git a/.vscode/settings.json b/.vscode/settings.json index 06337fda..014ffa92 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -10,5 +10,6 @@ "[typescriptreact]": { "editor.defaultFormatter": "esbenp.prettier-vscode" }, - "eslint.validate": ["typescript", "typescriptreact"] + "eslint.validate": ["typescript", "typescriptreact"], + "typescript.tsdk": "node_modules/typescript/lib" } diff --git a/components/factory/forms/CreateDenom.tsx b/components/factory/forms/CreateDenom.tsx index 1e641ec7..698c1800 100644 --- a/components/factory/forms/CreateDenom.tsx +++ b/components/factory/forms/CreateDenom.tsx @@ -2,7 +2,21 @@ import { TokenAction, TokenFormData } from '@/helpers/formReducer'; import { useTx, useFeeEstimation } from '@/hooks'; import { osmosis } from '@chalabi/manifestjs'; import Link from 'next/link'; -import { useState, useEffect } from 'react'; +import { useState } from 'react'; +import { Formik, Form } from 'formik'; +import Yup from '@/utils/yupExtensions'; +import { TextInput } from '@/components/react/inputs'; + +const DenomSchema = Yup.object().shape({ + subdenom: Yup.string() + .required('Subdenom is required') + .matches( + /^[uaqg][a-zA-Z0-9]+$/, + 'Subdenom must start with u, a, q, or g, followed by letters and numbers' + ) + .min(4, 'Subdenom must be at least 4 characters') + .max(44, 'Subdenom must not exceed 44 characters'), +}); export default function CreateDenom({ nextStep, @@ -15,53 +29,17 @@ export default function CreateDenom({ dispatch: React.Dispatch; address: string; }>) { - const [error, setError] = useState(null); - const [touched, setTouched] = useState(false); - - const updateField = (field: keyof TokenFormData, value: any) => { - dispatch({ type: 'UPDATE_FIELD', field, value }); - setTouched(true); - }; - const { createDenom } = osmosis.tokenfactory.v1beta1.MessageComposer.withTypeUrl; const [isSigning, setIsSigning] = useState(false); const { tx } = useTx('manifest'); const { estimateFee } = useFeeEstimation('manifest'); - const validateSubdenom = (value: string) => { - console.log('Validating subdenom', value); - if (value.length === 0) { - return 'Subdenom is required'; - } - if (!/^[a-zA-Z]/.test(value)) { - return 'Subdenom must start with a letter'; - } - if (!/^[a-zA-Z0-9]+$/.test(value)) { - return 'Subdenom can only contain letters and numbers'; - } - return null; - }; - - useEffect(() => { - if (touched) { - const validationError = validateSubdenom(formData.subdenom); - setError(validationError); - } - }, [formData.subdenom, touched]); - - const handleConfirm = async () => { - setTouched(true); - const validationError = validateSubdenom(formData.subdenom); - if (validationError) { - setError(validationError); - return; - } - + const handleConfirm = async (values: TokenFormData) => { setIsSigning(true); try { const msg = createDenom({ sender: address ?? '', - subdenom: formData.subdenom, + subdenom: values.subdenom, }); const fee = await estimateFee(address ?? '', [msg]); @@ -72,7 +50,6 @@ export default function CreateDenom({ }, }); } catch (error) { - setIsSigning(false); console.error('Error during transaction setup:', error); } finally { setIsSigning(false); @@ -87,45 +64,46 @@ export default function CreateDenom({

Create Denom

-
-
-
- - updateField('subdenom', e.target.value)} - onBlur={() => setTouched(true)} - /> - {touched && error && ( -

- {error} -

- )} + + {({ isValid, dirty, setFieldValue }) => ( + +
+ ) => { + dispatch({ + type: 'UPDATE_FIELD', + field: 'subdenom', + value: e.target.value, + }); + setFieldValue('subdenom', e.target.value); + }} + /> +

We recommend starting with 'u' (e.g., 'utoken')

-
-
-
- + )} - +