Skip to content

Commit

Permalink
✨ preview: handle composite paths
Browse files Browse the repository at this point in the history
  • Loading branch information
cruzdanilo committed Dec 3, 2024
1 parent c10c5bf commit 8db97f0
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions developer-preview/src/lib/getPreviewData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,15 @@ import type { 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);

const processFields = (
fields: FieldFormatter[],
definitions: Record<string, { label?: string; format?: string }>,
values: Record<string, string>,
values: Record<string, unknown>,
): Promise<{ label: string; displayValue: string }[]> =>
Promise.all(
fields.map(async (field) => {
Expand All @@ -33,7 +38,7 @@ const processFields = (
}

if (field.path) {
const value = values[field.path];
const value = get(values, field.path);
if (value) {
switch (field.format) {
case "date":
Expand All @@ -46,6 +51,7 @@ const processFields = (
}
case "addressName":
if (
typeof value === "string" &&
Array.isArray(field.params?.sources) &&
field.params.sources.includes("ens") &&
isAddress(value)
Expand All @@ -57,7 +63,7 @@ const processFields = (
}
}
default:
displayValue = value;
displayValue = String(value);
}
}
}
Expand Down Expand Up @@ -98,7 +104,7 @@ async function transformSimpleFormatToOperations(
? format.intent
: JSON.stringify(format.intent);

const values: Record<string, string> = {};
const values: Record<string, unknown> = {};
if (isHex(callData) && !isHex(signature)) {
const abiItem = parseAbiItem(`function ${signature}`) as AbiFunction;
try {
Expand All @@ -107,7 +113,7 @@ async function transformSimpleFormatToOperations(
data: callData,
});
for (const [index, { name }] of abiItem.inputs.entries()) {
if (name) values[name] = String(args[index]);
if (name) values[name] = args[index];
}
} catch {}
}
Expand Down

0 comments on commit 8db97f0

Please sign in to comment.