From d2f8e940048e56d9be204bf5b2cbcf8d29cc1dee Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Mon, 9 Oct 2023 11:02:22 +0100 Subject: [PATCH] feat(common): clarify resourceId (hex) from resource (object) (#1706) --- .changeset/thirty-cups-provide.md | 19 +++++++++ .changeset/wild-squids-bathe.md | 7 ++++ .../sync-test/data/setContractData.ts | 4 +- packages/cli/src/commands/trace.ts | 6 +-- packages/cli/src/utils/deploy.ts | 4 +- .../utils/systems/getGrantAccessCallData.ts | 4 +- .../getRegisterFunctionSelectorsCallData.ts | 6 +-- .../systems/getRegisterSystemCallData.ts | 4 +- .../utils/tables/getRegisterTableCallData.ts | 4 +- packages/cli/src/utils/tables/getTableIds.ts | 4 +- packages/common/package.json | 4 -- packages/common/src/common.ts | 6 ++- .../common/src/deprecated/TableId.test.ts | 33 --------------- packages/common/src/deprecated/TableId.ts | 41 ------------------- .../src/{ => deprecated}/createContract.ts | 2 +- packages/common/src/deprecated/getTableIds.ts | 6 --- .../common/src/deprecated/hexToResourceId.ts | 4 ++ packages/common/src/deprecated/index.ts | 2 - .../common/src/deprecated/resourceIdToHex.ts | 4 ++ packages/common/src/hexToResource.test.ts | 11 +++++ .../{hexToResourceId.ts => hexToResource.ts} | 8 ++-- packages/common/src/hexToResourceId.test.ts | 11 ----- packages/common/src/index.ts | 10 +++-- ...eIdToHex.test.ts => resourceToHex.test.ts} | 26 +++++++----- .../{resourceIdToHex.ts => resourceToHex.ts} | 10 ++--- packages/common/tsup.config.ts | 1 - .../dev-tools/src/actions/WriteSummary.tsx | 4 +- packages/dev-tools/src/events/LogsTable.tsx | 4 +- packages/store-sync/src/common.ts | 4 +- packages/store-sync/src/internalTableIds.ts | 6 +-- packages/store-sync/src/logToTable.ts | 4 +- .../store-sync/src/postgres/getTableKey.ts | 4 +- .../src/postgres/postgresStorage.ts | 4 +- .../src/recs/configToRecsComponents.ts | 4 +- packages/store-sync/src/recs/recsStorage.ts | 4 +- .../store-sync/src/sqlite/sqliteStorage.ts | 6 +-- 36 files changed, 121 insertions(+), 164 deletions(-) create mode 100644 .changeset/thirty-cups-provide.md create mode 100644 .changeset/wild-squids-bathe.md delete mode 100644 packages/common/src/deprecated/TableId.test.ts delete mode 100644 packages/common/src/deprecated/TableId.ts rename packages/common/src/{ => deprecated}/createContract.ts (65%) delete mode 100644 packages/common/src/deprecated/getTableIds.ts create mode 100644 packages/common/src/deprecated/hexToResourceId.ts delete mode 100644 packages/common/src/deprecated/index.ts create mode 100644 packages/common/src/deprecated/resourceIdToHex.ts create mode 100644 packages/common/src/hexToResource.test.ts rename packages/common/src/{hexToResourceId.ts => hexToResource.ts} (83%) delete mode 100644 packages/common/src/hexToResourceId.test.ts rename packages/common/src/{resourceIdToHex.test.ts => resourceToHex.test.ts} (64%) rename packages/common/src/{resourceIdToHex.ts => resourceToHex.ts} (61%) diff --git a/.changeset/thirty-cups-provide.md b/.changeset/thirty-cups-provide.md new file mode 100644 index 0000000000..04d79df7f2 --- /dev/null +++ b/.changeset/thirty-cups-provide.md @@ -0,0 +1,19 @@ +--- +"@latticexyz/common": minor +--- + +Renames `resourceIdToHex` to `resourceToHex` and `hexToResourceId` to `hexToResource`, to better distinguish between a resource ID (hex value) and a resource reference (type, namespace, name). + +```diff +- resourceIdToHex({ type: 'table', namespace: '', name: 'Position' }); ++ resourceToHex({ type: 'table', namespace: '', name: 'Position' }); +``` + +```diff +- hexToResourceId('0x...'); ++ hexToResource('0x...'); +``` + +Previous methods still exist but are now deprecated to ease migration and reduce breaking changes. These will be removed in a future version. + +Also removes the previously deprecated and unused table ID utils (replaced by these resource ID utils). diff --git a/.changeset/wild-squids-bathe.md b/.changeset/wild-squids-bathe.md new file mode 100644 index 0000000000..ff062dd16b --- /dev/null +++ b/.changeset/wild-squids-bathe.md @@ -0,0 +1,7 @@ +--- +"@latticexyz/cli": patch +"@latticexyz/dev-tools": patch +"@latticexyz/store-sync": patch +--- + +Moved to new resource ID utils. diff --git a/e2e/packages/sync-test/data/setContractData.ts b/e2e/packages/sync-test/data/setContractData.ts index 3b8ebc80f4..371175e72d 100644 --- a/e2e/packages/sync-test/data/setContractData.ts +++ b/e2e/packages/sync-test/data/setContractData.ts @@ -2,7 +2,7 @@ import { Page } from "@playwright/test"; import { Data } from "./types"; import { encodeTestData } from "./encodeTestData"; import { callWorld } from "./callWorld"; -import { resourceIdToHex } from "@latticexyz/common"; +import { resourceToHex } from "@latticexyz/common"; /** * Writes contract data by calling `world.setRecord` via the client @@ -14,7 +14,7 @@ export async function setContractData(page: Page, data: Data) { for (const record of records) { const promise = await callWorld(page, "setRecord", [ // TODO: add support for multiple namespaces after https://github.com/latticexyz/mud/issues/994 is resolved - resourceIdToHex({ type: "table", namespace: "", name: table }), + resourceToHex({ type: "table", namespace: "", name: table }), record.key, record.staticData, record.encodedLengths, diff --git a/packages/cli/src/commands/trace.ts b/packages/cli/src/commands/trace.ts index faca38ed3f..f7d2612957 100644 --- a/packages/cli/src/commands/trace.ts +++ b/packages/cli/src/commands/trace.ts @@ -9,12 +9,12 @@ import { StoreConfig } from "@latticexyz/store"; import { resolveWorldConfig, WorldConfig } from "@latticexyz/world"; import IBaseWorldAbi from "@latticexyz/world/out/IBaseWorld.sol/IBaseWorld.abi.json" assert { type: "json" }; import worldConfig from "@latticexyz/world/mud.config.js"; -import { resourceIdToHex } from "@latticexyz/common"; +import { resourceToHex } from "@latticexyz/common"; import { getExistingContracts } from "../utils/getExistingContracts"; import { getChainId } from "../utils/utils/getChainId"; // TODO account for multiple namespaces (https://github.com/latticexyz/mud/issues/994) -const systemsTableId = resourceIdToHex({ +const systemsTableId = resourceToHex({ type: "system", namespace: worldConfig.namespace, name: worldConfig.tables.Systems.name, @@ -80,7 +80,7 @@ const commandModule: CommandModule = { const systemTableFieldLayout = await WorldContract.getFieldLayout(systemsTableId); const labels: { name: string; address: string }[] = []; for (const name of names) { - const systemSelector = resourceIdToHex({ type: "system", namespace, name }); + const systemSelector = resourceToHex({ type: "system", namespace, name }); // Get the first field of `Systems` table (the table maps system name to its address and other data) const address = await WorldContract.getField(systemsTableId, [systemSelector], 0, systemTableFieldLayout); labels.push({ name, address }); diff --git a/packages/cli/src/utils/deploy.ts b/packages/cli/src/utils/deploy.ts index ca27aa2ba4..fd95d13ac2 100644 --- a/packages/cli/src/utils/deploy.ts +++ b/packages/cli/src/utils/deploy.ts @@ -22,7 +22,7 @@ import { getContractData } from "./utils/getContractData"; import { postDeploy } from "./utils/postDeploy"; import { setInternalFeePerGas } from "./utils/setInternalFeePerGas"; import { ContractCode } from "./utils/types"; -import { resourceIdToHex } from "@latticexyz/common"; +import { resourceToHex } from "@latticexyz/common"; export interface DeployConfig { profile?: string; @@ -151,7 +151,7 @@ export async function deploy( nonce: nonce++, contract: worldContract, func: "registerNamespace", - args: [resourceIdToHex({ type: "namespace", namespace: mudConfig.namespace, name: "" })], + args: [resourceToHex({ type: "namespace", namespace: mudConfig.namespace, name: "" })], }); console.log(chalk.green("Namespace registered")); } diff --git a/packages/cli/src/utils/systems/getGrantAccessCallData.ts b/packages/cli/src/utils/systems/getGrantAccessCallData.ts index e5d50b84ad..4dd97c23c3 100644 --- a/packages/cli/src/utils/systems/getGrantAccessCallData.ts +++ b/packages/cli/src/utils/systems/getGrantAccessCallData.ts @@ -1,6 +1,6 @@ import { System } from "./types"; import { CallData } from "../utils/types"; -import { resourceIdToHex } from "@latticexyz/common"; +import { resourceToHex } from "@latticexyz/common"; export async function getGrantAccessCallData(input: { systems: System[]; @@ -24,6 +24,6 @@ export async function getGrantAccessCallData(input: { function getGrantSystemAccessCallData(name: string, namespace: string, address: string): CallData { return { func: "grantAccess", - args: [resourceIdToHex({ type: "system", namespace, name }), address], + args: [resourceToHex({ type: "system", namespace, name }), address], }; } diff --git a/packages/cli/src/utils/systems/getRegisterFunctionSelectorsCallData.ts b/packages/cli/src/utils/systems/getRegisterFunctionSelectorsCallData.ts index 027714eb55..4721138ce3 100644 --- a/packages/cli/src/utils/systems/getRegisterFunctionSelectorsCallData.ts +++ b/packages/cli/src/utils/systems/getRegisterFunctionSelectorsCallData.ts @@ -1,4 +1,4 @@ -import { resourceIdToHex } from "@latticexyz/common"; +import { resourceToHex } from "@latticexyz/common"; import { System } from "./types"; import { loadFunctionSignatures, toFunctionSelector } from "./utils"; import { CallData } from "../utils/types"; @@ -46,12 +46,12 @@ function getRegisterFunctionSelectorCallData(input: { const functionSelector = toFunctionSelector(systemFunctionSignature); return { func: "registerRootFunctionSelector", - args: [resourceIdToHex({ type: "system", namespace, name }), systemFunctionSignature, functionSelector], + args: [resourceToHex({ type: "system", namespace, name }), systemFunctionSignature, functionSelector], }; } else { return { func: "registerFunctionSelector", - args: [resourceIdToHex({ type: "system", namespace, name }), systemFunctionSignature], + args: [resourceToHex({ type: "system", namespace, name }), systemFunctionSignature], }; } } diff --git a/packages/cli/src/utils/systems/getRegisterSystemCallData.ts b/packages/cli/src/utils/systems/getRegisterSystemCallData.ts index 4595b76ddb..8331311b37 100644 --- a/packages/cli/src/utils/systems/getRegisterSystemCallData.ts +++ b/packages/cli/src/utils/systems/getRegisterSystemCallData.ts @@ -1,4 +1,4 @@ -import { resourceIdToHex } from "@latticexyz/common"; +import { resourceToHex } from "@latticexyz/common"; import { System } from "./types"; import { CallData } from "../utils/types"; @@ -12,6 +12,6 @@ export async function getRegisterSystemCallData(input: { const systemAddress = await systemContracts[systemKey]; return { func: "registerSystem", - args: [resourceIdToHex({ type: "system", namespace, name: system.name }), systemAddress, system.openAccess], + args: [resourceToHex({ type: "system", namespace, name: system.name }), systemAddress, system.openAccess], }; } diff --git a/packages/cli/src/utils/tables/getRegisterTableCallData.ts b/packages/cli/src/utils/tables/getRegisterTableCallData.ts index 594c9a54b6..50621c9af2 100644 --- a/packages/cli/src/utils/tables/getRegisterTableCallData.ts +++ b/packages/cli/src/utils/tables/getRegisterTableCallData.ts @@ -1,7 +1,7 @@ import { encodeSchema, getStaticByteLength } from "@latticexyz/schema-type/deprecated"; import { StoreConfig } from "@latticexyz/store"; import { resolveAbiOrUserType } from "@latticexyz/store/codegen"; -import { resourceIdToHex } from "@latticexyz/common"; +import { resourceToHex } from "@latticexyz/common"; import { Table } from "./types"; import { fieldLayoutToHex } from "@latticexyz/protocol-parser"; import { CallData } from "../utils/types"; @@ -38,7 +38,7 @@ export function getRegisterTableCallData( func: "registerTable", args: [ // TODO: add support for table namespaces (https://github.com/latticexyz/mud/issues/994) - resourceIdToHex({ type: table.offchainOnly ? "offchainTable" : "table", namespace: storeConfig.namespace, name }), + resourceToHex({ type: table.offchainOnly ? "offchainTable" : "table", namespace: storeConfig.namespace, name }), fieldLayoutToHex(fieldLayout), encodeSchema(keyTypes), encodeSchema(schemaTypes), diff --git a/packages/cli/src/utils/tables/getTableIds.ts b/packages/cli/src/utils/tables/getTableIds.ts index fe771cc852..5002587783 100644 --- a/packages/cli/src/utils/tables/getTableIds.ts +++ b/packages/cli/src/utils/tables/getTableIds.ts @@ -1,13 +1,13 @@ import { StoreConfig } from "@latticexyz/store"; import { TableIds } from "./types"; -import { resourceIdToHex } from "@latticexyz/common"; +import { resourceToHex } from "@latticexyz/common"; import { hexToBytes } from "viem"; export function getTableIds(storeConfig: StoreConfig): TableIds { const tableIds: TableIds = {}; for (const [tableName, { name, offchainOnly }] of Object.entries(storeConfig.tables)) { tableIds[tableName] = hexToBytes( - resourceIdToHex({ + resourceToHex({ type: offchainOnly ? "offchainTable" : "table", namespace: storeConfig.namespace, name, diff --git a/packages/common/package.json b/packages/common/package.json index 54c39676cf..3a6720720e 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -13,7 +13,6 @@ ".": "./dist/index.js", "./chains": "./dist/chains.js", "./codegen": "./dist/codegen.js", - "./deprecated": "./dist/deprecated.js", "./errors": "./dist/errors.js", "./foundry": "./dist/foundry.js", "./type-utils": "./dist/type-utils.js", @@ -30,9 +29,6 @@ "codegen": [ "./src/codegen/index.ts" ], - "deprecated": [ - "./src/deprecated/index.ts" - ], "errors": [ "./src/errors/index.ts" ], diff --git a/packages/common/src/common.ts b/packages/common/src/common.ts index dd2cdf3162..04710d0405 100644 --- a/packages/common/src/common.ts +++ b/packages/common/src/common.ts @@ -1,7 +1,9 @@ +import { Hex } from "viem"; import { ResourceType } from "./resourceTypes"; -export type ResourceId = { +export type Resource = { + resourceId: Hex; + type: ResourceType; namespace: string; name: string; - type: ResourceType; }; diff --git a/packages/common/src/deprecated/TableId.test.ts b/packages/common/src/deprecated/TableId.test.ts deleted file mode 100644 index 30ad2a877a..0000000000 --- a/packages/common/src/deprecated/TableId.test.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { describe, it, expect } from "vitest"; -import { TableId } from "./TableId"; - -describe("TableId", () => { - it("can convert to hex string", () => { - const tableId = new TableId("namespace", "name"); - expect(tableId.toHex()).toMatchInlineSnapshot( - '"0x6e616d657370616365000000000000006e616d65000000000000000000000000"' - ); - }); - - it("can convert from hex string", () => { - const tableId = TableId.fromHex("0x6e616d657370616365000000000000006e616d65000000000000000000000000"); - expect(tableId.namespace).toMatchInlineSnapshot('"namespace"'); - expect(tableId.name).toMatchInlineSnapshot('"name"'); - }); - - it("truncates namespaces >16 bytes", () => { - const hex = "0x41566572794c6f6e674e616d657370616e616d65000000000000000000000000"; - expect(TableId.toHex("AVeryLongNamespace", "name")).toEqual(hex); - const tableId = new TableId("AVeryLongNamespace", "name"); - expect(tableId.toHex()).toEqual(hex); - expect(TableId.fromHex(tableId.toHex()).namespace).toMatchInlineSnapshot('"AVeryLongNamespa"'); - }); - - it("truncates names >16 bytes", () => { - const hex = "0x6e616d65737061636500000000000000416e556e6e65636573736172696c794c"; - expect(TableId.toHex("namespace", "AnUnnecessarilyLongName")).toEqual(hex); - const tableId = new TableId("namespace", "AnUnnecessarilyLongName"); - expect(tableId.toHex()).toEqual(hex); - expect(TableId.fromHex(tableId.toHex()).name).toMatchInlineSnapshot('"AnUnnecessarilyL"'); - }); -}); diff --git a/packages/common/src/deprecated/TableId.ts b/packages/common/src/deprecated/TableId.ts deleted file mode 100644 index 9fafd268ae..0000000000 --- a/packages/common/src/deprecated/TableId.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { Hex, stringToHex, hexToString, sliceHex, concatHex } from "viem"; - -/** @deprecated Use `tableIdToHex` and `hexToTableId` instead. */ -export class TableId { - readonly namespace: string; - readonly name: string; - - constructor(namespace: string, name: string) { - this.namespace = namespace.substring(0, 16); - this.name = name.substring(0, 16); - } - - toString(): string { - return `TableId<${this.namespace || "[empty]"}:${this.name || "[empty]"}>`; - } - - toHex(): Hex { - return TableId.toHex(this.namespace, this.name); - } - - static toHex(namespace: string, name: string): Hex { - return concatHex([ - stringToHex(namespace.substring(0, 16), { size: 16 }), - stringToHex(name.substring(0, 16), { size: 16 }), - ]); - } - - static fromHex(hex: Hex): TableId { - const namespace = hexToString(sliceHex(hex, 0, 16)).replace(/\0+$/, ""); - const name = hexToString(sliceHex(hex, 16, 32)).replace(/\0+$/, ""); - return new TableId(namespace, name); - } - - /** @deprecated Don't use this! This is a temporary hack for v2<>v1 compatibility until we can write v2 client libraries. This is here so it stays close to the formatting of `toString()` above. */ - static parse(tableIdString: string): TableId | null { - const match = tableIdString.match(/^TableId<(.+?):(.+?)>$/); - if (!match) return null; - const [, namespace, name] = match; - return new TableId(namespace === "[empty]" ? "" : namespace, name === "[empty]" ? "" : name); - } -} diff --git a/packages/common/src/createContract.ts b/packages/common/src/deprecated/createContract.ts similarity index 65% rename from packages/common/src/createContract.ts rename to packages/common/src/deprecated/createContract.ts index 04404ea27d..9d2cb90edc 100644 --- a/packages/common/src/createContract.ts +++ b/packages/common/src/deprecated/createContract.ts @@ -1,4 +1,4 @@ -import { getContract } from "./getContract"; +import { getContract } from "../getContract"; /** @deprecated use `getContract` instead */ export const createContract = getContract; diff --git a/packages/common/src/deprecated/getTableIds.ts b/packages/common/src/deprecated/getTableIds.ts deleted file mode 100644 index 3e77c665fa..0000000000 --- a/packages/common/src/deprecated/getTableIds.ts +++ /dev/null @@ -1,6 +0,0 @@ -import { TableId } from "./TableId"; - -/** @deprecated Use `tableIdToHex` and `hexToTableId` instead. */ -export function getTableIds(config: { namespace: string; tables: { [key: string]: unknown } }): TableId[] { - return Object.keys(config.tables).map((table) => new TableId(config.namespace, table)); -} diff --git a/packages/common/src/deprecated/hexToResourceId.ts b/packages/common/src/deprecated/hexToResourceId.ts new file mode 100644 index 0000000000..a77af4412a --- /dev/null +++ b/packages/common/src/deprecated/hexToResourceId.ts @@ -0,0 +1,4 @@ +import { hexToResource } from "../hexToResource"; + +/** @deprecated use `hexToResource` instead */ +export const hexToResourceId = hexToResource; diff --git a/packages/common/src/deprecated/index.ts b/packages/common/src/deprecated/index.ts deleted file mode 100644 index bf4b7bfeb9..0000000000 --- a/packages/common/src/deprecated/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./getTableIds"; -export * from "./TableId"; diff --git a/packages/common/src/deprecated/resourceIdToHex.ts b/packages/common/src/deprecated/resourceIdToHex.ts new file mode 100644 index 0000000000..810b60b94a --- /dev/null +++ b/packages/common/src/deprecated/resourceIdToHex.ts @@ -0,0 +1,4 @@ +import { resourceToHex } from "../resourceToHex"; + +/** @deprecated use `resourceToHex` instead */ +export const resourceIdToHex = resourceToHex; diff --git a/packages/common/src/hexToResource.test.ts b/packages/common/src/hexToResource.test.ts new file mode 100644 index 0000000000..6f81ecc4ca --- /dev/null +++ b/packages/common/src/hexToResource.test.ts @@ -0,0 +1,11 @@ +import { describe, it, expect } from "vitest"; +import { hexToResource } from "./hexToResource"; + +describe("hexToResource", () => { + it("can convert from hex string", () => { + const resource = hexToResource("0x74626e616d65737061636500000000006e616d65000000000000000000000000"); + expect(resource.type).toMatchInlineSnapshot('"table"'); + expect(resource.namespace).toMatchInlineSnapshot('"namespace"'); + expect(resource.name).toMatchInlineSnapshot('"name"'); + }); +}); diff --git a/packages/common/src/hexToResourceId.ts b/packages/common/src/hexToResource.ts similarity index 83% rename from packages/common/src/hexToResourceId.ts rename to packages/common/src/hexToResource.ts index 2f45ac2c17..27d14d3809 100644 --- a/packages/common/src/hexToResourceId.ts +++ b/packages/common/src/hexToResource.ts @@ -1,7 +1,7 @@ import { Hex, hexToString, sliceHex } from "viem"; -import { ResourceId } from "./common"; +import { Resource } from "./common"; import { ResourceType, resourceTypes } from "./resourceTypes"; -import { resourceTypeIds } from "./resourceIdToHex"; +import { resourceTypeIds } from "./resourceToHex"; import { ReverseMap } from "./type-utils/common"; const resourceTypeIdToType = Object.fromEntries( @@ -16,7 +16,7 @@ function getResourceType(resourceTypeId: string): ResourceType | undefined { } } -export function hexToResourceId(hex: Hex): ResourceId { +export function hexToResource(hex: Hex): Resource { const resourceTypeId = hexToString(sliceHex(hex, 0, 2)).replace(/\0+$/, ""); const type = getResourceType(resourceTypeId); const namespace = hexToString(sliceHex(hex, 2, 16)).replace(/\0+$/, ""); @@ -26,5 +26,5 @@ export function hexToResourceId(hex: Hex): ResourceId { throw new Error(`Unknown resource type: ${resourceTypeId}`); } - return { type, namespace, name }; + return { resourceId: hex, type, namespace, name }; } diff --git a/packages/common/src/hexToResourceId.test.ts b/packages/common/src/hexToResourceId.test.ts deleted file mode 100644 index 56ebfecfdb..0000000000 --- a/packages/common/src/hexToResourceId.test.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { describe, it, expect } from "vitest"; -import { hexToResourceId } from "./hexToResourceId"; - -describe("hexToResourceId", () => { - it("can convert from hex string", () => { - const resourceId = hexToResourceId("0x74626e616d65737061636500000000006e616d65000000000000000000000000"); - expect(resourceId.type).toMatchInlineSnapshot('"table"'); - expect(resourceId.namespace).toMatchInlineSnapshot('"namespace"'); - expect(resourceId.name).toMatchInlineSnapshot('"name"'); - }); -}); diff --git a/packages/common/src/index.ts b/packages/common/src/index.ts index b772a50ba8..569315b26c 100644 --- a/packages/common/src/index.ts +++ b/packages/common/src/index.ts @@ -5,13 +5,17 @@ export * from "./getContract"; export * from "./getBurnerPrivateKey"; export * from "./getNonceManager"; export * from "./getNonceManagerId"; -export * from "./hexToResourceId"; +export * from "./hexToResource"; export * from "./readHex"; -export * from "./resourceIdToHex"; +export * from "./resourceToHex"; export * from "./resourceTypes"; export * from "./spliceHex"; export * from "./transportObserver"; export * from "./writeContract"; /** @deprecated use `getContract` instead */ -export { createContract } from "./createContract"; +export { createContract } from "./deprecated/createContract"; +/** @deprecated use `resourceToHex` instead */ +export { resourceIdToHex } from "./deprecated/resourceIdToHex"; +/** @deprecated use `hexToResource` instead */ +export { hexToResourceId } from "./deprecated/hexToResourceId"; diff --git a/packages/common/src/resourceIdToHex.test.ts b/packages/common/src/resourceToHex.test.ts similarity index 64% rename from packages/common/src/resourceIdToHex.test.ts rename to packages/common/src/resourceToHex.test.ts index d8a1ca026d..77f82251dc 100644 --- a/packages/common/src/resourceIdToHex.test.ts +++ b/packages/common/src/resourceToHex.test.ts @@ -1,67 +1,71 @@ import { describe, it, expect } from "vitest"; -import { resourceIdToHex } from "./resourceIdToHex"; -import { hexToResourceId } from "./hexToResourceId"; +import { resourceToHex } from "./resourceToHex"; +import { hexToResource } from "./hexToResource"; -describe("resourceIdToHex", () => { +describe("resourceToHex", () => { it("can convert table resource to hex string", () => { - const hex = resourceIdToHex({ + const hex = resourceToHex({ type: "table", namespace: "namespace", name: "name", }); expect(hex).toMatchInlineSnapshot('"0x74626e616d65737061636500000000006e616d65000000000000000000000000"'); - expect(hexToResourceId(hex)).toMatchInlineSnapshot(` + expect(hexToResource(hex)).toMatchInlineSnapshot(` { "name": "name", "namespace": "namespace", + "resourceId": "0x74626e616d65737061636500000000006e616d65000000000000000000000000", "type": "table", } `); }); it("can convert offchain table resource to hex string", () => { - const hex = resourceIdToHex({ + const hex = resourceToHex({ type: "offchainTable", namespace: "namespace", name: "name", }); expect(hex).toMatchInlineSnapshot('"0x6f746e616d65737061636500000000006e616d65000000000000000000000000"'); - expect(hexToResourceId(hex)).toMatchInlineSnapshot(` + expect(hexToResource(hex)).toMatchInlineSnapshot(` { "name": "name", "namespace": "namespace", + "resourceId": "0x6f746e616d65737061636500000000006e616d65000000000000000000000000", "type": "offchainTable", } `); }); it("truncates namespaces >14 bytes", () => { - const hex = resourceIdToHex({ + const hex = resourceToHex({ type: "table", namespace: "AVeryLongNamespace", name: "name", }); expect(hex).toMatchInlineSnapshot('"0x746241566572794c6f6e674e616d65736e616d65000000000000000000000000"'); - expect(hexToResourceId(hex)).toMatchInlineSnapshot(` + expect(hexToResource(hex)).toMatchInlineSnapshot(` { "name": "name", "namespace": "AVeryLongNames", + "resourceId": "0x746241566572794c6f6e674e616d65736e616d65000000000000000000000000", "type": "table", } `); }); it("truncates names >16 bytes", () => { - const hex = resourceIdToHex({ + const hex = resourceToHex({ type: "table", namespace: "namespace", name: "AnUnnecessarilyLongName", }); expect(hex).toMatchInlineSnapshot('"0x74626e616d6573706163650000000000416e556e6e65636573736172696c794c"'); - expect(hexToResourceId(hex)).toMatchInlineSnapshot(` + expect(hexToResource(hex)).toMatchInlineSnapshot(` { "name": "AnUnnecessarilyL", "namespace": "namespace", + "resourceId": "0x74626e616d6573706163650000000000416e556e6e65636573736172696c794c", "type": "table", } `); diff --git a/packages/common/src/resourceIdToHex.ts b/packages/common/src/resourceToHex.ts similarity index 61% rename from packages/common/src/resourceIdToHex.ts rename to packages/common/src/resourceToHex.ts index bebe682aec..39ecd904d9 100644 --- a/packages/common/src/resourceIdToHex.ts +++ b/packages/common/src/resourceToHex.ts @@ -1,5 +1,5 @@ import { Hex, stringToHex, concatHex } from "viem"; -import { ResourceId } from "./common"; +import { Resource } from "./common"; import { ResourceType } from "./resourceTypes"; /** @internal */ @@ -13,11 +13,11 @@ export const resourceTypeIds = { system: "sy", } as const satisfies Record; -export function resourceIdToHex(resourceId: ResourceId): Hex { - const typeId = resourceTypeIds[resourceId.type]; +export function resourceToHex(resource: Omit): Hex { + const typeId = resourceTypeIds[resource.type]; return concatHex([ stringToHex(typeId, { size: 2 }), - stringToHex(resourceId.namespace.slice(0, 14), { size: 14 }), - stringToHex(resourceId.name.slice(0, 16), { size: 16 }), + stringToHex(resource.namespace.slice(0, 14), { size: 14 }), + stringToHex(resource.name.slice(0, 16), { size: 16 }), ]); } diff --git a/packages/common/tsup.config.ts b/packages/common/tsup.config.ts index 4b70cf758d..9432329d41 100644 --- a/packages/common/tsup.config.ts +++ b/packages/common/tsup.config.ts @@ -5,7 +5,6 @@ export default defineConfig({ index: "src/index.ts", chains: "src/chains/index.ts", codegen: "src/codegen/index.ts", - deprecated: "src/deprecated/index.ts", errors: "src/errors/index.ts", foundry: "src/foundry/index.ts", "type-utils": "src/type-utils/index.ts", diff --git a/packages/dev-tools/src/actions/WriteSummary.tsx b/packages/dev-tools/src/actions/WriteSummary.tsx index 77e4d9e227..fc4b011d3c 100644 --- a/packages/dev-tools/src/actions/WriteSummary.tsx +++ b/packages/dev-tools/src/actions/WriteSummary.tsx @@ -9,7 +9,7 @@ import { getTransaction } from "./getTransaction"; import { getTransactionReceipt } from "./getTransactionReceipt"; import { getTransactionResult } from "./getTransactionResult"; import { ErrorTrace } from "../ErrorTrace"; -import { ContractWrite, hexToResourceId } from "@latticexyz/common"; +import { ContractWrite, hexToResource } from "@latticexyz/common"; import { useDevToolsContext } from "../DevToolsContext"; import { hexKeyTupleToEntity } from "@latticexyz/store-sync/recs"; @@ -145,7 +145,7 @@ export function WriteSummary({ write }: Props) { {events.map(({ eventName, args }, i) => { - const table = hexToResourceId((args as any).tableId); + const table = hexToResource((args as any).tableId); // TODO: dedupe this with logs table so we can get both rendering the same return ( diff --git a/packages/dev-tools/src/events/LogsTable.tsx b/packages/dev-tools/src/events/LogsTable.tsx index c9b26b636e..5c969a6469 100644 --- a/packages/dev-tools/src/events/LogsTable.tsx +++ b/packages/dev-tools/src/events/LogsTable.tsx @@ -1,6 +1,6 @@ import { StorageAdapterLog } from "@latticexyz/store-sync"; import { EventIcon } from "./EventIcon"; -import { hexToResourceId } from "@latticexyz/common"; +import { hexToResource } from "@latticexyz/common"; // TODO: use react-table or similar for better perf with lots of logs @@ -22,7 +22,7 @@ export function LogsTable({ logs }: Props) { {logs.map((log) => { - const { namespace, name } = hexToResourceId(log.args.tableId); + const { namespace, name } = hexToResource(log.args.tableId); return ( - resourceIdToHex({ + resourceToHex({ type: storeConfig.tables[name as keyof typeof storeConfig.tables].offchainOnly ? "offchainTable" : "table", namespace: storeConfig.namespace, name, @@ -13,7 +13,7 @@ export const storeTableIds = Object.keys(storeConfig.tables).map((name) => ); const worldTableIds = Object.keys(worldConfig.tables).map((name) => - resourceIdToHex({ + resourceToHex({ type: worldConfig.tables[name as keyof typeof worldConfig.tables].offchainOnly ? "offchainTable" : "table", namespace: worldConfig.namespace, name, diff --git a/packages/store-sync/src/logToTable.ts b/packages/store-sync/src/logToTable.ts index 5dcdfd29e6..362329f684 100644 --- a/packages/store-sync/src/logToTable.ts +++ b/packages/store-sync/src/logToTable.ts @@ -1,7 +1,7 @@ import { hexToSchema, decodeValue, ValueSchema } from "@latticexyz/protocol-parser"; import { Hex, concatHex, decodeAbiParameters, parseAbiParameters } from "viem"; import { StorageAdapterLog, Table, schemasTable } from "./common"; -import { hexToResourceId } from "@latticexyz/common"; +import { hexToResource } from "@latticexyz/common"; // TODO: add tableToLog @@ -11,7 +11,7 @@ export function logToTable(log: StorageAdapterLog & { eventName: "Store_SetRecor console.warn("registerSchema event is expected to have only one key in key tuple, but got multiple", log); } - const table = hexToResourceId(tableId); + const table = hexToResource(tableId); const value = decodeValue( // TODO: remove cast when we have strong types for user types diff --git a/packages/store-sync/src/postgres/getTableKey.ts b/packages/store-sync/src/postgres/getTableKey.ts index e27ce16b58..c1d08a0c29 100644 --- a/packages/store-sync/src/postgres/getTableKey.ts +++ b/packages/store-sync/src/postgres/getTableKey.ts @@ -1,8 +1,8 @@ import { getAddress } from "viem"; import { Table } from "../common"; -import { hexToResourceId } from "@latticexyz/common"; +import { hexToResource } from "@latticexyz/common"; export function getTableKey({ address, tableId }: Pick): string { - const { namespace, name } = hexToResourceId(tableId); + const { namespace, name } = hexToResource(tableId); return `${getAddress(address)}:${namespace}:${name}`; } diff --git a/packages/store-sync/src/postgres/postgresStorage.ts b/packages/store-sync/src/postgres/postgresStorage.ts index 127889a855..36d4d60d6c 100644 --- a/packages/store-sync/src/postgres/postgresStorage.ts +++ b/packages/store-sync/src/postgres/postgresStorage.ts @@ -7,7 +7,7 @@ import { debug } from "./debug"; import { buildInternalTables } from "./buildInternalTables"; import { getTables } from "./getTables"; import { schemaVersion } from "./schemaVersion"; -import { hexToResourceId, spliceHex } from "@latticexyz/common"; +import { hexToResource, spliceHex } from "@latticexyz/common"; import { setupTables } from "./setupTables"; import { getTableKey } from "./getTableKey"; import { StorageAdapter, StorageAdapterBlock } from "../common"; @@ -85,7 +85,7 @@ export async function postgresStorage (table) => getTableKey(table) === getTableKey({ address: log.address, tableId: log.args.tableId }) ); if (!table) { - const { namespace, name } = hexToResourceId(log.args.tableId); + const { namespace, name } = hexToResource(log.args.tableId); debug(`table ${namespace}:${name} not found, skipping log`, log); continue; } diff --git a/packages/store-sync/src/recs/configToRecsComponents.ts b/packages/store-sync/src/recs/configToRecsComponents.ts index 0e83de5a8b..1b22f69a98 100644 --- a/packages/store-sync/src/recs/configToRecsComponents.ts +++ b/packages/store-sync/src/recs/configToRecsComponents.ts @@ -1,6 +1,6 @@ import { StoreConfig } from "@latticexyz/store"; import { SchemaAbiType } from "@latticexyz/schema-type"; -import { resourceIdToHex } from "@latticexyz/common"; +import { resourceToHex } from "@latticexyz/common"; import { World, defineComponent, Type } from "@latticexyz/recs"; import { ConfigToRecsComponents } from "./common"; import { schemaAbiTypeToRecsType } from "./schemaAbiTypeToRecsType"; @@ -27,7 +27,7 @@ export function configToRecsComponents( }, { // TODO: support table namespaces https://github.com/latticexyz/mud/issues/994 - id: resourceIdToHex({ + id: resourceToHex({ type: table.offchainOnly ? "offchainTable" : "table", namespace: config.namespace, name: tableName, diff --git a/packages/store-sync/src/recs/recsStorage.ts b/packages/store-sync/src/recs/recsStorage.ts index 54d34c02a5..10eca49779 100644 --- a/packages/store-sync/src/recs/recsStorage.ts +++ b/packages/store-sync/src/recs/recsStorage.ts @@ -3,7 +3,7 @@ import { debug } from "./debug"; import { World as RecsWorld, getComponentValue, hasComponent, removeComponent, setComponent } from "@latticexyz/recs"; import { defineInternalComponents } from "./defineInternalComponents"; import { getTableEntity } from "./getTableEntity"; -import { hexToResourceId, spliceHex } from "@latticexyz/common"; +import { hexToResource, spliceHex } from "@latticexyz/common"; import { decodeValueArgs } from "@latticexyz/protocol-parser"; import { Hex, size } from "viem"; import { isTableRegistrationLog } from "../isTableRegistrationLog"; @@ -65,7 +65,7 @@ export function recsStorage({ } for (const log of logs) { - const { namespace, name } = hexToResourceId(log.args.tableId); + const { namespace, name } = hexToResource(log.args.tableId); const table = getComponentValue( components.RegisteredTables, getTableEntity({ address: log.address, namespace, name }) diff --git a/packages/store-sync/src/sqlite/sqliteStorage.ts b/packages/store-sync/src/sqlite/sqliteStorage.ts index c010d8c8d7..7ea54ea3ec 100644 --- a/packages/store-sync/src/sqlite/sqliteStorage.ts +++ b/packages/store-sync/src/sqlite/sqliteStorage.ts @@ -12,7 +12,7 @@ import { schemaVersion } from "./schemaVersion"; import { StorageAdapter } from "../common"; import { isTableRegistrationLog } from "../isTableRegistrationLog"; import { logToTable } from "../logToTable"; -import { hexToResourceId, spliceHex } from "@latticexyz/common"; +import { hexToResource, spliceHex } from "@latticexyz/common"; import { decodeKey, decodeValueArgs } from "@latticexyz/protocol-parser"; // TODO: upgrade drizzle and use async sqlite interface for consistency @@ -61,7 +61,7 @@ export async function sqliteStorage({ logs.map((log) => JSON.stringify({ address: getAddress(log.address), - ...hexToResourceId(log.args.tableId), + ...hexToResource(log.args.tableId), }) ) ) @@ -87,7 +87,7 @@ export async function sqliteStorage({ (table) => table.address === getAddress(log.address) && table.tableId === log.args.tableId ); if (!table) { - const tableId = hexToResourceId(log.args.tableId); + const tableId = hexToResource(log.args.tableId); debug(`table ${tableId.namespace}:${tableId.name} not found, skipping log`, log); continue; }