Skip to content

Commit

Permalink
✨ preview: handle byte slices
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Dec 3, 2024
1 parent ae036e5 commit aaf576d
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions developer-preview/src/lib/getPreviewData.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import {
type AbiFunction,
bytesToHex,
createPublicClient,
decodeFunctionData,
erc20Abi,
getAbiItem,
hexToBytes,
http,
isAddress,
isHex,
Expand All @@ -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<string, unknown>)[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<string, unknown>)[key];
}, values);

const fetchAndParse = async (url: string, parse: (json: unknown) => ABI) => {
const response = await fetch(url);
Expand Down

0 comments on commit aaf576d

Please sign in to comment.