diff --git a/components/factory/forms/ConfirmationForm.tsx b/components/factory/forms/ConfirmationForm.tsx index 0f9c068..1d584bd 100644 --- a/components/factory/forms/ConfirmationForm.tsx +++ b/components/factory/forms/ConfirmationForm.tsx @@ -29,13 +29,19 @@ export default function ConfirmationForm({ const effectiveAddress = formData.isGroup && formData.groupPolicyAddress ? formData.groupPolicyAddress : address; - const fullDenom = `factory/${effectiveAddress}/${formData.subdenom}`; + const getDenomInfo = (subdenom: string) => { + const prefixedSubdenom = 'u' + subdenom; + const symbol = (subdenom.startsWith('u') ? subdenom.slice(1) : subdenom).toUpperCase(); + const fullDenom = `factory/${effectiveAddress}/${prefixedSubdenom}`; + return { prefixedSubdenom, symbol, fullDenom }; + }; + + const { prefixedSubdenom, symbol, fullDenom } = getDenomInfo(formData.subdenom); const handleConfirm = async () => { setIsSigning(true); const createAsGroup = async () => { - const symbol = formData.subdenom.slice(1).toUpperCase(); const msg = submitProposal({ groupPolicyAddress: formData.groupPolicyAddress || '', messages: [ @@ -44,7 +50,7 @@ export default function ConfirmationForm({ value: MsgCreateDenom.encode( createDenom({ sender: formData.groupPolicyAddress || '', - subdenom: formData.subdenom, + subdenom: prefixedSubdenom, }).value ).finish(), }), @@ -59,10 +65,10 @@ export default function ConfirmationForm({ { denom: fullDenom, exponent: 0, - aliases: [symbol], + aliases: [formData.display], }, { - denom: symbol, + denom: formData.display, exponent: 6, aliases: [fullDenom], }, @@ -99,7 +105,7 @@ export default function ConfirmationForm({ // First, create the denom const createDenomMsg = createDenom({ sender: address, - subdenom: formData.subdenom, + subdenom: prefixedSubdenom, }); const createDenomFee = await estimateFee(address, [createDenomMsg]); @@ -113,7 +119,6 @@ export default function ConfirmationForm({ return; } - const symbol = formData.subdenom.slice(1).toUpperCase(); // If createDenom is successful, proceed with setDenomMetadata const setMetadataMsg = setDenomMetadata({ sender: address, @@ -126,7 +131,7 @@ export default function ConfirmationForm({ aliases: [symbol], }, { - denom: symbol, + denom: formData.display, exponent: 6, aliases: [fullDenom], }, diff --git a/components/factory/forms/CreateDenom.tsx b/components/factory/forms/CreateDenom.tsx index d8f2b41..3ba727b 100644 --- a/components/factory/forms/CreateDenom.tsx +++ b/components/factory/forms/CreateDenom.tsx @@ -21,14 +21,13 @@ export default function CreateDenom({ const DenomSchema = Yup.object().shape({ subdenom: Yup.string() .required('Subdenom is required') - .matches(/^[u][a-zA-Z0-9]+$/, 'Subdenom must start with the letter u') .min(4, 'Subdenom must be at least 4 characters') .max(44, 'Subdenom must not exceed 44 characters') .noProfanity('Profanity is not allowed') .simulateDenomCreation(async () => { setIsSimulating(true); try { - return await simulateDenomCreation(formData.subdenom); + return await simulateDenomCreation(`u${formData.subdenom}`); } finally { setIsSimulating(false); } @@ -41,11 +40,32 @@ export default function CreateDenom({ { + try { + await DenomSchema.validate(values, { abortEarly: false }); + nextStep(); + } catch (err) { + if (err instanceof Yup.ValidationError) { + const errors = err.inner.reduce( + (acc: Record, error) => ({ + ...acc, + [error.path as string]: error.message, + }), + {} + ); + setErrors(errors); + } else { + console.error('Unexpected error:', err); + setErrors({ subdenom: 'An unexpected error occurred' }); + } + } finally { + setSubmitting(false); + } + }} + validateOnChange={false} + validateOnBlur={false} > - {({ setFieldValue, isValid, isSubmitting, isValidating, handleSubmit }) => ( + {({ setFieldValue, isValid, isSubmitting, isValidating, handleSubmit, setErrors }) => ( <>
@@ -57,8 +77,7 @@ export default function CreateDenom({ ) => { dispatch({ @@ -67,6 +86,7 @@ export default function CreateDenom({ value: e.target.value, }); setFieldValue('subdenom', e.target.value); + setErrors({}); }} rightElement={ isSimulating && ( @@ -126,6 +146,7 @@ export default function CreateDenom({