Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move some utils to common, clean up #1068

Merged
merged 5 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions e2e/packages/client-vanilla/src/mud/contractComponents.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Autogenerated file. Do not edit manually. */

import { TableId } from "@latticexyz/utils";
import { TableId } from "@latticexyz/common";
import { defineComponent, Type as RecsType, World } from "@latticexyz/recs";

export function defineContractComponents(world: World) {
Expand All @@ -14,7 +14,7 @@ export function defineContractComponents(world: World) {
},
{
metadata: {
contractId: tableId.toHexString(),
contractId: tableId.toHex(),
tableId: tableId.toString(),
},
}
Expand Down
2 changes: 1 addition & 1 deletion e2e/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Autogenerated file. Do not edit manually. */

import { TableId } from "@latticexyz/utils";
import { TableId } from "@latticexyz/common";
import { defineComponent, Type as RecsType, World } from "@latticexyz/recs";

export function defineContractComponents(world: World) {
Expand All @@ -14,7 +14,7 @@ export function defineContractComponents(world: World) {
},
{
metadata: {
contractId: tableId.toHexString(),
contractId: tableId.toHex(),
tableId: tableId.toString(),
},
}
Expand All @@ -29,7 +29,7 @@ export function defineContractComponents(world: World) {
},
{
metadata: {
contractId: tableId.toHexString(),
contractId: tableId.toHex(),
tableId: tableId.toString(),
},
}
Expand All @@ -44,7 +44,7 @@ export function defineContractComponents(world: World) {
},
{
metadata: {
contractId: tableId.toHexString(),
contractId: tableId.toHex(),
tableId: tableId.toString(),
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Autogenerated file. Do not edit manually. */

import { TableId } from "@latticexyz/utils";
import { TableId } from "@latticexyz/common";
import { defineComponent, Type as RecsType, World } from "@latticexyz/recs";

export function defineContractComponents(world: World) {
Expand All @@ -14,7 +14,7 @@ export function defineContractComponents(world: World) {
},
{
metadata: {
contractId: tableId.toHexString(),
contractId: tableId.toHex(),
tableId: tableId.toString(),
},
}
Expand All @@ -29,7 +29,7 @@ export function defineContractComponents(world: World) {
},
{
metadata: {
contractId: tableId.toHexString(),
contractId: tableId.toHex(),
tableId: tableId.toString(),
},
}
Expand All @@ -44,7 +44,7 @@ export function defineContractComponents(world: World) {
},
{
metadata: {
contractId: tableId.toHexString(),
contractId: tableId.toHex(),
tableId: tableId.toString(),
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Autogenerated file. Do not edit manually. */

import { TableId } from "@latticexyz/utils";
import { TableId } from "@latticexyz/common";
import { defineComponent, Type as RecsType, World } from "@latticexyz/recs";

export function defineContractComponents(world: World) {
Expand All @@ -14,7 +14,7 @@ export function defineContractComponents(world: World) {
},
{
metadata: {
contractId: tableId.toHexString(),
contractId: tableId.toHex(),
tableId: tableId.toString(),
},
}
Expand All @@ -29,7 +29,7 @@ export function defineContractComponents(world: World) {
},
{
metadata: {
contractId: tableId.toHexString(),
contractId: tableId.toHex(),
tableId: tableId.toString(),
},
}
Expand All @@ -44,7 +44,7 @@ export function defineContractComponents(world: World) {
},
{
metadata: {
contractId: tableId.toHexString(),
contractId: tableId.toHex(),
tableId: tableId.toString(),
},
}
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/commands/trace.ts
Original file line number Diff line number Diff line change
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/utils";
import { TableId } from "@latticexyz/common";
import { StoreConfig } from "@latticexyz/store";
import { resolveWorldConfig, WorldConfig } from "@latticexyz/world";
import { IBaseWorld } from "@latticexyz/world/types/ethers-contracts/IBaseWorld";
Expand Down Expand Up @@ -74,7 +74,7 @@ const commandModule: CommandModule<Options, Options> = {
for (const name of names) {
const systemSelector = new TableId(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.toHexString(), [systemSelector.toHexString()], 0);
const address = await WorldContract.getField(systemsTableId.toHex(), [systemSelector.toHex()], 0);
labels.push({ name, address });
}

Expand Down
4 changes: 2 additions & 2 deletions packages/cli/src/render-ts/renderRecsV1Tables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export function renderRecsV1Tables(options: RecsV1TableOptions) {

return `/* Autogenerated file. Do not edit manually. */

import { TableId } from "@latticexyz/utils";
import { TableId } from "@latticexyz/common";
import { defineComponent, Type as RecsType, World } from "@latticexyz/recs";

export function defineContractComponents(world: World) {
Expand All @@ -24,7 +24,7 @@ function renderDefineComponent(table: RecsV1TableOptions["tables"][number]) {
return defineComponent(world, {
${table.fields.map(({ name, recsTypeString }) => `${name}: ${recsTypeString},`).join("")}
}, {
metadata: { contractId: tableId.toHexString(), tableId: tableId.toString() },
metadata: { contractId: tableId.toHex(), tableId: tableId.toString() },
});
})()
`;
Expand Down
6 changes: 6 additions & 0 deletions packages/common/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": ["../../.eslintrc"],
"rules": {
"@typescript-eslint/explicit-function-return-type": "error"
}
}
9 changes: 7 additions & 2 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"license": "MIT",
"type": "module",
"exports": {
".": "./dist/index.js",
"./codegen": "./dist/codegen.js",
"./foundry": "./dist/foundry.js",
"./type-utils": "./dist/type-utils.js",
Expand All @@ -19,6 +20,9 @@
},
"typesVersions": {
"*": {
"index": [
"./src/index.ts"
],
"codegen": [
"./src/codegen/index.ts"
],
Expand All @@ -45,15 +49,16 @@
"clean": "pnpm run clean:js",
"clean:js": "rimraf dist",
"dev": "tsup --watch",
"test": "vitest typecheck --run --passWithNoTests"
"test": "vitest typecheck --run --passWithNoTests && vitest --run"
},
"dependencies": {
"@latticexyz/schema-type": "workspace:*",
"@solidity-parser/parser": "^0.16.0",
"chalk": "^5.2.0",
"execa": "^7.0.0",
"prettier": "^2.8.4",
"prettier-plugin-solidity": "^1.1.2"
"prettier-plugin-solidity": "^1.1.2",
"viem": "1.0.6"
},
"devDependencies": {
"@types/node": "^18.15.11",
Expand Down
35 changes: 35 additions & 0 deletions packages/common/src/TableId.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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("throws when converting namespaces >16 bytes", () => {
const tableId = new TableId("AVeryLongNamespace", "name");
expect(() => tableId.toHex()).toThrowErrorMatchingInlineSnapshot(`
"Size cannot exceed 16 bytes. Given size: 18 bytes.

Version: [email protected]"
`);
});

it("throws when converting names >16 bytes", () => {
const tableId = new TableId("namespace", "AnUnnecessarilyLongName");
expect(() => tableId.toHex()).toThrowErrorMatchingInlineSnapshot(`
"Size cannot exceed 16 bytes. Given size: 23 bytes.

Version: [email protected]"
`);
});

it("can convert from hex string", () => {
const tableId = TableId.fromHex("0x6e616d657370616365000000000000006e616d65000000000000000000000000");
expect(tableId.namespace).toMatchInlineSnapshot('"namespace"');
expect(tableId.name).toMatchInlineSnapshot('"name"');
});
});
37 changes: 37 additions & 0 deletions packages/common/src/TableId.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Hex, stringToHex, hexToString, sliceHex, concatHex } from "viem";

export class TableId {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we rename this to ResourceSelector? Or have a base class ResourceSelector this inherits from? (See #999 (comment))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohh I like that better, will do

Copy link
Member Author

@holic holic Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, gonna save this for later! in searching the codebase for TableId, we have a lot of matches and so might wanna rethink the boundaries of this (i.e. when to use "resource selector" vs "table ID")

namespace: string;
name: string;

constructor(namespace: string, name: string) {
this.namespace = namespace;
this.name = name;
}

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, { size: 16 }), stringToHex(name, { 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);
}
}
Loading