Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix address verification #131

Merged
merged 11 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions hooks/useEasyWrite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,7 @@ export const useEasyWrite = (
params: UsePrepareContractWriteConfig & { isCrossChain?: boolean }
) => {
const { notify } = useNotifications();
const {
config,
error: prepareError,
isLoading: prepareIsLoading,
} = usePrepareContractWrite(params);
const { config, error: prepareError } = usePrepareContractWrite(params);
const {
data: writeData,
error: writeError,
Expand All @@ -34,7 +30,7 @@ export const useEasyWrite = (
chainId: config.request?.chainId,
});
const error = prepareError || writeError;
const isLoading = prepareIsLoading || writeIsLoading;
const isLoading = isTransactionDataLoading || writeIsLoading;
const prev = usePrevious({
hash: writeData?.hash,
status: transactionData?.status,
Expand Down
25 changes: 11 additions & 14 deletions pages/[id]/delegate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const Delegate: NextPage = () => {
register,
handleSubmit,
formState: { errors },
} = useForm<FormData>();
} = useForm<FormData>({ mode: 'onChange' });

const l2VotingWeightFormatted = formatUnits(
l2VotingWeight || BigInt(0),
Expand All @@ -55,6 +55,9 @@ const Delegate: NextPage = () => {
if (delegateError) {
console.error(`Delegating failed ${delegateError}`);
}
const onSubmit = handleSubmit(async () => {
write?.();
});

return (
<>
Expand Down Expand Up @@ -114,13 +117,7 @@ const Delegate: NextPage = () => {
has been proposed in order to be considered for that proposal.
</p>
</div>
<form
className="py-3"
onSubmit={(event) => {
event?.preventDefault();
write!();
}}
>
<form className="py-3" onSubmit={onSubmit}>
<label className="block text-sm font-medium leading-6 text-gray-900">
Delegate Address
</label>
Expand All @@ -139,10 +136,10 @@ const Delegate: NextPage = () => {
},
validate: async (value) => {
const validAddress = isAddress(value);
if (validAddress) {
return true;
}
return 'Invalid address';
const isBalanceNonzero = (l2.token?.value || BigInt(0)) > BigInt(0);
if (validAddress && isBalanceNonzero) return true;
if (!isBalanceNonzero) return 'Cannot delegate with 0 balance.';
return 'Not a valid address.';
},
})}
/>
Expand All @@ -154,7 +151,7 @@ const Delegate: NextPage = () => {
</div>
{mounted && errors?.delegateAddress && (
<p className="mt-2 text-sm text-red-600" id="email-error">
Not a valid address.
{errors.delegateAddress.message}
</p>
)}
{mounted && chain?.id !== config.l2.chain.id ? (
Expand All @@ -170,7 +167,7 @@ const Delegate: NextPage = () => {
<button
className="flex flex-row mt-5 items-center rounded-md bg-indigo-600 px-3.5 py-2.5 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600 disabled:bg-gray-300 disabled:cursor-not-allowed"
type="submit"
disabled={(l2.token?.value || BigInt(0)) <= BigInt(0) || isLoading}
disabled={Boolean(errors?.delegateAddress) || isLoading}
>
Delegate
{isLoading && (
Expand Down