From 8c73ee7524a45b8c78d9decf3a89e8d455fb90d7 Mon Sep 17 00:00:00 2001
From: Kevin Ingersoll <kingersoll@gmail.com>
Date: Thu, 14 Sep 2023 10:54:36 +0100
Subject: [PATCH 1/2] add valueSchemaToFieldLayoutHex

---
 .../protocol-parser/src/fieldLayoutToHex.ts   |  1 +
 .../src/valueSchemaToFieldLayoutHex.ts        | 21 +++++++++++++++++++
 2 files changed, 22 insertions(+)
 create mode 100644 packages/protocol-parser/src/valueSchemaToFieldLayoutHex.ts

diff --git a/packages/protocol-parser/src/fieldLayoutToHex.ts b/packages/protocol-parser/src/fieldLayoutToHex.ts
index 25432a52e7..de52c1504d 100644
--- a/packages/protocol-parser/src/fieldLayoutToHex.ts
+++ b/packages/protocol-parser/src/fieldLayoutToHex.ts
@@ -1,6 +1,7 @@
 import { Hex } from "viem";
 import { FieldLayout } from "./common";
 
+/** @deprecated use `valueSchemaToFieldLayoutHex` instead */
 export function fieldLayoutToHex(fieldLayout: FieldLayout): Hex {
   const staticDataLength = fieldLayout.staticFieldLengths.reduce((totalLength, length) => totalLength + length, 0);
   return `0x${[
diff --git a/packages/protocol-parser/src/valueSchemaToFieldLayoutHex.ts b/packages/protocol-parser/src/valueSchemaToFieldLayoutHex.ts
new file mode 100644
index 0000000000..dc10d95944
--- /dev/null
+++ b/packages/protocol-parser/src/valueSchemaToFieldLayoutHex.ts
@@ -0,0 +1,21 @@
+import { Hex } from "viem";
+import { ValueSchema } from "./common";
+import { isDynamicAbiType, isStaticAbiType, staticAbiTypeToByteLength } from "@latticexyz/schema-type";
+
+// TODO: add tests once we have corresponding tests for FieldLayout.sol (bytes32 -> FieldLayout and vice versa)
+export function valueSchemaToFieldLayoutHex(valueSchema: ValueSchema): Hex {
+  const staticFields = Object.values(valueSchema).filter(isStaticAbiType);
+  const dynamicFields = Object.values(valueSchema).filter(isDynamicAbiType);
+
+  const staticFieldLengths = staticFields.map((fieldType) => staticAbiTypeToByteLength[fieldType]);
+  const staticDataLength = staticFieldLengths.reduce((dataLength, fieldLength) => dataLength + fieldLength, 0);
+
+  return `0x${[
+    staticDataLength.toString(16).padStart(4, "0"),
+    staticFields.length.toString(16).padStart(2, "0"),
+    dynamicFields.length.toString(16).padStart(2, "0"),
+    ...staticFieldLengths.map((fieldLength) => fieldLength.toString(16).padStart(2, "0")),
+  ]
+    .join("")
+    .padEnd(64, "0")}`;
+}

From f091db43c4444c2e223dc128a1b709d0d3a9c745 Mon Sep 17 00:00:00 2001
From: Kevin Ingersoll <kingersoll@gmail.com>
Date: Thu, 14 Sep 2023 05:00:52 -0700
Subject: [PATCH 2/2] Create silent-rice-argue.md

---
 .changeset/silent-rice-argue.md | 5 +++++
 1 file changed, 5 insertions(+)
 create mode 100644 .changeset/silent-rice-argue.md

diff --git a/.changeset/silent-rice-argue.md b/.changeset/silent-rice-argue.md
new file mode 100644
index 0000000000..dfa242b1d2
--- /dev/null
+++ b/.changeset/silent-rice-argue.md
@@ -0,0 +1,5 @@
+---
+"@latticexyz/protocol-parser": minor
+---
+
+Adds `valueSchemaToFieldLayoutHex` helper