From 28fce38cfe3931421646867b1e409e09c8287f0d Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Wed, 14 Jun 2023 21:48:31 -0700 Subject: [PATCH 1/3] refactor(store): move around exports --- packages/cli/scripts/generate-test-tables.ts | 3 ++- packages/cli/src/commands/dev-contracts.ts | 3 ++- packages/cli/src/commands/tablegen.ts | 3 ++- .../cli/src/render-ts/recsV1TableOptions.ts | 3 ++- packages/cli/src/utils/deploy.ts | 3 ++- packages/store/package.json | 17 +++++++----- .../render-solidity => codegen}/ephemeral.ts | 0 .../render-solidity => codegen}/field.ts | 0 .../render-solidity => codegen}/index.ts | 0 .../render-solidity => codegen}/record.ts | 0 .../renderTable.ts | 0 .../renderTableIndex.ts | 0 .../renderTypesFromConfig.ts | 0 .../tableOptions.ts | 0 .../render-solidity => codegen}/tablegen.ts | 0 .../render-solidity => codegen}/types.ts | 0 .../render-solidity => codegen}/userType.ts | 0 .../store/ts/{library => }/config/defaults.ts | 0 .../store/ts/{library => }/config/index.ts | 0 .../config/storeConfig.test-d.ts | 0 .../ts/{library => }/config/storeConfig.ts | 0 packages/store/ts/{library => }/index.ts | 4 ++- .../store/ts/register/configExtensions.ts | 2 +- packages/store/ts/register/mudConfig.ts | 2 +- packages/store/ts/register/typeExtensions.ts | 4 +-- packages/store/ts/scripts/tablegen.ts | 3 ++- packages/store/ts/storeEvents.ts | 6 +++++ packages/store/ts/storeEventsAbi.test.ts | 27 +++++++++++++++++++ packages/store/ts/storeEventsAbi.ts | 4 +++ packages/store/tsup.config.ts | 2 +- .../ts/plugins/snapsync/configExtensions.ts | 2 +- packages/world/ts/scripts/tablegen.ts | 3 ++- pnpm-lock.yaml | 10 ++++--- 33 files changed, 78 insertions(+), 23 deletions(-) rename packages/store/ts/{library/render-solidity => codegen}/ephemeral.ts (100%) rename packages/store/ts/{library/render-solidity => codegen}/field.ts (100%) rename packages/store/ts/{library/render-solidity => codegen}/index.ts (100%) rename packages/store/ts/{library/render-solidity => codegen}/record.ts (100%) rename packages/store/ts/{library/render-solidity => codegen}/renderTable.ts (100%) rename packages/store/ts/{library/render-solidity => codegen}/renderTableIndex.ts (100%) rename packages/store/ts/{library/render-solidity => codegen}/renderTypesFromConfig.ts (100%) rename packages/store/ts/{library/render-solidity => codegen}/tableOptions.ts (100%) rename packages/store/ts/{library/render-solidity => codegen}/tablegen.ts (100%) rename packages/store/ts/{library/render-solidity => codegen}/types.ts (100%) rename packages/store/ts/{library/render-solidity => codegen}/userType.ts (100%) rename packages/store/ts/{library => }/config/defaults.ts (100%) rename packages/store/ts/{library => }/config/index.ts (100%) rename packages/store/ts/{library => }/config/storeConfig.test-d.ts (100%) rename packages/store/ts/{library => }/config/storeConfig.ts (100%) rename packages/store/ts/{library => }/index.ts (70%) create mode 100644 packages/store/ts/storeEvents.ts create mode 100644 packages/store/ts/storeEventsAbi.test.ts create mode 100644 packages/store/ts/storeEventsAbi.ts diff --git a/packages/cli/scripts/generate-test-tables.ts b/packages/cli/scripts/generate-test-tables.ts index 313860fc8d..ec8174f3ee 100644 --- a/packages/cli/scripts/generate-test-tables.ts +++ b/packages/cli/scripts/generate-test-tables.ts @@ -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"; diff --git a/packages/cli/src/commands/dev-contracts.ts b/packages/cli/src/commands/dev-contracts.ts index f770920872..43902e4b82 100644 --- a/packages/cli/src/commands/dev-contracts.ts +++ b/packages/cli/src/commands/dev-contracts.ts @@ -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"; diff --git a/packages/cli/src/commands/tablegen.ts b/packages/cli/src/commands/tablegen.ts index d230494f5d..5bca2b0f3b 100644 --- a/packages/cli/src/commands/tablegen.ts +++ b/packages/cli/src/commands/tablegen.ts @@ -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 = { diff --git a/packages/cli/src/render-ts/recsV1TableOptions.ts b/packages/cli/src/render-ts/recsV1TableOptions.ts index e19739d806..c7bcbd184b 100644 --- a/packages/cli/src/render-ts/recsV1TableOptions.ts +++ b/packages/cli/src/render-ts/recsV1TableOptions.ts @@ -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"; diff --git a/packages/cli/src/utils/deploy.ts b/packages/cli/src/utils/deploy.ts index e1ea413eef..2ba43ec3fe 100644 --- a/packages/cli/src/utils/deploy.ts +++ b/packages/cli/src/utils/deploy.ts @@ -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"; diff --git a/packages/store/package.json b/packages/store/package.json index 9abca9b640..bbf4b22da0 100644 --- a/packages/store/package.json +++ b/packages/store/package.json @@ -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" ] } }, @@ -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" }, diff --git a/packages/store/ts/library/render-solidity/ephemeral.ts b/packages/store/ts/codegen/ephemeral.ts similarity index 100% rename from packages/store/ts/library/render-solidity/ephemeral.ts rename to packages/store/ts/codegen/ephemeral.ts diff --git a/packages/store/ts/library/render-solidity/field.ts b/packages/store/ts/codegen/field.ts similarity index 100% rename from packages/store/ts/library/render-solidity/field.ts rename to packages/store/ts/codegen/field.ts diff --git a/packages/store/ts/library/render-solidity/index.ts b/packages/store/ts/codegen/index.ts similarity index 100% rename from packages/store/ts/library/render-solidity/index.ts rename to packages/store/ts/codegen/index.ts diff --git a/packages/store/ts/library/render-solidity/record.ts b/packages/store/ts/codegen/record.ts similarity index 100% rename from packages/store/ts/library/render-solidity/record.ts rename to packages/store/ts/codegen/record.ts diff --git a/packages/store/ts/library/render-solidity/renderTable.ts b/packages/store/ts/codegen/renderTable.ts similarity index 100% rename from packages/store/ts/library/render-solidity/renderTable.ts rename to packages/store/ts/codegen/renderTable.ts diff --git a/packages/store/ts/library/render-solidity/renderTableIndex.ts b/packages/store/ts/codegen/renderTableIndex.ts similarity index 100% rename from packages/store/ts/library/render-solidity/renderTableIndex.ts rename to packages/store/ts/codegen/renderTableIndex.ts diff --git a/packages/store/ts/library/render-solidity/renderTypesFromConfig.ts b/packages/store/ts/codegen/renderTypesFromConfig.ts similarity index 100% rename from packages/store/ts/library/render-solidity/renderTypesFromConfig.ts rename to packages/store/ts/codegen/renderTypesFromConfig.ts diff --git a/packages/store/ts/library/render-solidity/tableOptions.ts b/packages/store/ts/codegen/tableOptions.ts similarity index 100% rename from packages/store/ts/library/render-solidity/tableOptions.ts rename to packages/store/ts/codegen/tableOptions.ts diff --git a/packages/store/ts/library/render-solidity/tablegen.ts b/packages/store/ts/codegen/tablegen.ts similarity index 100% rename from packages/store/ts/library/render-solidity/tablegen.ts rename to packages/store/ts/codegen/tablegen.ts diff --git a/packages/store/ts/library/render-solidity/types.ts b/packages/store/ts/codegen/types.ts similarity index 100% rename from packages/store/ts/library/render-solidity/types.ts rename to packages/store/ts/codegen/types.ts diff --git a/packages/store/ts/library/render-solidity/userType.ts b/packages/store/ts/codegen/userType.ts similarity index 100% rename from packages/store/ts/library/render-solidity/userType.ts rename to packages/store/ts/codegen/userType.ts diff --git a/packages/store/ts/library/config/defaults.ts b/packages/store/ts/config/defaults.ts similarity index 100% rename from packages/store/ts/library/config/defaults.ts rename to packages/store/ts/config/defaults.ts diff --git a/packages/store/ts/library/config/index.ts b/packages/store/ts/config/index.ts similarity index 100% rename from packages/store/ts/library/config/index.ts rename to packages/store/ts/config/index.ts diff --git a/packages/store/ts/library/config/storeConfig.test-d.ts b/packages/store/ts/config/storeConfig.test-d.ts similarity index 100% rename from packages/store/ts/library/config/storeConfig.test-d.ts rename to packages/store/ts/config/storeConfig.test-d.ts diff --git a/packages/store/ts/library/config/storeConfig.ts b/packages/store/ts/config/storeConfig.ts similarity index 100% rename from packages/store/ts/library/config/storeConfig.ts rename to packages/store/ts/config/storeConfig.ts diff --git a/packages/store/ts/library/index.ts b/packages/store/ts/index.ts similarity index 70% rename from packages/store/ts/library/index.ts rename to packages/store/ts/index.ts index aff09fcf79..d3a12ccc83 100644 --- a/packages/store/ts/library/index.ts +++ b/packages/store/ts/index.ts @@ -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"; diff --git a/packages/store/ts/register/configExtensions.ts b/packages/store/ts/register/configExtensions.ts index 64fd11bac1..3daf8b35ab 100644 --- a/packages/store/ts/register/configExtensions.ts +++ b/packages/store/ts/register/configExtensions.ts @@ -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. diff --git a/packages/store/ts/register/mudConfig.ts b/packages/store/ts/register/mudConfig.ts index 0077a7f942..afc08f9dd5 100644 --- a/packages/store/ts/register/mudConfig.ts +++ b/packages/store/ts/register/mudConfig.ts @@ -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 */ diff --git a/packages/store/ts/register/typeExtensions.ts b/packages/store/ts/register/typeExtensions.ts index 815103d2aa..88dd09dee4 100644 --- a/packages/store/ts/register/typeExtensions.ts +++ b/packages/store/ts/register/typeExtensions.ts @@ -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. diff --git a/packages/store/ts/scripts/tablegen.ts b/packages/store/ts/scripts/tablegen.ts index d5dd1a4302..57e6efc9de 100644 --- a/packages/store/ts/scripts/tablegen.ts +++ b/packages/store/ts/scripts/tablegen.ts @@ -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(); diff --git a/packages/store/ts/storeEvents.ts b/packages/store/ts/storeEvents.ts new file mode 100644 index 0000000000..f65d884db5 --- /dev/null +++ b/packages/store/ts/storeEvents.ts @@ -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; diff --git a/packages/store/ts/storeEventsAbi.test.ts b/packages/store/ts/storeEventsAbi.test.ts new file mode 100644 index 0000000000..80c9ac8098 --- /dev/null +++ b/packages/store/ts/storeEventsAbi.test.ts @@ -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.slice().sort((a, b) => a.name.localeCompare(b.name)); + expect(sortedStoreEventsAbi).toStrictEqual(expectedAbi); + }); +}); diff --git a/packages/store/ts/storeEventsAbi.ts b/packages/store/ts/storeEventsAbi.ts new file mode 100644 index 0000000000..3d383d6e1c --- /dev/null +++ b/packages/store/ts/storeEventsAbi.ts @@ -0,0 +1,4 @@ +import { parseAbi, AbiEvent } from "abitype"; +import { storeEvents } from "./storeEvents"; + +export const storeEventsAbi = parseAbi(storeEvents) satisfies readonly AbiEvent[]; diff --git a/packages/store/tsup.config.ts b/packages/store/tsup.config.ts index 4cc5802af0..e46c0dc59d 100644 --- a/packages/store/tsup.config.ts +++ b/packages/store/tsup.config.ts @@ -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, diff --git a/packages/world/ts/plugins/snapsync/configExtensions.ts b/packages/world/ts/plugins/snapsync/configExtensions.ts index 63f8e0094f..311e9d54e2 100644 --- a/packages/world/ts/plugins/snapsync/configExtensions.ts +++ b/packages/world/ts/plugins/snapsync/configExtensions.ts @@ -1,5 +1,5 @@ import { extendMUDCoreConfig, resolveTableId } from "@latticexyz/config"; -import { zPluginStoreConfig } from "@latticexyz/store/config"; +import { zPluginStoreConfig } from "@latticexyz/store/ts/config"; import { zSnapSyncPluginConfig } from "./plugin"; import { zPluginWorldConfig } from "../../library"; diff --git a/packages/world/ts/scripts/tablegen.ts b/packages/world/ts/scripts/tablegen.ts index f236eb7831..9c77691154 100644 --- a/packages/world/ts/scripts/tablegen.ts +++ b/packages/world/ts/scripts/tablegen.ts @@ -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(); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index be8b39f32b..8ec5832cc1 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -818,7 +818,7 @@ importers: version: link:../world abitype: specifier: 0.8.7 - version: 0.8.7(typescript@4.9.5) + version: 0.8.7(typescript@4.9.5)(zod@3.21.4) ethers: specifier: ^5.7.2 version: 5.7.2 @@ -925,6 +925,9 @@ importers: '@latticexyz/schema-type': specifier: workspace:* version: link:../schema-type + abitype: + specifier: 0.8.7 + version: 0.8.7(typescript@4.9.5)(zod@3.21.4) ethers: specifier: ^5.7.2 version: 5.7.2 @@ -4111,7 +4114,7 @@ packages: engines: {node: '>=6', npm: '>=3'} dev: false - /abitype@0.8.7(typescript@4.9.5): + /abitype@0.8.7(typescript@4.9.5)(zod@3.21.4): resolution: {integrity: sha512-wQ7hV8Yg/yKmGyFpqrNZufCxbszDe5es4AZGYPBitocfSqXtjrTG9JMWFcc4N30ukl2ve48aBTwt7NJxVQdU3w==} peerDependencies: typescript: '>=5.0.4' @@ -4121,6 +4124,7 @@ packages: optional: true dependencies: typescript: 4.9.5 + zod: 3.21.4 dev: false /abitype@0.8.7(typescript@5.0.4): @@ -14546,7 +14550,7 @@ packages: '@scure/bip32': 1.3.0 '@scure/bip39': 1.2.0 '@wagmi/chains': 1.1.0(typescript@4.9.5) - abitype: 0.8.7(typescript@4.9.5) + abitype: 0.8.7(typescript@4.9.5)(zod@3.21.4) isomorphic-ws: 5.0.0(ws@8.12.0) ws: 8.12.0 transitivePeerDependencies: From 82295bd2e45582d06e9244c1e3e05fe4766fa335 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Wed, 14 Jun 2023 22:12:15 -0700 Subject: [PATCH 2/3] fix import --- packages/world/ts/plugins/snapsync/configExtensions.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/world/ts/plugins/snapsync/configExtensions.ts b/packages/world/ts/plugins/snapsync/configExtensions.ts index 311e9d54e2..58234eac3b 100644 --- a/packages/world/ts/plugins/snapsync/configExtensions.ts +++ b/packages/world/ts/plugins/snapsync/configExtensions.ts @@ -1,5 +1,5 @@ import { extendMUDCoreConfig, resolveTableId } from "@latticexyz/config"; -import { zPluginStoreConfig } from "@latticexyz/store/ts/config"; +import { zPluginStoreConfig } from "@latticexyz/store"; import { zSnapSyncPluginConfig } from "./plugin"; import { zPluginWorldConfig } from "../../library"; From 6adc0442ed97a03fae35fff5669a899600944164 Mon Sep 17 00:00:00 2001 From: Kevin Ingersoll Date: Thu, 15 Jun 2023 08:33:38 -0700 Subject: [PATCH 3/3] diff array clone approach --- packages/store/ts/storeEventsAbi.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/store/ts/storeEventsAbi.test.ts b/packages/store/ts/storeEventsAbi.test.ts index 80c9ac8098..3ae0c19a56 100644 --- a/packages/store/ts/storeEventsAbi.test.ts +++ b/packages/store/ts/storeEventsAbi.test.ts @@ -21,7 +21,7 @@ describe("storeEventsAbi", () => { })) .sort((a, b) => a.name.localeCompare(b.name)); - const sortedStoreEventsAbi = storeEventsAbi.slice().sort((a, b) => a.name.localeCompare(b.name)); + const sortedStoreEventsAbi = [...storeEventsAbi].sort((a, b) => a.name.localeCompare(b.name)); expect(sortedStoreEventsAbi).toStrictEqual(expectedAbi); }); });