From 1e4083de79e4223795fee7bb1a5aa043848d3923 Mon Sep 17 00:00:00 2001 From: danilo neves cruz Date: Sun, 1 Dec 2024 13:23:01 +0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20preview:=20handle=20byte=20slices?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- developer-preview/src/lib/getPreviewData.tsx | 24 +++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/developer-preview/src/lib/getPreviewData.tsx b/developer-preview/src/lib/getPreviewData.tsx index 1002cb3..23a35b8 100644 --- a/developer-preview/src/lib/getPreviewData.tsx +++ b/developer-preview/src/lib/getPreviewData.tsx @@ -1,9 +1,11 @@ import { type AbiFunction, + bytesToHex, createPublicClient, decodeFunctionData, erc20Abi, getAbiItem, + hexToBytes, http, isAddress, isHex, @@ -18,9 +20,25 @@ import type { ABI, ERC7730Schema, FieldFormatter } from "~/types/ERC7730Schema"; const publicClient = createPublicClient({ chain: mainnet, transport: http() }); const get = (values: unknown, path: string) => - path - .split(".") - .reduce((acc, key) => acc && (acc as Record)[key], values); + path.split(".").reduce((acc, key) => { + if (!acc) return; + const slice = /^\[(-?\d+)?(?::(-?\d+))?\]$/.exec(key); + if (slice && isHex(acc)) { + return bytesToHex( + hexToBytes(acc).slice( + Number(slice[1]), + slice[2] + ? Number(slice[2]) >= 0 + ? Number(slice[2]) + 1 + : Number(slice[2]) === -1 + ? undefined + : Number(slice[2]) + : undefined, + ), + ); + } + return (acc as Record)[key]; + }, values); const fetchAndParse = async (url: string, parse: (json: unknown) => ABI) => { const response = await fetch(url);