Skip to content

Commit

Permalink
feat(common): replace TableId with tableIdToHex/hexToTableId (#1258)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Aug 9, 2023
1 parent 0d8cdad commit 6c67332
Show file tree
Hide file tree
Showing 45 changed files with 117 additions and 53 deletions.
10 changes: 10 additions & 0 deletions .changeset/tricky-olives-stare.md
@@ -0,0 +1,10 @@
---
"@latticexyz/cli": patch
"@latticexyz/common": major
"@latticexyz/dev-tools": patch
"@latticexyz/network": patch
"@latticexyz/std-client": patch
"@latticexyz/store-sync": patch
---

Add `tableIdToHex` and `hexToTableId` pure functions and move/deprecate `TableId`.
2 changes: 1 addition & 1 deletion docs/pages/world/modules.mdx
Expand Up @@ -124,7 +124,7 @@ Clients can use snap-sync to get all records on the World, then begin syncing re

```tsx
import { getSnapSyncRecords } from "@latticexyz/network";
import { getTableIds } from "@latticexyz/utils";
import { getTableIds } from "@latticexyz/common/deprecated";

...

Expand Down
2 changes: 1 addition & 1 deletion docs/pages/world/snap-sync.mdx
Expand Up @@ -95,7 +95,7 @@ In the client, we fetch all the records at the current block number then begin s

```tsx
import { getSnapSyncRecords } from "@latticexyz/network";
import { getTableIds } from "@latticexyz/utils";
import { getTableIds } from "@latticexyz/common/deprecated";

...

Expand Down
2 changes: 1 addition & 1 deletion e2e/packages/client-vanilla/src/mud/setupNetwork.ts
Expand Up @@ -6,7 +6,7 @@ import { world } from "./world";
import { Contract, Signer, utils } from "ethers";
import { JsonRpcProvider } from "@ethersproject/providers";
import { IWorld__factory } from "contracts/types/ethers-contracts/factories/IWorld__factory";
import { getTableIds } from "@latticexyz/utils";
import { getTableIds } from "@latticexyz/common/deprecated";
import storeConfig from "contracts/mud.config";

export type SetupNetworkResult = Awaited<ReturnType<typeof setupNetwork>>;
Expand Down
Expand Up @@ -6,7 +6,7 @@ import { world } from "./world";
import { Contract, Signer, utils } from "ethers";
import { JsonRpcProvider } from "@ethersproject/providers";
import { IWorld__factory } from "contracts/types/ethers-contracts/factories/IWorld__factory";
import { getTableIds } from "@latticexyz/utils";
import { getTableIds } from "@latticexyz/common/deprecated";
import storeConfig from "contracts/mud.config";

export type SetupNetworkResult = Awaited<ReturnType<typeof setupNetwork>>;
Expand Down
Expand Up @@ -6,7 +6,7 @@ import { world } from "./world";
import { Contract, Signer, utils } from "ethers";
import { JsonRpcProvider } from "@ethersproject/providers";
import { IWorld__factory } from "contracts/types/ethers-contracts/factories/IWorld__factory";
import { getTableIds } from "@latticexyz/utils";
import { getTableIds } from "@latticexyz/common/deprecated";
import storeConfig from "contracts/mud.config";

export type SetupNetworkResult = Awaited<ReturnType<typeof setupNetwork>>;
Expand Down
Expand Up @@ -6,7 +6,7 @@ import { world } from "./world";
import { Contract, Signer, utils } from "ethers";
import { JsonRpcProvider } from "@ethersproject/providers";
import { IWorld__factory } from "contracts/types/ethers-contracts/factories/IWorld__factory";
import { getTableIds } from "@latticexyz/utils";
import { getTableIds } from "@latticexyz/common/deprecated";
import storeConfig from "contracts/mud.config";

export type SetupNetworkResult = Awaited<ReturnType<typeof setupNetwork>>;
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/trace.ts
Expand Up @@ -5,7 +5,7 @@ import { ethers } from "ethers";
import { loadConfig } from "@latticexyz/config/node";
import { MUDError } from "@latticexyz/common/errors";
import { cast, getRpcUrl, getSrcDirectory } from "@latticexyz/common/foundry";
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { StoreConfig } from "@latticexyz/store";
import { resolveWorldConfig, WorldConfig } from "@latticexyz/world";
import { IBaseWorld } from "@latticexyz/world/types/ethers-contracts/IBaseWorld";
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/render-ts/renderRecsV1Tables.ts
@@ -1,4 +1,4 @@
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { RecsV1TableOptions } from "./types";

export function renderRecsV1Tables(options: RecsV1TableOptions) {
Expand Down
22 changes: 13 additions & 9 deletions packages/common/package.json
Expand Up @@ -11,21 +11,31 @@
"type": "module",
"exports": {
".": "./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",
"./utils": "./dist/utils.js",
"./errors": "./dist/errors.js",
"./chains": "./dist/chains.js"
"./utils": "./dist/utils.js"
},
"typesVersions": {
"*": {
"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"
],
Expand All @@ -34,12 +44,6 @@
],
"utils": [
"./src/utils/index.ts"
],
"errors": [
"./src/errors/index.ts"
],
"chains": [
"./src/chains/index.ts"
]
}
},
Expand Down
File renamed without changes.
@@ -1,5 +1,6 @@
import { Hex, stringToHex, hexToString, sliceHex, concatHex } from "viem";

/** @deprecated Use `tableIdToHex` and `hexToTableId` instead. */
export class TableId {
readonly namespace: string;
readonly name: string;
Expand Down
6 changes: 6 additions & 0 deletions packages/common/src/deprecated/getTableIds.ts
@@ -0,0 +1,6 @@
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));
}
2 changes: 2 additions & 0 deletions packages/common/src/deprecated/index.ts
@@ -0,0 +1,2 @@
export * from "./getTableIds";
export * from "./TableId";
10 changes: 10 additions & 0 deletions packages/common/src/hexToTableId.test.ts
@@ -0,0 +1,10 @@
import { describe, it, expect } from "vitest";
import { hexToTableId } from "./hexToTableId";

describe("hexToTableId", () => {
it("can convert from hex string", () => {
const tableId = hexToTableId("0x6e616d657370616365000000000000006e616d65000000000000000000000000");
expect(tableId.namespace).toMatchInlineSnapshot('"namespace"');
expect(tableId.name).toMatchInlineSnapshot('"name"');
});
});
7 changes: 7 additions & 0 deletions packages/common/src/hexToTableId.ts
@@ -0,0 +1,7 @@
import { Hex, hexToString, sliceHex } from "viem";

export function hexToTableId(hex: Hex): { namespace: string; name: string } {
const namespace = hexToString(sliceHex(hex, 0, 16)).replace(/\0+$/, "");
const name = hexToString(sliceHex(hex, 16, 32)).replace(/\0+$/, "");
return { namespace, name };
}
3 changes: 2 additions & 1 deletion packages/common/src/index.ts
@@ -1,3 +1,4 @@
export * from "./createBurnerAccount";
export * from "./hexToTableId";
export * from "./mudTransportObserver";
export * from "./TableId";
export * from "./tableIdToHex";
24 changes: 24 additions & 0 deletions packages/common/src/tableIdToHex.test.ts
@@ -0,0 +1,24 @@
import { describe, it, expect } from "vitest";
import { tableIdToHex } from "./tableIdToHex";
import { hexToTableId } from "./hexToTableId";

describe("tableIdToHex", () => {
it("can convert to hex string", () => {
const tableIdHex = tableIdToHex("namespace", "name");
expect(tableIdHex).toMatchInlineSnapshot('"0x6e616d657370616365000000000000006e616d65000000000000000000000000"');
});

it("truncates namespaces >16 bytes", () => {
const hex = "0x41566572794c6f6e674e616d657370616e616d65000000000000000000000000";
const tableIdHex = tableIdToHex("AVeryLongNamespace", "name");
expect(tableIdHex).toEqual(hex);
expect(hexToTableId(tableIdHex).namespace).toMatchInlineSnapshot('"AVeryLongNamespa"');
});

it("truncates names >16 bytes", () => {
const hex = "0x6e616d65737061636500000000000000416e556e6e65636573736172696c794c";
const tableIdHex = tableIdToHex("namespace", "AnUnnecessarilyLongName");
expect(tableIdHex).toEqual(hex);
expect(hexToTableId(tableIdHex).name).toMatchInlineSnapshot('"AnUnnecessarilyL"');
});
});
8 changes: 8 additions & 0 deletions packages/common/src/tableIdToHex.ts
@@ -0,0 +1,8 @@
import { Hex, stringToHex, concatHex } from "viem";

export function tableIdToHex(namespace: string, name: string): Hex {
return concatHex([
stringToHex(namespace.substring(0, 16), { size: 16 }),
stringToHex(name.substring(0, 16), { size: 16 }),
]);
}
5 changes: 3 additions & 2 deletions packages/common/tsup.config.ts
Expand Up @@ -3,12 +3,13 @@ import { defineConfig } from "tsup";
export default defineConfig({
entry: {
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",
utils: "src/utils/index.ts",
errors: "src/errors/index.ts",
chains: "src/chains/index.ts",
},
target: "esnext",
format: ["esm"],
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-tools/src/actions/TransactionSummary.tsx
@@ -1,7 +1,7 @@
import { decodeEventLog, decodeFunctionData, Hex, AbiEventSignatureNotFoundError } from "viem";
import { twMerge } from "tailwind-merge";
import { isDefined } from "@latticexyz/common/utils";
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { keyTupleToEntityID } from "@latticexyz/network/dev";
import { useStore } from "../useStore";
import { PendingIcon } from "../icons/PendingIcon";
Expand Down
2 changes: 1 addition & 1 deletion packages/dev-tools/src/tables/useTables.ts
@@ -1,5 +1,5 @@
import { isDefined } from "@latticexyz/common/utils";
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { useStore } from "../useStore";

export function useTables() {
Expand Down
1 change: 1 addition & 0 deletions packages/network/jest.config.js
Expand Up @@ -10,6 +10,7 @@ export default {
// jest can't handle esm imports, so we import the typescript source instead
"^@latticexyz/common$": "<rootDir>/../common/src/index.ts",
"^@latticexyz/common/chains$": "<rootDir>/../common/src/chains/index.ts",
"^@latticexyz/common/deprecated$": "<rootDir>/../common/src/deprecated/index.ts",
"^@latticexyz/common/utils$": "<rootDir>/../common/src/utils/index.ts",
"^@latticexyz/recs$": "<rootDir>/../recs/src/index.ts",
"^@latticexyz/schema-type$": "<rootDir>/../schema-type/src/typescript/index.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/dev/observables.ts
@@ -1,7 +1,7 @@
import { BehaviorSubject, Subject } from "rxjs";
import type { PublicClient, WalletClient, Chain, Transport } from "viem";
import type { CacheStore } from "../workers";
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { StoreEvent, EphemeralEvent } from "../v2/common";

// TODO: connection status?
Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/types.ts
Expand Up @@ -3,7 +3,7 @@ import { ExternalProvider } from "@ethersproject/providers";
import { Components, ComponentValue, Entity, SchemaOf } from "@latticexyz/recs";
import { TxMetadata } from "@latticexyz/services/ecs-stream";
import { Cached } from "@latticexyz/utils";
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { BaseContract, BigNumber, ContractInterface } from "ethers";
import { Observable } from "rxjs";
import { SyncState } from "./workers";
Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/v2/common.ts
@@ -1,4 +1,4 @@
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { SchemaType } from "@latticexyz/schema-type/deprecated";

export const schemaTableId = new TableId("mudstore", "schema");
Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/v2/decodeStoreSetField.ts
@@ -1,5 +1,5 @@
import { ComponentValue } from "@latticexyz/recs";
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { Contract } from "ethers";
import { registerSchema } from "./schemas/tableSchemas";
import { registerMetadata } from "./schemas/tableMetadata";
Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/v2/decodeStoreSetRecord.ts
@@ -1,4 +1,4 @@
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { Contract, utils } from "ethers";
import { registerSchema } from "./schemas/tableSchemas";
import { registerMetadata } from "./schemas/tableMetadata";
Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/v2/ecsEventFromLog.ts
@@ -1,7 +1,7 @@
import { Contract, utils } from "ethers";
import { Log } from "@ethersproject/providers";
import { LogDescription } from "@ethersproject/abi";
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { NetworkComponentUpdate, NetworkEvents } from "../types";
import { decodeStoreSetRecord } from "./decodeStoreSetRecord";
import { decodeStoreSetField } from "./decodeStoreSetField";
Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/v2/mode/syncTablesFromMode.ts
Expand Up @@ -2,7 +2,7 @@ import { ComponentValue } from "@latticexyz/recs";
import { AbiTypeToSchemaType, encodeSchema } from "@latticexyz/schema-type/deprecated";
import { QueryLayerClient } from "@latticexyz/services/mode";
import { arrayToHex } from "@latticexyz/utils";
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { Contract } from "ethers";
import { NetworkEvents } from "../../types";

Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/v2/schemas/tableMetadata.ts
@@ -1,4 +1,4 @@
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { Contract, utils } from "ethers";
import { metadataTableId, schemaTableId, TableMetadata } from "../common";
import { decodeData } from "./decodeData";
Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/v2/schemas/tableSchemas.ts
@@ -1,5 +1,5 @@
import { Contract } from "ethers";
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { TableSchema } from "../common";
import { decodeSchema } from "./decodeSchema";
import { IStore } from "@latticexyz/store/types/ethers-contracts/IStore.sol/IStore";
Expand Down
2 changes: 1 addition & 1 deletion packages/network/src/v2/snapSync/getSnapSyncRecords.ts
@@ -1,4 +1,4 @@
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { Hex } from "viem";
import snapSyncSystemAbi from "./snapSyncSystemAbi";
import { Contract, Signer, providers } from "ethers";
Expand Down
2 changes: 1 addition & 1 deletion packages/std-client/src/setup/setupMUDV2Network.ts
Expand Up @@ -14,7 +14,7 @@ import { BehaviorSubject, concatMap, from, Subject } from "rxjs";
import { Components, defineComponent, Type, World } from "@latticexyz/recs";
import { computed } from "mobx";
import { keccak256 } from "@latticexyz/utils";
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { World as WorldContract } from "@latticexyz/world/types/ethers-contracts/World";
import { IWorldKernel__factory } from "@latticexyz/world/types/ethers-contracts/factories/IWorldKernel.sol/IWorldKernel__factory";
import { defineStringComponent } from "../components";
Expand Down
2 changes: 1 addition & 1 deletion packages/store-sync/src/blockLogsToStorage.test.ts
Expand Up @@ -2,7 +2,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import { BlockLogsToStorageOptions, blockLogsToStorage } from "./blockLogsToStorage";
import storeConfig from "@latticexyz/store/mud.config";
import { isDefined } from "@latticexyz/common/utils";
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";

const mockedCallbacks = {
registerTables: vi.fn<
Expand Down
2 changes: 1 addition & 1 deletion packages/store-sync/src/blockLogsToStorage.ts
Expand Up @@ -7,7 +7,7 @@ import {
TableSchema,
} from "@latticexyz/protocol-parser";
import { StoreConfig, ConfigToKeyPrimitives as Key, ConfigToValuePrimitives as Value } from "@latticexyz/store";
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { Address, Hex, decodeAbiParameters, getAddress, parseAbiParameters } from "viem";
import { debug } from "./debug";
import { isDefined } from "@latticexyz/common/utils";
Expand Down
2 changes: 1 addition & 1 deletion packages/store-sync/src/recs/recsStorage.ts
Expand Up @@ -12,7 +12,7 @@ import {
updateComponent,
} from "@latticexyz/recs";
import { isDefined } from "@latticexyz/common/utils";
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { schemaToDefaults } from "../schemaToDefaults";
import { hexKeyTupleToEntity } from "./hexKeyTupleToEntity";
import { defineInternalComponents } from "./defineInternalComponents";
Expand Down
2 changes: 1 addition & 1 deletion packages/store-sync/src/sqlite/getTables.ts
@@ -1,7 +1,7 @@
import { BaseSQLiteDatabase } from "drizzle-orm/sqlite-core";
import { inArray } from "drizzle-orm";
import { Table } from "../common";
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { getTableName } from "./getTableName";
import { mudStoreTables } from "./internalTables";

Expand Down
2 changes: 1 addition & 1 deletion packages/store-sync/src/sqlite/sqliteStorage.ts
Expand Up @@ -4,7 +4,7 @@ import { and, eq, sql } from "drizzle-orm";
import { sqliteTableToSql } from "./sqliteTableToSql";
import { createSqliteTable } from "./createSqliteTable";
import { schemaToDefaults } from "../schemaToDefaults";
import { TableId } from "@latticexyz/common";
import { TableId } from "@latticexyz/common/deprecated";
import { BlockLogsToStorageOptions } from "../blockLogsToStorage";
import { StoreConfig } from "@latticexyz/store";
import { debug } from "./debug";
Expand Down
10 changes: 0 additions & 10 deletions packages/utils/src/v2/getTableIds.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/utils/src/v2/index.ts
Expand Up @@ -3,4 +3,3 @@ export * from "./bytesToString";
export * from "./hexToArray";
export * from "./isHex";
export * from "./stringToBytes";
export * from "./getTableIds";
2 changes: 1 addition & 1 deletion templates/phaser/packages/client/src/mud/setupNetwork.ts
Expand Up @@ -6,7 +6,7 @@ import { world } from "./world";
import { Contract, Signer, utils } from "ethers";
import { JsonRpcProvider } from "@ethersproject/providers";
import { IWorld__factory } from "contracts/types/ethers-contracts/factories/IWorld__factory";
import { getTableIds } from "@latticexyz/utils";
import { getTableIds } from "@latticexyz/common/deprecated";
import storeConfig from "contracts/mud.config";

export type SetupNetworkResult = Awaited<ReturnType<typeof setupNetwork>>;
Expand Down

0 comments on commit 6c67332

Please sign in to comment.