Skip to content

Commit

Permalink
refactor: move some utils to common, clean up (#1068)
Browse files Browse the repository at this point in the history
* move some utils into common, add linter

* migrate things to new TableId

* clean up

* ugh vscode why

* missed a couple return types
  • Loading branch information
holic authored Jun 23, 2023
1 parent 0f5d310 commit 17ea07b
Show file tree
Hide file tree
Showing 57 changed files with 272 additions and 192 deletions.
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 {
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

0 comments on commit 17ea07b

Please sign in to comment.