From 8a6e323f546143e46c8d55aa32373496bfec75a0 Mon Sep 17 00:00:00 2001 From: karooolis Date: Wed, 8 Jan 2025 17:33:02 +0200 Subject: [PATCH] initial work on handling tuples/arrays --- .../[worldAddress]/interact/FunctionField.tsx | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/interact/FunctionField.tsx b/packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/interact/FunctionField.tsx index 4b429cd10b..6d53e97ad6 100644 --- a/packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/interact/FunctionField.tsx +++ b/packages/explorer/src/app/(explorer)/[chainName]/worlds/[worldAddress]/interact/FunctionField.tsx @@ -35,10 +35,14 @@ type DecodedEvent = { }; const formSchema = z.object({ - inputs: z.array(z.string()), + inputs: z.array(z.union([z.string(), z.array(z.any()), z.record(z.any())])), value: z.string().optional(), }); +function isArrayOrTuple(type: string) { + return type.includes("[]") || type.includes("tuple"); +} + export function FunctionField({ worldAbi, functionAbi }: Props) { const operationType: FunctionType = functionAbi.stateMutability === "view" || functionAbi.stateMutability === "pure" @@ -55,6 +59,8 @@ export function FunctionField({ worldAbi, functionAbi }: Props) { const [txHash, setTxHash] = useState(); const txUrl = blockExplorerTransactionUrl({ hash: txHash, chainId }); + console.log(functionAbi); + const form = useForm>({ resolver: zodResolver(formSchema), defaultValues: { @@ -62,6 +68,17 @@ export function FunctionField({ worldAbi, functionAbi }: Props) { }, }); + const parseInput = (value: string, type: string) => { + try { + if (isArrayOrTuple(type)) { + return JSON.parse(value); + } + return value; + } catch (e) { + return value; + } + }; + async function onSubmit(values: z.infer) { if (!account.isConnected) { return openConnectModal?.(); @@ -70,12 +87,16 @@ export function FunctionField({ worldAbi, functionAbi }: Props) { setIsLoading(true); let toastId; try { + const parsedInputs = values.inputs.map((input, index) => + parseInput(input as string, functionAbi.inputs[index].type), + ); + if (operationType === FunctionType.READ) { const result = await readContract(wagmiConfig, { abi: worldAbi, address: worldAddress as Address, functionName: functionAbi.name, - args: values.inputs, + args: parsedInputs, chainId, }); @@ -86,7 +107,7 @@ export function FunctionField({ worldAbi, functionAbi }: Props) { abi: worldAbi, address: worldAddress as Address, functionName: functionAbi.name, - args: values.inputs, + args: parsedInputs, ...(values.value && { value: BigInt(values.value) }), chainId, }); @@ -134,7 +155,12 @@ export function FunctionField({ worldAbi, functionAbi }: Props) { name={`inputs.${index}`} render={({ field }) => ( - {input.name} + + {input.name} + {isArrayOrTuple(input.type) && ( + (Enter as JSON array) + )} +