Skip to content

Commit

Permalink
refactor(store,world): remove v2 -> v1 compat layer (#2950)
Browse files Browse the repository at this point in the history
  • Loading branch information
holic authored Jul 16, 2024
1 parent 6dcacc2 commit 5ff9f2a
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 352 deletions.
2 changes: 1 addition & 1 deletion e2e/packages/sync-test/data/callWithSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import CallWithSignatureAbi from "@latticexyz/world-modules/out/Unstable_CallWit

type CallWithSignatureAbi = typeof CallWithSignatureAbi;

type WorldContract = GetContractReturnType<CallWithSignatureAbi, PublicClient, WalletClient>;
type WorldContract = GetContractReturnType<CallWithSignatureAbi, PublicClient & WalletClient>;

type WriteMethodName = ExtractAbiFunctionNames<CallWithSignatureAbi>;
type WriteMethod<TMethod extends WriteMethodName> = ExtractAbiFunction<CallWithSignatureAbi, TMethod>;
Expand Down
22 changes: 14 additions & 8 deletions e2e/packages/sync-test/data/encodeTestData.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { mapObject } from "@latticexyz/utils";
import { encodeKey, encodeValueArgs, valueSchemaToFieldLayoutHex } from "@latticexyz/protocol-parser/internal";
import {
encodeKey,
encodeValueArgs,
getKeySchema,
getSchemaTypes,
getValueSchema,
valueSchemaToFieldLayoutHex,
} from "@latticexyz/protocol-parser/internal";
import { Data, EncodedData } from "./types";
import configV2 from "../../contracts/mud.config";
import { worldToV1 } from "@latticexyz/world/config/v2";

const config = worldToV1(configV2);
import config from "../../contracts/mud.config";

/**
* Turns the typed data into encoded data in the format expected by `world.setRecord`
Expand All @@ -14,9 +18,11 @@ export function encodeTestData(testData: Data) {
if (!records) return undefined;
const tableConfig = config.tables[table];
return records.map((record) => {
const key = encodeKey(tableConfig.keySchema, record.key);
const valueArgs = encodeValueArgs(tableConfig.valueSchema, record.value);
const fieldLayout = valueSchemaToFieldLayoutHex(tableConfig.valueSchema);
const keySchema = getSchemaTypes(getKeySchema(tableConfig));
const valueSchema = getSchemaTypes(getValueSchema(tableConfig));
const key = encodeKey(keySchema, record.key);
const valueArgs = encodeValueArgs(valueSchema, record.value);
const fieldLayout = valueSchemaToFieldLayoutHex(valueSchema);

return {
key,
Expand Down
8 changes: 3 additions & 5 deletions e2e/packages/sync-test/data/expectClientData.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Page, expect } from "@playwright/test";
import { Data } from "./types";
import configV2 from "../../contracts/mud.config";
import config from "../../contracts/mud.config";
import { encodeEntity } from "@latticexyz/store-sync/recs";
import { callPageFunction } from "./callPageFunction";
import { worldToV1 } from "@latticexyz/world/config/v2";

const config = worldToV1(configV2);
import { getKeySchema, getSchemaTypes } from "@latticexyz/protocol-parser/internal";

/**
* Confirms that the client state equals the given state by reading from the client's data store
Expand All @@ -15,7 +13,7 @@ export async function expectClientData(page: Page, data: Data) {
for (const record of records) {
const value = await callPageFunction(page, "getComponentValue", [
table,
encodeEntity(config.tables[table].keySchema, record.key),
encodeEntity(getSchemaTypes(getKeySchema(config.tables[table])), record.key),
]);
expect(value).toMatchObject(record.value);
}
Expand Down
2 changes: 1 addition & 1 deletion e2e/packages/sync-test/data/getWorld.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { GetContractReturnType, PublicClient, WalletClient } from "viem";

type WorldAbi = typeof IWorldAbi;

type WorldContract = GetContractReturnType<WorldAbi, PublicClient, WalletClient>;
type WorldContract = GetContractReturnType<WorldAbi, PublicClient & WalletClient>;

export function getWorld(page: Page) {
return page.evaluate(() => {
Expand Down
31 changes: 11 additions & 20 deletions e2e/packages/sync-test/data/types.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,19 @@
// Note: this expects the config to not use shortcuts but the full definitions for table schemas
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType } from "@latticexyz/schema-type/internal";
import configV2 from "../../contracts/mud.config";
import { Hex } from "viem";
import { worldToV1 } from "@latticexyz/world/config/v2";
import { getKeySchema, getSchemaPrimitives, getValueSchema } from "@latticexyz/protocol-parser/internal";
import { Schema } from "@latticexyz/config";
import config from "../../contracts/mud.config";

const config = worldToV1(configV2);
type config = typeof config;
type tables = config["tables"];

type SchemaToPrimitive<Schema> =
Schema extends Record<string, SchemaAbiType>
? { [key in keyof Schema]: SchemaAbiTypeToPrimitiveType<Schema[key]> }
: never;
type Key<tableLabel extends keyof tables> = getSchemaPrimitives<Schema & getKeySchema<tables[tableLabel]>>;
type Value<tableLabel extends keyof tables> = getSchemaPrimitives<getValueSchema<tables[tableLabel]>>;

type Key<Table extends keyof (typeof config)["tables"]> = SchemaToPrimitive<
(typeof config)["tables"][Table]["keySchema"]
>;
type Value<Table extends keyof (typeof config)["tables"]> = SchemaToPrimitive<
(typeof config)["tables"][Table]["valueSchema"]
>;

export type Datum<Table extends keyof (typeof config)["tables"] = keyof (typeof config)["tables"]> = {
key: Key<Table>;
value: Value<Table>;
export type Datum<tableLabel extends keyof tables = keyof tables> = {
key: Key<tableLabel>;
value: Value<tableLabel>;
};
export type Data = { [Table in keyof (typeof config)["tables"]]?: Array<Datum<Table>> };
export type Data = { [tableLabel in keyof tables]?: Array<Datum<tableLabel>> };

export type EncodedData<T extends Data = Data> = {
[Table in keyof T]: Array<{ key: Hex[]; staticData: Hex; encodedLengths: Hex; dynamicData: Hex; fieldLayout: Hex }>;
Expand Down
1 change: 1 addition & 0 deletions e2e/packages/sync-test/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"devDependencies": {
"@latticexyz/cli": "link:../../../packages/cli",
"@latticexyz/common": "link:../../../packages/common",
"@latticexyz/config": "link:../../../packages/config",
"@latticexyz/protocol-parser": "link:../../../packages/protocol-parser",
"@latticexyz/recs": "link:../../../packages/recs",
"@latticexyz/schema-type": "link:../../../packages/schema-type",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,14 @@ import { mudFoundry } from "@latticexyz/common/chains";
import { encodeEntity } from "@latticexyz/store-sync/recs";
import { callPageFunction } from "./data/callPageFunction";
import worldConfig from "@latticexyz/world/mud.config";
import { worldToV1 } from "@latticexyz/world/config/v2";
import { callWithSignatureTypes } from "@latticexyz/world/internal";
import { getWorld } from "./data/getWorld";
import { callWithSignature } from "./data/callWithSignature";
import IWorldAbi from "../contracts/out/IWorld.sol/IWorld.abi.json";
import { getSchemaTypes, getKeySchema } from "@latticexyz/protocol-parser/internal";

const DELEGATOR_PRIVATE_KEY = "0x67bbd1575ecc79b3247c7d7b87a5bc533ccb6a63955a9fefdfaf75853f7cd543";

const worldConfigV1 = worldToV1(worldConfig);

describe("callWithSignature", async () => {
const asyncErrorHandler = createAsyncErrorHandler();
let webserver: ViteDevServer;
Expand Down Expand Up @@ -99,7 +97,7 @@ describe("callWithSignature", async () => {
// Expect delegation to have been created
const value = await callPageFunction(page, "getComponentValue", [
"UserDelegationControl",
encodeEntity(worldConfigV1.tables.UserDelegationControl.keySchema, {
encodeEntity(getSchemaTypes(getKeySchema(worldConfig.tables.world__UserDelegationControl)), {
delegator: delegator.address,
delegatee,
}),
Expand Down
3 changes: 3 additions & 0 deletions e2e/pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion packages/protocol-parser/src/getKeySchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Schema, Table } from "@latticexyz/config";
export type getKeySchema<table extends Table> = Table extends table
? Schema
: {
readonly [fieldName in keyof table["schema"] & table["key"][number]]: table["schema"][fieldName];
readonly [fieldName in Extract<keyof table["schema"], table["key"][number]>]: table["schema"][fieldName];
};

export function getKeySchema<table extends Table>(table: table): getKeySchema<table> {
Expand Down
98 changes: 0 additions & 98 deletions packages/store/ts/config/v2/compat.test.ts

This file was deleted.

67 changes: 0 additions & 67 deletions packages/store/ts/config/v2/compat.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/store/ts/config/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export * from "./store";
export * from "./input";
export * from "./output";
export * from "./defaults";
export * from "./compat";
export * from "./codegen";
export * from "./enums";
export * from "./userTypes";
Loading

0 comments on commit 5ff9f2a

Please sign in to comment.