Skip to content

Commit

Permalink
fix: distributor ux improvements and validations
Browse files Browse the repository at this point in the history
  • Loading branch information
alanrsoares committed Nov 29, 2023
1 parent 12c4781 commit 396653a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 22 deletions.
4 changes: 2 additions & 2 deletions apps/maestro/src/features/AddErc20/AddErc20.state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import { z } from "zod";

import { logger } from "~/lib/logger";
import {
hex40Literal,
hex64Literal,
numericString,
optionalHexLiteral,
} from "~/lib/utils/validation";

const TOKEN_DETAILS_FORM_SCHEMA = z.object({
tokenName: z.string().min(1).max(32),
tokenSymbol: z.string().min(1).max(11),
tokenDecimals: z.coerce.number().min(1).max(18),
originTokenSupply: numericString(),
distributor: hex40Literal().optional(),
distributor: optionalHexLiteral(),
salt: hex64Literal(),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ export function useDeployAndRegisterRemoteInterchainTokenMutation(

const multicallArgs = useMemo(() => {
const distributorAddress =
input?.distributorAddress ?? input?.deployerAddress ?? deployerAddress;
input?.distributorAddress ?? input?.deployerAddress;

if (!input || !distributorAddress || !tokenId) {
return [];
Expand Down Expand Up @@ -193,7 +193,6 @@ export function useDeployAndRegisterRemoteInterchainTokenMutation(
return [deployTxData, ...registerTxData, ...transferTxData];
}, [
input,
deployerAddress,
tokenId,
withDecimals,
config.salt,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import {
EyeIcon,
EyeOffIcon,
FormControl,
HelpCircleIcon,
Label,
TextInput,
Tooltip,
} from "@axelarjs/ui";
import { useRef, useState, type FC } from "react";
import { type SubmitHandler } from "react-hook-form";
Expand Down Expand Up @@ -111,7 +113,6 @@ const TokenDetails: FC = () => {
})}
/>
</FormControl>

<div className="grid place-content-center pt-4 md:place-content-end">
<Button size="sm" onClick={() => setShowAdvanced(!showAdvanced)}>
advanced settings
Expand All @@ -126,20 +127,36 @@ const TokenDetails: FC = () => {
{showAdvanced && (
<>
<FormControl>
<Label htmlFor="distributor">Mint tokens to</Label>
<Label htmlFor="distributor">
<Label.Text className="inline-flex items-center gap-1">
Token Distributor
<Tooltip
position="right"
variant="info"
tip="This address will receive the minted tokens. It will also be able to mint, burn tokens and tranfer distributorship."
>
<HelpCircleIcon className="text-info mr-1 h-[1em]" />
</Tooltip>
</Label.Text>
</Label>
<FormInput
id="distributor"
placeholder="Enter account address to mint to"
placeholder="Enter token distributor address"
onKeyDown={preventNonHexInput}
defaultValue={state.tokenDetailsForm.getValues("distributor")}
{...register("distributor", {
disabled: isReadonly,
validate(value) {
if (!value) {
return false;
}

// this field is optional, so we only validate if it's not empty
if (!isAddress(String(value))) {
return "Invalid address";
}

return true;
return false;
},
})}
/>
Expand Down
28 changes: 14 additions & 14 deletions apps/maestro/src/lib/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,35 @@ export const VALID_NON_NUMERIC_KEYS = [
"End",
];

/**
* type guard for numeric keys
*
* @param key The key to check.
* @returns true if the key is a valid numeric key.
* @see https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values
*/
export const isValidNumericKey = (key: string) =>
VALID_NON_NUMERIC_KEYS.includes(key) || /^[0-9.]+$/.test(key);

export const preventNonNumericInput = (
e: React.KeyboardEvent<HTMLInputElement>
) => {
if (!isValidNumericKey(e.key)) {
if (VALID_NON_NUMERIC_KEYS.includes(e.key) || e.ctrlKey || e.metaKey) {
// allow valid non-numeric keys & ctrl shortcuts
return;
}
if (!/^[0-9.]+$/.test(e.key)) {
e.preventDefault();
}
};

export const preventNonHexInput = (
e: React.KeyboardEvent<HTMLInputElement>
) => {
if (VALID_NON_NUMERIC_KEYS.includes(e.key)) {
// allow valid non-numeric keys
if (VALID_NON_NUMERIC_KEYS.includes(e.key) || e.ctrlKey || e.metaKey) {
// allow valid non-numeric keys & ctrl shortcuts
return;
}
if (!/^[0-9a-fA-F]+$/.test(e.key)) {
if (!/^[0-9a-fA-FxX]+$/.test(e.key)) {
e.preventDefault();
}
};

const asHexLiteral = <T extends string>(x: T) => x as `0x${T}`;

const asHexLiteralOptional = <T extends string>(x: T | undefined) =>
x as `0x${T}` | undefined;

/**
* Zod schema to validate a variable length hex address
*/
Expand All @@ -69,6 +66,9 @@ export const hexLiteral = () =>
.regex(/^0x[0-9a-fA-F]+$/)
.transform(asHexLiteral);

export const optionalHexLiteral = () =>
z.string().optional().transform(asHexLiteralOptional);

/**
* Zod schema to validate a 40 character hex address
*/
Expand Down

2 comments on commit 396653a

@vercel
Copy link

@vercel vercel bot commented on 396653a Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

axelar-ui – ./packages/ui

ui.axelar.dev
axelar-ui-axelar-network.vercel.app
axelar-ui.vercel.app
axelar-ui-git-main-axelar-network.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 396653a Nov 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

axelar-registry – ./apps/registry

axelar-registry-git-main-axelar-network.vercel.app
axelar-registry-axelar-network.vercel.app

Please sign in to comment.