Skip to content

Commit

Permalink
early return if no dynamic fields
Browse files Browse the repository at this point in the history
  • Loading branch information
alvrs committed Jul 3, 2023
1 parent 2f9ad25 commit cb89f43
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions packages/protocol-parser/src/encodeRecord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export function encodeRecord(schema: Schema, values: readonly (StaticPrimitiveTy
.map((value, i) => encodeField(schema.staticFields[i], value).replace(/^0x/, ""))
.join("");

if (schema.dynamicFields.length === 0) return `0x${staticData}`;

const dynamicDataItems = dynamicValues.map((value, i) =>
encodeField(schema.dynamicFields[i], value).replace(/^0x/, "")
);
Expand All @@ -20,12 +22,9 @@ export function encodeRecord(schema: Schema, values: readonly (StaticPrimitiveTy

const dynamicData = dynamicDataItems.join("");

const packedCounter =
schema.dynamicFields.length > 0
? `${encodeField("uint56", dynamicTotalByteLength).replace(/^0x/, "")}${dynamicFieldByteLengths
.map((length) => encodeField("uint40", length).replace(/^0x/, ""))
.join("")}`.padEnd(64, "0")
: "";
const packedCounter = `${encodeField("uint56", dynamicTotalByteLength).replace(/^0x/, "")}${dynamicFieldByteLengths
.map((length) => encodeField("uint40", length).replace(/^0x/, ""))
.join("")}`.padEnd(64, "0");

return `0x${staticData}${packedCounter}${dynamicData}`;
}

0 comments on commit cb89f43

Please sign in to comment.