Skip to content

Commit

Permalink
move types around
Browse files Browse the repository at this point in the history
  • Loading branch information
holic committed Jul 31, 2023
1 parent 6ba5a5f commit 509380d
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 30 deletions.
1 change: 0 additions & 1 deletion packages/store-sync/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"@latticexyz/recs": "workspace:*",
"@latticexyz/schema-type": "workspace:*",
"@latticexyz/store": "workspace:*",
"@latticexyz/store-cache": "workspace:*",
"better-sqlite3": "^8.4.0",
"debug": "^4.3.4",
"drizzle-orm": "^0.27.0",
Expand Down
4 changes: 1 addition & 3 deletions packages/store-sync/src/blockLogsToStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,10 @@ import {
abiTypesToSchema,
TableSchema,
} from "@latticexyz/protocol-parser";
import { StoreConfig } from "@latticexyz/store";
import { StoreConfig, ConfigToKeyPrimitives as Key, ConfigToValuePrimitives as Value } from "@latticexyz/store";
import { TableId } from "@latticexyz/common";
import { Address, Hex, decodeAbiParameters, getAddress, parseAbiParameters } from "viem";
import { debug } from "./debug";
// TODO: move these type helpers into store?
import { Key, Value } from "@latticexyz/store-cache";
import { isDefined } from "@latticexyz/common/utils";
import { BlockLogs, StorageOperation, Table, TableName, TableNamespace } from "./common";

Expand Down
28 changes: 10 additions & 18 deletions packages/store-sync/src/common.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType, StaticAbiType } from "@latticexyz/schema-type";
import { Address, Hex } from "viem";
// TODO: move these type helpers into store?
import { Key, Value } from "@latticexyz/store-cache";
import { GetLogsResult, GroupLogsByBlockNumberResult, NonPendingLog } from "@latticexyz/block-logs-stream";
import { StoreEventsAbi, StoreConfig } from "@latticexyz/store";
import {
StoreEventsAbi,
StoreConfig,
KeySchema,
ValueSchema,
ConfigToKeyPrimitives as Key,
ConfigToValuePrimitives as Value,
} from "@latticexyz/store";

export type ChainId = number;
export type WorldId = `${ChainId}:${Address}`;

export type TableNamespace = string;
export type TableName = string;

export type KeySchema = Record<string, StaticAbiType>;
export type ValueSchema = Record<string, SchemaAbiType>;

export type SchemaToPrimitives<TSchema extends ValueSchema> = {
[key in keyof TSchema]: SchemaAbiTypeToPrimitiveType<TSchema[key]>;
};

export type TableRecord<TKeySchema extends KeySchema = KeySchema, TValueSchema extends ValueSchema = ValueSchema> = {
key: SchemaToPrimitives<TKeySchema>;
value: SchemaToPrimitives<TValueSchema>;
};

export type Table = {
address: Address;
tableId: Hex;
Expand All @@ -37,8 +29,8 @@ export type BlockLogs = GroupLogsByBlockNumberResult<StoreEventsLog>[number];

export type BaseStorageOperation = {
log: NonPendingLog<StoreEventsLog>;
namespace: string;
name: string;
namespace: TableNamespace;
name: TableName;
};

export type SetRecordOperation<TConfig extends StoreConfig> = BaseStorageOperation & {
Expand Down
2 changes: 1 addition & 1 deletion packages/store-sync/src/recs/common.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { KeySchema, ValueSchema } from "../common";
import { KeySchema, ValueSchema } from "@latticexyz/store";

export type StoreComponentMetadata = {
keySchema: KeySchema;
Expand Down
2 changes: 1 addition & 1 deletion packages/store-sync/src/recs/decodeEntity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Entity } from "@latticexyz/recs";
import { StaticAbiType } from "@latticexyz/schema-type";
import { Hex, decodeAbiParameters } from "viem";
import { SchemaToPrimitives } from "../common";
import { SchemaToPrimitives } from "@latticexyz/store";
import { entityToHexKeyTuple } from "./entityToHexKeyTuple";

export function decodeEntity<TKeySchema extends Record<string, StaticAbiType>>(
Expand Down
2 changes: 1 addition & 1 deletion packages/store-sync/src/recs/encodeEntity.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Entity } from "@latticexyz/recs";
import { StaticAbiType } from "@latticexyz/schema-type";
import { encodeAbiParameters } from "viem";
import { SchemaToPrimitives } from "../common";
import { SchemaToPrimitives } from "@latticexyz/store";
import { hexKeyTupleToEntity } from "./hexKeyTupleToEntity";

export function encodeEntity<TKeySchema extends Record<string, StaticAbiType>>(
Expand Down
3 changes: 2 additions & 1 deletion packages/store-sync/src/recs/syncToRecs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
getComponentValue,
setComponent,
} from "@latticexyz/recs";
import { BlockLogs, Table, TableRecord } from "../common";
import { BlockLogs, Table } from "../common";
import { TableRecord } from "@latticexyz/store";
import {
createBlockStream,
isNonPendingBlock,
Expand Down
2 changes: 1 addition & 1 deletion packages/store-sync/src/schemaToDefaults.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { schemaAbiTypeToDefaultValue } from "@latticexyz/schema-type";
import { ValueSchema, SchemaToPrimitives } from "./common";
import { ValueSchema, SchemaToPrimitives } from "@latticexyz/store";

export function schemaToDefaults<TSchema extends ValueSchema>(schema: TSchema): SchemaToPrimitives<TSchema> {
return Object.fromEntries(
Expand Down
48 changes: 48 additions & 0 deletions packages/store/ts/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { SchemaAbiType, SchemaAbiTypeToPrimitiveType, StaticAbiType } from "@latticexyz/schema-type";
import { FieldData, FullSchemaConfig, StoreConfig } from "./config";

export type KeySchema = Record<string, StaticAbiType>;
export type ValueSchema = Record<string, SchemaAbiType>;

/** Map a table schema like `{ value: "uint256" }` to its primitive types like `{ value: bigint }` */
export type SchemaToPrimitives<TSchema extends ValueSchema> = {
[key in keyof TSchema]: SchemaAbiTypeToPrimitiveType<TSchema[key]>;
};

export type TableRecord<TKeySchema extends KeySchema = KeySchema, TValueSchema extends ValueSchema = ValueSchema> = {
key: SchemaToPrimitives<TKeySchema>;
value: SchemaToPrimitives<TValueSchema>;
};

export type ConfigFieldTypeToPrimitiveType<T extends FieldData<string>> = T extends SchemaAbiType
? SchemaAbiTypeToPrimitiveType<T>
: T extends `${string}[${string}]` // field type might include enums and enum arrays, which are mapped to uint8/uint8[]
? number[] // map enum arrays to `number[]`
: number; // map enums to `number`

/** Map a table schema config like `{ value: "uint256", type: "SomeEnum" }` to its primitive types like `{ value: bigint, type: number }` */
export type SchemaConfigToPrimitives<T extends FullSchemaConfig> = {
[key in keyof T]: ConfigFieldTypeToPrimitiveType<T[key]>;
};

export type ConfigToTablesPrimitives<C extends StoreConfig> = {
[key in keyof C["tables"]]: {
key: SchemaConfigToPrimitives<C["tables"][key]["keySchema"]>;
value: SchemaConfigToPrimitives<C["tables"][key]["schema"]>;
};
};

export type ConfigToKeyPrimitives<
C extends StoreConfig,
Table extends keyof ConfigToTablesPrimitives<C>
> = ConfigToTablesPrimitives<C>[Table]["key"];

export type ConfigToValuePrimitives<
C extends StoreConfig,
Table extends keyof ConfigToTablesPrimitives<C>
> = ConfigToTablesPrimitives<C>[Table]["value"];

export type ConfigToRecordPrimitives<C extends StoreConfig, Table extends keyof ConfigToTablesPrimitives<C>> = {
key: ConfigToKeyPrimitives<C, Table>;
value: ConfigToValuePrimitives<C, Table>;
};
1 change: 1 addition & 0 deletions packages/store/ts/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
// (library neither creates nor extends MUDCoreContext when imported)
export * from "./config";

export * from "./common";
export * from "./storeEvents";
export * from "./storeEventsAbi";
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

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

0 comments on commit 509380d

Please sign in to comment.