diff --git a/packages/common/src/getByteLength.ts b/packages/common/src/getByteLength.ts deleted file mode 100644 index dfbe61e2ce..0000000000 --- a/packages/common/src/getByteLength.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { Hex } from "viem"; - -export function getByteLength(data: Hex): number { - // Remove `0x` prefix, then byte is 2 characters - return (data.length - 2) / 2; -} diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index c4d9e899ad..a15afa9877 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -9,4 +9,3 @@ export * from "./resourceIdToHex"; export * from "./resourceTypes"; export * from "./spliceHex"; export * from "./transportObserver"; -export * from "./getByteLength"; diff --git a/packages/store-sync/src/postgres/postgresStorage.ts b/packages/store-sync/src/postgres/postgresStorage.ts index fb61096b6c..127889a855 100644 --- a/packages/store-sync/src/postgres/postgresStorage.ts +++ b/packages/store-sync/src/postgres/postgresStorage.ts @@ -1,4 +1,4 @@ -import { Hex, PublicClient, concatHex } from "viem"; +import { Hex, PublicClient, concatHex, size } from "viem"; import { PgDatabase, QueryResultHKT } from "drizzle-orm/pg-core"; import { eq, inArray } from "drizzle-orm"; import { buildTable } from "./buildTable"; @@ -7,7 +7,7 @@ import { debug } from "./debug"; import { buildInternalTables } from "./buildInternalTables"; import { getTables } from "./getTables"; import { schemaVersion } from "./schemaVersion"; -import { getByteLength, hexToResourceId, spliceHex } from "@latticexyz/common"; +import { hexToResourceId, spliceHex } from "@latticexyz/common"; import { setupTables } from "./setupTables"; import { getTableKey } from "./getTableKey"; import { StorageAdapter, StorageAdapterBlock } from "../common"; @@ -132,12 +132,7 @@ export async function postgresStorage // TODO: verify that this returns what we expect (doesn't error/undefined on no record) const previousValue = (await tx.select().from(sqlTable).where(eq(sqlTable.__key, uniqueKey)).execute())[0]; const previousStaticData = (previousValue?.__staticData as Hex) ?? "0x"; - const newStaticData = spliceHex( - previousStaticData, - log.args.start, - getByteLength(log.args.data), - log.args.data - ); + const newStaticData = spliceHex(previousStaticData, log.args.start, size(log.args.data), log.args.data); const newValue = decodeValueArgs(table.valueSchema, { staticData: newStaticData, encodedLengths: (previousValue?.__encodedLengths as Hex) ?? "0x", diff --git a/packages/store-sync/src/recs/recsStorage.ts b/packages/store-sync/src/recs/recsStorage.ts index a9d1e78c33..f4c3672d2a 100644 --- a/packages/store-sync/src/recs/recsStorage.ts +++ b/packages/store-sync/src/recs/recsStorage.ts @@ -3,9 +3,9 @@ import { debug } from "./debug"; import { World as RecsWorld, getComponentValue, hasComponent, removeComponent, setComponent } from "@latticexyz/recs"; import { defineInternalComponents } from "./defineInternalComponents"; import { getTableEntity } from "./getTableEntity"; -import { getByteLength, hexToResourceId, spliceHex } from "@latticexyz/common"; +import { hexToResourceId, spliceHex } from "@latticexyz/common"; import { decodeValueArgs } from "@latticexyz/protocol-parser"; -import { Hex } from "viem"; +import { Hex, size } from "viem"; import { isTableRegistrationLog } from "../isTableRegistrationLog"; import { logToTable } from "../logToTable"; import { hexKeyTupleToEntity } from "./hexKeyTupleToEntity"; @@ -98,12 +98,7 @@ export function recsStorage({ // TODO: add tests that this works when no record had been set before const previousValue = getComponentValue(component, entity); const previousStaticData = (previousValue?.__staticData as Hex) ?? "0x"; - const newStaticData = spliceHex( - previousStaticData, - log.args.start, - getByteLength(log.args.data), - log.args.data - ); + const newStaticData = spliceHex(previousStaticData, log.args.start, size(log.args.data), log.args.data); const newValue = decodeValueArgs(table.valueSchema, { staticData: newStaticData, encodedLengths: (previousValue?.__encodedLengths as Hex) ?? "0x", diff --git a/packages/store-sync/src/sqlite/sqliteStorage.ts b/packages/store-sync/src/sqlite/sqliteStorage.ts index 4898b1b6a0..c010d8c8d7 100644 --- a/packages/store-sync/src/sqlite/sqliteStorage.ts +++ b/packages/store-sync/src/sqlite/sqliteStorage.ts @@ -1,4 +1,4 @@ -import { Hex, PublicClient, concatHex, getAddress } from "viem"; +import { Hex, PublicClient, concatHex, getAddress, size } from "viem"; import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core"; import { and, eq, sql } from "drizzle-orm"; import { sqliteTableToSql } from "./sqliteTableToSql"; @@ -12,7 +12,7 @@ import { schemaVersion } from "./schemaVersion"; import { StorageAdapter } from "../common"; import { isTableRegistrationLog } from "../isTableRegistrationLog"; import { logToTable } from "../logToTable"; -import { getByteLength, hexToResourceId, spliceHex } from "@latticexyz/common"; +import { hexToResourceId, spliceHex } from "@latticexyz/common"; import { decodeKey, decodeValueArgs } from "@latticexyz/protocol-parser"; // TODO: upgrade drizzle and use async sqlite interface for consistency @@ -131,12 +131,7 @@ export async function sqliteStorage({ // TODO: verify that this returns what we expect (doesn't error/undefined on no record) const previousValue = (await tx.select().from(sqlTable).where(eq(sqlTable.__key, uniqueKey)).execute())[0]; const previousStaticData = (previousValue?.__staticData as Hex) ?? "0x"; - const newStaticData = spliceHex( - previousStaticData, - log.args.start, - getByteLength(log.args.data), - log.args.data - ); + const newStaticData = spliceHex(previousStaticData, log.args.start, size(log.args.data), log.args.data); const newValue = decodeValueArgs(table.valueSchema, { staticData: newStaticData, encodedLengths: (previousValue?.__encodedLengths as Hex) ?? "0x",