|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { AbiEvent, AbiFunction, toFunctionSelector } from "viem"; |
| 4 | +import { formatAbiItem } from "viem/utils"; |
| 5 | +import * as z from "zod"; |
| 6 | +import { useState } from "react"; |
| 7 | +import "react18-json-view/src/dark.css"; |
| 8 | +import "react18-json-view/src/style.css"; |
| 9 | +import { useForm } from "react-hook-form"; |
| 10 | +import { zodResolver } from "@hookform/resolvers/zod"; |
| 11 | +import { CopyButton } from "../../../../../../components/CopyButton"; |
| 12 | +import { Button } from "../../../../../../components/ui/Button"; |
| 13 | +import { |
| 14 | + Form, |
| 15 | + FormControl, |
| 16 | + FormDescription, |
| 17 | + FormField, |
| 18 | + FormItem, |
| 19 | + FormLabel, |
| 20 | + FormMessage, |
| 21 | +} from "../../../../../../components/ui/Form"; |
| 22 | +import { Input } from "../../../../../../components/ui/Input"; |
| 23 | +import { Skeleton } from "../../../../../../components/ui/Skeleton"; |
| 24 | +import { cn } from "../../../../../../utils"; |
| 25 | +import { useWorldAbiQuery } from "../../../../queries/useWorldAbiQuery"; |
| 26 | +import { getErrorSelector } from "./getErrorSelector"; |
| 27 | + |
| 28 | +const formSchema = z.object({ |
| 29 | + selector: z.string().min(1).optional(), |
| 30 | +}); |
| 31 | + |
| 32 | +export function DecodeForm() { |
| 33 | + const { data, isLoading } = useWorldAbiQuery(); |
| 34 | + const form = useForm<z.infer<typeof formSchema>>({ |
| 35 | + resolver: zodResolver(formSchema), |
| 36 | + }); |
| 37 | + const [result, setResult] = useState<AbiFunction | AbiEvent>(); |
| 38 | + |
| 39 | + function onSubmit({ selector }: z.infer<typeof formSchema>) { |
| 40 | + const items = data?.abi.filter((item) => item.type === "function" || item.type === "error"); |
| 41 | + const abiItem = items?.find((item) => { |
| 42 | + if (item.type === "function") { |
| 43 | + return toFunctionSelector(item) === selector; |
| 44 | + } else if (item.type === "error") { |
| 45 | + return getErrorSelector(item) === selector; |
| 46 | + } |
| 47 | + |
| 48 | + return false; |
| 49 | + }); |
| 50 | + |
| 51 | + setResult(abiItem); |
| 52 | + } |
| 53 | + |
| 54 | + if (isLoading) { |
| 55 | + return <Skeleton className="h-[152px] w-full" />; |
| 56 | + } |
| 57 | + |
| 58 | + return ( |
| 59 | + <Form {...form}> |
| 60 | + <form onSubmit={form.handleSubmit(onSubmit)} className="space-y-4"> |
| 61 | + <FormField |
| 62 | + control={form.control} |
| 63 | + name="selector" |
| 64 | + render={({ field }) => ( |
| 65 | + <FormItem> |
| 66 | + <FormLabel>Encoded selector</FormLabel> |
| 67 | + <FormControl> |
| 68 | + <Input placeholder="0xf0f0f0f0" type="text" {...field} /> |
| 69 | + </FormControl> |
| 70 | + <FormDescription>Find the function or error by its selector</FormDescription> |
| 71 | + <FormMessage /> |
| 72 | + </FormItem> |
| 73 | + )} |
| 74 | + /> |
| 75 | + |
| 76 | + {form.formState.isSubmitted && ( |
| 77 | + <pre |
| 78 | + className={cn("text-md relative mt-4 rounded border border-white/20 p-3 text-sm", { |
| 79 | + "border-red-400 bg-red-100": !result, |
| 80 | + })} |
| 81 | + > |
| 82 | + {result ? ( |
| 83 | + <> |
| 84 | + <span className="mr-2 text-sm opacity-50">{result.type === "function" ? "function" : "error"}</span> |
| 85 | + <span>{formatAbiItem(result)}</span> |
| 86 | + <CopyButton value={JSON.stringify(result, null, 2)} className="absolute right-1.5 top-1.5" /> |
| 87 | + </> |
| 88 | + ) : ( |
| 89 | + <span className="text-red-700">No matching function or error found for this selector</span> |
| 90 | + )} |
| 91 | + </pre> |
| 92 | + )} |
| 93 | + |
| 94 | + <Button type="submit" size="sm"> |
| 95 | + Find |
| 96 | + </Button> |
| 97 | + </form> |
| 98 | + </Form> |
| 99 | + ); |
| 100 | +} |
0 commit comments