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

feat(store): add TS store events/ABI, move around exports #1034

Merged
merged 3 commits into from
Jun 15, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion packages/cli/scripts/generate-test-tables.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from "path";
import { tablegen } from "@latticexyz/store";
import { StoreConfig } from "@latticexyz/store";
import { tablegen } from "@latticexyz/store/codegen";
import { mudConfig } from "@latticexyz/world/register";
import { getSrcDirectory } from "@latticexyz/common/foundry";
import { logError } from "../src/utils/errors";
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/commands/dev-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { anvil, forge, getRpcUrl, getScriptDirectory, getSrcDirectory } from "@l
import chalk from "chalk";
import chokidar from "chokidar";
import { loadConfig, resolveConfigPath } from "@latticexyz/config/node";
import { StoreConfig, tablegen } from "@latticexyz/store";
import { StoreConfig } from "@latticexyz/store";
import { tablegen } from "@latticexyz/store/codegen";
import path from "path";
import { tsgen } from "../render-ts";
import { debounce } from "throttle-debounce";
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/commands/tablegen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from "path";
import type { CommandModule } from "yargs";
import { loadConfig } from "@latticexyz/config/node";
import { StoreConfig, tablegen } from "@latticexyz/store";
import { StoreConfig } from "@latticexyz/store";
import { tablegen } from "@latticexyz/store/codegen";
import { getSrcDirectory } from "@latticexyz/common/foundry";

type Options = {
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/render-ts/recsV1TableOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { StoreConfig, resolveAbiOrUserType } from "@latticexyz/store";
import { StoreConfig } from "@latticexyz/store";
import { resolveAbiOrUserType } from "@latticexyz/store/codegen";
import { schemaTypesToRecsTypeStrings } from "./schemaTypesToRecsTypeStrings";
import { RecsV1TableOptions } from "./types";

Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/utils/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { getOutDirectory, getScriptDirectory, cast, forge } from "@latticexyz/co
import { resolveWithContext } from "@latticexyz/config";
import { MUDError } from "@latticexyz/common/errors";
import { encodeSchema } from "@latticexyz/schema-type";
import { StoreConfig, resolveAbiOrUserType } from "@latticexyz/store";
import { StoreConfig } from "@latticexyz/store";
import { resolveAbiOrUserType } from "@latticexyz/store/codegen";
import { WorldConfig, resolveWorldConfig } from "@latticexyz/world";
import { IBaseWorld } from "@latticexyz/world/types/ethers-contracts/IBaseWorld";

Expand Down
17 changes: 11 additions & 6 deletions packages/store/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,26 @@
"license": "MIT",
"type": "module",
"exports": {
".": "./dist/ts/library/index.js",
".": "./dist/ts/index.js",
"./codegen": "./dist/ts/codegen/index.js",
"./config": "./dist/ts/config/index.js",
"./register": "./dist/ts/register/index.js",
"./abi/*": "./abi/*",
"./config": "./dist/ts/library/config/index.js",
"./*": "./dist/*"
},
"typesVersions": {
"*": {
"index": [
"./ts/library/index.ts"
"./ts/index.ts"
],
"register": [
"./ts/register/index.ts"
"codegen": [
"./ts/codegen/index.ts"
],
"config": [
"./ts/library/config/index.ts"
"./ts/config/index.ts"
],
"register": [
"./ts/register/index.ts"
]
}
},
Expand All @@ -52,6 +56,7 @@
"@latticexyz/common": "workspace:*",
"@latticexyz/config": "workspace:*",
"@latticexyz/schema-type": "workspace:*",
"abitype": "0.8.7",
"ethers": "^5.7.2",
"zod": "^3.21.4"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Importing library has no side-effects, unlike register
// (library neither creates nor extends MUDCoreContext when imported)
export * from "./config";
export * from "./render-solidity";

export * from "./storeEvents";
export * from "./storeEventsAbi";
2 changes: 1 addition & 1 deletion packages/store/ts/register/configExtensions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { extendMUDCoreConfig, fromZodErrorCustom } from "@latticexyz/config";
import { ZodError } from "zod";
import { zPluginStoreConfig } from "../library/config";
import { zPluginStoreConfig } from "../config";

extendMUDCoreConfig((config) => {
// This function gets called within mudConfig.
Expand Down
2 changes: 1 addition & 1 deletion packages/store/ts/register/mudConfig.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { mudCoreConfig, MUDCoreUserConfig } from "@latticexyz/config";
import { ExtractUserTypes, StringForUnion } from "@latticexyz/common/type-utils";
import { MUDUserConfig } from "../library";
import { MUDUserConfig } from "..";
import { ExpandMUDUserConfig } from "./typeExtensions";

/** mudCoreConfig wrapper to use generics in some options for better type inference */
Expand Down
4 changes: 2 additions & 2 deletions packages/store/ts/register/typeExtensions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { OrDefaults } from "@latticexyz/common/type-utils";
import { MUDCoreUserConfig } from "@latticexyz/config";
import { ExpandTablesConfig, StoreConfig, StoreUserConfig } from "../library/config";
import { DEFAULTS, PATH_DEFAULTS } from "../library/config/defaults";
import { ExpandTablesConfig, StoreConfig, StoreUserConfig } from "../config";
import { DEFAULTS, PATH_DEFAULTS } from "../config/defaults";

// Inject non-generic options into the core config.
// Re-exporting an interface of an existing module merges them, adding new options to the interface.
Expand Down
3 changes: 2 additions & 1 deletion packages/store/ts/scripts/tablegen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from "path";
import { loadConfig } from "@latticexyz/config/node";
import { getSrcDirectory } from "@latticexyz/common/foundry";
import { tablegen, StoreConfig } from "../library";
import { tablegen } from "../codegen";
import { StoreConfig } from "..";

const config = (await loadConfig()) as StoreConfig;
const srcDir = await getSrcDirectory();
Expand Down
6 changes: 6 additions & 0 deletions packages/store/ts/storeEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export const storeEvents = [
"event StoreDeleteRecord(bytes32 table, bytes32[] key)",
"event StoreSetField(bytes32 table, bytes32[] key, uint8 schemaIndex, bytes data)",
"event StoreSetRecord(bytes32 table, bytes32[] key, bytes data)",
"event StoreEphemeralRecord(bytes32 table, bytes32[] key, bytes data)",
] as const;
Copy link
Member Author

Choose a reason for hiding this comment

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

this is the main thing I wanted to import from another package into the browser bundle

27 changes: 27 additions & 0 deletions packages/store/ts/storeEventsAbi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { describe, expect, it } from "vitest";
import { storeEventsAbi } from "./storeEventsAbi";
import { IStore__factory } from "../types/ethers-contracts";

// Make sure `storeEvents` stays in sync with Solidity definition/events

describe("storeEventsAbi", () => {
it("should match the store ABI", () => {
const expectedAbi = IStore__factory.abi
.filter((item) => item.type === "event")
.map((item) => ({
// transform because typechain adds a bunch of data that abitype doesn't care about
type: item.type,
name: item.name,
inputs: [
...item.inputs.map((input) => ({
name: input.name,
type: input.type,
})),
],
}))
.sort((a, b) => a.name.localeCompare(b.name));

const sortedStoreEventsAbi = [...storeEventsAbi].sort((a, b) => a.name.localeCompare(b.name));
expect(sortedStoreEventsAbi).toStrictEqual(expectedAbi);
});
});
4 changes: 4 additions & 0 deletions packages/store/ts/storeEventsAbi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { parseAbi, AbiEvent } from "abitype";
import { storeEvents } from "./storeEvents";

export const storeEventsAbi = parseAbi(storeEvents) satisfies readonly AbiEvent[];
2 changes: 1 addition & 1 deletion packages/store/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from "tsup";

export default defineConfig({
entry: ["mud.config.ts", "ts/library/index.ts", "ts/register/index.ts", "ts/library/config/index.ts"],
entry: ["mud.config.ts", "ts/index.ts", "ts/codegen/index.ts", "ts/config/index.ts", "ts/register/index.ts"],
target: "esnext",
format: ["esm"],
dts: false,
Expand Down
2 changes: 1 addition & 1 deletion packages/world/ts/plugins/snapsync/configExtensions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { extendMUDCoreConfig, resolveTableId } from "@latticexyz/config";
import { zPluginStoreConfig } from "@latticexyz/store/config";
import { zPluginStoreConfig } from "@latticexyz/store";
import { zSnapSyncPluginConfig } from "./plugin";
import { zPluginWorldConfig } from "../../library";

Expand Down
3 changes: 2 additions & 1 deletion packages/world/ts/scripts/tablegen.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import path from "path";
import { loadConfig } from "@latticexyz/config/node";
import { getSrcDirectory } from "@latticexyz/common/foundry";
import { StoreConfig, tablegen } from "@latticexyz/store";
import { StoreConfig } from "@latticexyz/store";
import { tablegen } from "@latticexyz/store/codegen";

const config = (await loadConfig()) as StoreConfig;
const srcDir = await getSrcDirectory();
Expand Down
10 changes: 7 additions & 3 deletions pnpm-lock.yaml

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